RakanAlsheraiwi commited on
Commit
44794ec
1 Parent(s): f0d127a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -33,11 +33,11 @@ def detect_and_draw_image(input_image):
33
 
34
  return input_image, df
35
 
36
- # دالة لاكتشاف الكائنات في الفيديو
37
  def detect_and_draw_video(video_path):
38
  cap = cv2.VideoCapture(video_path)
39
  frames = []
40
  overall_counts = {}
 
41
 
42
  while cap.isOpened():
43
  ret, frame = cap.read()
@@ -51,15 +51,24 @@ def detect_and_draw_video(video_path):
51
  for detection in detections:
52
  xmin, ymin, xmax, ymax, conf, class_id = detection
53
  label = model.names[int(class_id)]
54
- overall_counts[label] = overall_counts.get(label, 0) + 1
55
-
56
- cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2)
57
- cv2.putText(frame, f"{label}: {conf:.2f}", (int(xmin), int(ymin) - 10),
58
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
 
 
 
 
 
 
 
 
59
 
60
  frames.append(frame)
61
 
62
  cap.release()
 
63
  output_path = 'output.mp4'
64
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480))
65
 
@@ -67,7 +76,10 @@ def detect_and_draw_video(video_path):
67
  out.write(frame)
68
  out.release()
69
 
 
70
  translated_labels = translator(list(overall_counts.keys()))
 
 
71
  df = pd.DataFrame({
72
  'Label (English)': list(overall_counts.keys()),
73
  'Label (Arabic)': [t['translation_text'] for t in translated_labels],
@@ -76,6 +88,7 @@ def detect_and_draw_video(video_path):
76
 
77
  return output_path, df
78
 
 
79
  # واجهة صورة
80
  image_interface = gr.Interface(
81
  fn=detect_and_draw_image,
 
33
 
34
  return input_image, df
35
 
 
36
  def detect_and_draw_video(video_path):
37
  cap = cv2.VideoCapture(video_path)
38
  frames = []
39
  overall_counts = {}
40
+ seen_objects = [] # قائمة لتتبع الكائنات التي تم اكتشافها
41
 
42
  while cap.isOpened():
43
  ret, frame = cap.read()
 
51
  for detection in detections:
52
  xmin, ymin, xmax, ymax, conf, class_id = detection
53
  label = model.names[int(class_id)]
54
+ current_object = (label, int(xmin), int(ymin), int(xmax), int(ymax))
55
+
56
+ # التحقق من وجود الكائن في قائمة seen_objects
57
+ if not any(existing[0] == label and
58
+ (existing[1] < xmax and existing[3] > xmin and
59
+ existing[2] < ymax and existing[4] > ymin) for existing in seen_objects):
60
+ seen_objects.append(current_object)
61
+ overall_counts[label] = overall_counts.get(label, 0) + 1
62
+
63
+ # رسم المستطيل والكلمات على الإطار
64
+ cv2.rectangle(frame, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (255, 0, 0), 2)
65
+ cv2.putText(frame, f"{label}: {conf:.2f}", (int(xmin), int(ymin) - 10),
66
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
67
 
68
  frames.append(frame)
69
 
70
  cap.release()
71
+
72
  output_path = 'output.mp4'
73
  out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, (640, 480))
74
 
 
76
  out.write(frame)
77
  out.release()
78
 
79
+ # ترجمة التسميات إلى العربية
80
  translated_labels = translator(list(overall_counts.keys()))
81
+
82
+ # إنشاء DataFrame لتخزين النتائج
83
  df = pd.DataFrame({
84
  'Label (English)': list(overall_counts.keys()),
85
  'Label (Arabic)': [t['translation_text'] for t in translated_labels],
 
88
 
89
  return output_path, df
90
 
91
+
92
  # واجهة صورة
93
  image_interface = gr.Interface(
94
  fn=detect_and_draw_image,