PolarisFTL commited on
Commit
4b4644f
·
verified ·
1 Parent(s): 16830e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
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 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():
@@ -45,9 +32,8 @@ def get_image_list():
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(
@@ -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
- # 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()
 
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()