muhammadsalmanalfaridzi commited on
Commit
586e4bb
·
verified ·
1 Parent(s): 002be3a

Update dino.txt

Browse files
Files changed (1) hide show
  1. dino.txt +57 -48
dino.txt CHANGED
@@ -1,14 +1,13 @@
1
- def detect_objects_in_video_with_dinox(video_path):
2
  temp_output_path = "/tmp/output_video.mp4"
3
  temp_frames_dir = tempfile.mkdtemp()
4
  frame_count = 0
5
- previous_detections = {} # For keeping track of detections in previous frames
6
 
7
- # DINO-X Configuration (For unclassified products)
8
  dinox_config = Config(DINOX_API_KEY)
9
  dinox_client = Client(dinox_config)
10
- DINOX_PROMPT = "beverage . bottle . cans . boxed milk . milk" # Customize based on your unclassified products
11
-
12
  try:
13
  # Convert video to MP4 if necessary
14
  if not video_path.endswith(".mp4"):
@@ -36,79 +35,88 @@ def detect_objects_in_video_with_dinox(video_path):
36
  frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
37
  cv2.imwrite(frame_path, frame)
38
 
39
- # ================== YOLO Detection (Nestlé Products) ==================
40
- predictions = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
41
 
42
- # Track current frame detections for YOLO
43
- current_detections_yolo = {}
44
- for prediction in predictions['predictions']:
45
  class_name = prediction['class']
46
  x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
47
  object_id = f"{class_name}_{x}_{y}"
48
 
49
- if object_id not in current_detections_yolo:
50
- current_detections_yolo[object_id] = class_name
51
 
52
  # Draw bounding box for detected products
