flocolombari
commited on
Commit
•
4a02f29
1
Parent(s):
17b72e4
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,59 @@ import scipy
|
|
7 |
import torch
|
8 |
import time
|
9 |
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def video_to_descriptions(video, target_language="en"):
|
|
|
|
|
|
|
|
|
13 |
|
14 |
start_time = time.time()
|
15 |
print("START TIME = ", start_time)
|
@@ -31,9 +81,8 @@ def video_to_descriptions(video, target_language="en"):
|
|
31 |
ret, frame = cap.read()
|
32 |
if not ret:
|
33 |
break
|
34 |
-
|
35 |
-
|
36 |
-
if frame_count % (fps * 2) == 0:
|
37 |
|
38 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
39 |
|
@@ -41,15 +90,32 @@ def video_to_descriptions(video, target_language="en"):
|
|
41 |
|
42 |
outputs = ImgToText(pil_img)
|
43 |
description = outputs[0]['generated_text']
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
frame_count += 1
|
48 |
|
49 |
cap.release()
|
50 |
|
51 |
-
concatenated_description = " ".join(descriptions)
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
print("SUMMARIZATION : " + summarized_description)
|
54 |
|
55 |
translated_text = translator(summarized_description)[0]["translation_text"]
|
|
|
7 |
import torch
|
8 |
import time
|
9 |
|
10 |
+
def detect_scene_changes(video_path, threshold):
|
11 |
+
"""
|
12 |
+
Détecte les changements de plan dans une vidéo.
|
13 |
|
14 |
+
Parameters:
|
15 |
+
- video_path: chemin vers le fichier vidéo
|
16 |
+
- threshold: seuil de différence pour détecter un changement de plan
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
Une liste des numéros d'images où un changement de plan est détecté.
|
20 |
+
"""
|
21 |
+
|
22 |
+
cap = cv2.VideoCapture(video_path)
|
23 |
+
|
24 |
+
if not cap.isOpened():
|
25 |
+
print("Erreur lors de l'ouverture de la vidéo.")
|
26 |
+
return []
|
27 |
+
|
28 |
+
ret, prev_frame = cap.read()
|
29 |
+
if not ret:
|
30 |
+
print("Erreur lors de la lecture de la vidéo.")
|
31 |
+
return []
|
32 |
+
|
33 |
+
prev_frame_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)
|
34 |
+
|
35 |
+
scene_changes = []
|
36 |
+
|
37 |
+
frame_number = 0
|
38 |
+
while True:
|
39 |
+
ret, current_frame = cap.read()
|
40 |
+
if not ret:
|
41 |
+
break
|
42 |
+
|
43 |
+
current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
|
44 |
+
|
45 |
+
# Calculer la différence absolue entre les deux images
|
46 |
+
diff = cv2.absdiff(prev_frame_gray, current_frame_gray)
|
47 |
+
mean_diff = np.mean(diff)
|
48 |
+
|
49 |
+
if mean_diff > threshold:
|
50 |
+
scene_changes.append(frame_number)
|
51 |
+
|
52 |
+
prev_frame_gray = current_frame_gray
|
53 |
+
frame_number += 1
|
54 |
+
|
55 |
+
cap.release()
|
56 |
+
return scene_changes
|
57 |
|
58 |
def video_to_descriptions(video, target_language="en"):
|
59 |
+
|
60 |
+
threshold = 30.0
|
61 |
+
|
62 |
+
scene_changes = detect_scene_changes(video, threshold)
|
63 |
|
64 |
start_time = time.time()
|
65 |
print("START TIME = ", start_time)
|
|
|
81 |
ret, frame = cap.read()
|
82 |
if not ret:
|
83 |
break
|
84 |
+
|
85 |
+
if (frame_count % (fps * 3) == 0) or (frame_count in scene_changes) :
|
|
|
86 |
|
87 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
88 |
|
|
|
90 |
|
91 |
outputs = ImgToText(pil_img)
|
92 |
description = outputs[0]['generated_text']
|
93 |
+
|
94 |
+
if (frame_count in scene_changes):
|
95 |
+
descriptions.append(" There has been a scene change, now we can observe " + description)
|
96 |
+
print(str(frame_count) + " | CHANGEMENT DE PLAN | " + outputs[0]['generated_text'])
|
97 |
+
|
98 |
+
else:
|
99 |
+
descriptions.append(" we can see that " + description)
|
100 |
+
print(str(frame_count) + " | " + outputs[0]['generated_text'])
|
101 |
+
|
102 |
frame_count += 1
|
103 |
|
104 |
cap.release()
|
105 |
|
106 |
+
concatenated_description = " ".join(descriptions).split("There has been a scene change, now we can observe")
|
107 |
+
plan_number = 1
|
108 |
+
summarized_description = f"We can see the Scene number {plan_number}, where "
|
109 |
+
|
110 |
+
for plan in concatenated_description
|
111 |
+
if not (summarized_description == "We can see the Scene number 1, where "):
|
112 |
+
summarized_description += f"There has been a scene change, now we can observe the Scene number {plan_number}, where "
|
113 |
+
summarized_description += Summarize(plan, max_length=20)[0]["summary_text"]
|
114 |
+
plan_number += 1
|
115 |
+
else:
|
116 |
+
summarized_description += Summarize(plan, max_length=20)[0]["summary_text"]
|
117 |
+
plan_number += 1
|
118 |
+
|
119 |
print("SUMMARIZATION : " + summarized_description)
|
120 |
|
121 |
translated_text = translator(summarized_description)[0]["translation_text"]
|