Dr-Swopt 1 week ago
parent
commit
2c432fa26a

BIN
data/employees/aladdin.jpg


BIN
data/employees/ds_model_vggface_detector_opencv_aligned_normalization_base_expand_0.pkl


+ 40 - 4
grpc_server.py

@@ -1,14 +1,14 @@
+import sys
 import tempfile
 import grpc
-import sys
 from concurrent import futures
 import os
 from deepface import DeepFace
 
+
 sys.path.insert(0, os.path.join(os.path.dirname(__file__), "proto"))
+from proto import face_recognition_pb2, face_recognition_pb2_grpc
 
-from proto import face_recognition_pb2
-from proto import face_recognition_pb2_grpc
 
 EMPLOYEE_DB_PATH = "data/employees"
 
@@ -75,7 +75,43 @@ class FaceRecognitionServicer(face_recognition_pb2_grpc.FaceRecognitionServiceSe
                 try:
                     os.remove(temp_file)
                 except Exception as cleanup_error:
-                    print(f"[WARN] Could not delete temp file {temp_file}: {cleanup_error}")
+                    print(
+                        f"[WARN] Could not delete temp file {temp_file}: {cleanup_error}")
+
+    def EnrollFace(self, request, context):
+        """
+        Handles enrollment of a new employee.
+        Saves the uploaded image in EMPLOYEE_DB_PATH with the employee's name.
+        """
+        try:
+            name = request.name
+            image_bytes = request.image
+
+            if not name or not image_bytes:
+                return face_recognition_pb2.EnrollFaceResponse(
+                    success=False,
+                    message="Name or image missing"
+                )
+
+            os.makedirs(EMPLOYEE_DB_PATH, exist_ok=True)
+            file_path = os.path.join(EMPLOYEE_DB_PATH, f"{name}.jpg")
+
+            with open(file_path, "wb") as f:
+                f.write(image_bytes)
+
+            print(f"[INFO] Enrolled employee: {name}, saved at {file_path}")
+
+            return face_recognition_pb2.EnrollFaceResponse(
+                success=True,
+                message="Enrollment successful"
+            )
+
+        except Exception as e:
+            print(f"[ERROR] Enrollment failed: {e}")
+            return face_recognition_pb2.EnrollFaceResponse(
+                success=False,
+                message=str(e)
+            )
 
     def GetAllEmployees(self, request, context):
         try:

+ 5 - 2
proto/face_recognition.proto

@@ -42,8 +42,9 @@ message DeleteRequest {
   string name = 1;
 }
 
-message DeleteResponse {
+message DeleteEmployeeResponse {
   bool success = 1;
+  string message = 2;
 }
 
 // The Face Recognition Service
@@ -53,5 +54,7 @@ service FaceRecognitionService {
 
   // Updated RPC to return employee name + photo
   rpc GetAllEmployees(EmployeeListRequest) returns (EmployeeListResponse);
-  rpc DeleteEmployee(DeleteRequest) returns (DeleteResponse);
+
+  // Fixed to match the message name
+  rpc DeleteEmployee(DeleteRequest) returns (DeleteEmployeeResponse);
 }