LaurianeMD commited on
Commit
3970640
·
verified ·
1 Parent(s): e8771bf

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +89 -64
  2. requirements.txt +4 -1
  3. yolo12m.pt +3 -0
app.py CHANGED
@@ -1,64 +1,89 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from ultralytics import YOLO
4
+ import tempfile
5
+ import os
6
+
7
+ # Charger le modèle
8
+ model = YOLO("yolo12m.pt") # ou yolov8n.pt
9
+
10
+ # Classes à détecter
11
+ target_classes = ["backpack", "suitcase", "handbag"]
12
+ class_name_to_id = {name: idx for idx, name in model.names.items()}
13
+ target_ids = [class_name_to_id[c] for c in target_classes]
14
+
15
+ def process_video(video_path):
16
+ cap = cv2.VideoCapture(video_path)
17
+ ret, frame = cap.read()
18
+ if not ret:
19
+ return "Erreur lecture vidéo"
20
+
21
+ H, W = frame.shape[:2]
22
+ line_y = int(H * 0.6)
23
+ tolerance = 25
24
+
25
+ counted_ids = set()
26
+ class_counts = {c: 0 for c in target_classes}
27
+ total_count = 0
28
+
29
+ # Output temporaire
30
+ temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
31
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
32
+ out = cv2.VideoWriter(temp_out.name, fourcc, 20, (W, H))
33
+
34
+ cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
35
+
36
+ while True:
37
+ ret, frame = cap.read()
38
+ if not ret:
39
+ break
40
+
41
+ results = model.track(frame, persist=True, classes=target_ids, verbose=False)
42
+
43
+ if results[0].boxes.id is not None:
44
+ ids = results[0].boxes.id.int().cpu().tolist()
45
+ clss = results[0].boxes.cls.int().cpu().tolist()
46
+ boxes = results[0].boxes.xyxy.cpu().tolist()
47
+
48
+ for obj_id, cls, box in zip(ids, clss, boxes):
49
+ x1, y1, x2, y2 = map(int, box)
50
+ center_x = (x1 + x2) // 2
51
+ center_y = (y1 + y2) // 2
52
+ class_name = model.names[cls]
53
+
54
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
55
+ cv2.putText(frame, f"{class_name} ID:{obj_id}", (x1, y1 - 10),
56
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
57
+
58
+ if obj_id not in counted_ids and (line_y - tolerance < center_y < line_y + tolerance):
59
+ counted_ids.add(obj_id)
60
+ total_count += 1
61
+ class_counts[class_name] += 1
62
+
63
+ # Affichage des résultats
64
+ y_offset = 30
65
+ for name in target_classes:
66
+ cv2.putText(frame, f"{name}: {class_counts[name]}", (20, y_offset),
67
+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2)
68
+ y_offset += 30
69
+ cv2.putText(frame, f"Total: {total_count}", (20, y_offset + 10),
70
+ cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
71
+ cv2.line(frame, (0, line_y), (W, line_y), (0, 0, 255), 2)
72
+
73
+ out.write(frame)
74
+
75
+ cap.release()
76
+ out.release()
77
+ return temp_out.name
78
+
79
+ # Interface Gradio
80
+ demo = gr.Interface(
81
+ fn=process_video,
82
+ inputs=gr.Video(label="Importer une vidéo de bagages"),
83
+ outputs=gr.Video(label="Vidéo annotée avec comptage"),
84
+ title="🎒 Compteur intelligent de bagages",
85
+ description="Détecte et compte les sacs, valises, et sacs à dos avec YOLO."
86
+ )
87
+
88
+ if __name__ == "__main__":
89
+ demo.launch()
requirements.txt CHANGED
@@ -1 +1,4 @@
1
- huggingface_hub==0.25.2
 
 
 
 
1
+ opencv-python==4.11.0.86
2
+ deep-sort-realtime==1.3.1
3
+ ultralytics==8.3.141
4
+ gradio
yolo12m.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4c6d179786eddf6134ee469ae2f4ce04cbe4e9d1a47d6b669d9cd6b9c6c513d8
3
+ size 40904955