Testing_Model / app.py
jfriesen's picture
Create app.py
20b8ae6
import gradio as gr
import cv2
import numpy as np
# Function to perform object detection on the input image
def detect_objects(input_image):
# Preprocess the image (if required) and perform object detection
# Replace this with your own object detection code
processed_image = preprocess_image(input_image)
bounding_boxes = perform_object_detection(processed_image)
detections = extract_detections(bounding_boxes)
return detections
# Helper function to preprocess the input image
def preprocess_image(image):
# Replace this with your own image preprocessing code
processed_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return processed_image
# Helper function to perform object detection on the preprocessed image
def perform_object_detection(image):
# Replace this with your own object detection code
# Make sure the bounding boxes are in (xmin, ymin, xmax, ymax) format
bounding_boxes = [((100, 100, 300, 300), 'object1'), ((400, 200, 600, 400), 'object2')]
return bounding_boxes
# Helper function to extract the detected objects from the bounding boxes
def extract_detections(bounding_boxes):
detections = [{'box': box, 'label': label} for box, label in bounding_boxes]
return detections
# Create a function for deleting a detection based on its index
def delete_detection(index):
del detections[index]
# Create a function for saving the detections
def save_detections():
# Implement the saving logic here
print("Detections saved.")
# Define the Gradio input and output interfaces
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'])
# Create the delete button interface
delete_button = gr.outputs.Button(label="Delete")
# Create the interfaces for saving the detections and deleting the bounding boxes
save_button = gr.outputs.Button(label="Save Detections")
delete_button = gr.outputs.Button(label="Delete")
# Create the function for deleting a detection based on button click
def delete_box(inputs):
delete_detection(inputs["delete_button"])
return detections
# Create the function for saving the detections based on button click
def save_boxes():
save_detections()
# Combine the inputs, outputs, and functions into a Gradio interface
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"
)
# Run the interface
interface.launch()