Update app.py
Browse files
app.py
CHANGED
@@ -38,92 +38,62 @@ 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 |
-
#
|
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 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
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 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
"class": class_name,
|
78 |
-
"box": dinox_box,
|
79 |
-
"confidence": obj.score
|
80 |
-
})
|
81 |
-
|
82 |
total_competitor = sum(competitor_class_count.values())
|
83 |
-
|
84 |
-
#
|
85 |
-
result_text = "Product
|
86 |
for class_name, count in nestle_class_count.items():
|
87 |
result_text += f"{class_name}: {count}\n"
|
88 |
-
result_text += f"\nTotal Products
|
89 |
-
|
90 |
-
|
91 |
-
|
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 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
# Define a list of target classes to rename
|
111 |
-
unclassified_classes = ["beverage", "cans", "bottle", "mixed box"]
|
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, 1.0, (0, 0, 255), 3)
|
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 |
|
|
|
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 |
+
# YOLO Detection (Nestlé products)
|
44 |
yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
|
|
|
|
|
45 |
nestle_class_count = {}
|
46 |
nestle_boxes = []
|
47 |
for pred in yolo_pred['predictions']:
|
48 |
class_name = pred['class']
|
49 |
nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1
|
50 |
nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height']))
|
|
|
51 |
total_nestle = sum(nestle_class_count.values())
|
52 |
+
|
53 |
+
# CountGD Detection (Competitor products)
|
54 |
+
url = "https://api.landing.ai/v1/tools/text-to-object-detection"
|
55 |
+
files = {"image": open(temp_path, "rb")}
|
56 |
+
data = {"prompts": ["mixed box"], "model": "countgd"}
|
57 |
+
headers = {"Authorization": "Basic COUNTGD_API_KEY"} # Replace with actual API key
|
58 |
+
response = requests.post(url, files=files, data=data, headers=headers)
|
59 |
+
result = response.json()
|
60 |
+
|
|
|
|
|
|
|
|
|
61 |
competitor_class_count = {}
|
62 |
competitor_boxes = []
|
63 |
+
if 'data' in result:
|
64 |
+
for obj in result['data'][0]:
|
65 |
+
if 'bounding_box' in obj:
|
66 |
+
x, y, x2, y2 = obj['bounding_box']
|
67 |
+
class_name = "unclassified" # CountGD does not classify, so use generic label
|
68 |
+
competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1
|
69 |
+
competitor_boxes.append((x, y, x2, y2))
|
|
|
|
|
|
|
|
|
|
|
70 |
total_competitor = sum(competitor_class_count.values())
|
71 |
+
|
72 |
+
# Format Output
|
73 |
+
result_text = "Product Nestlé\n\n"
|
74 |
for class_name, count in nestle_class_count.items():
|
75 |
result_text += f"{class_name}: {count}\n"
|
76 |
+
result_text += f"\nTotal Products Nestlé: {total_nestle}\n\n"
|
77 |
+
result_text += f"Total Unclassified Products: {total_competitor}\n" if total_competitor else "No Unclassified Products detected\n"
|
78 |
+
|
79 |
+
# Visualization
|
|
|
|
|
|
|
|
|
|
|
80 |
img = cv2.imread(temp_path)
|
|
|
|
|
81 |
for pred in yolo_pred['predictions']:
|
82 |
x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
|
83 |
cv2.rectangle(img, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2)
|
84 |
+
cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0), 3)
|
85 |
+
|
86 |
+
for x1, y1, x2, y2 in competitor_boxes:
|
87 |
+
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2)
|
88 |
+
cv2.putText(img, "unclassified", (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255), 3)
|
89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
output_path = "/tmp/combined_output.jpg"
|
91 |
cv2.imwrite(output_path, img)
|
|
|
92 |
return output_path, result_text
|
93 |
+
|
94 |
except Exception as e:
|
95 |
return temp_path, f"Error: {str(e)}"
|
96 |
+
|
97 |
finally:
|
98 |
os.remove(temp_path)
|
99 |
|