Akshayram1 commited on
Commit
70e9024
·
verified ·
1 Parent(s): a8b36a4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import mediapipe as mp
4
+ import streamlit as st
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ # Initialize the MediaPipe Face Detection and Drawing utilities
9
+ mp_face_detection = mp.solutions.face_detection
10
+ mp_drawing = mp.solutions.drawing_utils
11
+
12
+ # Function to detect faces and save each detected face as an image
13
+ def detect_and_save_faces(image, output_folder="output"):
14
+ if not os.path.exists(output_folder):
15
+ os.makedirs(output_folder)
16
+
17
+ # Convert image to RGB for MediaPipe
18
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
19
+
20
+ # Detect faces
21
+ with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
22
+ results = face_detection.process(image_rgb)
23
+
24
+ face_images = []
25
+ # Draw face detections and save each face
26
+ if results.detections:
27
+ for idx, detection in enumerate(results.detections):
28
+ bboxC = detection.location_data.relative_bounding_box
29
+ h, w, _ = image.shape
30
+
31
+ # Get the bounding box coordinates
32
+ x_min = int(bboxC.xmin * w)
33
+ y_min = int(bboxC.ymin * h)
34
+ box_width = int(bboxC.width * w)
35
+ box_height = int(bboxC.height * h)
36
+
37
+ # Extract the face from the image
38
+ face_image = image[y_min:y_min + box_height, x_min:x_min + box_width]
39
+ face_images.append(face_image)
40
+
41
+ # Save the detected face
42
+ face_output_path = os.path.join(output_folder, f"face_{idx+1}.jpg")
43
+ cv2.imwrite(face_output_path, face_image)
44
+
45
+ # Draw bounding boxes on the original image
46
+ for detection in results.detections:
47
+ mp_drawing.draw_detection(image, detection)
48
+
49
+ return image, face_images
50
+
51
+ # Streamlit UI for face detection
52
+ st.title("Face Detection and Extraction")
53
+
54
+ # File uploader
55
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
56
+
57
+ if uploaded_file is not None:
58
+ # Convert the uploaded file to an OpenCV image
59
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
60
+ image = cv2.imdecode(file_bytes, 1)
61
+
62
+ # Run face detection and save faces
63
+ output_folder = "output"
64
+ detected_image, face_images = detect_and_save_faces(image, output_folder)
65
+
66
+ # Display the original image with detected faces
67
+ st.subheader("Original Image with Detected Faces")
68
+ st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected Faces", use_column_width=True)
69
+
70
+ # Display the extracted faces
71
+ st.subheader("Extracted Faces")
72
+ if face_images:
73
+ for idx, face in enumerate(face_images):
74
+ st.image(cv2.cvtColor(face, cv2.COLOR_BGR2RGB), caption=f"Face {idx+1}", use_column_width=False)
75
+ else:
76
+ st.write("No faces detected.")