Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,20 +20,7 @@ def save_image(image, filename):
|
|
| 20 |
def predict(image):
|
| 21 |
result_image = detect_objects(image)
|
| 22 |
output_path = save_image(result_image, "output.png")
|
| 23 |
-
return
|
| 24 |
-
|
| 25 |
-
# Gradio interface for directory prediction
|
| 26 |
-
def dir_predict(dir_origin_path):
|
| 27 |
-
img_names = os.listdir(dir_origin_path)
|
| 28 |
-
output_images = []
|
| 29 |
-
for img_name in img_names:
|
| 30 |
-
if img_name.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
|
| 31 |
-
image_path = os.path.join(dir_origin_path, img_name)
|
| 32 |
-
image = Image.open(image_path)
|
| 33 |
-
r_image = detect_objects(image)
|
| 34 |
-
output_path = save_image(r_image, img_name.replace(".jpg", ".png"))
|
| 35 |
-
output_images.append(output_path)
|
| 36 |
-
return output_images
|
| 37 |
|
| 38 |
# Function to list images in the 'img' folder
|
| 39 |
def get_image_list():
|
|
@@ -45,9 +32,8 @@ def get_image_list():
|
|
| 45 |
return img_paths
|
| 46 |
|
| 47 |
# Gradio interface components
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
image_select = gr.inputs.Dropdown(get_image_list, label="Select Image from 'img' Folder")
|
| 51 |
|
| 52 |
# Gradio app for single image prediction
|
| 53 |
iface = gr.Interface(
|
|
@@ -58,20 +44,29 @@ iface = gr.Interface(
|
|
| 58 |
description="Select an image from the 'img' folder to detect objects using YOLO model."
|
| 59 |
)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
description="Detect objects in all images within the 'img' directory."
|
| 71 |
-
)
|
| 72 |
|
| 73 |
# Combine both interfaces
|
| 74 |
-
app = gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Launch the app
|
| 77 |
app.launch()
|
|
|
|
| 20 |
def predict(image):
|
| 21 |
result_image = detect_objects(image)
|
| 22 |
output_path = save_image(result_image, "output.png")
|
| 23 |
+
return result_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Function to list images in the 'img' folder
|
| 26 |
def get_image_list():
|
|
|
|
| 32 |
return img_paths
|
| 33 |
|
| 34 |
# Gradio interface components
|
| 35 |
+
image_output = gr.outputs.Image(type="pil", label="Output Image")
|
| 36 |
+
image_select = gr.inputs.Radio(get_image_list, label="Select Image from 'img' Folder")
|
|
|
|
| 37 |
|
| 38 |
# Gradio app for single image prediction
|
| 39 |
iface = gr.Interface(
|
|
|
|
| 44 |
description="Select an image from the 'img' folder to detect objects using YOLO model."
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Custom function to display images in a gallery format
|
| 48 |
+
def gallery_update():
|
| 49 |
+
image_list = get_image_list()
|
| 50 |
+
return gr.Gallery.update(value=image_list)
|
| 51 |
|
| 52 |
+
# Gradio gallery component for displaying images in the 'img' folder
|
| 53 |
+
gallery = gr.Gallery(get_image_list(), label="Image Gallery")
|
| 54 |
+
|
| 55 |
+
# Update gallery when Gradio interface loads
|
| 56 |
+
gallery.update(gallery_update)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Combine both interfaces
|
| 59 |
+
app = gr.Blocks()
|
| 60 |
+
|
| 61 |
+
with app:
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
gallery.render()
|
| 65 |
+
with gr.Column():
|
| 66 |
+
image = gr.Image(type="pil", label="Selected Image")
|
| 67 |
+
predict_button = gr.Button("Predict")
|
| 68 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
| 69 |
+
predict_button.click(predict, inputs=image, outputs=output_image)
|
| 70 |
|
| 71 |
# Launch the app
|
| 72 |
app.launch()
|