Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,30 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from PIL import Image
|
| 3 |
import gradio as gr
|
| 4 |
-
from tqdm import tqdm
|
| 5 |
from yolo import YOLO
|
| 6 |
|
| 7 |
-
# Initialize YOLO model
|
| 8 |
yolo = YOLO()
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
"""
|
| 14 |
-
try:
|
| 15 |
-
print("Starting image prediction...") # Debug log
|
| 16 |
-
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 17 |
-
print("Prediction completed.") # Debug log
|
| 18 |
-
return r_image
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"Error during image prediction: {e}") # Debug log
|
| 21 |
-
return None
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"""
|
| 27 |
-
try:
|
| 28 |
-
print(f"Processing directory: {input_dir}") # Debug log
|
| 29 |
-
img_names = os.listdir(input_dir)
|
| 30 |
-
results = []
|
| 31 |
-
for img_name in tqdm(img_names):
|
| 32 |
-
if img_name.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
|
| 33 |
-
image_path = os.path.join(input_dir, img_name)
|
| 34 |
-
image = Image.open(image_path)
|
| 35 |
-
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 36 |
-
if not os.path.exists(output_dir):
|
| 37 |
-
os.makedirs(output_dir)
|
| 38 |
-
output_path = os.path.join(output_dir, img_name.replace(".jpg", ".png"))
|
| 39 |
-
r_image.save(output_path, quality=95, subsampling=0)
|
| 40 |
-
results.append((img_name, output_path))
|
| 41 |
-
print(f"Directory processing completed: {input_dir}") # Debug log
|
| 42 |
-
return results
|
| 43 |
-
except Exception as e:
|
| 44 |
-
print(f"Error during directory prediction: {e}") # Debug log
|
| 45 |
-
return []
|
| 46 |
-
|
| 47 |
-
def inference(image, mode='predict', crop=False, count=True, input_dir=None, output_dir=None):
|
| 48 |
-
try:
|
| 49 |
-
print(f"Received mode: {mode}, crop: {crop}, count: {count}") # Debug log
|
| 50 |
-
if mode == 'predict':
|
| 51 |
-
return predict_image(image, crop=crop, count=count)
|
| 52 |
-
elif mode == 'dir_predict' and input_dir and output_dir:
|
| 53 |
-
return predict_directory(input_dir, output_dir, crop=crop, count=count)
|
| 54 |
-
else:
|
| 55 |
-
raise ValueError("Invalid mode or missing directories for 'dir_predict' mode.")
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"Error in inference function: {e}") # Debug log
|
| 58 |
-
return None
|
| 59 |
-
|
| 60 |
-
# Gradio interface setup
|
| 61 |
-
title = "YOLO Image Prediction"
|
| 62 |
-
description = "This demo allows you to perform image prediction using a YOLO model. You can either predict a single image or all images in a directory."
|
| 63 |
-
|
| 64 |
-
example_images = [
|
| 65 |
-
"img/1.png",
|
| 66 |
-
"img/2.png",
|
| 67 |
-
"img/3.png",
|
| 68 |
-
"img/4.png",
|
| 69 |
-
"img/5.png",
|
| 70 |
-
"img/6.png",
|
| 71 |
-
"img/7.png",
|
| 72 |
-
"img/8.png",
|
| 73 |
-
"img/10.png",
|
| 74 |
-
]
|
| 75 |
|
| 76 |
def reset_interface():
|
| 77 |
return gr.update(value=None), gr.update(visible=False)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
.
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
gr.Markdown(f"### {title}")
|
|
@@ -91,20 +33,16 @@ with gr.Blocks() as demo:
|
|
| 91 |
with gr.Row():
|
| 92 |
with gr.Column():
|
| 93 |
img_input = gr.Image(type="pil", label="Upload an Image")
|
| 94 |
-
mode_input = gr.Radio(choices=["predict", "dir_predict"], label="Mode", value="predict")
|
| 95 |
-
crop_input = gr.Checkbox(value=False, label="Crop")
|
| 96 |
-
count_input = gr.Checkbox(value=True, label="Count")
|
| 97 |
submit_btn = gr.Button("Submit")
|
| 98 |
with gr.Column():
|
| 99 |
output = gr.Image(type="pil", label="Prediction Result")
|
| 100 |
|
| 101 |
-
submit_btn.click(fn=
|
| 102 |
demo.load(reset_interface, None, [output])
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
)
|
| 109 |
|
| 110 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from yolo import YOLO
|
| 3 |
|
|
|
|
| 4 |
yolo = YOLO()
|
| 5 |
|
| 6 |
+
def predict(image):
|
| 7 |
+
r_image = yolo.detect_image(image)
|
| 8 |
+
return r_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
title = "MASFNet: Multi-scale Adaptive Sampling Fusion Network for Object Detection in Adverse Weather "
|
| 11 |
+
description = ""
|
| 12 |
+
article = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def reset_interface():
|
| 15 |
return gr.update(value=None), gr.update(visible=False)
|
| 16 |
|
| 17 |
+
example_images = [
|
| 18 |
+
["img/1.png"],
|
| 19 |
+
["img/2.png"],
|
| 20 |
+
["img/3.png"],
|
| 21 |
+
["img/4.png"],
|
| 22 |
+
["img/5.png"],
|
| 23 |
+
["img/6.png"],
|
| 24 |
+
["img/7.png"],
|
| 25 |
+
["img/8.png"],
|
| 26 |
+
["img/9.png"],
|
| 27 |
+
]
|
| 28 |
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
gr.Markdown(f"### {title}")
|
|
|
|
| 33 |
with gr.Row():
|
| 34 |
with gr.Column():
|
| 35 |
img_input = gr.Image(type="pil", label="Upload an Image")
|
|
|
|
|
|
|
|
|
|
| 36 |
submit_btn = gr.Button("Submit")
|
| 37 |
with gr.Column():
|
| 38 |
output = gr.Image(type="pil", label="Prediction Result")
|
| 39 |
|
| 40 |
+
submit_btn.click(fn=predict, inputs=img_input, outputs=output)
|
| 41 |
demo.load(reset_interface, None, [output])
|
| 42 |
+
gr.Examples(
|
| 43 |
+
examples=example_images,
|
| 44 |
+
inputs=img_input,
|
| 45 |
+
)
|
| 46 |
+
|
|
|
|
| 47 |
|
| 48 |
demo.launch()
|