Update dino.txt
Browse files
dino.txt
CHANGED
@@ -1,129 +1,275 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
video_path, err = convert_video_to_mp4(video_path, temp_output_path)
|
15 |
-
if not video_path:
|
16 |
-
return None, f"Video conversion error: {err}"
|
17 |
-
|
18 |
-
# Read video and process frames
|
19 |
-
video = cv2.VideoCapture(video_path)
|
20 |
-
frame_rate = int(video.get(cv2.CAP_PROP_FPS))
|
21 |
-
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
22 |
-
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
23 |
-
frame_size = (frame_width, frame_height)
|
24 |
-
|
25 |
-
# VideoWriter for output video
|
26 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
27 |
-
output_video = cv2.VideoWriter(temp_output_path, fourcc, frame_rate, frame_size)
|
28 |
-
|
29 |
-
while True:
|
30 |
-
ret, frame = video.read()
|
31 |
-
if not ret:
|
32 |
-
break
|
33 |
-
|
34 |
-
# Save frame temporarily for predictions
|
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 |
-
# Ganti nama kelas menjadi 'unclassified'
|
77 |
-
class_name = "unclassified" # Ganti label menjadi 'unclassified'
|
78 |
-
|
79 |
-
if class_name not in unclassified_counts:
|
80 |
-
unclassified_counts[class_name] = 1
|
81 |
-
else:
|
82 |
-
unclassified_counts[class_name] += 1
|
83 |
-
|
84 |
-
# Draw bounding box for unclassified objects
|
85 |
-
x1, y1, x2, y2 = obj.bbox
|
86 |
-
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
|
87 |
-
cv2.putText(frame, f"{class_name} {obj.score:.2f}", (int(x1), int(y1-10)),
|
88 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
89 |
-
|
90 |
-
# --- Teks Overlay untuk menghitung produk ---
|
91 |
-
# Format counting untuk Nestlé (dari YOLO)
|
92 |
-
nestle_count_text = ""
|
93 |
-
total_nestle = 0
|
94 |
-
for class_name, count in nestle_counts.items():
|
95 |
-
nestle_count_text += f"{class_name}: {count}\n"
|
96 |
-
total_nestle += count
|
97 |
-
nestle_count_text += f"\nTotal Nestlé Products: {total_nestle}"
|
98 |
-
|
99 |
-
# Format counting untuk Unclassified (dari DINO-X)
|
100 |
-
unclassified_count_text = ""
|
101 |
-
total_unclassified = 0
|
102 |
-
for class_name, count in unclassified_counts.items():
|
103 |
-
unclassified_count_text += f"{class_name}: {count}\n"
|
104 |
-
total_unclassified += count
|
105 |
-
unclassified_count_text += f"\nTotal Unclassified Products: {total_unclassified}"
|
106 |
-
|
107 |
-
# Overlay teks ke frame
|
108 |
-
y_offset = 20
|
109 |
-
for line in nestle_count_text.split("\n"):
|
110 |
-
cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
111 |
-
y_offset += 30
|
112 |
-
|
113 |
-
y_offset += 30 # Slight gap between sections
|
114 |
-
for line in unclassified_count_text.split("\n"):
|
115 |
-
cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
116 |
-
y_offset += 30
|
117 |
-
|
118 |
-
# Write processed frame to output video
|
119 |
-
output_video.write(frame)
|
120 |
-
frame_count += 1
|
121 |
-
|
122 |
-
video.release()
|
123 |
-
output_video.release()
|
124 |
-
|
125 |
-
return temp_output_path
|
126 |
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
import requests
|
7 |
+
from dds_cloudapi_sdk import Config, Client
|
8 |
+
from dds_cloudapi_sdk.tasks.dinox import DinoxTask
|
9 |
+
from dds_cloudapi_sdk import TextPrompt
|
10 |
+
from dds_cloudapi_sdk.tasks.types import DetectionTarget
|
11 |
+
from roboflow import Roboflow
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
|
14 |
+
# ========== Konfigurasi ==========
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Roboflow Config
|
18 |
+
rf_api_key = os.getenv("ROBOFLOW_API_KEY")
|
19 |
+
workspace = os.getenv("ROBOFLOW_WORKSPACE")
|
20 |
+
project_name = os.getenv("ROBOFLOW_PROJECT")
|
21 |
+
model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
|
22 |
+
|
23 |
+
# DINO-X Config
|
24 |
+
DINOX_API_KEY = os.getenv("DINO_X_API_KEY")
|
25 |
+
DINOX_PROMPT = "beverage . bottle . cans . boxed milk . milk"
|
26 |
+
|
27 |
+
# Inisialisasi Model YOLO (Roboflow)
|
28 |
+
rf = Roboflow(api_key=rf_api_key)
|
29 |
+
project = rf.workspace(workspace).project(project_name)
|
30 |
+
yolo_model = project.version(model_version).model
|
31 |
+
|
32 |
+
# Inisialisasi DINO-X API Client
|
33 |
+
dinox_config = Config(DINOX_API_KEY)
|
34 |
+
dinox_client = Client(dinox_config)
|
35 |
+
|
36 |
+
# Fungsi untuk mendeteksi objek pada gambar dan video
|
37 |
+
def detect_combined(image_path_or_video_path, is_video=False):
|
38 |
+
# Jika input adalah video
|
39 |
+
if is_video:
|
40 |
+
return detect_objects_in_video(image_path_or_video_path)
|
41 |
+
|
42 |
+
# Jika input adalah gambar
|
43 |
+
return detect_objects_in_image(image_path_or_video_path)
|
44 |
+
|
45 |
+
def detect_objects_in_image(image_path):
|
46 |
+
try:
|
47 |
+
# Membaca gambar
|
48 |
+
img = cv2.imread(image_path)
|
49 |
+
|
50 |
+
# --- Deteksi menggunakan YOLO (Nestlé) ---
|
51 |
+
yolo_pred = yolo_model.predict(image_path, confidence=50, overlap=80).json()
|
52 |
+
|
53 |
+
# Hitung produk Nestlé per kelas
|
54 |
+
nestle_class_count = {}
|
55 |
+
nestle_boxes = []
|
56 |
+
for pred in yolo_pred['predictions']:
|
57 |
+
class_name = pred['class']
|
58 |
+
nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1
|
59 |
+
nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height']))
|
60 |
+
|
61 |
+
# --- Deteksi menggunakan DINO-X (Unclassified Products) ---
|
62 |
+
image_url = dinox_client.upload_file(image_path)
|
63 |
+
task = DinoxTask(
|
64 |
+
image_url=image_url,
|
65 |
+
prompts=[TextPrompt(text=DINOX_PROMPT)],
|
66 |
+
bbox_threshold=0.25,
|
67 |
+
targets=[DetectionTarget.BBox]
|
68 |
+
)
|
69 |
+
dinox_client.run_task(task)
|
70 |
+
dinox_pred = task.result.objects
|
71 |
+
|
72 |
+
# Hitung produk kompetitor yang tidak tumpang tindih dengan deteksi YOLO
|
73 |
+
competitor_class_count = {}
|
74 |
+
competitor_boxes = []
|
75 |
+
for obj in dinox_pred:
|
76 |
+
dinox_box = obj.bbox
|
77 |
+
# Filter objek yang sudah terdeteksi oleh YOLO (Overlap detection)
|
78 |
+
if not is_overlap(dinox_box, nestle_boxes): # Ignore if overlap with YOLO detections
|
79 |
+
class_name = obj.category.strip().lower()
|
80 |
+
competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1
|
81 |
+
competitor_boxes.append({
|
82 |
+
"class": class_name,
|
83 |
+
"box": dinox_box,
|
84 |
+
"confidence": obj.score
|
85 |
+
})
|
86 |
+
|
87 |
+
# --- Overlay Teks untuk Total Produk ---
|
88 |
+
nestle_count_text = ""
|
89 |
+
total_nestle = 0
|
90 |
+
for class_name, count in nestle_class_count.items():
|
91 |
+
nestle_count_text += f"{class_name}: {count}\n"
|
92 |
+
total_nestle += count
|
93 |
+
nestle_count_text += f"\nTotal Nestlé Products: {total_nestle}"
|
94 |
+
|
95 |
+
unclassified_count_text = ""
|
96 |
+
total_unclassified = 0
|
97 |
+
for class_name, count in competitor_class_count.items():
|
98 |
+
unclassified_count_text += f"{class_name}: {count}\n"
|
99 |
+
total_unclassified += count
|
100 |
+
unclassified_count_text += f"\nTotal Unclassified Products: {total_unclassified}"
|
101 |
+
|
102 |
+
# --- Visualisasi Deteksi YOLO (Nestlé) ---
|
103 |
+
for pred in yolo_pred['predictions']:
|
104 |
+
x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
|
105 |
+
cv2.rectangle(img, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
|
106 |
+
cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
|
107 |
+
|
108 |
+
# --- Visualisasi Deteksi DINO-X (Unclassified) ---
|
109 |
+
for comp in competitor_boxes:
|
110 |
+
x1, y1, x2, y2 = comp['box']
|
111 |
+
display_name = "unclassified"
|
112 |
+
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
|
113 |
+
cv2.putText(img, f"{display_name} {comp['confidence']:.2f}",
|
114 |
+
(int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
115 |
+
|
116 |
+
# Simpan gambar output
|
117 |
+
output_path = "/tmp/combined_output_image.jpg"
|
118 |
+
cv2.imwrite(output_path, img)
|
119 |
+
|
120 |
+
return output_path, nestle_count_text + "\n" + unclassified_count_text
|
121 |
+
|
122 |
+
except Exception as e:
|
123 |
+
return image_path, f"Error: {str(e)}"
|
124 |
+
|
125 |
def detect_objects_in_video(video_path):
|
126 |
temp_output_path = "/tmp/output_video.mp4"
|
127 |
temp_frames_dir = tempfile.mkdtemp()
|
128 |
frame_count = 0
|
129 |
previous_detections = {} # Untuk menyimpan deteksi objek dari frame sebelumnya
|
130 |
|
131 |
+
# Membuka video
|
132 |
+
video = cv2.VideoCapture(video_path)
|
133 |
+
frame_rate = int(video.get(cv2.CAP_PROP_FPS))
|
134 |
+
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
135 |
+
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
136 |
+
frame_size = (frame_width, frame_height)
|
137 |
|
138 |
+
# VideoWriter untuk menyimpan hasil video
|
139 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
140 |
+
output_video = cv2.VideoWriter(temp_output_path, fourcc, frame_rate, frame_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
+
while True:
|
143 |
+
ret, frame = video.read()
|
144 |
+
if not ret:
|
145 |
+
break
|
146 |
+
|
147 |
+
# Simpan frame sementara untuk prediksi
|
148 |
+
frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
|
149 |
+
cv2.imwrite(frame_path, frame)
|
150 |
+
|
151 |
+
# --- Deteksi menggunakan YOLO (Nestlé) ---
|
152 |
+
yolo_pred = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
|
153 |
+
|
154 |
+
# Hitung produk Nestlé per kelas
|
155 |
+
nestle_class_count = {}
|
156 |
+
nestle_boxes = []
|
157 |
+
for pred in yolo_pred['predictions']:
|
158 |
+
class_name = pred['class']
|
159 |
+
nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1
|
160 |
+
nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height']))
|
161 |
+
|
162 |
+
# --- Deteksi menggunakan DINO-X (Unclassified Products) ---
|
163 |
+
image_url = dinox_client.upload_file(frame_path)
|
164 |
+
task = DinoxTask(
|
165 |
+
image_url=image_url,
|
166 |
+
prompts=[TextPrompt(text=DINOX_PROMPT)],
|
167 |
+
bbox_threshold=0.25,
|
168 |
+
targets=[DetectionTarget.BBox]
|
169 |
+
)
|
170 |
+
dinox_client.run_task(task)
|
171 |
+
dinox_pred = task.result.objects
|
172 |
+
|
173 |
+
# Hitung produk kompetitor yang tidak tumpang tindih dengan deteksi YOLO
|
174 |
+
competitor_class_count = {}
|
175 |
+
competitor_boxes = []
|
176 |
+
for obj in dinox_pred:
|
177 |
+
dinox_box = obj.bbox
|
178 |
+
# Filter objek yang sudah terdeteksi oleh YOLO (Overlap detection)
|
179 |
+
if not is_overlap(dinox_box, nestle_boxes): # Ignore if overlap with YOLO detections
|
180 |
+
class_name = obj.category.strip().lower()
|
181 |
+
competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1
|
182 |
+
competitor_boxes.append({
|
183 |
+
"class": class_name,
|
184 |
+
"box": dinox_box,
|
185 |
+
"confidence": obj.score
|
186 |
+
})
|
187 |
+
|
188 |
+
# --- Overlay Teks untuk Total Produk ---
|
189 |
+
nestle_count_text = ""
|
190 |
+
total_nestle = 0
|
191 |
+
for class_name, count in nestle_class_count.items():
|
192 |
+
nestle_count_text += f"{class_name}: {count}\n"
|
193 |
+
total_nestle += count
|
194 |
+
nestle_count_text += f"\nTotal Nestlé Products: {total_nestle}"
|
195 |
+
|
196 |
+
unclassified_count_text = ""
|
197 |
+
total_unclassified = 0
|
198 |
+
for class_name, count in competitor_class_count.items():
|
199 |
+
unclassified_count_text += f"{class_name}: {count}\n"
|
200 |
+
total_unclassified += count
|
201 |
+
unclassified_count_text += f"\nTotal Unclassified Products: {total_unclassified}"
|
202 |
+
|
203 |
+
# Overlay teks ke frame
|
204 |
+
y_offset = 20
|
205 |
+
for line in nestle_count_text.split("\n"):
|
206 |
+
cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
207 |
+
y_offset += 30
|
208 |
+
|
209 |
+
y_offset += 30 # Slight gap between sections
|
210 |
+
for line in unclassified_count_text.split("\n"):
|
211 |
+
cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
|
212 |
+
y_offset += 30
|
213 |
+
|
214 |
+
# --- Visualisasi Deteksi YOLO (Nestlé) ---
|
215 |
+
for pred in yolo_pred['predictions']:
|
216 |
+
x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
|
217 |
+
cv2.rectangle(frame, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
|
218 |
+
cv2.putText(frame, pred['class'], (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
|
219 |
+
|
220 |
+
# --- Visualisasi Deteksi DINO-X (Unclassified) ---
|
221 |
+
for comp in competitor_boxes:
|
222 |
+
x1, y1, x2, y2 = comp['box']
|
223 |
+
display_name = "unclassified"
|
224 |
+
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
|
225 |
+
cv2.putText(frame, f"{display_name} {comp['confidence']:.2f}",
|
226 |
+
(int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
|
227 |
+
|
228 |
+
# Tulis frame ke video output
|
229 |
+
output_video.write(frame)
|
230 |
+
frame_count += 1
|
231 |
+
|
232 |
+
video.release()
|
233 |
+
output_video.release()
|
234 |
+
|
235 |
+
return temp_output_path
|
236 |
+
|
237 |
+
def is_overlap(box1, boxes2, threshold=0.3):
|
238 |
+
# Fungsi untuk deteksi overlap bounding box
|
239 |
+
x1_min, y1_min, x1_max, y1_max = box1
|
240 |
+
for b2 in boxes2:
|
241 |
+
x2, y2, w2, h2 = b2
|
242 |
+
x2_min = x2 - w2/2
|
243 |
+
x2_max = x2 + w2/2
|
244 |
+
y2_min = y2 - h2/2
|
245 |
+
y2_max = y2 + h2/2
|
246 |
+
|
247 |
+
# Hitung area overlap
|
248 |
+
dx = min(x1_max, x2_max) - max(x1_min, x2_min)
|
249 |
+
dy = min(y1_max, y2_max) - max(y1_min, y2_min)
|
250 |
+
if (dx >= 0) and (dy >= 0):
|
251 |
+
area_overlap = dx * dy
|
252 |
+
area_box1 = (x1_max - x1_min) * (y1_max - y1_min)
|
253 |
+
if area_overlap / area_box1 > threshold:
|
254 |
+
return True
|
255 |
+
return False
|
256 |
+
|
257 |
+
# ========== Gradio Interface ==========
|
258 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")) as iface:
|
259 |
+
gr.Markdown("""<div style="text-align: center;"><h1>NESTLE - STOCK COUNTING</h1></div>""")
|
260 |
+
|
261 |
+
with gr.Row():
|
262 |
+
with gr.Column():
|
263 |
+
input_image = gr.Image(type="pil", label="Input Image")
|
264 |
+
detect_image_button = gr.Button("Detect Image")
|
265 |
+
output_image = gr.Image(label="Detect Object")
|
266 |
+
output_text = gr.Textbox(label="Counting Object")
|
267 |
+
detect_image_button.click(fn=detect_combined, inputs=input_image, outputs=[output_image, output_text])
|
268 |
+
|
269 |
+
with gr.Column():
|
270 |
+
input_video = gr.Video(label="Input Video")
|
271 |
+
detect_video_button = gr.Button("Detect Video")
|
272 |
+
output_video = gr.Video(label="Output Video")
|
273 |
+
detect_video_button.click(fn=detect_objects_in_video, inputs=input_video, outputs=[output_video])
|
274 |
|
275 |
+
iface.launch()
|