Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Function to perform object detection on the input image
|
6 |
+
def detect_objects(input_image):
|
7 |
+
# Preprocess the image (if required) and perform object detection
|
8 |
+
# Replace this with your own object detection code
|
9 |
+
processed_image = preprocess_image(input_image)
|
10 |
+
bounding_boxes = perform_object_detection(processed_image)
|
11 |
+
detections = extract_detections(bounding_boxes)
|
12 |
+
return detections
|
13 |
+
|
14 |
+
# Helper function to preprocess the input image
|
15 |
+
def preprocess_image(image):
|
16 |
+
# Replace this with your own image preprocessing code
|
17 |
+
processed_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
18 |
+
return processed_image
|
19 |
+
|
20 |
+
# Helper function to perform object detection on the preprocessed image
|
21 |
+
def perform_object_detection(image):
|
22 |
+
# Replace this with your own object detection code
|
23 |
+
# Make sure the bounding boxes are in (xmin, ymin, xmax, ymax) format
|
24 |
+
bounding_boxes = [((100, 100, 300, 300), 'object1'), ((400, 200, 600, 400), 'object2')]
|
25 |
+
return bounding_boxes
|
26 |
+
|
27 |
+
# Helper function to extract the detected objects from the bounding boxes
|
28 |
+
def extract_detections(bounding_boxes):
|
29 |
+
detections = [{'box': box, 'label': label} for box, label in bounding_boxes]
|
30 |
+
return detections
|
31 |
+
|
32 |
+
# Create a function for deleting a detection based on its index
|
33 |
+
def delete_detection(index):
|
34 |
+
del detections[index]
|
35 |
+
|
36 |
+
# Create a function for saving the detections
|
37 |
+
def save_detections():
|
38 |
+
# Implement the saving logic here
|
39 |
+
print("Detections saved.")
|
40 |
+
|
41 |
+
# Define the Gradio input and output interfaces
|
42 |
+
image_input = gr.inputs.Image()
|
43 |
+
bounding_box_list_output = gr.outputs.Label(num_top_classes=0, label_type='list', type='box', box_data=lambda img: img['box'])
|
44 |
+
|
45 |
+
# Create the delete button interface
|
46 |
+
delete_button = gr.outputs.Button(label="Delete")
|
47 |
+
|
48 |
+
# Create the interfaces for saving the detections and deleting the bounding boxes
|
49 |
+
save_button = gr.outputs.Button(label="Save Detections")
|
50 |
+
delete_button = gr.outputs.Button(label="Delete")
|
51 |
+
|
52 |
+
# Create the function for deleting a detection based on button click
|
53 |
+
def delete_box(inputs):
|
54 |
+
delete_detection(inputs["delete_button"])
|
55 |
+
return detections
|
56 |
+
|
57 |
+
# Create the function for saving the detections based on button click
|
58 |
+
def save_boxes():
|
59 |
+
save_detections()
|
60 |
+
|
61 |
+
# Combine the inputs, outputs, and functions into a Gradio interface
|
62 |
+
interface = gr.Interface(
|
63 |
+
fn=detect_objects,
|
64 |
+
inputs=image_input,
|
65 |
+
outputs=[bounding_box_list_output, delete_button, save_button],
|
66 |
+
title="Object Detection Interface",
|
67 |
+
description="Upload an image and detect objects",
|
68 |
+
allow_flagging=False,
|
69 |
+
theme="default"
|
70 |
+
)
|
71 |
+
|
72 |
+
# Run the interface
|
73 |
+
interface.launch()
|