|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
|
|
|
|
def detect_objects(input_image): |
|
|
|
|
|
processed_image = preprocess_image(input_image) |
|
bounding_boxes = perform_object_detection(processed_image) |
|
detections = extract_detections(bounding_boxes) |
|
return detections |
|
|
|
|
|
def preprocess_image(image): |
|
|
|
processed_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
return processed_image |
|
|
|
|
|
def perform_object_detection(image): |
|
|
|
|
|
bounding_boxes = [((100, 100, 300, 300), 'object1'), ((400, 200, 600, 400), 'object2')] |
|
return bounding_boxes |
|
|
|
|
|
def extract_detections(bounding_boxes): |
|
detections = [{'box': box, 'label': label} for box, label in bounding_boxes] |
|
return detections |
|
|
|
|
|
def delete_detection(index): |
|
del detections[index] |
|
|
|
|
|
def save_detections(): |
|
|
|
print("Detections saved.") |
|
|
|
|
|
image_input = gr.inputs.Image() |
|
bounding_box_list_output = gr.outputs.Label(num_top_classes=0, label_type='list', type='box', box_data=lambda img: img['box']) |
|
|
|
|
|
delete_button = gr.outputs.Button(label="Delete") |
|
|
|
|
|
save_button = gr.outputs.Button(label="Save Detections") |
|
delete_button = gr.outputs.Button(label="Delete") |
|
|
|
|
|
def delete_box(inputs): |
|
delete_detection(inputs["delete_button"]) |
|
return detections |
|
|
|
|
|
def save_boxes(): |
|
save_detections() |
|
|
|
|
|
interface = gr.Interface( |
|
fn=detect_objects, |
|
inputs=image_input, |
|
outputs=[bounding_box_list_output, delete_button, save_button], |
|
title="Object Detection Interface", |
|
description="Upload an image and detect objects", |
|
allow_flagging=False, |
|
theme="default" |
|
) |
|
|
|
|
|
interface.launch() |
|
|