|
import gradio as gr |
|
from dotenv import load_dotenv |
|
from roboflow import Roboflow |
|
import tempfile |
|
import os |
|
import requests |
|
import cv2 |
|
import numpy as np |
|
from dds_cloudapi_sdk import Config, Client |
|
from dds_cloudapi_sdk.tasks.dinox import DinoxTask |
|
from dds_cloudapi_sdk.tasks.types import DetectionTarget |
|
from dds_cloudapi_sdk import TextPrompt |
|
import supervision as sv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
rf_api_key = os.getenv("ROBOFLOW_API_KEY") |
|
workspace = os.getenv("ROBOFLOW_WORKSPACE") |
|
project_name = os.getenv("ROBOFLOW_PROJECT") |
|
model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION")) |
|
|
|
|
|
DINOX_API_KEY = os.getenv("DINO_X_API_KEY") |
|
DINOX_PROMPT = "beverage . food . drink . bottle" |
|
|
|
|
|
rf = Roboflow(api_key=rf_api_key) |
|
project = rf.workspace(workspace).project(project_name) |
|
yolo_model = project.version(model_version).model |
|
|
|
dinox_config = Config(DINOX_API_KEY) |
|
dinox_client = Client(dinox_config) |
|
|
|
|
|
def detect_combined(image): |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: |
|
image.save(temp_file, format="JPEG") |
|
temp_path = temp_file.name |
|
|
|
try: |
|
|
|
yolo_pred = yolo_model.predict(temp_path, confidence=60, overlap=80).json() |
|
|
|
|
|
nestle_class_count = {} |
|
nestle_boxes = [] |
|
for pred in yolo_pred['predictions']: |
|
class_name = pred['class'] |
|
nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1 |
|
nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height'])) |
|
|
|
total_nestle = sum(nestle_class_count.values()) |
|
|
|
|
|
image_url = dinox_client.upload_file(temp_path) |
|
task = DinoxTask( |
|
image_url=image_url, |
|
prompts=[TextPrompt(text=DINOX_PROMPT)], |
|
bbox_threshold=0.25, |
|
targets=[DetectionTarget.BBox] |
|
) |
|
dinox_client.run_task(task) |
|
dinox_pred = task.result.objects |
|
|
|
|
|
competitor_class_count = {} |
|
competitor_boxes = [] |
|
for obj in dinox_pred: |
|
dinox_box = obj.bbox |
|
if not is_overlap(dinox_box, nestle_boxes): |
|
class_name = obj.category.strip().lower() |
|
competitor_class_count[class_name] = competitor_class_count.get(class_name, 0) + 1 |
|
competitor_boxes.append({ |
|
"class": class_name, |
|
"box": dinox_box, |
|
"confidence": obj.score |
|
}) |
|
|
|
total_competitor = sum(competitor_class_count.values()) |
|
|
|
|
|
result_text = "Product Nestle\n\n" |
|
for class_name, count in nestle_class_count.items(): |
|
result_text += f"{class_name}: {count}\n" |
|
result_text += f"\nTotal Product Nestle: {total_nestle}\n\n" |
|
|
|
result_text += "Competitor Products\n\n" |
|
if competitor_class_count: |
|
for class_name, count in competitor_class_count.items(): |
|
result_text += f"{class_name}: {count}\n" |
|
else: |
|
result_text += "No competitors detected\n" |
|
result_text += f"\nTotal Competitor: {total_competitor}" |
|
|
|
|
|
img = cv2.imread(temp_path) |
|
|
|
|
|
for pred in yolo_pred['predictions']: |
|
x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height'] |
|
cv2.rectangle(img, (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), (0,255,0), 2) |
|
cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)), |
|
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2) |
|
|
|
|
|
for comp in competitor_boxes: |
|
x1, y1, x2, y2 = comp['box'] |
|
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2) |
|
cv2.putText(img, f"{comp['class']} {comp['confidence']:.2f}", |
|
(int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2) |
|
|
|
output_path = "/tmp/combined_output.jpg" |
|
cv2.imwrite(output_path, img) |
|
|
|
return output_path, result_text |
|
|
|
except Exception as e: |
|
return temp_path, f"Error: {str(e)}" |
|
finally: |
|
os.remove(temp_path) |
|
|
|
def is_overlap(box1, boxes2, threshold=0.3): |
|
|
|
x1_min, y1_min, x1_max, y1_max = box1 |
|
for b2 in boxes2: |
|
x2, y2, w2, h2 = b2 |
|
x2_min = x2 - w2/2 |
|
x2_max = x2 + w2/2 |
|
y2_min = y2 - h2/2 |
|
y2_max = y2 + h2/2 |
|
|
|
|
|
dx = min(x1_max, x2_max) - max(x1_min, x2_min) |
|
dy = min(y1_max, y2_max) - max(y1_min, y2_min) |
|
if (dx >= 0) and (dy >= 0): |
|
area_overlap = dx * dy |
|
area_box1 = (x1_max - x1_min) * (y1_max - y1_min) |
|
if area_overlap / area_box1 > threshold: |
|
return True |
|
return False |
|
|
|
|
|
with gr.Blocks() as iface: |
|
with gr.Row(): |
|
input_image = gr.Image(type="pil", label="Input Image") |
|
output_image = gr.Image(label="Detection Result") |
|
output_text = gr.Textbox(label="Product Counts") |
|
|
|
detect_button = gr.Button("Detect Products") |
|
detect_button.click( |
|
fn=detect_combined, |
|
inputs=input_image, |
|
outputs=[output_image, output_text] |
|
) |
|
|
|
iface.launch() |