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

Delete app-error.py

Browse files
Files changed (1) hide show
  1. app-error.py +0 -275
app-error.py DELETED
@@ -1,275 +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 subprocess
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 . cans . boxed milk . milk" # 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=50, 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
- # Filter objek yang sudah terdeteksi oleh YOLO (Overlap detection)
73
- if not is_overlap(dinox_box, nestle_boxes): # Ignore if overlap with YOLO detections
74
- class_name = obj.category.strip().lower() # Normalisasi nama kelas
75
- competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1
76
- competitor_boxes.append({
77
- "class": class_name,
78
- "box": dinox_box,
79
- "confidence": obj.score
80
- })
81
-
82
- total_competitor = sum(competitor_class_count.values())
83
-
84
- # ========== [3] Format Output ==========
85
- result_text = "Product Nestle\n\n"
86
- for class_name, count in nestle_class_count.items():
87
- result_text += f"{class_name}: {count}\n"
88
- result_text += f"\nTotal Products Nestle: {total_nestle}\n\n"
89
-
90
- #result_text += "Competitor Products\n\n"
91
- if competitor_class_count:
92
- result_text += f"Total Unclassified Products: {total_competitor}\n" # Hanya total, tidak per kelas
93
- else:
94
- result_text += "No Unclassified Products detected\n"
95
-
96
- # ========== [4] Visualisasi ==========
97
- img = cv2.imread(temp_path)
98
-
99
- # Nestlé (Hijau)
100
- for pred in yolo_pred['predictions']:
101
- x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
102
- cv2.rectangle(img, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
103
- cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)),
104
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
105
-
106
- # Kompetitor (Merah) dengan nama 'unclassified'
107
- for comp in competitor_boxes:
108
- x1, y1, x2, y2 = comp['box']
109
-
110
- # Define a list of target classes to rename
111
- unclassified_classes = ["beverage", "cans", "bottle", "boxed milk", "milk"]
112
-
113
- # Normalize the class name to be case-insensitive and check if it's in the unclassified list
114
- display_name = "unclassified" if any(class_name in comp['class'].lower() for class_name in unclassified_classes) else comp['class']
115
-
116
- cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
117
- cv2.putText(img, f"{display_name} {comp['confidence']:.2f}",
118
- (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
119
-
120
- output_path = "/tmp/combined_output.jpg"
121
- cv2.imwrite(output_path, img)
122
-
123
- return output_path, result_text
124
-
125
- except Exception as e:
126
- return temp_path, f"Error: {str(e)}"
127
- finally:
128
- os.remove(temp_path)
129
-
130
- def is_overlap(box1, boxes2, threshold=0.3):
131
- # Fungsi untuk deteksi overlap bounding box
132
- x1_min, y1_min, x1_max, y1_max = box1
133
- for b2 in boxes2:
134
- x2, y2, w2, h2 = b2
135
- x2_min = x2 - w2/2
136
- x2_max = x2 + w2/2
137
- y2_min = y2 - h2/2
138
- y2_max = y2 + h2/2
139
-
140
- # Hitung area overlap
141
- dx = min(x1_max, x2_max) - max(x1_min, x2_min)
142
- dy = min(y1_max, y2_max) - max(y1_min, y2_min)
143
- if (dx >= 0) and (dy >= 0):
144
- area_overlap = dx * dy
145
- area_box1 = (x1_max - x1_min) * (y1_max - y1_min)
146
- if area_overlap / area_box1 > threshold:
147
- return True
148
- return False
149
-
150
- # ========== Fungsi untuk Deteksi Video ==========
151
-
152
- def convert_video_to_mp4(input_path, output_path):
153
- try:
154
- subprocess.run(['ffmpeg', '-i', input_path, '-vcodec', 'libx264', '-acodec', 'aac', output_path], check=True)
155
- return output_path
156
- except subprocess.CalledProcessError as e:
157
- return None, f"Error converting video: {e}"
158
-
159
- def detect_objects_in_video(video_path):
160
- temp_output_path = "/tmp/output_video.mp4"
161
- temp_frames_dir = tempfile.mkdtemp()
162
- frame_count = 0
163
- previous_detections = {} # Untuk menyimpan deteksi objek dari frame sebelumnya
164
-
165
- try:
166
- # Convert video to MP4 if necessary
167
- if not video_path.endswith(".mp4"):
168
- video_path, err = convert_video_to_mp4(video_path, temp_output_path)
169
- if not video_path:
170
- return None, f"Video conversion error: {err}"
171
-
172
- # Read video and process frames
173
- video = cv2.VideoCapture(video_path)
174
- frame_rate = int(video.get(cv2.CAP_PROP_FPS))
175
- frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
176
- frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
177
- frame_size = (frame_width, frame_height)
178
-
179
- # VideoWriter for output video
180
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
181
- output_video = cv2.VideoWriter(temp_output_path, fourcc, frame_rate, frame_size)
182
-
183
- while True:
184
- ret, frame = video.read()
185
- if not ret:
186
- break
187
-
188
- # Save frame temporarily for predictions
189
- frame_path = os.path.join(temp_frames_dir, f"frame_{frame_count}.jpg")
190
- cv2.imwrite(frame_path, frame)
191
-
192
- # Process predictions for the current frame
193
- predictions = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
194
-
195
- # Track current frame detections
196
- current_detections = {}
197
- for prediction in predictions['predictions']:
198
- class_name = prediction['class']
199
- x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
200
- # Generate a unique ID for each detection (can use coordinates or other method)
201
- object_id = f"{class_name}_{x}_{y}"
202
-
203
- if object_id not in current_detections:
204
- current_detections[object_id] = class_name
205
-
206
- # Draw bounding box for detected objects
207
- cv2.rectangle(frame, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
208
- cv2.putText(frame, class_name, (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
209
-
210
- # Calculate the changes from previous detections
211
- removed_objects = set(previous_detections.keys()) - set(current_detections.keys())
212
- new_objects = set(current_detections.keys()) - set(previous_detections.keys())
213
-
214
- # Update counts for objects
215
- object_counts = {}
216
- for detection_id in current_detections.keys():
217
- class_name = current_detections[detection_id]
218
- object_counts[class_name] = object_counts.get(class_name, 0) + 1
219
-
220
- # Update object counts based on removed objects
221
- for detection_id in removed_objects:
222
- class_name = previous_detections[detection_id]
223
- if class_name in object_counts:
224
- object_counts[class_name] -= 1
225
- if object_counts[class_name] <= 0:
226
- del object_counts[class_name]
227
-
228
- # Generate display text for counts
229
- count_text = ""
230
- total_product_count = 0
231
- for class_name, count in object_counts.items():
232
- count_text += f"{class_name}: {count}\n"
233
- total_product_count += count
234
- count_text += f"\nTotal Product: {total_product_count}"
235
-
236
- # Overlay the counts text onto the frame
237
- y_offset = 20
238
- for line in count_text.split("\n"):
239
- cv2.putText(frame, line, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
240
- y_offset += 30 # Move down for next line
241
-
242
- # Write processed frame to output video
243
- output_video.write(frame)
244
- frame_count += 1
245
-
246
- # Update previous_detections for the next frame
247
- previous_detections = current_detections
248
-
249
- video.release()
250
- output_video.release()
251
-
252
- return temp_output_path
253
-
254
- except Exception as e:
255
- return None, f"An error occurred: {e}"
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()