Aziz-AI98 commited on
Commit
d0bc55c
·
verified ·
1 Parent(s): c01af97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -27
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, "الرجاء رفع ملف فيديو صالح أولاً."
@@ -17,18 +17,17 @@ def process_gate_video(video_path):
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():
@@ -36,7 +35,7 @@ def process_gate_video(video_path):
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:
@@ -46,8 +45,7 @@ def process_gate_video(video_path):
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 على الشخص
@@ -55,27 +53,16 @@ def process_gate_video(video_path):
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
 
 
7
  # تحميل نموذج YOLOv8
8
  model = YOLO("yolov8n.pt")
9
 
10
+ @spaces.GPU # ملاحظة: احذف هذا السطر إذا كنت تجربه محلياً أو في كولاب بدون هابي فيس، واتركه إذا كان في Hugging Face
11
  def process_gate_video(video_path):
12
  if video_path is None:
13
  return None, "الرجاء رفع ملف فيديو صالح أولاً."
 
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
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
  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:
 
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 على الشخص
 
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