flocolombari commited on
Commit
0cad1a1
·
1 Parent(s): d6268bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -79
app.py CHANGED
@@ -1,95 +1,48 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image
4
- import moviepy.editor as mp
5
- import numpy as np
6
- import os
7
-
8
- # Étape 1: Configurez vos pipelines
9
- model1 = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
10
- #model2 = pipeline("summarization", model="ainize/kobart-news")
11
- #model3 = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-pt")
12
- #model4 = pipeline("text-to-speech", model="microsoft/speecht5_tts")
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
- def process_video(video):
21
-
22
- if not os.path.exists("/main/images/"):
23
- os.makedirs("/main/images/")
24
 
 
 
 
 
25
  # Ouvrir la vidéo
26
- cap = cv2.VideoCapture(video)
27
- if not cap.isOpened():
28
- print("Erreur lors de l'ouverture de la vidéo.")
29
- return
30
-
31
- # Fréquence d'images de la vidéo
32
  fps = int(cap.get(cv2.CAP_PROP_FPS))
33
-
34
- # Nombre total d'images dans la vidéo
35
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
36
-
37
- # Calculer le nombre d'images à sauter pour obtenir une image toutes les demi-secondes
38
- frames_to_skip = int(fps * interval)
39
-
40
- count = 0
41
- for i in range(0, total_frames, frames_to_skip):
42
- cap.set(cv2.CAP_PROP_POS_FRAMES, i)
43
- ret, frame = cap.read()
44
-
45
- # Si la lecture a réussi, enregistrez l'image
46
- if ret:
47
- output_path = os.path.join("/main/images/", f"frame_{count}.jpg")
48
- cv2.imwrite(output_path, frame)
49
- count += 1
50
- cap.release()
51
-
52
- fichiers = os.listdir("/main/images/")
53
- output_texts = []
54
-
55
- for fichier in fichiers:
56
 
57
- if fichier.endswith(".jpg") or fichier.endswith(".png"):
58
- # Construisez le chemin complet vers le fichier
59
- chemin_complet = os.path.join(chemin_dossier, fichier)
60
- model1_output = model1(image)
61
- output_texts.append(model1_output["generated_text"])
62
-
63
 
64
- # Convertir chaque frame en Image pour pouvoir l'utiliser dans le pipeline
65
- #image = Image.fromarray(frame)
66
-
67
- # Étape 3: Utiliser le modèle 1
68
- #model1_output = model1(image)
69
-
70
- # Étape 4: Utiliser le modèle 2
71
- #model2_output = model2(model1_output["generated_text"])
72
-
73
- # Étape 5: Utiliser le modèle 3
74
- #model3_output = model3(model2_output["generated_text"])
75
 
76
- #output_texts.append(model1_output["generated_text"])
 
 
 
 
 
 
 
77
 
 
78
 
79
- # Étape 6: Utiliser le modèle 4 pour générer l'audio
80
- #model4_output = model4(" ".join(output_texts))
81
 
82
- # Récupérer l'audio et le retourner
83
- #audio_output = model4_output["..."] # Remplacer "..." avec la clé appropriée
84
- #return audio_output
85
- return " ".join(output_texts)
86
 
87
- # Créer une interface gradio
88
  iface = gr.Interface(
89
- fn=process_video,
90
- inputs=gr.inputs.Video(label="Votre Vidéo"),
91
  outputs="text",
92
- live=True
93
  )
94
 
95
- iface.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import cv2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ def video_to_descriptions(video):
6
+ # Charger le modèle via pipeline
7
+ model = pipeline('image-to-text', model='nlpconnect/vit-gpt2-image-captioning')
8
+
9
  # Ouvrir la vidéo
10
+ cap = cv2.VideoCapture(video.name)
 
 
 
 
 
11
  fps = int(cap.get(cv2.CAP_PROP_FPS))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ descriptions = []
14
+ frame_count = 0
 
 
 
 
15
 
16
+ while True:
17
+ ret, frame = cap.read()
18
+ if not ret:
19
+ break
 
 
 
 
 
 
 
20
 
21
+ # Extraire une image toutes les demi-secondes
22
+ if frame_count % (fps // 2) == 0:
23
+ # Convertir l'image en RGB
24
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
+ # Obtenir la description de l'image
26
+ outputs = model(frame_rgb)
27
+ description = outputs[0]['describe-text']
28
+ descriptions.append(description)
29
 
30
+ frame_count += 1
31
 
32
+ # Fermer le lecteur vidéo
33
+ cap.release()
34
 
35
+ # Concaténer les descriptions
36
+ concatenated_descriptions = " ".join(descriptions)
37
+
38
+ return concatenated_descriptions
39
 
 
40
  iface = gr.Interface(
41
+ fn=video_to_descriptions,
42
+ inputs=gr.inputs.Video(type="file", label="Importez une vidéo"),
43
  outputs="text",
44
+ live=False
45
  )
46
 
47
+ if __name__ == "__app__":
48
+ iface.launch()