Rahulk2197 commited on
Commit
5a7dd92
·
verified ·
1 Parent(s): b04e0c1

Upload 3 files

Browse files
22.6_AffectNet_10K_part2.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:92ef53adb843700faa3c54ae6f3e0f4105e04e099f9190dd66aafc360afdb2bf
3
+ size 16425358
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ opencv-python==4.10.0.84
3
+ torch
4
+ torchvision
5
+ numpy
6
+ timm
7
+ mediapipe
8
+ pandas
9
+ pillow
streamlit_app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import torch
4
+ import torchvision.transforms as transforms
5
+ from PIL import Image
6
+ import numpy as np
7
+ import timm
8
+ import torch.nn as nn
9
+ import mediapipe as mp
10
+ import time
11
+ import tempfile
12
+ import pandas as pd
13
+
14
+ # Initialize device
15
+ device = "cpu"
16
+ # st.write(f"Using CUDA: {torch.cuda.is_available()}")
17
+
18
+ # Define the transformation to apply to the images
19
+ transform = transforms.Compose(
20
+ [
21
+ transforms.Resize((224, 224)),
22
+ transforms.ToTensor(),
23
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
24
+ ]
25
+ )
26
+ change_list = []
27
+ # Load the model
28
+ model = timm.create_model("tf_efficientnet_b0_ns", pretrained=False)
29
+ model.classifier = nn.Sequential(nn.Linear(in_features=1280, out_features=7))
30
+ model = torch.load(
31
+ "22.6_AffectNet_10K_part2.pt",map_location=device
32
+ )
33
+ model.to(device)
34
+ model.eval()
35
+
36
+ # Initialize MediaPipe Face Detection
37
+ mp_face_detection = mp.solutions.face_detection
38
+ mp_drawing = mp.solutions.drawing_utils
39
+
40
+ # Streamlit interface
41
+ st.title("Emotion Detection from Video")
42
+ st.write("Upload a video file to detect emotions.")
43
+
44
+ uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
45
+
46
+ if uploaded_file is not None:
47
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
48
+ temp_file.write(uploaded_file.read())
49
+ video_path = temp_file.name
50
+
51
+ cap = cv2.VideoCapture(video_path)
52
+
53
+ histogram = {i: 0 for i in range(7)}
54
+ mat = [[0 for _ in range(7)] for _ in range(7)]
55
+ prev_emotion = None
56
+ current_emotion = None
57
+
58
+ begin = time.time()
59
+ with mp_face_detection.FaceDetection(
60
+ model_selection=0, min_detection_confidence=0.5
61
+ ) as face_detection:
62
+ while cap.isOpened():
63
+ ret, frame = cap.read()
64
+ if not ret:
65
+ break
66
+
67
+ # Convert frame to RGB for MediaPipe
68
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
69
+
70
+ # Process the frame and detect faces
71
+ results = face_detection.process(rgb_frame)
72
+
73
+ if results.detections:
74
+ for detection in results.detections:
75
+ # Get bounding box
76
+ bboxC = detection.location_data.relative_bounding_box
77
+ ih, iw, _ = frame.shape
78
+ x, y, w, h = (
79
+ int(bboxC.xmin * iw),
80
+ int(bboxC.ymin * ih),
81
+ int(bboxC.width * iw),
82
+ int(bboxC.height * ih),
83
+ )
84
+
85
+ # Extract the region of interest (the face)
86
+ face = frame[y : y + h, x : x + w]
87
+
88
+ # Convert the face to a PIL image
89
+ face_pil = Image.fromarray(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
90
+
91
+ # Apply transformations
92
+ face_tensor = transform(face_pil).unsqueeze(0).to(device)
93
+
94
+ # Pass the face through the neural network
95
+ with torch.no_grad():
96
+ output = model(face_tensor)
97
+ _, predicted = torch.max(output, 1)
98
+
99
+ label_dict = {
100
+ 0: "angry",
101
+ 1: "disgust",
102
+ 2: "fear",
103
+ 3: "happy",
104
+ 4: "neutral",
105
+ 5: "sad",
106
+ 6: "surprised",
107
+ }
108
+
109
+ # Draw a rectangle around the face and label it with the prediction
110
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
111
+ label = f"{label_dict[predicted.item()]}"
112
+ current_emotion = predicted.item()
113
+ if current_emotion != prev_emotion:
114
+ current_time = time.time() - begin
115
+ if prev_emotion != None:
116
+ st.write(
117
+ f"Change detected: {label_dict[prev_emotion]} -> {label_dict[current_emotion]} at {current_time}"
118
+ )
119
+ change_list.append(
120
+ f"Change detected: {label_dict[prev_emotion]} -> {label_dict[current_emotion]} at {current_time}"
121
+ )
122
+ if prev_emotion is not None:
123
+ mat[current_emotion][prev_emotion] += 1
124
+
125
+ prev_emotion = current_emotion
126
+ histogram[predicted.item()] += 1
127
+ cv2.putText(
128
+ frame,
129
+ label,
130
+ (x, y - 10),
131
+ cv2.FONT_HERSHEY_SIMPLEX,
132
+ 0.9,
133
+ (255, 0, 0),
134
+ 2,
135
+ )
136
+
137
+ # Display the resulting frame
138
+ st.image(frame, channels="BGR")
139
+
140
+ # Release the capture and close the windows
141
+ cap.release()
142
+
143
+ end = time.time()
144
+ st.write(f"Total runtime of the program is {end - begin}")
145
+
146
+ # Plot histogram
147
+ st.write("Emotion Distribution")
148
+ x = ["angry", "disgust", "fear", "happy", "neutral", "sad", "surprised"]
149
+ y = list(histogram.values())
150
+ total = sum(y)
151
+ y_new = [(i / total) * 100 for i in y]
152
+
153
+ st.bar_chart({"Emotions": x, "Percentage": y_new})
154
+
155
+ print(mat)
156
+ data = {
157
+ "angry": mat[0],
158
+ "disgust": mat[1],
159
+ "fear": mat[2],
160
+ "happy": mat[3],
161
+ "neutral": mat[4],
162
+ "sad": mat[5],
163
+ "surprise": mat[6],
164
+ }
165
+
166
+ st.write("Change Matrix")
167
+ st.write("Y - axis -> initial emotion")
168
+ st.write("X - axis -> next emotion")
169
+ df = pd.DataFrame(
170
+ data,
171
+ index=["angry", "disgust", "fear", "happy", "neutral", "sad", "surprised"],
172
+ )
173
+ st.table(df)
174
+ # for i in mat:
175
+ # st.write(i[7], i[0:7])
176
+
177
+ st.write("Change List")
178
+ st.write(change_list)
179
+
180
+ else:
181
+ st.write("Please upload a video file to start emotion detection.")