File size: 11,778 Bytes
32661ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641e943
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import warnings
from functions.models import models_dict
warnings.filterwarnings('ignore', category=UserWarning, module='tensorflow')
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
import logging
logging.getLogger('absl').setLevel(logging.ERROR)
from moviepy.editor import VideoFileClip
import pandas as pd
from tqdm import tqdm
import time
import json
import cv2
import dlib
from collections import Counter
import statistics
import shutil
import asyncio
import traceback

from functions.valence_arousal import va_predict
from functions.speech import speech_predict
from functions.eye_track import Facetrack, eye_track_predict
from functions.fer import extract_face,fer_predict,plot_graph,filter
# from app.utils.session import send_analytics, send_individual_analytics_files, send_combined_analytics_files, send_error
# from app.utils.socket import ConnectionManager
from typing import Callable
session_data={}
dnn_net=models_dict['face'][0]
predictor=models_dict['face'][1]
speech_model=models_dict['speech']
valence_dict_path=models_dict['vad'][0]
arousal_dict_path=models_dict['vad'][1]
dominance_dict_path=models_dict['vad'][2]
valence_arousal_model=models_dict['valence_fer'][1]
val_ar_feat_model=models_dict['valence_fer'][0]
fer_model=models_dict['fer']

def analyze_live_video(video_path: str, uid: str, user_id: str, count: int, final: bool, log: Callable[[str], None]):
	try:
		#initilalizing lists 
		global session_data
		if uid not in session_data:
			session_data[uid] = {
					"vcount":[],
					"duration":[],

					"eye": [],

					"fer": [],
					"valence":[],
					"arousal":[],
					"stress":[],

					"blinks": [],
					"class_wise_frame_counts": [],

					"speech_emotions": [],
					"speech_data":[],
					"word_weights_list": []
				}
		print(f"UID: {uid}, User ID: {user_id}, Count: {count}, Final: {final}, Video: {video_path}")
		log(f"Analyzing video for question - {count}")

		output_dir = os.path.join('output',str(uid))
		print(output_dir)
		if not os.path.exists(output_dir):
				os.makedirs(output_dir)
		# Wait for previous files to be written if final
		if final and count > 1:
			for i in range(1, count):
				previous_file_name = os.path.join(output_dir, f"{i}.json")
				print(previous_file_name)
				while not os.path.exists(previous_file_name):
					time.sleep(1) 

		video_clip = VideoFileClip(video_path)
		video_clip = video_clip.set_fps(30)
		print("Duration: ", video_clip.duration)
		session_data[uid]['vcount'].append(count)
		session_data[uid]['duration'].append(video_clip.duration)
		fps = video_clip.fps
		audio = video_clip.audio
		audio_path = os.path.join(output_dir,'extracted_audio.wav')
		audio.write_audiofile(audio_path)
		video_frames = [frame for frame in video_clip.iter_frames()]

		#Face extraction
		print("extracting faces")
		faces=[extract_face(frame,dnn_net,predictor) for frame in tqdm(video_frames)]
		print(f'{len([face for face in faces if face is not None])} faces found.')


		##EYE TRACKING
		fc=Facetrack()
		log(f"Extracting eye features for question - {count}")
		eye_preds,blink_durations,total_blinks=eye_track_predict(fc,faces,fps)
		print(len(eye_preds))
		print("total_blinks- ",total_blinks)	
		session_data[uid]['eye'].append(eye_preds)
		session_data[uid]['blinks'].append(blink_durations)
		

		#FACIAL EXPRESSION RECOGNITION
		log(f"Extracting facial features for question - {count}")
		fer_emotions,class_wise_frame_count,em_tensors=fer_predict(faces,fps,fer_model)
		print("face emotions",len(fer_emotions))
		session_data[uid]['fer'].append(fer_emotions)
		session_data[uid]['class_wise_frame_counts'].append(class_wise_frame_count)
		
		#VALENCE AROUSAL STRESS
		valence_list,arousal_list,stress_list=va_predict(valence_arousal_model,val_ar_feat_model,faces,list(em_tensors))
		session_data[uid]['valence'].append(valence_list)
		session_data[uid]['arousal'].append(arousal_list)
		session_data[uid]['stress'].append(stress_list)
		log(f"Extracting speech features for question - {count}")
		emotions,major_emotion,word=speech_predict(audio_path,speech_model,valence_dict_path,arousal_dict_path,dominance_dict_path)
		session_data[uid]['speech_emotions'].append(emotions)
		session_data[uid]['word_weights_list'].append(word['word_weights'])
		session_data[uid]['speech_data'].append([float(word['average_pause_length'] if word and word['average_pause_length'] else 0),float(word['articulation_rate'] if word and word['articulation_rate'] else 0),float(word['speaking_rate'] if word and word['speaking_rate'] else 0)])
		log(f"Generating the metadata for question - {count}")

		
		# Create Meta Data
		meta_data={}
		try:
			avg_blink_duration= float(sum(blink_durations)/(len(blink_durations)))
		except:
			avg_blink_duration=0
		meta_data['vcount']=count
		meta_data['eye_emotion_recognition'] = {
			"blink_durations": blink_durations,
			"avg_blink_duration":avg_blink_duration,
			"total_blinks": total_blinks,
			"duration":video_clip.duration
		}
    
		meta_data['facial_emotion_recognition'] = {
			"class_wise_frame_count": class_wise_frame_count,
		}
		meta_data['speech_emotion_recognition'] = {
		'major_emotion':str(major_emotion),
		'pause_length':float(word['average_pause_length']),
		'articulation_rate':float(word['articulation_rate']),
		'speaking_rate':float(word['speaking_rate']),
		'word_weights':word['word_weights']
		}


		file_path=audio_path
		if os.path.exists(file_path):
				os.remove(file_path)
		print(f"{file_path} deleted")
		file_path='segment.wav'
		if os.path.exists(file_path):
				os.remove(file_path)
		print(f"{file_path} deleted")
		

		print("Individual: ", meta_data)
		if not final:
			print("Not final Executing")
			log(f"Saving analytics for question - {count}")
		# 	send_analytics(valence_plot, arousal_plot,{
		# 		"uid": uid,
		# 		"user_id": user_id, 
		# 		"individual": meta_data,
		# 		"count": count
		# 	})
			print("Sent analytics")
		# 	send_individual_analytics_files(uid, output_dir, count)
			dummy_file_path = os.path.join(output_dir, f'{count}.json')
			print("Writing dummy file: ", dummy_file_path)
			with open(dummy_file_path, 'w') as dummy_file:
				json.dump({"status": "completed"}, dummy_file)
			return
	
		# Process combined
		log(f"Processing gathered data for final output")

		vcount=session_data[uid]['vcount']
		sorted_indices = sorted(range(len(vcount)), key=lambda i: vcount[i])
		for key in session_data[uid]:
			# Only sort lists that are the same length as vcount
			if len(session_data[uid][key]) == len(vcount):
				session_data[uid][key] = [session_data[uid][key][i] for i in sorted_indices]

		videos=len(session_data[uid]['vcount'])
		#INDIV PLOT SAVING
		combined_speech=[]
		combined_valence=[]
		combined_arousal=[]
		combined_stress=[]
		combined_fer=[]
		combined_eye=[]
		vid_index=[]
		combined_speech=[]
		combined_blinks=[]
		for i in range(videos):
			for j in range(len(session_data[uid]['speech_emotions'][i])):
				vid_index.append(i+1)
			combined_speech+=session_data[uid]['speech_emotions'][i]
		timestamps=[i*3 for i in range(len(combined_speech))]
		df = pd.DataFrame({
			'timestamps':timestamps,
			'video_index':vid_index,
			'speech_emotion':combined_speech
		})
		df.to_csv(os.path.join(output_dir,'combined_speech.csv'), index=False)



		vid_index=[]
		for i in range(videos):
			timestamps=[j/30 for j in range(len(session_data[uid]['valence'][i]))]			
			for j in range(len(timestamps)):
				vid_index.append(i+1)
			folder_path=os.path.join(output_dir,f"{session_data[uid]['vcount'][i]}")
			os.makedirs(folder_path, exist_ok=True)
			plot_graph(timestamps,session_data[uid]['valence'][i],'valence',os.path.join(folder_path,'valence.png'))
			plot_graph(timestamps,session_data[uid]['arousal'][i],'arousal',os.path.join(folder_path,'arousal.png'))
			plot_graph(timestamps,session_data[uid]['stress'][i],'stress',os.path.join(folder_path,'stress.png'))
			combined_arousal+=session_data[uid]['arousal'][i]
			combined_valence+=session_data[uid]['valence'][i]
			combined_stress+=session_data[uid]['stress'][i]
			combined_fer+=session_data[uid]['fer'][i]
			combined_blinks+=session_data[uid]['blinks'][i]
			# combined_class_wise_frame_count+=session_data[uid]['class_wise_frame_counts'][i]
			try:
				max_value=max([x for x in combined_eye if isinstance(x, (int, float))])
			except:
				max_value=0
			session_data[uid]['eye'][i]=[x + max_value if isinstance(x, (int, float)) else x for x in session_data[uid]['eye'][i]]
			combined_eye+=session_data[uid]['eye'][i]
		
		timestamps=[i/fps for i in range(len(combined_arousal))]
		plot_graph(timestamps,combined_valence,'valence',os.path.join(output_dir,'valence.png'))
		plot_graph(timestamps,combined_arousal,'arousal',os.path.join(output_dir,'arousal.png'))
		plot_graph(timestamps,combined_stress,'stress',os.path.join(output_dir,'stress.png'))
		print(len(timestamps),len(vid_index),len(combined_fer),len(combined_valence),len(combined_arousal),len(combined_stress),len(combined_eye))
		df = pd.DataFrame({
			'timestamps':timestamps,
			'video_index': vid_index,  # Add a column for video index
			'fer': combined_fer,
			'valence': combined_valence,
			'arousal': combined_arousal,
			'stress': combined_stress,
			'eye': combined_eye,
		})
		df.to_csv(os.path.join(output_dir,'combined_data.csv'), index=False)

		#generate metadata for Combined
		comb_meta_data={}
		try:
			avg_blink_duration= float(sum(combined_blinks)/(len(combined_blinks)))
		except:
			avg_blink_duration=0

		total_blinks=max([x for x in combined_eye if isinstance(x, (int, float))])

		comb_meta_data['eye_emotion_recognition'] = {
			"avg_blink_duration":avg_blink_duration,
			"total_blinks": total_blinks,
		}

		dict_list = session_data[uid]['class_wise_frame_counts']

		result = {}
		for d in dict_list:
			for key,value in d.items():
				result[key]=result.get(key,0)+value
		comb_meta_data['facial_emotion_recognition'] = {
			"class_wise_frame_count": result,
		}

		combined_weights = Counter()
		for word_weight in session_data[uid]['word_weights_list']:
			combined_weights.update(word_weight)
		combined_weights_dict = dict(combined_weights)
		print(combined_weights_dict)
		comb_meta_data['speech_emotion_recognition'] = {
		'major_emotion':str(major_emotion),
		'pause_length':statistics.mean([row[0] for row in session_data[uid]['speech_data']]),
		'articulation_rate':statistics.mean([row[1] for row in session_data[uid]['speech_data']]),
		'speaking_rate':statistics.mean([row[2] for row in session_data[uid]['speech_data']]),
		'word_weights':combined_weights_dict
		}
		with open(os.path.join(output_dir,'combined.json'), 'w') as json_file:
			json.dump(comb_meta_data, json_file)
		log(f"Saving analytics for final output")
		# send_analytics(valence_plot, arousal_plot,{
		# 	"uid": uid,
		# 	"user_id": user_id, 
		# 	"individual": meta_data,
		# 	"combined": combined_meta_data,
		# 	"count": count
		# })
		# send_individual_analytics_files(uid, output_dir, count)
		# send_combined_analytics_files(uid, output_dir)

		# shutil.rmtree(output_dir)
		# print(f"Deleted output directory: {output_dir}")
	except Exception as e:
		print("Error analyzing video...: ", e)
		error_trace = traceback.format_exc()
		print("Error Trace: ", error_trace)
		log(f"Error analyzing video for question - {count}")
		# send_error(uid, {
		# 	"message": str(e),
		# 	"trace": error_trace
		# })
		shutil.rmtree('output')
		print(f"Deleted output directory: {output_dir}")
		

# st=time.time()
# # analyze_live_video(video_path, uid, user_id, count, final, log)
# analyze_live_video('videos/s2.webm', 1,1,1,False,print)
# analyze_live_video('videos/a4.webm', 1,1,2,True,print)

# analyze_live_video('videos/s2.webm', 1,1,2,True,print)
# print("time taken - ",time.time()-st)