simar007 commited on
Commit
c960a70
·
verified ·
1 Parent(s): bc318ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -1,42 +1,53 @@
1
  import gradio as gr
2
- import face_recognition
 
3
  import numpy as np
4
  from PIL import Image, ImageDraw
5
 
6
- def recognize_faces(image):
7
- # Convert uploaded image to RGB
8
- image_rgb = np.array(image.convert("RGB"))
9
 
10
- # Detect face locations and encodings
11
- face_locations = face_recognition.face_locations(image_rgb)
12
- face_encodings = face_recognition.face_encodings(image_rgb, face_locations)
 
13
 
14
- # Draw bounding boxes
15
- pil_image = Image.fromarray(image_rgb)
16
- draw = ImageDraw.Draw(pil_image)
17
 
18
- for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
19
- name = "Unknown" # Placeholder — can later be replaced with real matching logic
 
 
 
 
 
 
 
 
 
20
 
21
- # Draw box
22
- draw.rectangle(((left, top), (right, bottom)), outline=(0, 255, 0), width=3)
23
 
24
- # Label below the face
25
- text_width, text_height = draw.textsize(name)
26
- draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 255, 0))
27
- draw.text((left + 6, bottom - text_height - 5), name, fill=(0, 0, 0))
28
 
29
- del draw
30
- return pil_image
 
31
 
32
 
33
  # Gradio Interface
34
  demo = gr.Interface(
35
- fn=recognize_faces,
36
  inputs=gr.Image(type="pil", label="Upload an image"),
37
  outputs=gr.Image(label="Detected Faces"),
38
- title="Face Recognition (Future-Ready)",
39
- description="Uploads an image, detects faces, and labels them as Unknown. Can be extended for identity recognition later.",
40
  )
41
 
42
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ import cv2
3
+ import mediapipe as mp
4
  import numpy as np
5
  from PIL import Image, ImageDraw
6
 
7
+ # Initialize MediaPipe Face Detection
8
+ mp_face_detection = mp.solutions.face_detection
9
+ mp_drawing = mp.solutions.drawing_utils
10
 
11
+ def detect_faces(image):
12
+ # Convert to OpenCV format
13
+ img = np.array(image.convert("RGB"))
14
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
15
 
16
+ # Create a face detection object
17
+ with mp_face_detection.FaceDetection(model_selection=0, min_detection_confidence=0.5) as face_detection:
18
+ results = face_detection.process(img)
19
 
20
+ # Draw results
21
+ if results.detections:
22
+ for detection in results.detections:
23
+ bboxC = detection.location_data.relative_bounding_box
24
+ h, w, _ = img.shape
25
+ x, y, width, height = (
26
+ int(bboxC.xmin * w),
27
+ int(bboxC.ymin * h),
28
+ int(bboxC.width * w),
29
+ int(bboxC.height * h),
30
+ )
31
 
32
+ # Draw bounding box
33
+ cv2.rectangle(img_rgb, (x, y), (x + width, y + height), (0, 255, 0), 3)
34
 
35
+ # Add label "Unknown"
36
+ cv2.rectangle(img_rgb, (x, y - 25), (x + 100, y), (0, 255, 0), -1)
37
+ cv2.putText(img_rgb, "Unknown", (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
 
38
 
39
+ # Convert back to PIL for Gradio
40
+ result_image = Image.fromarray(cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB))
41
+ return result_image
42
 
43
 
44
  # Gradio Interface
45
  demo = gr.Interface(
46
+ fn=detect_faces,
47
  inputs=gr.Image(type="pil", label="Upload an image"),
48
  outputs=gr.Image(label="Detected Faces"),
49
+ title="Simple Face Detection (Lightweight)",
50
+ description="Detects faces using MediaPipe and labels them as 'Unknown'. Future-ready for adding identity recognition.",
51
  )
52
 
53
  if __name__ == "__main__":