Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,12 @@ import os
|
|
| 4 |
from yolo import YOLO
|
| 5 |
from tqdm import tqdm
|
| 6 |
|
| 7 |
-
|
| 8 |
yolo = YOLO()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
img_folder = "img"
|
| 12 |
img_files = [os.path.join(img_folder, file) for file in os.listdir(img_folder) if file.endswith(('.png', '.jpg', '.jpeg'))]
|
| 13 |
|
| 14 |
-
# 预测单张图像
|
| 15 |
def predict_single_image(image, crop=False, count=True):
|
| 16 |
try:
|
| 17 |
r_image = yolo.detect_image(image, crop=crop, count=count)
|
|
@@ -19,23 +17,20 @@ def predict_single_image(image, crop=False, count=True):
|
|
| 19 |
except Exception as e:
|
| 20 |
return str(e)
|
| 21 |
|
| 22 |
-
# 创建图片选择器
|
| 23 |
def select_image(image_path):
|
| 24 |
image = Image.open(image_path)
|
| 25 |
return image
|
| 26 |
|
| 27 |
-
# 使用Gradio创建界面
|
| 28 |
with gr.Blocks() as iface:
|
| 29 |
with gr.Row():
|
| 30 |
with gr.Column():
|
| 31 |
-
selected_image = gr.Image(label="Selected Image", width=
|
| 32 |
img_selector = gr.Dropdown(choices=img_files, label="Select an image from folder")
|
| 33 |
img_selector.change(select_image, img_selector, selected_image)
|
| 34 |
with gr.Column():
|
| 35 |
-
output_image = gr.Image(label="Output Image", width=
|
| 36 |
predict_button = gr.Button("Predict")
|
| 37 |
predict_button.click(predict_single_image, inputs=selected_image, outputs=output_image)
|
| 38 |
|
| 39 |
-
# 启动应用
|
| 40 |
if __name__ == "__main__":
|
| 41 |
iface.launch()
|
|
|
|
| 4 |
from yolo import YOLO
|
| 5 |
from tqdm import tqdm
|
| 6 |
|
| 7 |
+
|
| 8 |
yolo = YOLO()
|
| 9 |
|
| 10 |
+
img_folder = "img/"
|
|
|
|
| 11 |
img_files = [os.path.join(img_folder, file) for file in os.listdir(img_folder) if file.endswith(('.png', '.jpg', '.jpeg'))]
|
| 12 |
|
|
|
|
| 13 |
def predict_single_image(image, crop=False, count=True):
|
| 14 |
try:
|
| 15 |
r_image = yolo.detect_image(image, crop=crop, count=count)
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
return str(e)
|
| 19 |
|
|
|
|
| 20 |
def select_image(image_path):
|
| 21 |
image = Image.open(image_path)
|
| 22 |
return image
|
| 23 |
|
|
|
|
| 24 |
with gr.Blocks() as iface:
|
| 25 |
with gr.Row():
|
| 26 |
with gr.Column():
|
| 27 |
+
selected_image = gr.Image(label="Selected Image", width=600, height=400)
|
| 28 |
img_selector = gr.Dropdown(choices=img_files, label="Select an image from folder")
|
| 29 |
img_selector.change(select_image, img_selector, selected_image)
|
| 30 |
with gr.Column():
|
| 31 |
+
output_image = gr.Image(label="Output Image", width=600, height=400)
|
| 32 |
predict_button = gr.Button("Predict")
|
| 33 |
predict_button.click(predict_single_image, inputs=selected_image, outputs=output_image)
|
| 34 |
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
iface.launch()
|