-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Frontend Support for vector search function and improve face recognition #43
Changes from all commits
c8c30fc
a86697a
df1a373
c3a0f54
1362215
7f12528
096aba7
dba54f2
ee9b9dc
c2de1cc
b570ad6
2a7602f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,6 @@ venv/ | |
*.pyc | ||
.vscode/ | ||
__pyc | ||
FaceRec/static/Images/uploads/* | ||
Images/dbImages/* | ||
Images/Faces/* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import base64 | ||
import io | ||
import json | ||
import os | ||
import cv2 | ||
from flask import Blueprint | ||
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", | ||
__name__, | ||
template_folder="../../templates/", | ||
static_folder="../../static/", | ||
) | ||
|
||
cap = cv2.VideoCapture(0) | ||
|
||
|
||
# function for displaying live video | ||
def display_live_video(): | ||
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 | ||
@Edit_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 | ||
@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", "") | ||
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") | ||
|
||
|
||
# Route to display captured image | ||
@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: | ||
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") | ||
return render_template("index.html", image_path=image_path) | ||
|
||
@Edit_blueprint.route("/edit/<int:EmployeeCode>", 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: | ||
image_data = json.load(file) | ||
encoded_image = image_data.get("base64_image", "") | ||
payload = { | ||
"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 | ||
) | ||
url.status_code | ||
# logger.info(url.json()) | ||
|
||
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}") | ||
Check failure Code scanning / CodeQL Partial server-side request forgery Critical
Part of the URL of this request depends on a
user-provided value Error loading related location Loading |
||
# 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." | ||
|
||
Comment on lines
+1
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CODE REVIEW
class VideoProcessor:
# Add methods for capturing, displaying, and processing video
pass These changes will organize the code better and improve the maintainability of the video processing logic. |
Check failure
Code scanning / CodeQL
Partial server-side request forgery Critical