Devasy23 / FaceRec

An advanced facial recognition system designed for real-time identification using deep learning models and optimized vector search. Features include face detection, embedding generation, and scalable deployment options.
Apache License 2.0
29 stars 25 forks source link

some major improvements in FaceRec/app/main/Edit.py #73

Open bhavya2021245 opened 1 week ago

bhavya2021245 commented 1 week ago

Describe the bug The Flask application exhibits several critical issues that impact its reliability, security, and maintainability:

1.Improper Resource Management for Video Capture: The camera resource (cv2.VideoCapture(0)) is initialized at the module level and is never released. This leads to the camera remaining occupied even after the application stops, preventing other applications or subsequent runs from accessing the camera.

2.Lack of Comprehensive Error Handling and Input Validation: The application does not adequately validate user inputs, allowing invalid or malicious data to be processed. Additionally, exceptions during camera access, file operations, or HTTP requests are not gracefully handled, potentially causing the application to crash or behave unpredictably.

3.Use of Global Variables Leading to Potential Race Conditions: Global variables (EmployeeCode, Name, gender, Dept, encoded_image) are used to store temporary data across different routes. This practice can lead to race conditions in a multi-threaded environment, making the application unreliable and difficult to maintain.

To Reproduce Steps to reproduce the issues:

1.Improper Resource Management: Start the Flask application. Access the video feed via the /video_feed route. Stop the Flask application without properly releasing the camera resource. Attempt to restart the Flask application or use the camera in another application. Observe that the camera cannot be accessed due to it being locked by the previous instance.

2.Lack of Comprehensive Error Handling and Input Validation: Start the Flask application. Navigate to the /capture route. Submit the form with invalid data (e.g., non-numeric EmployeeCode, empty fields). Observe that the application may crash or behave unpredictably without providing meaningful error messages.

3.Use of Global Variables Leading to Potential Race Conditions: Start the Flask application. Simultaneously make multiple requests to the /capture route from different clients or browser tabs. Observe that global variables may be overwritten or accessed inconsistently, leading to incorrect data being processed or displayed.

Expected behavior The application should exhibit the following behaviors to address the identified issues:

1.Proper Resource Management for Video Capture: The camera resource should be initialized in a thread-safe manner and properly released when no longer needed or when the application stops. This ensures that other applications or subsequent runs can access the camera without issues.

2.Enhanced Error Handling and Input Validation: All user inputs should be validated to ensure they meet required formats and constraints. The application should handle exceptions gracefully by catching errors and providing informative feedback to the user without crashing.

3.Refactoring to Avoid Global Variables and Improve Code Structure: Temporary data should be stored within the scope of individual requests using mechanisms like Flask’s g object instead of global variables. This prevents race conditions and ensures data integrity across concurrent requests. Additionally, the code should be modularized to enhance readability and maintainability.

bhavya2021245 commented 1 week ago

Please assign me this issue under hacktoberfest-accepted and gssoc-ext

devansh-shah-11 commented 1 week ago

sure - go ahead

bhavya2021245 commented 1 week ago

You have assigned this issue to me right??

Devasy23 commented 1 week ago

yes @bhavya2021245

You have assigned this issue to me right??