53
  cv2.rectangle(frame, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
54
  cv2.putText(frame, class_name, (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
55
 
56
- # ================== DINO-X Detection (Unclassified Products) ==================
 
 
 
 
 
 
 
 
 
57
  image_url = dinox_client.upload_file(frame_path)
58
  task = DinoxTask(
59
  image_url=image_url,
60
- prompts=[TextPrompt(text=DINOX_PROMPT)]
61
  )
62
  dinox_client.run_task(task)
63
  dinox_pred = task.result.objects
64
 
65
- # Process DINO-X detections for unclassified products
66
- current_detections_dinox = {}
67
  for obj in dinox_pred:
68
- dinox_box = obj.bbox
69
  class_name = obj.category.strip().lower()
 
 
 
 
70
 
71
- # Check if the object is unclassified
72
- object_id = f"{class_name}_{dinox_box[0]}_{dinox_box[1]}"
73
-
74
- if object_id not in current_detections_dinox:
75
- current_detections_dinox[object_id] = class_name
76
-
77
- # Draw bounding box for unclassified products
78
- x1, y1, x2, y2 = dinox_box
79
  cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
80
- cv2.putText(frame, f"{class_name} {obj.score:.2f}", (int(x1), int(y1-10)),
81
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
82
 
83
- # ================== Update Counts ==================
84
- all_class_count = {}
85
- for class_name in current_detections_yolo.values():
86
- all_class_count[class_name] = all_class_count.get(class_name, 0) + 1
87
-
88
- for class_name in current_detections_dinox.values():
89
- all_class_count[class_name] = all_class_count.get(class_name, 0) + 1
90
-
91
- # Generate count text
92
- count_text = ""
93
- total_product_count = 0
94
- for class_name, count in all_class_count.items():
95
- count_text += f"{class_name}: {count}\n"
96
- total_product_count += count
97
- count_text += f"\nTotal Product: {total_product_count}"
98
-
99
- # Overlay the counts text onto the frame
 
100
  y_offset = 20
101
- for line in count_text.split("\n"):
 
 
 
 
 
102
  cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
103
- y_offset += 30 # Move down for next line
104
 
105
  # Write processed frame to output video
106
  output_video.write(frame)
107
  frame_count += 1
108
 
109
- # Update previous detections for the next frame
110
- previous_detections = current_detections_yolo
111
-
112
  video.release()
113
  output_video.release()
114
 
@@ -116,3 +124,4 @@ def detect_objects_in_video_with_dinox(video_path):
116
 
117
  except Exception as e:
118
  return None, f"An error occurred: {e}"
 
 
1
+ def detect_objects_in_video(video_path):
2
  temp_output_path = "/tmp/output_video.mp4"
3
  temp_frames_dir = tempfile.mkdtemp()
4
  frame_count = 0
5
+ previous_detections = {} # Untuk menyimpan deteksi objek dari frame sebelumnya
6
 
7
+ # Inisialisasi DINO-X untuk deteksi unclassified products
8
  dinox_config = Config(DINOX_API_KEY)
9
  dinox_client = Client(dinox_config)
10
+
 
11
  try:
12
  # Convert video to MP4 if necessary
13
  if not video_path.endswith(".mp4"):
 
35
  frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
36
  cv2.imwrite(frame_path, frame)
37
 
38
+ # Process predictions for the current frame using YOLO (Nestlé products)
39
+ yolo_pred = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
40
 
41
+ # Track current frame detections (Nestlé)
42
+ current_detections = {}
43
+ for prediction in yolo_pred['predictions']:
44
  class_name = prediction['class']
45
  x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
46
  object_id = f"{class_name}_{x}_{y}"
47
 
48
+ if object_id not in current_detections:
49
+ current_detections[object_id] = class_name
50
 
51
  # Draw bounding box for detected products
52
  cv2.rectangle(frame, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
53
  cv2.putText(frame, class_name, (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
54
 
55
+ # Calculate product counts (Nestlé)
56
+ nestle_counts = {}
57
+ for detection_id in current_detections.keys():
58
+ class_name = current_detections[detection_id]
59
+ nestle_counts[class_name] = nestle_counts.get(class_name, 0) + 1
60
+
61
+ # Update previous_detections for the next frame
62
+ previous_detections = current_detections
63
+
64
+ # --- Deteksi Unclassified Products menggunakan DINO-X ---
65
  image_url = dinox_client.upload_file(frame_path)
66
  task = DinoxTask(
67
  image_url=image_url,
68
+ prompts=[TextPrompt(text=DINOX_PROMPT)] # Define the DINO-X prompt here
69
  )
70
  dinox_client.run_task(task)
71
  dinox_pred = task.result.objects
72
 
73
+ # Filter & Hitung Unclassified Products
74
+ unclassified_counts = {}
75
  for obj in dinox_pred:
 
76
  class_name = obj.category.strip().lower()
77
+ if class_name not in unclassified_counts:
78
+ unclassified_counts[class_name] = 1
79
+ else:
80
+ unclassified_counts[class_name] += 1
81
 
82
+ # Draw bounding box for unclassified objects
83
+ x1, y1, x2, y2 = obj.bbox
 
 
 
 
 
 
84
  cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
85
+ cv2.putText(frame, f"{class_name} {obj.score:.2f}", (int(x1), int(y1-10)),
86
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
87
 
88
+ # --- Teks Overlay untuk menghitung produk ---
89
+ # Format counting untuk Nestlé (dari YOLO)
90
+ nestle_count_text = ""
91
+ total_nestle = 0
92
+ for class_name, count in nestle_counts.items():
93
+ nestle_count_text += f"{class_name}: {count}\n"
94
+ total_nestle += count
95
+ nestle_count_text += f"\nTotal Nestlé Products: {total_nestle}"
96
+
97
+ # Format counting untuk Unclassified (dari DINO-X)
98
+ unclassified_count_text = ""
99
+ total_unclassified = 0
100
+ for class_name, count in unclassified_counts.items():
101
+ unclassified_count_text += f"{class_name}: {count}\n"
102
+ total_unclassified += count
103
+ unclassified_count_text += f"\nTotal Unclassified Products: {total_unclassified}"
104
+
105
+ # Overlay teks ke frame
106
  y_offset = 20
107
+ for line in nestle_count_text.split("\n"):
108
+ cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
109
+ y_offset += 30
110
+
111
+ y_offset += 30 # Slight gap between sections
112
+ for line in unclassified_count_text.split("\n"):
113
  cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
114
+ y_offset += 30
115
 
116
  # Write processed frame to output video
117
  output_video.write(frame)
118
  frame_count += 1
119
 
 
 
 
120
  video.release()
121
  output_video.release()
122
 
 
124
 
125
  except Exception as e:
126
  return None, f"An error occurred: {e}"
127
+