Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,11 @@ import numpy as np
|
|
| 4 |
import spaces
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
| 7 |
-
|
| 8 |
-
# 1. تحميل نموذج YOLO (سيتم تحميله تلقائياً عند تشغيل المساحة)
|
| 9 |
model = YOLO("yolov8n.pt")
|
| 10 |
|
| 11 |
@spaces.GPU
|
| 12 |
def process_gate_video(video_path):
|
| 13 |
-
"""
|
| 14 |
-
دالة لمعالجة الفيديو، تتبع الأجسام عبر YOLO، وعد الأشخاص العابرين للبوابة.
|
| 15 |
-
"""
|
| 16 |
if video_path is None:
|
| 17 |
return None, "الرجاء رفع ملف فيديو صالح أولاً."
|
| 18 |
|
|
@@ -20,22 +16,27 @@ def process_gate_video(video_path):
|
|
| 20 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 21 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 22 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
output_path = "output_counted_gate.mp4"
|
| 26 |
-
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 27 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 28 |
|
| 29 |
people_count = 0
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
while cap.isOpened():
|
| 34 |
success, frame = cap.read()
|
| 35 |
if not success:
|
| 36 |
break
|
| 37 |
|
| 38 |
-
# استخدام التتبع
|
| 39 |
results = model.track(frame, persist=True, classes=[0], verbose=False)
|
| 40 |
|
| 41 |
if results[0].boxes.id is not None:
|
|
@@ -44,26 +45,39 @@ def process_gate_video(video_path):
|
|
| 44 |
|
| 45 |
for box, track_id in zip(boxes, track_ids):
|
| 46 |
x1, y1, x2, y2 = map(int, box)
|
|
|
|
|
|
|
| 47 |
center_x = int((x1 + x2) / 2)
|
| 48 |
center_y = int(y2)
|
| 49 |
|
| 50 |
-
# رسم المربع
|
| 51 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 52 |
cv2.putText(frame, f"ID: {track_id}", (x1, y1 - 10),
|
| 53 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
if
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
people_count += 1
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
cv2.line(frame, (0, line_y), (width, line_y), (0, 0, 255), 3)
|
| 63 |
|
| 64 |
-
# كتابة العداد الحالي على ال
|
| 65 |
-
cv2.putText(frame, f"Total Count: {people_count}", (30,
|
| 66 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1.
|
| 67 |
|
| 68 |
out.write(frame)
|
| 69 |
|
|
@@ -73,10 +87,10 @@ def process_gate_video(video_path):
|
|
| 73 |
result_message = f"تم الانتهاء من المعالجة بنجاح. إجمالي الأشخاص العابرين: {people_count}"
|
| 74 |
return output_path, result_message
|
| 75 |
|
| 76 |
-
#
|
| 77 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 78 |
gr.Markdown("# 🚪 نظام عد الأشخاص الذكي عبر البوابات")
|
| 79 |
-
gr.Markdown("قم برفع فيديو لكاميرا المراقبة
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
with gr.Column():
|
|
@@ -93,6 +107,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 93 |
outputs=[video_output, text_output]
|
| 94 |
)
|
| 95 |
|
| 96 |
-
# تشغيل التطبيق
|
| 97 |
if __name__ == "__main__":
|
| 98 |
demo.launch()
|
|
|
|
| 4 |
import spaces
|
| 5 |
from ultralytics import YOLO
|
| 6 |
|
| 7 |
+
# تحميل نموذج YOLOv8
|
|
|
|
| 8 |
model = YOLO("yolov8n.pt")
|
| 9 |
|
| 10 |
@spaces.GPU
|
| 11 |
def process_gate_video(video_path):
|
|
|
|
|
|
|
|
|
|
| 12 |
if video_path is None:
|
| 13 |
return None, "الرجاء رفع ملف فيديو صالح أولاً."
|
| 14 |
|
|
|
|
| 16 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 19 |
+
if fps <= 0:
|
| 20 |
+
fps = 25 # قيمة افتراضية في حال لم يتم قراءة الـ fps
|
| 21 |
+
|
| 22 |
+
# استخدام ترميز 'avc1' لضمان عمل الفيديو داخل متصفح الويب وفي واجهة Hugging Face
|
| 23 |
output_path = "output_counted_gate.mp4"
|
| 24 |
+
fourcc = cv2.VideoWriter_fourcc(*'avc1')
|
| 25 |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 26 |
|
| 27 |
people_count = 0
|
| 28 |
+
# تخزين آخر إحداثي Y لكل ID لتحديد اتجاه العبور بدقة
|
| 29 |
+
track_history = {}
|
| 30 |
+
|
| 31 |
+
# تحديد موقع خط العد (في منتصف الارتفاع تقريباً أو حسب رغبتك)
|
| 32 |
+
line_y = int(height * 0.5)
|
| 33 |
|
| 34 |
while cap.isOpened():
|
| 35 |
success, frame = cap.read()
|
| 36 |
if not success:
|
| 37 |
break
|
| 38 |
|
| 39 |
+
# استخدام التتبع لتتبع الأشخاص عبر الإطارات
|
| 40 |
results = model.track(frame, persist=True, classes=[0], verbose=False)
|
| 41 |
|
| 42 |
if results[0].boxes.id is not None:
|
|
|
|
| 45 |
|
| 46 |
for box, track_id in zip(boxes, track_ids):
|
| 47 |
x1, y1, x2, y2 = map(int, box)
|
| 48 |
+
|
| 49 |
+
# استخدام مركز أسفل الشخص (منطقة الأقدام) لتحديد العبور بدقة أدق
|
| 50 |
center_x = int((x1 + x2) / 2)
|
| 51 |
center_y = int(y2)
|
| 52 |
|
| 53 |
+
# رسم المربع واسم الـ ID على الشخص
|
| 54 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 55 |
cv2.putText(frame, f"ID: {track_id}", (x1, y1 - 10),
|
| 56 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 57 |
+
|
| 58 |
+
# تتبع حركة الشخص عبر الإطارات لمعرفة اتجاه العبور
|
| 59 |
+
if track_id in track_history:
|
| 60 |
+
prev_y = track_history[track_id]
|
| 61 |
+
|
| 62 |
+
# منطق العد: إذا كان الشخص يتحرك وعبر خط العد (من الأعلى إلى الأسفل أو العكس)
|
| 63 |
+
# نتأكد أن الـ ID لم يتم حسابه مسبقاً بطريقة تمنع التكرار
|
| 64 |
+
if prev_y < line_y and center_y >= line_y:
|
| 65 |
+
# عبر للأسفل (أو للخارج)
|
| 66 |
people_count += 1
|
| 67 |
+
# نقوم بتحديث التاريخ لمنع عد نفس الـ ID مرتين في نفس الممر
|
| 68 |
+
track_history[track_id] = center_y + 1000 # قيمة وهمية لمنع التكرار
|
| 69 |
+
elif prev_y > line_y and center_y <= line_y:
|
| 70 |
+
# عبر للأعلى (اختياري، يمكنك تفعيله إذا أردت عد الاتجاهين)
|
| 71 |
+
pass
|
| 72 |
+
|
| 73 |
+
track_history[track_id] = center_y
|
| 74 |
+
|
| 75 |
+
# رسم خط العد باللون الأحمر الواضح
|
| 76 |
cv2.line(frame, (0, line_y), (width, line_y), (0, 0, 255), 3)
|
| 77 |
|
| 78 |
+
# كتابة العداد الحالي على الفيديو
|
| 79 |
+
cv2.putText(frame, f"Total Count: {people_count}", (30, 60),
|
| 80 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3)
|
| 81 |
|
| 82 |
out.write(frame)
|
| 83 |
|
|
|
|
| 87 |
result_message = f"تم الانتهاء من المعالجة بنجاح. إجمالي الأشخاص العابرين: {people_count}"
|
| 88 |
return output_path, result_message
|
| 89 |
|
| 90 |
+
# بناء واجهة Gradio
|
| 91 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 92 |
gr.Markdown("# 🚪 نظام عد الأشخاص الذكي عبر البوابات")
|
| 93 |
+
gr.Markdown("قم برفع فيديو لكاميرا المراقبة لمعالجة الفيديو، تتبع الأجسام، وعد الأشخاص بدقة.")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
|
|
|
| 107 |
outputs=[video_output, text_output]
|
| 108 |
)
|
| 109 |
|
|
|
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch()
|