From c76981c6f144e998961f757df6181e0139932fb4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 Aug 2024 00:01:51 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .github/workflows/changelog-ci.yml | 1 - .gitignore | 2 +- API/route.py | 54 ++++---- FaceRec/app/main/Edit.py | 121 +++++++++--------- FaceRec/app/main/Employee.py | 100 ++++++++------- FaceRec/app/main/Face.py | 55 ++++---- FaceRec/app/main/VideoImage.py | 79 ++++++------ FaceRec/app/main/_init_.py | 9 +- FaceRec/templates/edit.html | 4 +- FaceRec/templates/recognition.html | 34 ++--- FaceRec/templates/table.html | 2 +- ImageDB.faceEntries.json | 2 +- Model-Training/Evaluate_model_II.ipynb | 2 +- .../Evaluated_model_try2_vs_try3.ipynb | 2 +- Model-Training/eval-mark-I.py | 27 +++- .../facerec_indianfaces_comapred.ipynb | 2 +- changelog-config.yaml | 2 +- 17 files changed, 271 insertions(+), 227 deletions(-) diff --git a/.github/workflows/changelog-ci.yml b/.github/workflows/changelog-ci.yml index d9df9e6..7a546eb 100644 --- a/.github/workflows/changelog-ci.yml +++ b/.github/workflows/changelog-ci.yml @@ -36,4 +36,3 @@ jobs: # release_name: Release ${{ github.run_number }} # draft: false # prerelease: false - diff --git a/.gitignore b/.gitignore index 9ec1c71..f3218c8 100644 --- a/.gitignore +++ b/.gitignore @@ -143,4 +143,4 @@ __pyc FaceRec/static/Images/uploads/* Images/dbImages/* Images/Faces/* -Images/ \ No newline at end of file +Images/ diff --git a/API/route.py b/API/route.py index b9523fb..9d0cd59 100644 --- a/API/route.py +++ b/API/route.py @@ -8,7 +8,8 @@ from datetime import datetime from io import BytesIO from typing import List -from tensorflow.keras.models import load_model + +import numpy as np from bson import ObjectId from deepface import DeepFace from dotenv import load_dotenv @@ -17,11 +18,11 @@ from fastapi import HTTPException from fastapi import Response from fastapi import UploadFile +from keras.preprocessing import image from matplotlib import pyplot as plt from PIL import Image from pydantic import BaseModel -import numpy as np -from keras.preprocessing import image +from tensorflow.keras.models import load_model from API.database import Database from API.utils import init_logging_config @@ -41,6 +42,8 @@ collection3 = 'VectorDB' # Models for the data to be sent and received by the server + + class Employee(BaseModel): EmployeeCode: int Name: str @@ -55,42 +58,44 @@ class UpdateEmployee(BaseModel): Department: str Images: list[str] + def load_and_preprocess_image(img_path, target_size=(160, 160)): - + img = image.load_img(img_path, target_size=target_size) img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) img_array /= 255.0 return img_array + def calculate_embeddings(image_filename): - """ Calculate embeddings for the provided image. - + Args: image_filename (str): The path to the image file. - + Returns: list: A list of embeddings for the image. """ - + face_image_data = DeepFace.extract_faces( image_filename, enforce_detection=False, ) new_image_path = f'Images/Faces/tmp.jpg' - + if face_image_data[0]['face'] is not None: plt.imsave(new_image_path, face_image_data[0]['face']) - + img_array = load_and_preprocess_image(new_image_path) - model=load_model('Model/embedding_trial3.h5') + model = load_model('Model/embedding_trial3.h5') embedding = model.predict(img_array)[0] embedding_list = embedding.tolist() logging.info(f'Embedding created') - + return embedding_list + @router.post('/recalculate_embeddings') async def recalculate_embeddings(): """ @@ -107,15 +112,15 @@ async def recalculate_embeddings(): for employee in employees_mongo: print(employee, type(employee)) embeddings = [] - + # In the initial version, the images were stored in the 'Image' field if 'Images' in employee: images = employee['Images'] else: images = [employee['Image']] - + for encoded_image in images: - + pil_image = Image.open(BytesIO(base64.b64decode(encoded_image))) image_filename = f'{employee["Name"]}.png' pil_image.save(image_filename) @@ -172,7 +177,7 @@ async def create_new_faceEntry(Employee: Employee): # embedding = DeepFace.represent( # image_filename, model_name='Facenet512', detector_backend='mtcnn', # ) - + embeddings.append(calculate_embeddings(image_filename)) # os.remove(image_filename) @@ -305,11 +310,11 @@ async def update_employees(EmployeeCode: int, Employee: UpdateEmployee): image_filename = f'{Employee.Name}.png' pil_image.save(image_filename) logging.debug(f'Image saved {Employee.Name}') - + # embedding = DeepFace.represent( # image_filename, model_name='Facenet', detector_backend='mtcnn', # ) - + embeddings.append(calculate_embeddings(image_filename)) # os.remove(image_filename) @@ -382,7 +387,7 @@ async def recognize_face(Face: UploadFile = File(...)): logging.info('Recognizing Face') try: # Code to calculate embeddings via Original Facenet model - + img_data = await Face.read() image_filename = 'temp.png' with open(image_filename, 'wb') as f: @@ -390,20 +395,21 @@ async def recognize_face(Face: UploadFile = File(...)): # embedding = DeepFace.represent( # img_path='temp.png', model_name='Facenet512', detector_backend='mtcnn', # ) - + # Code to calculate embeddings via Finetuned Facenet model face_image_data = DeepFace.extract_faces( image_filename, detector_backend='mtcnn', enforce_detection=False, ) - + if face_image_data and face_image_data[0]['face'] is not None: - + plt.imsave(f'Images/Faces/tmp.jpg', face_image_data[0]['face']) face_image_path = f'Images/Faces/tmp.jpg' img_array = load_and_preprocess_image(face_image_path) - + model = load_model('Model/embedding_trial3.h5') - embedding_list = model.predict(img_array)[0] # Get the first prediction + embedding_list = model.predict( + img_array)[0] # Get the first prediction print(embedding_list, type(embedding_list)) embedding = embedding_list.tolist() result = client2.vector_search(collection3, embedding) diff --git a/FaceRec/app/main/Edit.py b/FaceRec/app/main/Edit.py index 990bd5b..2d66c64 100644 --- a/FaceRec/app/main/Edit.py +++ b/FaceRec/app/main/Edit.py @@ -1,26 +1,31 @@ +from __future__ import annotations + import base64 import io import json import os + import cv2 +import requests from flask import Blueprint +from flask import redirect +from flask import render_template +from flask import request from flask import Response as flask_response -from flask import redirect, render_template, request from PIL import Image -import requests - + from FaceRec.config import Config - + Edit_blueprint = Blueprint( - "Edit_blueprint", + 'Edit_blueprint', __name__, - template_folder="../../templates/", - static_folder="../../static/", + template_folder='../../templates/', + static_folder='../../static/', ) - + cap = cv2.VideoCapture(0) - - + + # function for displaying live video def display_live_video(): while True: @@ -28,60 +33,62 @@ def display_live_video(): if not success: break frame = cv2.flip(frame, 1) - ret, buffer = cv2.imencode(".jpg", frame) + ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes if not ret: break yield ( - b"--frame\r\n" - b"Content-Type: image/jpeg\r\n\r\n" + bytearray(buffer) + b"\r\n\r\n" + b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + + bytearray(buffer) + b'\r\n\r\n' ) - - + + # Route for displaying video -@Edit_blueprint.route("/video_feed") +@Edit_blueprint.route('/video_feed') def video_feed(): return flask_response( - display_live_video(), mimetype="multipart/x-mixed-replace;boundary=frame" + display_live_video(), mimetype='multipart/x-mixed-replace;boundary=frame', ) - - + + # Route for capturing image from video -@Edit_blueprint.route("/capture", methods=["GET", "POST"]) +@Edit_blueprint.route('/capture', methods=['GET', 'POST']) def capture(): global EmployeeCode global Name global gender global Dept global encoded_image - EmployeeCode = request.form.get("EmployeeCode", "") - Name = request.form.get("Name", "") - gender = request.form.get("gender", "") - Dept = request.form.get("Department", "") + EmployeeCode = request.form.get('EmployeeCode', '') + Name = request.form.get('Name', '') + gender = request.form.get('gender', '') + Dept = request.form.get('Department', '') ret, frame = cap.read(True) frame = cv2.flip(frame, 1) - _, buffer = cv2.imencode(".jpg", frame) - encoded_image = base64.b64encode(buffer).decode("utf-8") - with open(Config.image_data_file, "w") as file: - json.dump({"base64_image": encoded_image}, file) - return redirect("Image") - - + _, buffer = cv2.imencode('.jpg', frame) + encoded_image = base64.b64encode(buffer).decode('utf-8') + with open(Config.image_data_file, 'w') as file: + json.dump({'base64_image': encoded_image}, file) + return redirect('Image') + + # Route to display captured image -@Edit_blueprint.route("/Image", methods=["GET"]) +@Edit_blueprint.route('/Image', methods=['GET']) def display_image(): if os.path.exists(Config.image_data_file): - with open(Config.image_data_file, "r") as file: + with open(Config.image_data_file) as file: image_data = json.load(file) - encoded_image = image_data.get("base64_image", "") + encoded_image = image_data.get('base64_image', '') decoded_image_data = base64.b64decode(encoded_image) image = Image.open(io.BytesIO(decoded_image_data)) - filename = "final.png" - image.save(os.path.join(Config.upload_image_path[0], filename), quality=100) + filename = 'final.png' + image.save(os.path.join( + Config.upload_image_path[0], filename), quality=100) image = sorted( os.listdir(Config.upload_image_path[0]), key=lambda x: os.path.getatime( - os.path.join(Config.upload_image_path[0], x) + os.path.join(Config.upload_image_path[0], x), ), reverse=True, ) @@ -91,34 +98,35 @@ def display_image(): else: recent_image = None image_path = os.path.join(Config.upload_image_path[0], recent_image) - print("done") - return render_template("index.html", image_path=image_path) - -@Edit_blueprint.route("/edit/", methods=["POST", "GET"]) + print('done') + return render_template('index.html', image_path=image_path) + + +@Edit_blueprint.route('/edit/', methods=['POST', 'GET']) def edit(EmployeeCode): - if request.method == "POST": - Name = request.form["Name"] - gender = request.form["Gender"] - Department = request.form["Department"] - with open(Config.image_data_file, "r") as file: + if request.method == 'POST': + Name = request.form['Name'] + gender = request.form['Gender'] + Department = request.form['Department'] + with open(Config.image_data_file) as file: image_data = json.load(file) - encoded_image = image_data.get("base64_image", "") + encoded_image = image_data.get('base64_image', '') payload = { - "Name": Name, - "gender": gender, - "Department": Department, - "Image": encoded_image, + 'Name': Name, + 'gender': gender, + 'Department': Department, + 'Image': encoded_image, } # logger.info(payload) try: url = requests.put( - f"http://127.0.0.1:8000/update/{EmployeeCode}", json=payload + f"http://127.0.0.1:8000/update/{EmployeeCode}", json=payload, ) url.status_code # logger.info(url.json()) - - return redirect("/") - + + return redirect('/') + except requests.exceptions.RequestException as e: print(f"Request failed: {e}") response = requests.get(f"http://127.0.0.1:8000/read/{EmployeeCode}") @@ -126,7 +134,6 @@ def edit(EmployeeCode): # logger.info(response.json()) if response.status_code == 200: employee_data = response.json() - return render_template("edit.html", employee_data=employee_data) + return render_template('edit.html', employee_data=employee_data) else: return f"Error {response.status_code}: Failed to retrieve employee data." - diff --git a/FaceRec/app/main/Employee.py b/FaceRec/app/main/Employee.py index e17657e..b32579e 100644 --- a/FaceRec/app/main/Employee.py +++ b/FaceRec/app/main/Employee.py @@ -1,35 +1,41 @@ from __future__ import annotations import base64 +import io import json import os + import cv2 -import io import requests -from flask import Blueprint, jsonify, redirect, render_template, request +from flask import Blueprint +from flask import jsonify +from flask import redirect +from flask import render_template +from flask import request from PIL import Image + from FaceRec.config import Config - + video_capture = cv2.VideoCapture(0) flk_blueprint = Blueprint( 'flk_blueprint ', __name__, - template_folder="../../templates/", - static_folder="../../static/", + template_folder='../../templates/', + static_folder='../../static/', # capture_image="../../Capture image/" ) - - -@flk_blueprint.route("/") + + +@flk_blueprint.route('/') def Main_page(): path = str(Config.upload_image_path[0]) if not os.path.exists(path): os.makedirs(path, exist_ok=True) else: pass - return redirect("DisplayingEmployees") - - + return redirect('DisplayingEmployees') + + # Displaying all records @flk_blueprint.route('/DisplayingEmployees') def display_information(): @@ -40,32 +46,32 @@ def display_information(): # logger.info(resp.status_code) # logger.info(resp.json()) employees = resp.json() - + except requests.exceptions.RequestException as e: print(f"Request failed: {e}") - return render_template("table.html", employees=employees) - - + return render_template('table.html', employees=employees) + + # To add employee record @flk_blueprint.route('/Add_employee') def add_employee(): - return render_template("index.html") - - + return render_template('index.html') + + # To submit the form data to server and save it in database @flk_blueprint.route('/submit_form', methods=['POST']) def submit_form(): - - Employee_Code = request.form["EmployeeCode"] - Name = request.form["Name"] - gender = request.form["Gender"] - Department = request.form["Department"] - - if request.files["File"]: - if "File" not in request.files: - return jsonify({"message": "No file part"}), 400 - file = request.files["File"] - allowed_extensions = {"png", "jpg", "jpeg"} + + Employee_Code = request.form['EmployeeCode'] + Name = request.form['Name'] + gender = request.form['Gender'] + Department = request.form['Department'] + + if request.files['File']: + if 'File' not in request.files: + return jsonify({'message': 'No file part'}), 400 + file = request.files['File'] + allowed_extensions = {'png', 'jpg', 'jpeg'} if ( '.' not in file.filename or file.filename.split('.')[-1].lower() not in allowed_extensions @@ -73,11 +79,11 @@ def submit_form(): return jsonify({'message': 'File extension is not valid'}), 400 if file: image_data = file.read() - encoded_image = base64.b64encode(image_data).decode("utf-8") - with open(Config.image_data_file, "w") as file: - json.dump({"base64_image": encoded_image}, file) - - with open(Config.image_data_file, "r") as file: + encoded_image = base64.b64encode(image_data).decode('utf-8') + with open(Config.image_data_file, 'w') as file: + json.dump({'base64_image': encoded_image}, file) + + with open(Config.image_data_file) as file: image_data = json.load(file) encoded_image = image_data.get('base64_image', '') jsonify( @@ -89,7 +95,7 @@ def submit_form(): 'encoded_image': encoded_image, }, ) - + payload = { 'EmployeeCode': Employee_Code, 'Name': Name, @@ -97,7 +103,7 @@ def submit_form(): 'Department': Department, 'Image': encoded_image, } - url = "http://127.0.0.1:8000/create_new_faceEntry" + url = 'http://127.0.0.1:8000/create_new_faceEntry' payload.status_code # try: # resp = requests.post( @@ -113,17 +119,17 @@ def submit_form(): # resp.status_code # except requests.exceptions.RequestException as e: # print(f"Request failed: {e}") - jsonify({"message": "Successfully executed"}) - print("Executed.") + jsonify({'message': 'Successfully executed'}) + print('Executed.') if payload.status_code == 200: - return redirect("DisplayingEmployees") + return redirect('DisplayingEmployees') else: - return jsonify({"message": "Failed to execute"}) - - + return jsonify({'message': 'Failed to execute'}) + + # To edit an employee details - - + + # To delete employee details @flk_blueprint.route('/Delete/', methods=['DELETE', 'GET']) def Delete(EmployeeCode): @@ -131,7 +137,5 @@ def Delete(EmployeeCode): return jsonify({'message': 'Employee code should be an integer'}, 400) response = requests.delete(f'http://127.0.0.1:8000/delete/{EmployeeCode}') jsonify(response.json()) - - return redirect("/DisplayingEmployees") - - + + return redirect('/DisplayingEmployees') diff --git a/FaceRec/app/main/Face.py b/FaceRec/app/main/Face.py index e0aa44b..7457c2d 100644 --- a/FaceRec/app/main/Face.py +++ b/FaceRec/app/main/Face.py @@ -1,54 +1,63 @@ +from __future__ import annotations + import base64 import io import json import os + import cv2 +import requests from flask import Blueprint +from flask import redirect +from flask import render_template +from flask import request from flask import Response as flask_response -from flask import redirect, render_template, request from PIL import Image -import requests - + from FaceRec.config import Config - + Face_Rec_blueprint = Blueprint( - "Face_Rec_blueprint", + 'Face_Rec_blueprint', __name__, - template_folder="../../templates/", - static_folder="../../static/", + template_folder='../../templates/', + static_folder='../../static/', ) cap = cv2.VideoCapture(0) - + + def display_live_video(): while True: success, frame = cap.read(True) # Read a frame from the camera if not success: break frame = cv2.flip(frame, 1) - ret, buffer = cv2.imencode(".jpg", frame) + ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes if not ret: break yield ( - b"--frame\r\n" - b"Content-Type: image/jpeg\r\n\r\n" + bytearray(buffer) + b"\r\n\r\n" + b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + + bytearray(buffer) + b'\r\n\r\n' ) - -@Face_Rec_blueprint.route("/recognize_employee") + + +@Face_Rec_blueprint.route('/recognize_employee') def recognize_employee(): - return render_template("recognition.html") - -@Face_Rec_blueprint.route("/video_feed") + return render_template('recognition.html') + + +@Face_Rec_blueprint.route('/video_feed') def video_feed(): return flask_response( - display_live_video(), mimetype="multipart/x-mixed-replace;boundary=frame" + display_live_video(), mimetype='multipart/x-mixed-replace;boundary=frame', ) - - - -@Face_Rec_blueprint.route("/Recognize", methods=["GET","POST"]) + + +@Face_Rec_blueprint.route('/Recognize', methods=['GET', 'POST']) def recognize(): files = {'image': (open(f"captured_image.jpg", 'rb'), 'image/jpeg')} - fastapi_url = 'http://127.0.0.1:8000/recognize_face' # Replace with your FastAPI URL + # Replace with your FastAPI URL + fastapi_url = 'http://127.0.0.1:8000/recognize_face' response = requests.post(fastapi_url, files=files) - return render_template("recognition.html", response_text = response.text) + return render_template('recognition.html', response_text=response.text) diff --git a/FaceRec/app/main/VideoImage.py b/FaceRec/app/main/VideoImage.py index 07ff142..2577d47 100644 --- a/FaceRec/app/main/VideoImage.py +++ b/FaceRec/app/main/VideoImage.py @@ -4,28 +4,29 @@ import io import json import os + import cv2 +import requests from flask import Blueprint +from flask import jsonify from flask import redirect from flask import render_template from flask import request from flask import Response as flask_response -from flask import redirect, render_template, request,jsonify from PIL import Image -import requests - + from FaceRec.config import Config - + employee_blueprint = Blueprint( 'employee_blueprint', __name__, template_folder='../../templates/', static_folder='../../static/', ) - + cap = cv2.VideoCapture(0) - - + + # function for displaying live video def display_live_video(): while True: @@ -42,16 +43,16 @@ def display_live_video(): b'Content-Type: image/jpeg\r\n\r\n' + bytearray(buffer) + b'\r\n\r\n' ) - - + + # Route for displaying video @employee_blueprint.route('/video_feed') def video_feed(): return flask_response( display_live_video(), mimetype='multipart/x-mixed-replace;boundary=frame', ) - - + + # Route for capturing image from video @employee_blueprint.route('/capture', methods=['GET', 'POST']) def capture(): @@ -66,13 +67,13 @@ def capture(): Dept = request.form.get('Department', '') ret, frame = cap.read(True) frame = cv2.flip(frame, 1) - _, buffer = cv2.imencode(".jpg", frame) - encoded_image = base64.b64encode(buffer).decode("utf-8") - with open(Config.image_data_file, "w") as file: - json.dump({"base64_image": encoded_image}, file) - return redirect("Image") - - + _, buffer = cv2.imencode('.jpg', frame) + encoded_image = base64.b64encode(buffer).decode('utf-8') + with open(Config.image_data_file, 'w') as file: + json.dump({'base64_image': encoded_image}, file) + return redirect('Image') + + # Route to display captured image @employee_blueprint.route('/Image', methods=['GET']) def display_image(): @@ -101,23 +102,26 @@ def display_image(): else: recent_image = None image_path = os.path.join(Config.upload_image_path[0], recent_image) - print("done") - return render_template("index.html", image_path=image_path) - -#Below route are of Recognition - -@employee_blueprint.route("/capturing", methods=["GET", "POST"]) + print('done') + return render_template('index.html', image_path=image_path) + +# Below route are of Recognition + + +@employee_blueprint.route('/capturing', methods=['GET', 'POST']) def capturing(): ret, frame = cap.read(True) frame = cv2.flip(frame, 1) - _, buffer = cv2.imencode(".jpg", frame) - encoded_image = base64.b64encode(buffer).decode("utf-8") - with open(Config.image_data_file, "w") as file: - json.dump({"base64_image": encoded_image}, file) - return redirect("Pic") - + _, buffer = cv2.imencode('.jpg', frame) + encoded_image = base64.b64encode(buffer).decode('utf-8') + with open(Config.image_data_file, 'w') as file: + json.dump({'base64_image': encoded_image}, file) + return redirect('Pic') + # Route to display captured image -@employee_blueprint.route("/Pic", methods=["GET","POST"]) + + +@employee_blueprint.route('/Pic', methods=['GET', 'POST']) def display_pic(): if os.path.exists(Config.image_data_file): with open(Config.image_data_file) as file: @@ -144,14 +148,13 @@ def display_pic(): else: recent_image = None image_path = os.path.join(Config.upload_image_path[0], recent_image) - print("done") - files = {'Face': open(os.path.join(Config.upload_image_path[0],"final.jpg"), 'rb')} + print('done') + files = {'Face': open(os.path.join( + Config.upload_image_path[0], 'final.jpg'), 'rb')} try: - fastapi_url = 'http://127.0.0.1:8000/recognize_face' + fastapi_url = 'http://127.0.0.1:8000/recognize_face' req = requests.post(fastapi_url, files=files) - data= req.content + data = req.content return (data) except Exception as e: - print("Error:", e) - - + print('Error:', e) diff --git a/FaceRec/app/main/_init_.py b/FaceRec/app/main/_init_.py index 618b344..37c57a3 100644 --- a/FaceRec/app/main/_init_.py +++ b/FaceRec/app/main/_init_.py @@ -2,11 +2,12 @@ from flask import Flask +from FaceRec.app.main.Edit import Edit_blueprint from FaceRec.app.main.Employee import flk_blueprint -from FaceRec.app.main.VideoImage import employee_blueprint from FaceRec.app.main.Face import Face_Rec_blueprint -from FaceRec.app.main.Edit import Edit_blueprint -app = Flask(__name__, template_folder="../../templates/", static_folder="../../static/") +from FaceRec.app.main.VideoImage import employee_blueprint +app = Flask(__name__, template_folder='../../templates/', + static_folder='../../static/') # To register blueprints of flask app.register_blueprint(flk_blueprint) @@ -15,5 +16,7 @@ app.register_blueprint(Edit_blueprint) # function to run server of Flast + + def run_flask(): app.run(host='127.0.0.1', port=5000) diff --git a/FaceRec/templates/edit.html b/FaceRec/templates/edit.html index 80b135c..ef1c4ae 100644 --- a/FaceRec/templates/edit.html +++ b/FaceRec/templates/edit.html @@ -203,7 +203,7 @@

Employee D