PolarisFTL commited on
Commit
23faa2c
·
verified ·
1 Parent(s): a01fa1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -67
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
- # Gradio interface components
39
- image_input = gr.inputs.Image(type="pil", label="Input Image")
40
- image_output = gr.outputs.Image(type="file", label="Output Image")
41
-
42
- # Gradio app
43
- iface = gr.Interface(
44
- fn=predict,
45
- inputs=image_input,
46
- outputs=image_output,
47
- title="YOLO Object Detection",
48
- description="Upload an image to detect objects using YOLO model."
49
- )
50
-
51
- # Directory prediction interface
52
- dir_input = gr.inputs.Textbox(label="Directory Path")
53
- dir_output = gr.outputs.Textbox(label="Output Paths")
54
-
55
- iface_dir = gr.Interface(
56
- fn=dir_predict,
57
- inputs=dir_input,
58
- outputs=dir_output,
59
- title="YOLO Object Detection for Directory",
60
- description="Provide a directory path to detect objects in all images within the directory."
61
- )
62
-
63
- # Combine both interfaces
64
- app = gr.TabbedInterface([iface, iface_dir], ["Single Image Prediction", "Directory Prediction"])
65
-
66
- # Launch the app
67
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
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()