Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,77 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
from yolo import YOLO
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# Initialize YOLO model
|
| 7 |
-
yolo = YOLO()
|
| 8 |
-
|
| 9 |
-
def detect_objects(image, crop=False, count=True):
|
| 10 |
-
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 11 |
-
return r_image
|
| 12 |
-
|
| 13 |
-
def save_image(image, filename):
|
| 14 |
-
if not os.path.exists("img_out"):
|
| 15 |
-
os.makedirs("img_out")
|
| 16 |
-
image.save(os.path.join("img_out", filename), quality=95, subsampling=0)
|
| 17 |
-
return os.path.join("img_out", filename)
|
| 18 |
-
|
| 19 |
-
# Gradio interface for single image prediction
|
| 20 |
-
def predict(image):
|
| 21 |
-
result_image = detect_objects(image)
|
| 22 |
-
output_path = save_image(result_image, "output.png")
|
| 23 |
-
return output_path
|
| 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 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from yolo import YOLO
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize YOLO model
|
| 7 |
+
yolo = YOLO()
|
| 8 |
+
|
| 9 |
+
def detect_objects(image, crop=False, count=True):
|
| 10 |
+
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 11 |
+
return r_image
|
| 12 |
+
|
| 13 |
+
def save_image(image, filename):
|
| 14 |
+
if not os.path.exists("img_out"):
|
| 15 |
+
os.makedirs("img_out")
|
| 16 |
+
image.save(os.path.join("img_out", filename), quality=95, subsampling=0)
|
| 17 |
+
return os.path.join("img_out", filename)
|
| 18 |
+
|
| 19 |
+
# Gradio interface for single image prediction
|
| 20 |
+
def predict(image):
|
| 21 |
+
result_image = detect_objects(image)
|
| 22 |
+
output_path = save_image(result_image, "output.png")
|
| 23 |
+
return output_path
|
| 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():
|
| 40 |
+
image_folder = "img"
|
| 41 |
+
if not os.path.exists(image_folder):
|
| 42 |
+
os.makedirs(image_folder)
|
| 43 |
+
img_names = [f for f in os.listdir(image_folder) if f.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))]
|
| 44 |
+
img_paths = [os.path.join(image_folder, img_name) for img_name in img_names]
|
| 45 |
+
return img_paths
|
| 46 |
+
|
| 47 |
+
# Gradio interface components
|
| 48 |
+
image_input = gr.inputs.Image(type="pil", label="Input Image")
|
| 49 |
+
image_output = gr.outputs.Image(type="file", label="Output Image")
|
| 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(
|
| 54 |
+
fn=predict,
|
| 55 |
+
inputs=image_select,
|
| 56 |
+
outputs=image_output,
|
| 57 |
+
title="YOLO Object Detection",
|
| 58 |
+
description="Select an image from the 'img' folder to detect objects using YOLO model."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Directory prediction interface
|
| 62 |
+
dir_input = gr.inputs.Textbox(default="img", label="Directory Path")
|
| 63 |
+
dir_output = gr.outputs.Textbox(label="Output Paths")
|
| 64 |
+
|
| 65 |
+
iface_dir = gr.Interface(
|
| 66 |
+
fn=dir_predict,
|
| 67 |
+
inputs=dir_input,
|
| 68 |
+
outputs=dir_output,
|
| 69 |
+
title="YOLO Object Detection for Directory",
|
| 70 |
+
description="Detect objects in all images within the 'img' directory."
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Combine both interfaces
|
| 74 |
+
app = gr.TabbedInterface([iface, iface_dir], ["Single Image Prediction", "Directory Prediction"])
|
| 75 |
+
|
| 76 |
+
# Launch the app
|
| 77 |
+
app.launch()
|