PolarisFTL commited on
Commit
caa478b
·
verified ·
1 Parent(s): 482fecd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -7,6 +7,10 @@ from tqdm import tqdm
7
  # 初始化YOLO模型
8
  yolo = YOLO()
9
 
 
 
 
 
10
  # 预测单张图像
11
  def predict_single_image(image, crop=False, count=True):
12
  try:
@@ -15,19 +19,22 @@ def predict_single_image(image, crop=False, count=True):
15
  except Exception as e:
16
  return str(e)
17
 
 
 
 
 
18
 
19
  # 使用Gradio创建界面
20
- iface = gr.Interface(
21
- fn=predict_single_image,
22
- inputs=gr.inputs.Image(type="pil"),
23
- outputs=gr.outputs.Image(type="pil"),
24
- title="YOLO 图像检测器",
25
- description="上传一张图像,使用YOLO模型进行对象检测。",
26
- examples=[
27
- ["img/1.png"],
28
- ["img/2.png"]
29
- ]
30
- )
31
 
32
  # 启动应用
33
  if __name__ == "__main__":
 
7
  # 初始化YOLO模型
8
  yolo = YOLO()
9
 
10
+ # 获取 img 文件夹中的图片列表
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:
 
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")
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")
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__":