princeml commited on
Commit
4150e33
1 Parent(s): 8612efa

Update app1.py

Browse files
Files changed (1) hide show
  1. app1.py +20 -102
app1.py CHANGED
@@ -1,103 +1,21 @@
1
- import numpy as np
2
  import cv2
3
- import streamlit as st
4
- from tensorflow import keras
5
- from keras.models import model_from_json
6
- from keras.preprocessing.image import img_to_array
7
- from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
8
-
9
- # load model
10
- emotion_dict = {0:'angry', 1 :'happy', 2: 'neutral', 3:'sad', 4: 'surprise'}
11
- # load json and create model
12
- json_file = open('emotion_model1.json', 'r')
13
- loaded_model_json = json_file.read()
14
- json_file.close()
15
- classifier = model_from_json(loaded_model_json)
16
-
17
- # load weights into new model
18
- classifier.load_weights("emotion_model1.h5")
19
-
20
- #load face
21
- try:
22
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
23
- except Exception:
24
- st.write("Error loading cascade classifiers")
25
-
26
- class VideoTransformer(VideoTransformerBase):
27
- def transform(self, frame):
28
- img = frame.to_ndarray(format="bgr24")
29
-
30
- #image gray
31
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
32
- faces = face_cascade.detectMultiScale(
33
- image=img_gray, scaleFactor=1.3, minNeighbors=5)
34
- for (x, y, w, h) in faces:
35
- cv2.rectangle(img=img, pt1=(x, y), pt2=(
36
- x + w, y + h), color=(255, 0, 0), thickness=2)
37
- roi_gray = img_gray[y:y + h, x:x + w]
38
- roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
39
- if np.sum([roi_gray]) != 0:
40
- roi = roi_gray.astype('float') / 255.0
41
- roi = img_to_array(roi)
42
- roi = np.expand_dims(roi, axis=0)
43
- prediction = classifier.predict(roi)[0]
44
- maxindex = int(np.argmax(prediction))
45
- finalout = emotion_dict[maxindex]
46
- output = str(finalout)
47
- label_position = (x, y)
48
- cv2.putText(img, output, label_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
49
-
50
- return img
51
-
52
- def main():
53
- # Face Analysis Application #
54
- st.title("Real Time Face Emotion Detection Application")
55
- activiteis = ["Home", "Webcam Face Detection", "About"]
56
- choice = st.sidebar.selectbox("Select Activity", activiteis)
57
- st.sidebar.markdown(
58
-
59
- if choice == "Home":
60
- html_temp_home1 = """<div style="background-color:#6D7B8D;padding:10px">
61
- <h4 style="color:white;text-align:center;">
62
- Face Emotion detection application using OpenCV, Custom CNN model and Streamlit.</h4>
63
- </div>
64
- </br>"""
65
- st.markdown(html_temp_home1, unsafe_allow_html=True)
66
- st.write("""
67
- The application has two functionalities.
68
-
69
- 1. Real time face detection using web cam feed.
70
-
71
- 2. Real time face emotion recognization.
72
-
73
- """)
74
- elif choice == "Webcam Face Detection":
75
- st.header("Webcam Live Feed")
76
- st.write("Click on start to use webcam and detect your face emotion")
77
- webrtc_streamer(key="example", video_transformer_factory=VideoTransformer)
78
-
79
- elif choice == "By_Image":
80
- st.subheader("About this app")
81
- html_temp_about1= """<div style="background-color:#6D7B8D;padding:10px">
82
- <h4 style="color:white;text-align:center;">
83
- Real time face emotion detection application using OpenCV, Custom Trained CNN model and Streamlit.</h4>
84
- </div>
85
- </br>"""
86
- st.markdown(html_temp_about1, unsafe_allow_html=True)
87
-
88
- html_temp4 = """
89
- <div style="background-color:#98AFC7;padding:10px">
90
-
91
- <h4 style="color:white;text-align:center;">Thanks for Visiting</h4>
92
- </div>
93
- <br></br>
94
- <br></br>"""
95
-
96
- st.markdown(html_temp4, unsafe_allow_html=True)
97
-
98
- else:
99
- pass
100
-
101
-
102
- if __name__ == "__main__":
103
- main()
 
1
+ # import tensorflow as tf
2
  import cv2
3
+ import numpy as np
4
+ # from glob import glob
5
+ # from models import Yolov4
6
+ import gradio as gr
7
+ # model = Yolov4(weight_path="yolov4.weights", class_name_path='coco_classes.txt')
8
+ def gradio_wrapper(img):
9
+ global model
10
+ #print(np.shape(img))
11
+ # results = model.predict(img)
12
+ # return results[0]
13
+ demo = gr.Interface(
14
+ gradio_wrapper,
15
+ #gr.Image(source="webcam", streaming=True, flip=True),
16
+ gr.Image(source="webcam", streaming=True),
17
+ "image",
18
+ live=True
19
+ )
20
+
21
+ demo.launch()