Anushree1 commited on
Commit
1347891
·
verified ·
1 Parent(s): 532c75c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
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 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
 
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