Skip to content

Commit

Permalink
Merge branch 'merge-code-mark-1' of https://github.com/devansh-shah-1…
Browse files Browse the repository at this point in the history
…1/FaceRec into merge-code-mark-1
  • Loading branch information
Devasy23 committed Mar 5, 2024
2 parents 9196c1b + 0a31d03 commit 2e7782f
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 8 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Pylint

on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
45 changes: 45 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest --cov .
continue-on-error: true
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: devansh-shah-11/FaceRec
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@Devasy23
@Devansh-shah-11
16 changes: 12 additions & 4 deletions FaceRec/app/main/Employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ def submit_form():
"Name": Name,
"gender": gender,
"Department": Department,
"encoded_image": encoded_image,
"Image": encoded_image,
}
url = "http://127.0.0.1:8000/create_new_faceEntry"
try:
resp = requests.post(url=url, data=payload)
resp = requests.post(url=url, json={
"EmployeeCode": 134,
"Name": "Name",
"gender": "gender",
"Department": "Department",
"Image": "your_image",
})
resp.status_code
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
jsonify({"message": "Successfully executed"})
print("Executed.")
return redirect("DisplayingEmployees")

if resp.status_code==200:
return redirect("DisplayingEmployees")
else:
return jsonify({"message": "Failed to execute"})

# To edit an employee details
@flk_blueprint.route("/edit/<int:EmployeeCode>", methods=["POST", "GET"])
Expand Down
2 changes: 1 addition & 1 deletion FaceRec/static/Images/image_data.json

Large diffs are not rendered by default.

Binary file modified FaceRec/static/Images/uploads/final.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ anyio==4.2.0
asttokens==2.4.1
astunparse==1.6.3
attrs==23.2.0
absl-py==2.1.0
annotated-types==0.6.0
anyio==4.2.0
astunparse==1.6.3
beautifulsoup4==4.12.2
black==24.2.0
blinker==1.7.0
Expand Down Expand Up @@ -130,3 +134,5 @@ wcwidth==0.2.13
Werkzeug==3.0.1
wrapt==1.14.1
yarl==1.9.4

zipp==3.17.0
Empty file added route/__init__.py
Empty file.
86 changes: 86 additions & 0 deletions route/route.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
import os
from datetime import datetime
from pymongo import MongoClient
from random import randint
import uuid
from deepface import DeepFace
from matplotlib import pyplot as plt

IMAGEDIR = "test-faces/"

mongodb_uri ='mongodb://localhost:27017/'
port = 8000
client = MongoClient(mongodb_uri,port)

db = client["ImageDB"]
faceEntries = db["faceEntries"]

app = FastAPI()

# deprecated
@app.post("/upload/")
async def register_face(file: UploadFile = File(...)):

file.filename = f"{uuid.uuid4()}.jpg"
contents = await file.read()

#save the file
with open(f"{IMAGEDIR}{file.filename}", "wb") as f:
f.write(contents)
# db.images.insert_one({"filename": file.filename, "contents": contents})
return {"filename": file.filename}


@app.post("/create_new_faceEntry")
async def create_new_faceEntry(id:int, age: int, gender: str, image: UploadFile = File(...)):
# Generate a unique ID
# id = uuid.uuid4()

# Get the current time
time = datetime.now()

# Read the image file
image_data = await image.read()
print(image.filename)
# Save the original image in a specified directory
with open(f"../Images/dbImages/{image.filename}", "wb") as f:
f.write(image_data)

# Extract the face from the image
face_image_data = DeepFace.extract_faces(f"../Images/dbImages/{image.filename}", detector_backend="mtcnn")

# Save the face image in a specified directory
plt.imsave(f"../Images/Faces/{image.filename}", face_image_data[0]['face'])

# Calculate the embeddings of the face image
embeddings = DeepFace.represent(f"../Images/dbImages/{image.filename}", model_name="Facenet", detector_backend="mtcnn")

# Store the data in the database
db.faceEntries.insert_one({
"id": id,
"age": age,
"gender": gender,
"time": time,
"embeddings": embeddings,
# "face-img": face_image_data,
})

return {"message": "Face entry created successfully"}

@app.get("/show/")
async def read_random_file():

# get random file from the image directory
files = os.listdir(IMAGEDIR)
random_index = randint(0, len(files) - 1)

path = f"{IMAGEDIR}{files[random_index]}"

return FileResponse(path)

@app.delete("/delete/{filename}")
async def delete_file(filename: str):
os.remove(f"{IMAGEDIR}/{filename}")
return {"message": "Face deleted successfully"}
46 changes: 43 additions & 3 deletions test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -554,7 +554,7 @@
"{'message': 'Face entry created successfully'}"
]
},
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -623,6 +623,46 @@
"response = requests.post(url, json=data)\n",
"response.json()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"url = \"http://127.0.0.1:8000/create_new_faceEntry\"\n",
"resp = requests.post(url=url, json={\n",
" \"EmployeeCode\": 134,\n",
" \"Name\": \"Name\",\n",
" \"gender\": \"gender\",\n",
" \"Department\": \"Department\",\n",
" \"Image\": \"your_image\",\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<Response [500]>\n"
]
}
],
"source": [
"print(resp)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -641,7 +681,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.9.16"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2e7782f

Please sign in to comment.