Anushree1 commited on
Commit
ebe0104
·
verified ·
1 Parent(s): 1d28894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -1,31 +1,44 @@
1
  import gradio as gr
2
  import cv2
3
  import os
 
 
4
 
5
  # Define the folder path where images will be saved
6
- dataset_path = "/home/user/app/dataset" # Adjust path as needed
7
-
8
- # Ensure the directory exists
9
  if not os.path.exists(dataset_path):
10
  os.makedirs(dataset_path)
11
 
12
- # Function to capture and save images
13
- def capture_image(image, name):
 
 
 
 
 
 
 
 
 
 
14
  person_folder = os.path.join(dataset_path, name)
15
- os.makedirs(person_folder, exist_ok=True) # Create a folder for each person if not exists
16
 
 
17
  image_count = len(os.listdir(person_folder))
18
  image_path = os.path.join(person_folder, f"{image_count + 1}.jpg")
19
- cv2.imwrite(image_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) # Save image in BGR format for OpenCV
20
 
21
- return f"Image saved for {name} at {image_path}"
22
 
23
- # Gradio interface for capturing images
24
  iface = gr.Interface(
25
- fn=capture_image,
26
- inputs=["webcam", "text"],
27
- outputs="text",
28
- description="Enter your name and capture your image for the dataset."
 
29
  )
30
 
 
31
  iface.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import os
4
+ 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 or your local environment
 
 
9
  if not os.path.exists(dataset_path):
10
  os.makedirs(dataset_path)
11
 
12
+ # Function to predict emotion and save the image
13
+ def capture_and_recognize(image, name):
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
+ # Create a folder for each person if not exists
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.COLOR_RGB2BGR)) # Save in BGR format for OpenCV
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=capture_and_recognize,
37
+ inputs=[gr.Image(type="pil", source="webcam"), gr.Textbox()], # Webcam input and Name input
38
+ outputs="text", # Text output for saved image path and emotion
39
+ title="Attendance Image Capture with Emotion Recognition",
40
+ description="Capture an image via webcam, enter your name, and the image will be saved with emotion recognition."
41
  )
42
 
43
+ # Launch the Gradio app
44
  iface.launch()