Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +61 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
|
| 5 |
+
# Initialize the MediaPipe Face Detection and Drawing utilities
|
| 6 |
+
mp_face_detection = mp.solutions.face_detection
|
| 7 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 8 |
+
|
| 9 |
+
# Function to detect faces and save each detected face as an image
|
| 10 |
+
def detect_and_save_faces(input_image_path, output_folder):
|
| 11 |
+
if not os.path.exists(output_folder):
|
| 12 |
+
os.makedirs(output_folder)
|
| 13 |
+
|
| 14 |
+
# Read the image
|
| 15 |
+
image = cv2.imread(input_image_path)
|
| 16 |
+
if image is None:
|
| 17 |
+
print(f"Error loading image: {input_image_path}")
|
| 18 |
+
return
|
| 19 |
+
|
| 20 |
+
# Convert image to RGB for MediaPipe
|
| 21 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 22 |
+
|
| 23 |
+
# Detect faces
|
| 24 |
+
with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
|
| 25 |
+
results = face_detection.process(image_rgb)
|
| 26 |
+
|
| 27 |
+
# Draw face detections and save each face
|
| 28 |
+
if results.detections:
|
| 29 |
+
for idx, detection in enumerate(results.detections):
|
| 30 |
+
bboxC = detection.location_data.relative_bounding_box
|
| 31 |
+
h, w, _ = image.shape
|
| 32 |
+
|
| 33 |
+
# Get the bounding box coordinates
|
| 34 |
+
x_min = int(bboxC.xmin * w)
|
| 35 |
+
y_min = int(bboxC.ymin * h)
|
| 36 |
+
box_width = int(bboxC.width * w)
|
| 37 |
+
box_height = int(bboxC.height * h)
|
| 38 |
+
|
| 39 |
+
# Extract the face from the image
|
| 40 |
+
face_image = image[y_min:y_min + box_height, x_min:x_min + box_width]
|
| 41 |
+
|
| 42 |
+
# Save the detected face
|
| 43 |
+
face_output_path = os.path.join(output_folder, f"face_{idx+1}.jpg")
|
| 44 |
+
cv2.imwrite(face_output_path, face_image)
|
| 45 |
+
print(f"Saved face {idx+1} to {face_output_path}")
|
| 46 |
+
|
| 47 |
+
# Display the original image with bounding boxes drawn on detected faces
|
| 48 |
+
for detection in results.detections:
|
| 49 |
+
mp_drawing.draw_detection(image, detection)
|
| 50 |
+
|
| 51 |
+
# Save the image with the detected faces
|
| 52 |
+
output_image_path = os.path.join(output_folder, "detected_faces_image.jpg")
|
| 53 |
+
cv2.imwrite(output_image_path, image)
|
| 54 |
+
print(f"Saved image with detected faces to {output_image_path}")
|
| 55 |
+
|
| 56 |
+
# Paths
|
| 57 |
+
input_image_path = "WhatsApp Image 2024-10-08 at 10.09.07_c47dc282.jpg" # Provide your input image path here
|
| 58 |
+
output_folder = "output" # Folder to save detected faces
|
| 59 |
+
|
| 60 |
+
# Run the face detection and saving process
|
| 61 |
+
detect_and_save_faces(input_image_path, output_folder)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
mediapipe
|
| 2 |
+
opencv-python
|