From ef830d3aad3b6223b7b7cc819938bee5e61c0952 Mon Sep 17 00:00:00 2001 From: Bhavya Gupta Date: Sun, 13 Oct 2024 13:47:44 +0530 Subject: [PATCH 1/3] Update Edit.py --- FaceRec/app/main/Edit.py | 302 ++++++++++++++++++--------------------- 1 file changed, 137 insertions(+), 165 deletions(-) diff --git a/FaceRec/app/main/Edit.py b/FaceRec/app/main/Edit.py index e5956de..51a8b89 100644 --- a/FaceRec/app/main/Edit.py +++ b/FaceRec/app/main/Edit.py @@ -7,11 +7,9 @@ import cv2 import requests -from flask import Blueprint -from flask import Response as flask_response -from flask import redirect, render_template, request +from flask import Blueprint, Response as flask_response, redirect, render_template, request, g, flash from PIL import Image - +from contextlib import contextmanager from FaceRec.config import Config Edit_blueprint = Blueprint( @@ -21,191 +19,165 @@ static_folder="../../static/", ) -cap = cv2.VideoCapture(0) + +@contextmanager +def open_camera(): + """Context manager to open and release the camera resource.""" + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + raise Exception("Cannot open camera") + try: + yield cap + finally: + cap.release() + + +def validate_input(data: dict) -> bool: + """Validates the input form data.""" + if not data.get("EmployeeCode").isdigit(): + flash("Invalid EmployeeCode. It should be numeric.") + return False + if not data.get("Name"): + flash("Name cannot be empty.") + return False + if not data.get("gender"): + flash("Gender cannot be empty.") + return False + if not data.get("Department"): + flash("Department cannot be empty.") + return False + return True -# function for displaying live video def display_live_video(): - """ - Generator for displaying live video from the camera. - - Yields frames as JPEG images. - """ - while True: - success, frame = cap.read() # Read a frame from the camera - if not success: - break - frame = cv2.flip(frame, 1) - 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" - ) - - -# Route for displaying video + """Generator for displaying live video from the camera.""" + with open_camera() as cap: + while True: + success, frame = cap.read() + if not success: + break + frame = cv2.flip(frame, 1) + ret, buffer = cv2.imencode(".jpg", frame) + if not ret: + break + yield ( + b"--frame\r\n" + b"Content-Type: image/jpeg\r\n\r\n" + buffer.tobytes() + b"\r\n\r\n" + ) + + @Edit_blueprint.route("/video_feed") def video_feed(): - """Route for displaying live video from the camera. - - Returns a multipart response with a JPEG image for each frame from the camera. - """ + """Route for displaying live video from the camera.""" return flask_response( display_live_video(), - mimetype="multipart/x-mixed-replace;boundary=frame", + 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=["POST"]) def capture(): - """Route for capturing an image from the video feed. - - This route is used to capture a single frame from the video feed and save it to a file. - The frame is flipped horizontally before saving. - - The image is stored in a file specified by the `Config.image_data_file` variable. - - The response is a redirect to the "Image" route, which displays the captured image. - - The request is expected to be a POST request with the following form data: - - EmployeeCode: The employee code for the person in the image. - - Name: The name of the person in the image. - - gender: The gender of the person in the image. - - Department: The department of the person in the image. - """ - 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", "") - 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) + """Route for capturing an image from the video feed.""" + form_data = { + "EmployeeCode": request.form.get("EmployeeCode", ""), + "Name": request.form.get("Name", ""), + "gender": request.form.get("gender", ""), + "Department": request.form.get("Department", "") + } + + if not validate_input(form_data): + return redirect("capture") + + try: + with open_camera() as cap: + ret, frame = cap.read() + if not ret: + flash("Failed to capture the image.") + return redirect("capture") + + frame = cv2.flip(frame, 1) + _, buffer = cv2.imencode(".jpg", frame) + encoded_image = base64.b64encode(buffer).decode("utf-8") + + g.employee_data = form_data + g.encoded_image = encoded_image + + with open(Config.image_data_file, "w") as file: + json.dump({"base64_image": encoded_image}, file) + except Exception as e: + flash(f"Error capturing image: {e}") + return redirect("capture") + return redirect("Image") -# Route to display captured image @Edit_blueprint.route("/Image", methods=["GET"]) def display_image(): - """Route to display the captured image. - - This route reads the image data from a file specified by the - `Config.image_data_file` variable and displays it in the template. - - The image is saved to a file in the directory specified by the - `Config.upload_image_path` variable. - - The most recent image is displayed. - - The image is displayed in the template with the name "image_path". - - Returns: - A rendered template with the image path. - """ - if os.path.exists(Config.image_data_file): - with open(Config.image_data_file) as file: - image_data = json.load(file) - 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, - ) - image = sorted( - os.listdir(Config.upload_image_path[0]), - key=lambda x: os.path.getatime( - os.path.join(Config.upload_image_path[0], x), - ), - reverse=True, - ) - if image: - recent_image = image[0] - image_path = os.path.join(Config.upload_image_path[0], recent_image) - else: - recent_image = None - image_path = os.path.join(Config.upload_image_path[0], recent_image) - print("done") + """Route to display the captured image.""" + try: + if os.path.exists(Config.image_data_file): + with open(Config.image_data_file) as file: + image_data = json.load(file) + + 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) + + image_files = sorted( + os.listdir(Config.upload_image_path[0]), + key=lambda x: os.path.getatime(os.path.join(Config.upload_image_path[0], x)), + reverse=True + ) + recent_image = image_files[0] if image_files else None + else: + recent_image = None + except Exception as e: + flash(f"Error loading image: {e}") + return render_template("index.html", image_path=None) + + image_path = os.path.join(Config.upload_image_path[0], recent_image) if recent_image else None return render_template("index.html", image_path=image_path) @Edit_blueprint.route("/edit/", methods=["POST", "GET"]) def edit(EmployeeCode): - """Edit an existing employee. - - This route allows users to edit an existing employee record. The - employee is identified by the EmployeeCode, which is a required - parameter. - - The route accepts both GET and POST requests. A GET request will - retrieve the employee data from the database and display it in - the template. A POST request will update the employee data in the - database with the values provided in the form. - - The form data is expected to contain the following fields: - - - Name - - gender - - Department - - The image is expected to be stored in the `Config.image_data_file` - file. - - The most recent image is displayed. - - The image is displayed in the template with the name "image_path". - - Returns: - A rendered template with the image path if the request is a - GET, or a redirect to the home page if the request is a POST. - """ + """Edit an existing employee.""" 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", "") - payload = { - "Name": Name, - "gender": gender, - "Department": Department, - "Image": encoded_image, + form_data = { + "Name": request.form["Name"], + "gender": request.form["Gender"], + "Department": request.form["Department"] } - # logger.info(payload) + + if not validate_input(form_data): + return redirect(f"/edit/{EmployeeCode}") + try: - url = requests.put( - f"http://127.0.0.1:8000/update/{EmployeeCode}", - json=payload, - ) - url.status_code - # logger.info(url.json()) + with open(Config.image_data_file) as file: + image_data = json.load(file) - return redirect("/") + encoded_image = image_data.get("base64_image", "") + payload = {**form_data, "Image": encoded_image} + response = requests.put(f"http://127.0.0.1:8000/update/{EmployeeCode}", json=payload) + if response.status_code != 200: + flash(f"Failed to update employee: {response.status_code}") + 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}") - # logger.info(response.status_code) - # logger.info(response.json()) - if response.status_code == 200: - employee_data = response.json() - return render_template("edit.html", employee_data=employee_data) - else: - return f"Error {response.status_code}: Failed to retrieve employee data." + flash(f"Request failed: {e}") + return redirect(f"/edit/{EmployeeCode}") + + try: + response = requests.get(f"http://127.0.0.1:8000/read/{EmployeeCode}") + if response.status_code == 200: + employee_data = response.json() + return render_template("edit.html", employee_data=employee_data) + else: + flash(f"Error {response.status_code}: Failed to retrieve employee data.") + return render_template("edit.html", employee_data=None) + except requests.exceptions.RequestException as e: + flash(f"Error fetching employee data: {e}") + return render_template("edit.html", employee_data=None) From 4b8c5f248b2bfcd421be00c70c0fa914bc57f674 Mon Sep 17 00:00:00 2001 From: Bhavya Gupta Date: Sun, 13 Oct 2024 13:51:49 +0530 Subject: [PATCH 2/3] Update Edit.py --- FaceRec/app/main/Edit.py | 78 +++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/FaceRec/app/main/Edit.py b/FaceRec/app/main/Edit.py index 51a8b89..ce9c580 100644 --- a/FaceRec/app/main/Edit.py +++ b/FaceRec/app/main/Edit.py @@ -9,7 +9,6 @@ import requests from flask import Blueprint, Response as flask_response, redirect, render_template, request, g, flash from PIL import Image -from contextlib import contextmanager from FaceRec.config import Config Edit_blueprint = Blueprint( @@ -19,16 +18,20 @@ static_folder="../../static/", ) +# Global variable for video capture +cap = cv2.VideoCapture(0) -@contextmanager -def open_camera(): - """Context manager to open and release the camera resource.""" - cap = cv2.VideoCapture(0) +def initialize_camera(): + """Initialize the camera resource if it's not already opened.""" + global cap if not cap.isOpened(): - raise Exception("Cannot open camera") - try: - yield cap - finally: + cap.open(0) + + +def release_camera(): + """Release the camera resource when no longer needed.""" + global cap + if cap.isOpened(): cap.release() @@ -51,19 +54,19 @@ def validate_input(data: dict) -> bool: def display_live_video(): """Generator for displaying live video from the camera.""" - with open_camera() as cap: - while True: - success, frame = cap.read() - if not success: - break - frame = cv2.flip(frame, 1) - ret, buffer = cv2.imencode(".jpg", frame) - if not ret: - break - yield ( - b"--frame\r\n" - b"Content-Type: image/jpeg\r\n\r\n" + buffer.tobytes() + b"\r\n\r\n" - ) + initialize_camera() # Ensure camera is initialized + while True: + success, frame = cap.read() + if not success: + break + frame = cv2.flip(frame, 1) + ret, buffer = cv2.imencode(".jpg", frame) + if not ret: + break + yield ( + b"--frame\r\n" + b"Content-Type: image/jpeg\r\n\r\n" + buffer.tobytes() + b"\r\n\r\n" + ) @Edit_blueprint.route("/video_feed") @@ -89,21 +92,21 @@ def capture(): return redirect("capture") try: - with open_camera() as cap: - ret, frame = cap.read() - if not ret: - flash("Failed to capture the image.") - return redirect("capture") - - frame = cv2.flip(frame, 1) - _, buffer = cv2.imencode(".jpg", frame) - encoded_image = base64.b64encode(buffer).decode("utf-8") - - g.employee_data = form_data - g.encoded_image = encoded_image - - with open(Config.image_data_file, "w") as file: - json.dump({"base64_image": encoded_image}, file) + initialize_camera() # Ensure camera is initialized + ret, frame = cap.read() + if not ret: + flash("Failed to capture the image.") + return redirect("capture") + + frame = cv2.flip(frame, 1) + _, buffer = cv2.imencode(".jpg", frame) + encoded_image = base64.b64encode(buffer).decode("utf-8") + + g.employee_data = form_data + g.encoded_image = encoded_image + + with open(Config.image_data_file, "w") as file: + json.dump({"base64_image": encoded_image}, file) except Exception as e: flash(f"Error capturing image: {e}") return redirect("capture") @@ -181,3 +184,4 @@ def edit(EmployeeCode): except requests.exceptions.RequestException as e: flash(f"Error fetching employee data: {e}") return render_template("edit.html", employee_data=None) + From 6b876712f1ea18959539b6a108374adf328760b3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Oct 2024 08:27:22 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- FaceRec/app/main/Edit.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/FaceRec/app/main/Edit.py b/FaceRec/app/main/Edit.py index ce9c580..4749798 100644 --- a/FaceRec/app/main/Edit.py +++ b/FaceRec/app/main/Edit.py @@ -7,8 +7,11 @@ import cv2 import requests -from flask import Blueprint, Response as flask_response, redirect, render_template, request, g, flash +from flask import Blueprint +from flask import Response as flask_response +from flask import flash, g, redirect, render_template, request from PIL import Image + from FaceRec.config import Config Edit_blueprint = Blueprint( @@ -21,6 +24,7 @@ # Global variable for video capture cap = cv2.VideoCapture(0) + def initialize_camera(): """Initialize the camera resource if it's not already opened.""" global cap @@ -85,7 +89,7 @@ def capture(): "EmployeeCode": request.form.get("EmployeeCode", ""), "Name": request.form.get("Name", ""), "gender": request.form.get("gender", ""), - "Department": request.form.get("Department", "") + "Department": request.form.get("Department", ""), } if not validate_input(form_data): @@ -101,7 +105,7 @@ def capture(): frame = cv2.flip(frame, 1) _, buffer = cv2.imencode(".jpg", frame) encoded_image = base64.b64encode(buffer).decode("utf-8") - + g.employee_data = form_data g.encoded_image = encoded_image @@ -121,18 +125,21 @@ def display_image(): if os.path.exists(Config.image_data_file): with open(Config.image_data_file) as file: image_data = json.load(file) - + 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) + image.save(os.path.join( + Config.upload_image_path[0], filename), quality=100) image_files = sorted( os.listdir(Config.upload_image_path[0]), - key=lambda x: os.path.getatime(os.path.join(Config.upload_image_path[0], x)), - reverse=True + key=lambda x: os.path.getatime( + os.path.join(Config.upload_image_path[0], x) + ), + reverse=True, ) recent_image = image_files[0] if image_files else None else: @@ -141,7 +148,11 @@ def display_image(): flash(f"Error loading image: {e}") return render_template("index.html", image_path=None) - image_path = os.path.join(Config.upload_image_path[0], recent_image) if recent_image else None + image_path = ( + os.path.join(Config.upload_image_path[0], recent_image) + if recent_image + else None + ) return render_template("index.html", image_path=image_path) @@ -152,7 +163,7 @@ def edit(EmployeeCode): form_data = { "Name": request.form["Name"], "gender": request.form["Gender"], - "Department": request.form["Department"] + "Department": request.form["Department"], } if not validate_input(form_data): @@ -165,7 +176,9 @@ def edit(EmployeeCode): encoded_image = image_data.get("base64_image", "") payload = {**form_data, "Image": encoded_image} - response = requests.put(f"http://127.0.0.1:8000/update/{EmployeeCode}", json=payload) + response = requests.put( + f"http://127.0.0.1:8000/update/{EmployeeCode}", json=payload + ) if response.status_code != 200: flash(f"Failed to update employee: {response.status_code}") return redirect("/") @@ -179,9 +192,9 @@ def edit(EmployeeCode): employee_data = response.json() return render_template("edit.html", employee_data=employee_data) else: - flash(f"Error {response.status_code}: Failed to retrieve employee data.") + flash( + f"Error {response.status_code}: Failed to retrieve employee data.") return render_template("edit.html", employee_data=None) except requests.exceptions.RequestException as e: flash(f"Error fetching employee data: {e}") return render_template("edit.html", employee_data=None) -