muhammadsalmanalfaridzi commited on
Commit
652ca17
·
verified ·
1 Parent(s): 402e547

Delete app-dinoo.py

Browse files
Files changed (1) hide show
  1. app-dinoo.py +0 -239
app-dinoo.py DELETED
@@ -1,239 +0,0 @@
1
- import gradio as gr
2
- from dotenv import load_dotenv
3
- from roboflow import Roboflow
4
- import tempfile
5
- import os
6
- import requests
7
- import cv2
8
- import numpy as np
9
- from dds_cloudapi_sdk import Config, Client
10
- from dds_cloudapi_sdk.tasks.dinox import DinoxTask
11
- from dds_cloudapi_sdk.tasks.types import DetectionTarget
12
- from dds_cloudapi_sdk import TextPrompt
13
- import supervision as sv
14
-
15
- # ========== Konfigurasi ==========
16
- load_dotenv()
17
-
18
- # Roboflow Config
19
- rf_api_key = os.getenv("ROBOFLOW_API_KEY")
20
- workspace = os.getenv("ROBOFLOW_WORKSPACE")
21
- project_name = os.getenv("ROBOFLOW_PROJECT")
22
- model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
23
-
24
- # DINO-X Config
25
- DINOX_API_KEY = os.getenv("DINO_X_API_KEY")
26
- DINOX_PROMPT = "beverage . bottle" # Customize sesuai produk kompetitor : food . drink
27
-
28
- # Inisialisasi Model
29
- rf = Roboflow(api_key=rf_api_key)
30
- project = rf.workspace(workspace).project(project_name)
31
- yolo_model = project.version(model_version).model
32
-
33
- dinox_config = Config(DINOX_API_KEY)
34
- dinox_client = Client(dinox_config)
35
-
36
- # ========== Fungsi Deteksi Kombinasi ==========
37
- def detect_combined(image):
38
- with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
39
- image.save(temp_file, format="JPEG")
40
- temp_path = temp_file.name
41
-
42
- try:
43
- # ========== [1] YOLO: Deteksi Produk Nestlé (Per Class) ==========
44
- yolo_pred = yolo_model.predict(temp_path, confidence=60, overlap=80).json()
45
-
46
- # Hitung per class Nestlé
47
- nestle_class_count = {}
48
- nestle_boxes = []
49
- for pred in yolo_pred['predictions']:
50
- class_name = pred['class']
51
- nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1
52
- nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height']))
53
-
54
- total_nestle = sum(nestle_class_count.values())
55
-
56
- # ========== [2] DINO-X: Deteksi Kompetitor ==========
57
- image_url = dinox_client.upload_file(temp_path)
58
- task = DinoxTask(
59
- image_url=image_url,
60
- prompts=[TextPrompt(text=DINOX_PROMPT)],
61
- bbox_threshold=0.25,
62
- targets=[DetectionTarget.BBox]
63
- )
64
- dinox_client.run_task(task)
65
- dinox_pred = task.result.objects
66
-
67
- # Filter & Hitung Kompetitor
68
- competitor_class_count = {}
69
- competitor_boxes = []
70
- for obj in dinox_pred:
71
- dinox_box = obj.bbox
72
- if not is_overlap(dinox_box, nestle_boxes):
73
- class_name = obj.category.strip().lower() # Normalisasi nama kelas
74
- competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1
75
- competitor_boxes.append({
76
- "class": class_name,
77
- "box": dinox_box,
78
- "confidence": obj.score
79
- })
80
-
81
- total_competitor = sum(competitor_class_count.values())
82
-
83
- # ========== [3] Format Output ==========
84
- result_text = "Product Nestle\n\n"
85
- for class_name, count in nestle_class_count.items():
86
- result_text += f"{class_name}: {count}\n"
87
- result_text += f"\nTotal Product Nestle: {total_nestle}\n\n"
88
-
89
- result_text += "Competitor Products\n\n"
90
- if competitor_class_count:
91
- for class_name, count in competitor_class_count.items():
92
- result_text += f"{class_name}: {count}\n"
93
- else:
94
- result_text += "No competitors detected\n"
95
- result_text += f"\nTotal Competitor: {total_competitor}"
96
-
97
- # ========== [4] Visualisasi ==========
98
- img = cv2.imread(temp_path)
99
-
100
- # Nestlé (Hijau)
101
- for pred in yolo_pred['predictions']:
102
- x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
103
- cv2.rectangle(img, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
104
- cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)),
105
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
106
-
107
- # Kompetitor (Merah)
108
- for comp in competitor_boxes:
109
- x1, y1, x2, y2 = comp['box']
110
- cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2)
111
- cv2.putText(img, f"{comp['class']} {comp['confidence']:.2f}",
112
- (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
113
-
114
- output_path = "/tmp/combined_output.jpg"
115
- cv2.imwrite(output_path, img)
116
-
117
- return output_path, result_text
118
-
119
- except Exception as e:
120
- return temp_path, f"Error: {str(e)}"
121
- finally:
122
- os.remove(temp_path)
123
-
124
- def is_overlap(box1, boxes2, threshold=0.3):
125
- # Fungsi untuk deteksi overlap bounding box
126
- x1_min, y1_min, x1_max, y1_max = box1
127
- for b2 in boxes2:
128
- x2, y2, w2, h2 = b2
129
- x2_min = x2 - w2/2
130
- x2_max = x2 + w2/2
131
- y2_min = y2 - h2/2
132
- y2_max = y2 + h2/2
133
-
134
- # Hitung area overlap
135
- dx = min(x1_max, x2_max) - max(x1_min, x2_min)
136
- dy = min(y1_max, y2_max) - max(y1_min, y2_min)
137
- if (dx >= 0) and (dy >= 0):
138
- area_overlap = dx * dy
139
- area_box1 = (x1_max - x1_min) * (y1_max - y1_min)
140
- if area_overlap / area_box1 > threshold:
141
- return True
142
- return False
143
-
144
- # ========== Fungsi untuk Deteksi Video ==========
145
- def detect_objects_in_video(video_path):
146
- temp_output_path = "/tmp/output_video.mp4"
147
- temp_frames_dir = tempfile.mkdtemp()
148
-
149
- try:
150
- # Baca video dan ekstrak frame
151
- video = cv2.VideoCapture(video_path)
152
- frame_rate = int(video.get(cv2.CAP_PROP_FPS))
153
- frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
154
- frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
155
- frame_size = (frame_width, frame_height)
156
- frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
157
-
158
- # VideoWriter untuk membuat video keluaran
159
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
160
- output_video = cv2.VideoWriter(temp_output_path, fourcc, frame_rate, frame_size)
161
-
162
- frame_index = 0
163
- while True:
164
- ret, frame = video.read()
165
- if not ret:
166
- break
167
-
168
- # Simpan frame sementara untuk prediksi
169
- frame_path = os.path.join(temp_frames_dir, f"frame_{frame_index}.jpg")
170
- cv2.imwrite(frame_path, frame)
171
-
172
- # Deteksi objek pada frame
173
- predictions = yolo_model.predict(frame_path, confidence=60, overlap=80).json()
174
-
175
- # Hitung objek per kelas
176
- class_count = {}
177
- for prediction in predictions['predictions']:
178
- class_name = prediction['class']
179
- class_count[class_name] = class_count.get(class_name, 0) + 1
180
-
181
- # Menyusun teks untuk jumlah objek yang terdeteksi (vertikal)
182
- text_offset = 30 # Jarak antara setiap baris teks
183
- y_position = 30 # Posisi Y awal untuk menampilkan teks
184
- for class_name, count in class_count.items():
185
- # Menulis teks untuk setiap kelas
186
- cv2.putText(frame, f"{class_name}: {count}", (10, y_position),
187
- cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
188
- y_position += text_offset # Geser posisi teks untuk baris berikutnya
189
-
190
- # Gambar bounding box pada objek
191
- for prediction in predictions['predictions']:
192
- x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
193
- class_name = prediction['class']
194
- color = (0, 255, 0) # Hijau
195
- cv2.rectangle(frame, (int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)), color, 2)
196
- cv2.putText(frame, class_name, (int(x - w/2), int(y - h/2 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
197
-
198
- # Tambahkan frame ke video keluaran
199
- output_video.write(frame)
200
- frame_index += 1
201
-
202
- video.release()
203
- output_video.release()
204
-
205
- return temp_output_path # Return only the output video path, no message.
206
-
207
- except Exception as e:
208
- return None, f"An error occurred: {e}"
209
-
210
- # ========== Gradio Interface ==========
211
- with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")) as iface:
212
- gr.Markdown("""
213
- <div style="text-align: center;">
214
- <h1>NESTLE - STOCK COUNTING</h1>
215
- </div>
216
- """)
217
- with gr.Row():
218
- with gr.Column():
219
- input_image = gr.Image(type="pil", label="Input Image")
220
- detect_image_button = gr.Button("Detect Image")
221
- output_image = gr.Image(label="Detect Object")
222
- output_text = gr.Textbox(label="Counting Object")
223
- detect_image_button.click(
224
- fn=detect_combined,
225
- inputs=input_image,
226
- outputs=[output_image, output_text]
227
- )
228
-
229
- with gr.Column():
230
- input_video = gr.Video(label="Input Video")
231
- detect_video_button = gr.Button("Detect Video")
232
- output_video = gr.Video(label="Output Video")
233
- detect_video_button.click(
234
- fn=detect_objects_in_video,
235
- inputs=input_video,
236
- outputs=[output_video]
237
- )
238
-
239
- iface.launch()