Docfile commited on
Commit
59d73a5
·
verified ·
1 Parent(s): d8c204d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from collections import defaultdict
3
+ import cv2
4
+ import numpy as np
5
+ import tempfile
6
+ from ultralytics import YOLO
7
+ import os
8
+ import telegram # Import de la bibliothèque
9
+ import asyncio # Nécessaire pour les opérations asynchrones
10
+
11
+ # --- CONFIGURATION TELEGRAM ---
12
+ # Récupère les identifiants depuis les variables d'environnement
13
+ TELEGRAM_BOT_TOKEN = "8489885905:AAFoNtXgszbL28yX8ogvQdL6lBHXa9lwHHI"
14
+ TELEGRAM_CHAT_ID = "-1002689269933"
15
+
16
+ # 1. Charger le modèle une seule fois au démarrage de l'application
17
+ print("Chargement du modèle YOLOv8...")
18
+ model = YOLO("yolov11n.pt")
19
+ print("Modèle chargé.")
20
+
21
+ # 2. Fonction de traitement vidéo (inchangée)
22
+ def track_objects(video_in_path):
23
+ cap = cv2.VideoCapture(video_in_path)
24
+ if not cap.isOpened():
25
+ raise gr.Error("Erreur : Impossible d'ouvrir la vidéo d'entrée.")
26
+
27
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
28
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
29
+ fps = cap.get(cv2.CAP_PROP_FPS)
30
+
31
+ video_out_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
32
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
33
+ out = cv2.VideoWriter(video_out_path, fourcc, fps, (width, height))
34
+
35
+ track_history = defaultdict(lambda: [])
36
+
37
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
38
+ print(f"Début du traitement de {total_frames} images.")
39
+
40
+ while cap.isOpened():
41
+ success, frame = cap.read()
42
+ if not success:
43
+ break
44
+
45
+ results = model.track(frame, persist=True)
46
+ annotated_frame = results[0].plot() # .plot() gère déjà le cas où il n'y a pas de détection
47
+
48
+ if results[0].boxes is not None and results[0].boxes.id is not None:
49
+ boxes = results[0].boxes.xywh.cpu()
50
+ track_ids = results[0].boxes.id.int().cpu().tolist()
51
+
52
+ for box, track_id in zip(boxes, track_ids):
53
+ x, y, w, h = box
54
+ track = track_history[track_id]
55
+ track.append((float(x), float(y)))
56
+ if len(track) > 30:
57
+ track.pop(0)
58
+
59
+ points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
60
+ cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=3)
61
+
62
+ out.write(annotated_frame)
63
+
64
+ cap.release()
65
+ out.release()
66
+ print("Traitement vidéo terminé.")
67
+ return video_out_path
68
+
69
+ # 3. Nouvelle fonction pour envoyer la vidéo sur Telegram
70
+ async def send_to_telegram(video_path):
71
+ if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
72
+ error_msg = "Erreur : Les variables d'environnement TELEGRAM_BOT_TOKEN et TELEGRAM_CHAT_ID ne sont pas configurées."
73
+ print(error_msg)
74
+ return error_msg
75
+
76
+ try:
77
+ print("Initialisation du bot Telegram...")
78
+ bot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)
79
+ print(f"Envoi de la vidéo {video_path} au chat ID {TELEGRAM_CHAT_ID}...")
80
+
81
+ # Envoi de la vidéo
82
+ async with bot:
83
+ with open(video_path, 'rb') as video_file:
84
+ await bot.send_video(
85
+ chat_id=TELEGRAM_CHAT_ID,
86
+ video=video_file,
87
+ caption="Voici la vidéo traitée avec le suivi d'objets YOLOv8."
88
+ )
89
+
90
+ success_msg = "Vidéo envoyée avec succès sur Telegram !"
91
+ print(success_msg)
92
+ return success_msg
93
+ except Exception as e:
94
+ error_msg = f"Une erreur est survenue lors de l'envoi sur Telegram : {e}"
95
+ print(error_msg)
96
+ return error_msg
97
+
98
+ # 4. Fonction principale qui orchestre le tout pour Gradio
99
+ async def process_and_send(video_in_path):
100
+ if video_in_path is None:
101
+ raise gr.Error("Veuillez d'abord uploader une vidéo.")
102
+
103
+ # Étape 1: Traiter la vidéo
104
+ gr.Info("Le traitement de la vidéo a commencé...")
105
+ processed_video_path = track_objects(video_in_path)
106
+
107
+ # Étape 2: Envoyer sur Telegram
108
+ gr.Info("Traitement terminé. Envoi sur Telegram...")
109
+ telegram_status = await send_to_telegram(processed_video_path)
110
+
111
+ # Étape 3: Renvoyer les résultats à l'interface Gradio
112
+ return processed_video_path, telegram_status
113
+
114
+ # 5. Créer l'interface Gradio
115
+ with gr.Blocks() as demo:
116
+ gr.Markdown("# Démonstration de Suivi d'Objets avec YOLOv8 et envoi sur Telegram")
117
+ gr.Markdown("Uploadez une vidéo. Une fois le traitement terminé, le résultat s'affichera ici et sera envoyé sur votre chat Telegram.")
118
+
119
+ with gr.Row():
120
+ video_input = gr.Video(label="Vidéo d'entrée")
121
+ video_output = gr.Video(label="Vidéo avec suivi")
122
+
123
+ status_textbox = gr.Textbox(label="Statut de l'envoi Telegram", interactive=False)
124
+
125
+ btn = gr.Button("Lancer le suivi et Envoyer sur Telegram")
126
+ btn.click(
127
+ fn=process_and_send,
128
+ inputs=video_input,
129
+ outputs=[video_output, status_textbox]
130
+ )
131
+
132
+ # Lancer l'application
133
+ if __name__ == "__main__":
134
+ demo.launch()