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