File size: 15,096 Bytes
890de26
492a71a
890de26
 
 
 
 
 
 
 
 
 
 
55675a3
890de26
0073adb
 
 
 
 
 
 
 
 
890de26
 
 
0073adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2455fea
0073adb
2455fea
0073adb
 
 
 
890de26
 
 
0073adb
890de26
 
 
 
 
077653d
 
 
46390ae
30a0d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
890de26
30a0d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
890de26
30a0d7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153c3c8
044c8e7
30a0d7d
 
 
492a71a
890de26
153c3c8
46390ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0073adb
 
 
46390ae
 
0073adb
46390ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0073adb
46390ae
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import torch
import time
import numpy as np
import mediapipe as mp
from PIL import Image
import cv2
from pytorch_grad_cam.utils.image import show_cam_on_image

# Importing necessary components for the Gradio app
from model import pth_model_static, pth_model_dynamic, cam, pth_processing
from face_utils import get_box, display_info
from config import DICT_EMO, config_data
from plot import statistics_plot
from moviepy.editor import AudioFileClip

import soundfile as sf
import torchaudio
from speechbrain.pretrained.interfaces import foreign_class
from paraformer import AudioReader, CttPunctuator, FSMNVad, ParaformerOffline
from gradio_client import Client

##############################################################################################
client = Client("Liusuthu/TextDepression")

mp_face_mesh = mp.solutions.face_mesh


classifier = foreign_class(
    source="pretrained_models/local-speechbrain/emotion-recognition-wav2vec2-IEMOCAP",  # ".\\emotion-recognition-wav2vec2-IEMOCAP"
    pymodule_file="custom_interface.py",
    classname="CustomEncoderWav2vec2Classifier",
    savedir="pretrained_models/local-speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
)
ASR_model = ParaformerOffline()
vad = FSMNVad()
punc = CttPunctuator()


#########################################################################################
def text_api(text:str):
    result = client.predict(
        text,  # str  in '输入文字' Textbox component
        api_name="/predict",
    )
    return result


def classify_continuous(audio):
    print(type(audio))
    print(audio)
    sample_rate, signal = audio  # 这是语音的输入
    signal = signal.astype(np.float32)
    signal /= np.max(np.abs(signal))
    sf.write("data/a.wav", signal, sample_rate)
    signal, sample_rate = torchaudio.load("data/a.wav")
    signal1 = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)(
        signal
    )
    torchaudio.save("data/out.wav", signal1, 16000, encoding="PCM_S", bits_per_sample=16)
    Audio = "data/out.wav"
    speech, sample_rate = AudioReader.read_wav_file(Audio)
    if signal == "none":
        return "none", "none", "haha"
    else:
        segments = vad.segments_offline(speech)
        text_results = ""
        for part in segments:
            _result = ASR_model.infer_offline(
                speech[part[0] * 16 : part[1] * 16], hot_words="任意热词 空格分开"
            )
            text_results += punc.punctuate(_result)[0]

        out_prob, score, index, text_lab = classifier.classify_batch(signal1)
        print(type(out_prob.squeeze(0).numpy()))
        print(out_prob.squeeze(0).numpy())
        print(type(text_lab[-1]))
        print(text_lab[-1])
        return text_results, out_prob.squeeze(0).numpy(), text_lab[-1], Audio



def preprocess_image_and_predict(inp):
    return None, None, None

def preprocess_video_and_predict(video):    
    return None, None, None, None



#to return scores


