ParthCodes commited on
Commit
1e3589d
·
verified ·
1 Parent(s): 9e75130

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -121
app.py CHANGED
@@ -1,127 +1,22 @@
1
- import math
2
- from io import BytesIO
3
  import gradio as gr
4
  import cv2
5
- import os
6
- import requests
7
- from pydub import AudioSegment
8
- from faster_whisper import WhisperModel
9
-
10
- model = WhisperModel("small", device="cpu", compute_type="int8")
11
-
12
- API_KEY = os.getenv("API_KEY")
13
-
14
- FACE_API_URL = "https://api-inference.huggingface.co/models/dima806/facial_emotions_image_detection"
15
- TEXT_API_URL = "https://api-inference.huggingface.co/models/SamLowe/roberta-base-go_emotions"
16
- headers = {"Authorization": "Bearer " + API_KEY + ""}
17
-
18
-
19
- def extract_frames(video_path):
20
- cap = cv2.VideoCapture(video_path)
21
  fps = int(cap.get(cv2.CAP_PROP_FPS))
22
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
23
- interval = fps
24
- result = []
25
-
26
- for i in range(0, total_frames, interval):
27
- cap.set(cv2.CAP_PROP_POS_FRAMES, i)
28
- ret, frame = cap.read()
29
- if ret:
30
- _, img_encoded = cv2.imencode('.jpg', frame)
31
- img_bytes = img_encoded.tobytes()
32
-
33
- response = requests.post(FACE_API_URL, headers=headers, data=img_bytes)
34
- result.append({item['label']: item['score'] for item in response.json()})
35
-
36
- print("Frame extraction completed.")
37
-
38
- cap.release()
39
- print(result)
40
- return result
41
-
42
-
43
- def analyze_sentiment(text):
44
- response = requests.post(TEXT_API_URL, headers=headers, json=text)
45
- print(response.json())
46
- sentiment_list = response.json()[0]
47
- print(sentiment_list)
48
- sentiment_results = {result['label']: result['score'] for result in sentiment_list}
49
- return sentiment_results
50
-
51
-
52
- def video_to_audio(input_video):
53
- audio = AudioSegment.from_file(input_video)
54
- audio_binary = audio.export(format="wav").read()
55
- audio_bytesio = BytesIO(audio_binary)
56
-
57
- segments, info = model.transcribe(audio_bytesio, beam_size=5)
58
-
59
- print("Detected language '%s' with probability %f" % (info.language, info.language_probability))
60
-
61
- frames_sentiments = extract_frames(input_video)
62
-
63
- transcript = ''
64
- final_output = []
65
- for segment in segments:
66
- transcript = transcript + segment.text + " "
67
- print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
68
- transcript_segment_sentiment = analyze_sentiment(segment.text)
69
-
70
- emotion_totals = {
71
- 'admiration': 0.0,
72
- 'amusement': 0.0,
73
- 'angry': 0.0,
74
- 'annoyance': 0.0,
75
- 'approval': 0.0,
76
- 'caring': 0.0,
77
- 'confusion': 0.0,
78
- 'curiosity': 0.0,
79
- 'desire': 0.0,
80
- 'disappointment': 0.0,
81
- 'disapproval': 0.0,
82
- 'disgust': 0.0,
83
- 'embarrassment': 0.0,
84
- 'excitement': 0.0,
85
- 'fear': 0.0,
86
- 'gratitude': 0.0,
87
- 'grief': 0.0,
88
- 'happy': 0.0,
89
- 'love': 0.0,
90
- 'nervousness': 0.0,
91
- 'optimism': 0.0,
92
- 'pride': 0.0,
93
- 'realization': 0.0,
94
- 'relief': 0.0,
95
- 'remorse': 0.0,
96
- 'sad': 0.0,
97
- 'surprise': 0.0,
98
- 'neutral': 0.0
99
- }
100
-
101
- counter = 0
102
- for i in range(math.ceil(segment.start), math.floor(segment.end)):
103
- for emotion in frames_sentiments[i].keys():
104
- emotion_totals[emotion] += frames_sentiments[i].get(emotion)
105
- counter += 1
106
-
107
- for emotion in emotion_totals:
108
- emotion_totals[emotion] /= counter
109
-
110
- video_segment_sentiment = emotion_totals
111
-
112
- segment_finals = {segment.id: (segment.text, segment.start, segment.end, transcript_segment_sentiment,
113
- video_segment_sentiment)}
114
- final_output.append(segment_finals)
115
- print(segment_finals)
116
- print(final_output)
117
-
118
- print(final_output)
119
-
120
- return final_output
121
 
 
122
 
123
- gr.Interface(
124
- fn=video_to_audio,
125
- inputs=gr.Video(sources=["upload"]),
126
- outputs=gr.Textbox()
127
- ).launch()
 
 
 
1
  import gradio as gr
2
  import cv2
3
+ import moviepy.editor as mpe
4
+ from moviepy.editor import VideoFileClip
5
+
6
+ def process(video_path):
7
+ print(video_path)
8
+
9
+ clip = mpe.VideoFileClip(video_path)
10
+ clip.write_videofile('mp4file.mp4', fps=60)
11
+
12
+ cap = cv2.VideoCapture('mp4file.mp4')
 
 
 
 
 
 
13
  fps = int(cap.get(cv2.CAP_PROP_FPS))
14
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
15
+ interval = int(fps/2)
16
+ print(interval, total_frames)
17
+ return interval, total_frames
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ demo = gr.Interface(fn=process, inputs=gr.Video(format='mp4'), outputs=["textbox", "textbox"], title="Video Frame Counter")
20
 
21
+ if __name__ == "__main__":
22
+ demo.launch()