aniudupa commited on
Commit
5b10e55
·
verified ·
1 Parent(s): 63d87c2

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -33
main.py CHANGED
@@ -30,10 +30,9 @@ def load_student_encodings(folder_path: str):
30
  if not os.path.exists(folder_path):
31
  return {"error": f"Folder '{folder_path}' does not exist"}
32
 
33
- # Create face detector and feature extractor
34
  face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
35
- face_recognizer = cv2.face.LBPHFaceRecognizer_create()
36
-
37
  # Check if a model already exists for this folder
38
  model_path = os.path.join(folder_path, "face_model.pkl")
39
  if os.path.exists(model_path):
@@ -73,38 +72,41 @@ def load_student_encodings(folder_path: str):
73
 
74
  def process_image(image_array, students):
75
  """Compare uploaded image with stored student images using OpenCV."""
76
- # Convert to grayscale
77
- gray = cv2.cvtColor(image_array, cv2.COLOR_BGR2GRAY)
78
-
79
- # Initialize face detector
80
- face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
81
-
82
- # Detect faces
83
- faces = face_detector.detectMultiScale(gray, 1.3, 5)
84
-
85
- if len(faces) == 0:
86
- return {"error": "No faces detected in the image"}
87
-
88
- # List to store recognized students
89
- present_students = set()
90
-
91
- # Compare each detected face with student face images
92
- for (x, y, w, h) in faces:
93
- face_img = gray[y:y+h, x:x+w]
94
- face_img = cv2.resize(face_img, (100, 100))
95
 
96
- for name, student_face in students.items():
97
- # Simple template matching for demonstration
98
- # In a production system, you'd use a proper face recognition algorithm
99
- result = cv2.matchTemplate(face_img, student_face, cv2.TM_CCOEFF_NORMED)
100
- similarity = np.max(result)
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # Threshold for recognition
103
- if similarity > 0.6:
104
- present_students.add(name)
105
-
106
- missing_students = set(students.keys()) - present_students
107
- return {"missing_students": list(missing_students)}
 
 
 
 
 
 
 
 
108
 
109
  @app.post("/create_folder/")
110
  async def create_folder(org_name: str = Form(...), folder_name: str = Form(...), files: List[UploadFile] = File(...)):
 
30
  if not os.path.exists(folder_path):
31
  return {"error": f"Folder '{folder_path}' does not exist"}
32
 
33
+ # Create face detector
34
  face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
35
+
 
36
  # Check if a model already exists for this folder
37
  model_path = os.path.join(folder_path, "face_model.pkl")
38
  if os.path.exists(model_path):
 
72
 
73
  def process_image(image_array, students):
74
  """Compare uploaded image with stored student images using OpenCV."""
75
+ try:
76
+ # Convert to grayscale
77
+ gray = cv2.cvtColor(image_array, cv2.COLOR_BGR2GRAY)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ # Initialize face detector
80
+ face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
81
+
82
+ # Detect faces
83
+ faces = face_detector.detectMultiScale(gray, 1.3, 5)
84
+
85
+ if len(faces) == 0:
86
+ return {"error": "No faces detected in the image"}
87
+
88
+ # List to store recognized students
89
+ present_students = set()
90
+
91
+ # Compare each detected face with student face images
92
+ for (x, y, w, h) in faces:
93
+ face_img = gray[y:y+h, x:x+w]
94
+ face_img = cv2.resize(face_img, (100, 100))
95
 
96
+ for name, student_face in students.items():
97
+ # Simple template matching for demonstration
98
+ # In a production system, you'd use a proper face recognition algorithm
99
+ result = cv2.matchTemplate(face_img, student_face, cv2.TM_CCOEFF_NORMED)
100
+ similarity = np.max(result)
101
+
102
+ # Threshold for recognition
103
+ if similarity > 0.6:
104
+ present_students.add(name)
105
+
106
+ missing_students = set(students.keys()) - present_students
107
+ return {"present_students": list(present_students), "missing_students": list(missing_students)}
108
+ except Exception as e:
109
+ return {"error": f"Error processing image: {str(e)}"}
110
 
111
  @app.post("/create_folder/")
112
  async def create_folder(org_name: str = Form(...), folder_name: str = Form(...), files: List[UploadFile] = File(...)):