###########################################################################################################################
def video_score(video):
    
    cap = cv2.VideoCapture(video)
    w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = np.round(cap.get(cv2.CAP_PROP_FPS))

    path_save_video_face = 'result_face.mp4'
    vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))

    # path_save_video_hm = 'result_hm.mp4'
    # vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))

    lstm_features = []
    count_frame = 1
    count_face = 0
    probs = []
    frames = []
    last_output = None
    last_heatmap = None 
    cur_face = None

    with mp_face_mesh.FaceMesh(
    max_num_faces=1,
    refine_landmarks=False,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as face_mesh:

        while cap.isOpened():
            _, frame = cap.read()
            if frame is None: break

            frame_copy = frame.copy()
            frame_copy.flags.writeable = False
            frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
            results = face_mesh.process(frame_copy)
            frame_copy.flags.writeable = True

            if results.multi_face_landmarks:
                for fl in results.multi_face_landmarks:
                    startX, startY, endX, endY  = get_box(fl, w, h)
                    cur_face = frame_copy[startY:endY, startX: endX]

                    if count_face%config_data.FRAME_DOWNSAMPLING == 0:
                        cur_face_copy = pth_processing(Image.fromarray(cur_face))
                        with torch.no_grad():
                            features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()

                        # grayscale_cam = cam(input_tensor=cur_face_copy)
                        # grayscale_cam = grayscale_cam[0, :]
                        # cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
                        # cur_face_hm = np.float32(cur_face_hm) / 255
                        # heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
                        # last_heatmap = heatmap
        
                        if len(lstm_features) == 0:
                            lstm_features = [features]*10
                        else:
                            lstm_features = lstm_features[1:] + [features]

                        lstm_f = torch.from_numpy(np.vstack(lstm_features))
                        lstm_f = torch.unsqueeze(lstm_f, 0)
                        with torch.no_grad():
                            output = pth_model_dynamic(lstm_f).detach().numpy()
                        last_output = output

                        if count_face == 0:
                            count_face += 1

                    else:
                        if last_output is not None:
                            output = last_output
                            # heatmap = last_heatmap

                        elif last_output is None:
                            output = np.empty((1, 7))
                            output[:] = np.nan
                            
                    probs.append(output[0])
                    frames.append(count_frame)
            else:
                if last_output is not None:
                    lstm_features = []
                    empty = np.empty((7))
                    empty[:] = np.nan
                    probs.append(empty)
                    frames.append(count_frame)                        

            if cur_face is not None:
                # heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)

                cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
                cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
                cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
                vid_writer_face.write(cur_face)
                # vid_writer_hm.write(heatmap_f)

            count_frame += 1
            if count_face != 0:
                count_face += 1

        vid_writer_face.release()
        # vid_writer_hm.release()

        stat = statistics_plot(frames, probs)

        if not stat:
            return None, None

    #for debug
    print(type(frames))
    print(frames)
    print(type(probs))
    print(probs)        
    # to calculate scores
    nan=float('nan')
    s1 = 0
    s2 = 0
    s3 = 0
    s4 = 0
    s5 = 0
    s6 = 0
    s7 = 0
    frames_len=len(frames)
    for i in range(frames_len):
        if np.isnan(probs[i][0]):
            frames_len=frames_len-1
        else: 
            s1=s1+probs[i][0]
            s2=s2+probs[i][1]
            s3=s3+probs[i][2]
            s4=s4+probs[i][3]
            s5=s5+probs[i][4]
            s6=s6+probs[i][5]
            s7=s7+probs[i][6]
    s1=s1/frames_len
    s2=s2/frames_len
    s3=s3/frames_len
    s4=s4/frames_len
    s5=s5/frames_len
    s6=s6/frames_len
    s7=s7/frames_len
    scores=[s1,s2,s3,s4,s5,s6,s7]
    scores_str=str(scores)
    with open("local_data/data.txt",'a', encoding="utf8") as f:
        f.write(scores_str+'\n')

    with open("local_data/data.txt",'r', encoding="utf8") as f:
        for i in f:
            print(i)

    score1=0*scores[0]-8*scores[1]+4*scores[2]+0*scores[3]+2*scores[4]+2*scores[5]+4*scores[6]
    
    #trans the audio file
    my_audio_clip = AudioFileClip(video)
    my_audio_clip.write_audiofile("data/audio.wav",ffmpeg_params=["-ac","1"])
    time.sleep(2)
    
    return stat,score1,"data/audio.wav"
    
#     #trans the audio file
#     my_audio_clip = AudioFileClip(video)
#     my_audio_clip.write_audiofile("newaudio.wav",ffmpeg_params=["-ac","1"])
    
#     # Audio="data/audio.wav"
#     # text,prob2,label,path=classify_continuous(Audio)
#     #0是抑郁概率,1是非抑郁概率
#     # score2=10*prob2[0]-10*prob[1]
#     # print(prob2,label)
#     # score2=0
#     # print("score2=",score2)

#     # text_result="demo"
#     # # text_result=text_api(text)
#     # print(text_result)
    
#     return score1



#可以运行的代码,誊抄用
# def preprocess_video_and_rank(video):
    
#     cap = cv2.VideoCapture(video)
#     w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
#     h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#     fps = np.round(cap.get(cv2.CAP_PROP_FPS))

#     path_save_video_face = 'result_face.mp4'
#     vid_writer_face = cv2.VideoWriter(path_save_video_face, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))

#     # path_save_video_hm = 'result_hm.mp4'
#     # vid_writer_hm = cv2.VideoWriter(path_save_video_hm, cv2.VideoWriter_fourcc(*'mp4v'), fps, (224, 224))

#     lstm_features = []
#     count_frame = 1
#     count_face = 0
#     probs = []
#     frames = []
#     last_output = None
#     last_heatmap = None 
#     cur_face = None

#     with mp_face_mesh.FaceMesh(
#     max_num_faces=1,
#     refine_landmarks=False,
#     min_detection_confidence=0.5,
#     min_tracking_confidence=0.5) as face_mesh:

#         while cap.isOpened():
#             _, frame = cap.read()
#             if frame is None: break

#             frame_copy = frame.copy()
#             frame_copy.flags.writeable = False
#             frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
#             results = face_mesh.process(frame_copy)
#             frame_copy.flags.writeable = True

#             if results.multi_face_landmarks:
#                 for fl in results.multi_face_landmarks:
#                     startX, startY, endX, endY  = get_box(fl, w, h)
#                     cur_face = frame_copy[startY:endY, startX: endX]

#                     if count_face%config_data.FRAME_DOWNSAMPLING == 0:
#                         cur_face_copy = pth_processing(Image.fromarray(cur_face))
#                         with torch.no_grad():
#                             features = torch.nn.functional.relu(pth_model_static.extract_features(cur_face_copy)).detach().numpy()

#                         # grayscale_cam = cam(input_tensor=cur_face_copy)
#                         # grayscale_cam = grayscale_cam[0, :]
#                         # cur_face_hm = cv2.resize(cur_face,(224,224), interpolation = cv2.INTER_AREA)
#                         # cur_face_hm = np.float32(cur_face_hm) / 255
#                         # heatmap = show_cam_on_image(cur_face_hm, grayscale_cam, use_rgb=False)
#                         # last_heatmap = heatmap
        
#                         if len(lstm_features) == 0:
#                             lstm_features = [features]*10
#                         else:
#                             lstm_features = lstm_features[1:] + [features]

#                         lstm_f = torch.from_numpy(np.vstack(lstm_features))
#                         lstm_f = torch.unsqueeze(lstm_f, 0)
#                         with torch.no_grad():
#                             output = pth_model_dynamic(lstm_f).detach().numpy()
#                         last_output = output

#                         if count_face == 0:
#                             count_face += 1

#                     else:
#                         if last_output is not None:
#                             output = last_output
#                             # heatmap = last_heatmap

#                         elif last_output is None:
#                             output = np.empty((1, 7))
#                             output[:] = np.nan
                            
#                     probs.append(output[0])
#                     frames.append(count_frame)
#             else:
#                 if last_output is not None:
#                     lstm_features = []
#                     empty = np.empty((7))
#                     empty[:] = np.nan
#                     probs.append(empty)
#                     frames.append(count_frame)                        

#             if cur_face is not None:
#                 # heatmap_f = display_info(heatmap, 'Frame: {}'.format(count_frame), box_scale=.3)

#                 cur_face = cv2.cvtColor(cur_face, cv2.COLOR_RGB2BGR)
#                 cur_face = cv2.resize(cur_face, (224,224), interpolation = cv2.INTER_AREA)
#                 cur_face = display_info(cur_face, 'Frame: {}'.format(count_frame), box_scale=.3)
#                 vid_writer_face.write(cur_face)
#                 # vid_writer_hm.write(heatmap_f)

#             count_frame += 1
#             if count_face != 0:
#                 count_face += 1

#         vid_writer_face.release()
#         # vid_writer_hm.release()

#         stat = statistics_plot(frames, probs)

#         if not stat:
#             return None, None

#     #for debug
#     print(type(frames))
#     print(frames)
#     print(type(probs))
#     print(probs)        
#     # to calculate scores
#     nan=float('nan')
#     s1 = 0
#     s2 = 0
#     s3 = 0
#     s4 = 0
#     s5 = 0
#     s6 = 0
#     s7 = 0
#     frames_len=len(frames)
#     for i in range(frames_len):
#         if np.isnan(probs[i][0]):
#             frames_len=frames_len-1
#         else: 
#             s1=s1+probs[i][0]
#             s2=s2+probs[i][1]
#             s3=s3+probs[i][2]
#             s4=s4+probs[i][3]
#             s5=s5+probs[i][4]
#             s6=s6+probs[i][5]
#             s7=s7+probs[i][6]
#     s1=s1/frames_len
#     s2=s2/frames_len
#     s3=s3/frames_len
#     s4=s4/frames_len
#     s5=s5/frames_len
#     s6=s6/frames_len
#     s7=s7/frames_len
#     scores=[s1,s2,s3,s4,s5,s6,s7]
#     scores_str=str(scores)
#     with open("local_data/data.txt",'a', encoding="utf8") as f:
#         f.write(scores_str+'\n')

#     with open("local_data/data.txt",'r', encoding="utf8") as f:
#         for i in f:
#             print(i)


#     #trans the audio file
#     my_audio_clip = AudioFileClip(video)
#     my_audio_clip.write_audiofile("data/audio.wav",ffmpeg_params=["-ac","1"])
    
#     return stat,scores_str,"data/audio.wav"