AniBirage commited on
Commit
38c84fb
·
verified ·
1 Parent(s): c3ef9a9

Upload folder using huggingface_hub

Browse files
Assignment.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from keras.models import load_model
4
+ import gradio as gr
5
+
6
+ # Load pre-trained model
7
+ model = load_model("emotion_detection_model.h5")
8
+
9
+ # Load OpenCV's pre-trained face detector
10
+ face_cascade = cv2.CascadeClassifier(
11
+ cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
12
+ )
13
+
14
+ # Define emotion labels
15
+ EMOTIONS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
16
+
17
+
18
+ # Function to detect and display emotions on faces
19
+ def detect_emotions(image):
20
+ frame = image.copy()
21
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
22
+ faces = face_cascade.detectMultiScale(gray, 1.3, 5)
23
+
24
+ for x, y, w, h in faces:
25
+ face_roi = gray[y : y + h, x : x + w]
26
+ resized_roi = cv2.resize(face_roi, (48, 48))
27
+ normalized_roi = resized_roi / 255.0
28
+ reshaped_roi = normalized_roi.reshape(-1, 48, 48, 1)
29
+
30
+ # Predict emotions
31
+ predictions = model.predict(reshaped_roi)
32
+ emotion_label = EMOTIONS[np.argmax(predictions)]
33
+
34
+ # Display emotion label on face
35
+ cv2.putText(
36
+ frame,
37
+ emotion_label,
38
+ (x, y - 10),
39
+ cv2.FONT_HERSHEY_SIMPLEX,
40
+ 0.9,
41
+ (36, 255, 12),
42
+ 2,
43
+ )
44
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
45
+
46
+ return frame
47
+
48
+
49
+ # Define Gradio interface
50
+ iface = gr.Interface(
51
+ fn=detect_emotions,
52
+ inputs="image",
53
+ outputs="image",
54
+ title="Emotion Detection",
55
+ description="Upload an Image to Detect Emotions in Faces",
56
+ )
57
+
58
+ # Run Gradio app
59
+ iface.launch(share=True)
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
  title: DD
3
- emoji: 🏢
4
- colorFrom: green
5
- colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.25.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: DD
3
+ app_file: Assignment.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.25.0
 
 
6
  ---
 
 
emotion_detection_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ccfaeabadde5ff6fa9b82e01a12f9d13e96ada7b17798ead57d247d81941993f
3
+ size 17366976
haarcascade_frontalface_default.xml ADDED
The diff for this file is too large to render. See raw diff