Aziz-AI98 commited on
Commit
eee755a
·
verified ·
1 Parent(s): a1e1295

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import gradio as gr
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+
6
+ # 1. تحميل نموذج YOLO (سيتم تحميله تلقائياً عند تشغيل المساحة)
7
+ model = YOLO("yolov8n.pt")
8
+
9
+ def process_gate_video(video_path):
10
+ """
11
+ دالة لمعالجة الفيديو، تتبع الأجسام عبر YOLO، وعد الأشخاص العابرين للبوابة.
12
+ """
13
+ if video_path is None:
14
+ return None, "الرجاء رفع ملف فيديو صالح أولاً."
15
+
16
+ cap = cv2.VideoCapture(video_path)
17
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
18
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
19
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
20
+
21
+ # إعداد ملف الفيديو الناتج مع ترميز يدعم تشغيله داخل المتصفح
22
+ output_path = "output_counted_gate.mp4"
23
+ fourcc = cv2.VideoWriter_fourcc(*'avc1') # أو استخدم 'mp4v' حسب المتوفر
24
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
25
+
26
+ people_count = 0
27
+ tracked_ids = set()
28
+ line_y = int(height * 0.6) # تحديد مكان خط العد افتراضياً في أسفل/منتصف الفيديو
29
+
30
+ while cap.isOpened():
31
+ success, frame = cap.read()
32
+ if not success:
33
+ break
34
+
35
+ # استخدام التتبع (track) وتحديد الفئة 0 الخاصة بالأشخاص (Persons)
36
+ results = model.track(frame, persist=True, classes=[0], verbose=False)
37
+
38
+ if results[0].boxes.id is not None:
39
+ boxes = results[0].boxes.xyxy.cpu().numpy()
40
+ track_ids = results[0].boxes.id.int().cpu().numpy()
41
+
42
+ for box, track_id in zip(boxes, track_ids):
43
+ x1, y1, x2, y2 = map(int, box)
44
+ center_x = int((x1 + x2) / 2)
45
+ center_y = int(y2)
46
+
47
+ # رسم المربع المحيط بالشخص واسم المعرف الخاص به
48
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
49
+ cv2.putText(frame, f"ID: {track_id}", (x1, y1 - 10),
50
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
51
+
52
+ # منطق العد عند عبور الخط الافتراضي
53
+ if line_y - 10 < center_y < line_y + 10:
54
+ if track_id not in tracked_ids:
55
+ people_count += 1
56
+ tracked_ids.add(track_id)
57
+
58
+ # رسم خط العد باللون الأحمر على الفريم
59
+ cv2.line(frame, (0, line_y), (width, line_y), (0, 0, 255), 3)
60
+
61
+ # كتابة العداد الحالي على الشاشة
62
+ cv2.putText(frame, f"Total Count: {people_count}", (30, 50),
63
+ cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
64
+
65
+ out.write(frame)
66
+
67
+ cap.release()
68
+ out.release()
69
+
70
+ result_message = f"تم الانتهاء من المعالجة بنجاح. إجمالي الأشخاص العابرين: {people_count}"
71
+ return output_path, result_message
72
+
73
+ # 2. بناء واجهة Gradio التفاعلية
74
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
75
+ gr.Markdown("# 🚪 نظام عد الأشخاص الذكي عبر البوابات")
76
+ gr.Markdown("قم برفع فيديو لكاميرا المراقبة الخاصة بالبوابة، وسيقوم النظام بتعقب الأشخاص وعدهم تلقائياً باستخدام **YOLO** و **OpenCV**.")
77
+
78
+ with gr.Row():
79
+ with gr.Column():
80
+ video_input = gr.Video(label="فيديو البوابة المدخل")
81
+ submit_btn = gr.Button("بدء العد والمعالجة", variant="primary")
82
+
83
+ with gr.Column():
84
+ video_output = gr.Video(label="الفيديو بعد المعالجة والعد")
85
+ text_output = gr.Textbox(label="ملخص النتيجة")
86
+
87
+ submit_btn.click(
88
+ fn=process_gate_video,
89
+ inputs=video_input,
90
+ outputs=[video_output, text_output]
91
+ )
92
+
93
+ # تشغيل التطبيق
94
+ if __name__ == "__main__":
95
+ demo.launch()