SS8297 commited on
Commit
4d49d54
·
1 Parent(s): ad76a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -9
app.py CHANGED
@@ -1,8 +1,16 @@
1
  import streamlit as st
2
- from streamlit_webrtc import webrtc_streamer
3
  import av
4
  import os
5
  from twilio.rest import Client
 
 
 
 
 
 
 
 
6
 
7
  os.environ["TWILIO_ACCOUNT_SID"] = "ACf1e76f3fd6e9cbca940decc4ed443c20"
8
  os.environ["TWILIO_AUTH_TOKEN"] = "56a1d1ee494933269fe042706392ac9f"
@@ -21,21 +29,105 @@ def get_ice_servers():
21
  token = client.tokens.create()
22
 
23
  return token.ice_servers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
25
  def video_frame_callback(frame):
26
  img = frame.to_ndarray(format="bgr24")
27
 
28
- flipped = img[::-1,:,:] if flip else img
 
 
 
 
 
 
29
 
30
- return av.VideoFrame.from_ndarray(flipped, format="bgr24")
 
 
 
 
 
 
31
 
 
32
 
33
- flip = st.checkbox("Flip")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- webrtc_streamer(
36
- key="example",
37
- video_frame_callback=video_frame_callback,
38
- rtc_configuration={ "iceServers": get_ice_servers() }
39
- )
40
 
 
41
 
 
1
  import streamlit as st
2
+ from streamlit_webrtc import webrtc_streamer, WebRtcMode
3
  import av
4
  import os
5
  from twilio.rest import Client
6
+ from streamlit_image_select import image_select
7
+ import cv2 as cv
8
+ import numpy as np
9
+ import math
10
+ from feat import Detector
11
+ from feat.utils import FEAT_EMOTION_COLUMNS
12
+ import torch
13
+ from PIL import Image
14
 
15
  os.environ["TWILIO_ACCOUNT_SID"] = "ACf1e76f3fd6e9cbca940decc4ed443c20"
16
  os.environ["TWILIO_AUTH_TOKEN"] = "56a1d1ee494933269fe042706392ac9f"
 
29
  token = client.tokens.create()
30
 
31
  return token.ice_servers
32
+
33
+ def eye_aspect_ratio(eye):
34
+
35
+ A = math.dist(eye[1], eye[5])
36
+ B = math.dist(eye[2], eye[4])
37
+
38
+ C = math.dist(eye[0], eye[3])
39
+
40
+ ear = (A + B) / (2.0 * C)
41
+
42
+ return ear
43
+
44
+ def detect_eyes(landmarks, img, threshold):
45
+ lm = landmarks
46
+ eyes = np.array(lm[0][0][36:48], np.int32)
47
+
48
+ left_eye = eyes[0:6]
49
+ right_eye = eyes[6:12]
50
+ ear = max(eye_aspect_ratio(left_eye), eye_aspect_ratio(right_eye))
51
+ left_eye = left_eye.reshape((-1,1,2))
52
+ right_eye = right_eye.reshape((-1,1,2))
53
+ cv.polylines(img, [left_eye], True, (0, 255, 255))
54
+ cv.polylines(img, [right_eye], True, (255, 0, 255))
55
+
56
+ if (ear > threshold):
57
+ return True
58
+ else:
59
+ return False
60
+
61
+ def proc_image(img, detector):
62
+ detected_faces = detector.detect_faces(img)
63
+ if (len(detected_faces[0]) < 1):
64
+ return img
65
+ detected_landmarks = detector.detect_landmarks(img, detected_faces)
66
+ detected_emotions = detector.detect_emotions(img, detected_faces, detected_landmarks)
67
+ is_eye_open = detect_eyes(detected_landmarks, img, 0.12)
68
+ eye_dict = {True: "Eyes Open", False: "Eyes Closed"}
69
+
70
+ em = detected_emotions[0]
71
+ em_labels = em.argmax(axis=1)
72
+
73
+ for face, label in zip(detected_faces[0], em_labels):
74
+ (x0, y0, x1, y1, p) = face
75
+ img = cv.rectangle(img, (int(x0), int(y0)), (int(x1), int(y1)), color = (0, 0, 255), thickness = 3)
76
+ cv.putText(img, FEAT_EMOTION_COLUMNS[label], (int(x0)-10, int(y0)-10), fontFace = 0, color = (0, 0, 255), thickness = 2, fontScale = 1)
77
+ cv.putText(img, eye_dict[is_eye_open], (0, 25), fontFace = 0, color = (0, 0, 255), thickness = 2, fontScale = 1)
78
+
79
+ return img
80
 
81
+ def image_processing(frame):
82
+ return proc_image(img, detector) if recog else img
83
+
84
  def video_frame_callback(frame):
85
  img = frame.to_ndarray(format="bgr24")
86
 
87
+ ann = proc_image(img, detector) if recog else img
88
+
89
+ return av.VideoFrame.from_ndarray(ann, format="bgr24")
90
+
91
+ detector = Detector(face_model="retinaface", landmark_model= "pfld", au_model = "xgb", emotion_model="resmasknet")
92
+ source = "Webcam"
93
+ recog = True
94
 
95
+ source = st.radio(
96
+ label = "Image source for emotion recognition",
97
+ options = ["Webcam", "Images"],
98
+ horizontal = True,
99
+ label_visibility = "collapsed",
100
+ args = (source, )
101
+ )
102
 
103
+ has_cam = True if (source == "Webcam") else False
104
 
105
+ stream = st.container()
106
+ with stream:
107
+ if has_cam:
108
+ webrtc_streamer(
109
+ key="example",
110
+ mode=WebRtcMode.SENDRECV,
111
+ video_frame_callback=video_frame_callback,
112
+ rtc_configuration={ "iceServers": get_ice_servers() },
113
+ media_stream_constraints={"video": True, "audio": False},
114
+ async_processing=True,
115
+ )
116
+ else:
117
+ pic = st.container()
118
+ frame = image_select(
119
+ label="Try the classifier on one of the provided examples!",
120
+ images=[
121
+ "ex0.jpg",
122
+ "ex1.jpg",
123
+ "ex2.jpg",
124
+ "ex3.jpg",
125
+ ],
126
+ use_container_width= False
127
+ )
128
+ img = np.array(Image.open(frame))
129
+ pic.image(image_processing(img), width = 704)
130
 
 
 
 
 
 
131
 
132
+ recog = st.toggle(":green[Emotion recogntion]", key = "stream", value = True)
133