Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,61 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
-
from PIL import Image
|
| 4 |
import random
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
"""
|
| 13 |
if image_option == "Upload My Image" and uploaded_image is not None:
|
| 14 |
img = uploaded_image
|
| 15 |
-
|
| 16 |
else:
|
| 17 |
-
img_path = random.choice(
|
| 18 |
img = Image.open(img_path)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
with gr.Blocks() as demo:
|
| 24 |
gr.Markdown("## Face Recognition Test Run")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
submit = gr.Button("Process Image")
|
| 30 |
output_image = gr.Image(label="Processed Image")
|
| 31 |
status = gr.Textbox(label="Status")
|
| 32 |
-
|
| 33 |
-
submit.click(
|
| 34 |
-
|
| 35 |
if __name__ == "__main__":
|
| 36 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import face_recognition
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import os
|
|
|
|
| 5 |
import random
|
| 6 |
|
| 7 |
+
# Path to the data folder containing sample images
|
| 8 |
+
DATA_FOLDER = "data/"
|
| 9 |
|
| 10 |
+
# Load sample images from the data folder
|
| 11 |
+
sample_images = [os.path.join(DATA_FOLDER, img) for img in os.listdir(DATA_FOLDER) if img.lower().endswith(('png', 'jpg', 'jpeg'))]
|
| 12 |
+
|
| 13 |
+
def recognize_faces(image_option, uploaded_image):
|
| 14 |
"""
|
| 15 |
+
Perform face recognition on the selected image.
|
| 16 |
"""
|
| 17 |
if image_option == "Upload My Image" and uploaded_image is not None:
|
| 18 |
img = uploaded_image
|
| 19 |
+
status = "Processed the uploaded image."
|
| 20 |
else:
|
| 21 |
+
img_path = random.choice(sample_images)
|
| 22 |
img = Image.open(img_path)
|
| 23 |
+
status = f"Processed a sample image: {os.path.basename(img_path)}"
|
| 24 |
+
|
| 25 |
+
# Convert PIL image to numpy array
|
| 26 |
+
img_array = face_recognition.load_image_file(img)
|
| 27 |
+
|
| 28 |
+
# Find all face locations and face encodings in the image
|
| 29 |
+
face_locations = face_recognition.face_locations(img_array)
|
| 30 |
+
face_encodings = face_recognition.face_encodings(img_array, face_locations)
|
| 31 |
+
|
| 32 |
+
# Convert back to PIL image for drawing
|
| 33 |
+
pil_image = Image.fromarray(img_array)
|
| 34 |
+
draw = ImageDraw.Draw(pil_image)
|
| 35 |
+
|
| 36 |
+
# Iterate over each face found in the image
|
| 37 |
+
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
|
| 38 |
+
# Draw a box around the face
|
| 39 |
+
draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255), width=2)
|
| 40 |
+
|
| 41 |
+
# Clean up the drawing library
|
| 42 |
+
del draw
|
| 43 |
|
| 44 |
+
return pil_image, status
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("## Face Recognition Test Run")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
image_option = gr.Radio(["Upload My Image", "Use Sample Image"], label="Select an Option")
|
| 52 |
+
uploaded_image = gr.Image(label="Upload Image (if selected)", type="pil", interactive=True)
|
| 53 |
+
|
| 54 |
submit = gr.Button("Process Image")
|
| 55 |
output_image = gr.Image(label="Processed Image")
|
| 56 |
status = gr.Textbox(label="Status")
|
| 57 |
+
|
| 58 |
+
submit.click(recognize_faces, inputs=[image_option, uploaded_image], outputs=[output_image, status])
|
| 59 |
+
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|