Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,39 +5,41 @@ import numpy as np
|
|
5 |
from deepface import DeepFace
|
6 |
|
7 |
# Define the folder path where images will be saved
|
8 |
-
dataset_path = "/content/dataset" # For Colab
|
|
|
|
|
9 |
if not os.path.exists(dataset_path):
|
10 |
os.makedirs(dataset_path)
|
11 |
|
12 |
-
# Function to
|
13 |
-
def
|
14 |
-
# Convert Gradio image (PIL format) to OpenCV image
|
15 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
16 |
-
|
17 |
# Analyze the emotion using DeepFace
|
18 |
result = DeepFace.analyze(img, actions=['emotion'])
|
19 |
|
20 |
# Get the dominant emotion
|
21 |
dominant_emotion = result[0]['dominant_emotion']
|
22 |
|
23 |
-
#
|
24 |
person_folder = os.path.join(dataset_path, name)
|
25 |
-
os.makedirs(person_folder, exist_ok=True)
|
26 |
|
27 |
-
# Save the image in the person's folder
|
28 |
image_count = len(os.listdir(person_folder))
|
29 |
image_path = os.path.join(person_folder, f"{image_count + 1}.jpg")
|
30 |
-
cv2.imwrite(image_path, cv2.cvtColor(img, cv2.
|
31 |
|
32 |
return f"Image saved for {name} with emotion: {dominant_emotion} at {image_path}"
|
33 |
|
34 |
# Define the Gradio interface
|
35 |
iface = gr.Interface(
|
36 |
-
fn=
|
37 |
-
inputs=[gr.Image(type="pil"
|
38 |
-
outputs="text",
|
39 |
-
title="
|
40 |
-
description="Capture an image
|
|
|
41 |
)
|
42 |
|
43 |
# Launch the Gradio app
|
|
|
5 |
from deepface import DeepFace
|
6 |
|
7 |
# Define the folder path where images will be saved
|
8 |
+
dataset_path = "/content/dataset" # For Colab, this will save in your Colab environment
|
9 |
+
|
10 |
+
# Ensure the directory exists
|
11 |
if not os.path.exists(dataset_path):
|
12 |
os.makedirs(dataset_path)
|
13 |
|
14 |
+
# Function to capture, save image, and predict emotion
|
15 |
+
def capture_and_predict(image, name):
|
16 |
+
# Convert Gradio image (PIL format) to an OpenCV image
|
17 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
18 |
+
|
19 |
# Analyze the emotion using DeepFace
|
20 |
result = DeepFace.analyze(img, actions=['emotion'])
|
21 |
|
22 |
# Get the dominant emotion
|
23 |
dominant_emotion = result[0]['dominant_emotion']
|
24 |
|
25 |
+
# Save the image with a timestamp in the dataset folder
|
26 |
person_folder = os.path.join(dataset_path, name)
|
27 |
+
os.makedirs(person_folder, exist_ok=True) # Create a folder for each person if not exists
|
28 |
|
|
|
29 |
image_count = len(os.listdir(person_folder))
|
30 |
image_path = os.path.join(person_folder, f"{image_count + 1}.jpg")
|
31 |
+
cv2.imwrite(image_path, cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # Save the image in RGB format for consistency
|
32 |
|
33 |
return f"Image saved for {name} with emotion: {dominant_emotion} at {image_path}"
|
34 |
|
35 |
# Define the Gradio interface
|
36 |
iface = gr.Interface(
|
37 |
+
fn=capture_and_predict,
|
38 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Enter your name")],
|
39 |
+
outputs="text",
|
40 |
+
title="Capture and Predict Facial Emotion",
|
41 |
+
description="Capture an image from your webcam, enter your name, and the system will predict your emotion and save the image.",
|
42 |
+
live=True
|
43 |
)
|
44 |
|
45 |
# Launch the Gradio app
|