Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
-
from ultralyticsplus import YOLO
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
|
8 |
-
def image_preprocess(image):
|
9 |
-
|
10 |
-
img_height, img_width = image.shape[0:2]
|
11 |
-
image_converted = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
12 |
-
ih, iw = [input_size, input_size] # [input_size, input_size] = [640, 640]
|
13 |
-
h, w, _ = image.shape # [1944, 2592]
|
14 |
-
|
15 |
-
scale = min(iw/w, ih/h) # min(0.2469, 0.3292) = 0.2469
|
16 |
-
nw, nh = int(scale * w), int(scale * h) # [640, 480]
|
17 |
-
image_resized = cv2.resize(image_converted, (nw, nh))
|
18 |
-
|
19 |
-
image_padded = np.full(shape=[ih, iw, 3], fill_value=128.0)
|
20 |
-
dw, dh = (iw - nw) // 2, (ih-nh) // 2 # [0, 80]
|
21 |
-
image_padded[dh:nh+dh, dw:nw+dw, :] = image_resized # image_padded[80:256, 32:224]
|
22 |
-
image_padded = image_padded / 255.
|
23 |
-
# image_resized = image_resized / 255.
|
24 |
-
image_padded = image_padded[np.newaxis, ...].astype(np.float32)
|
25 |
-
image_padded = np.moveaxis(image_padded, -1, 1)
|
26 |
-
|
27 |
-
|
28 |
-
return image_padded, img_width, img_height, image
|
29 |
|
30 |
model = YOLO('best (1).pt')
|
31 |
|
@@ -33,15 +12,72 @@ def response(image):
|
|
33 |
print(image)
|
34 |
results = model(image)
|
35 |
for i, r in enumerate(results):
|
|
|
36 |
# Plot results image
|
37 |
-
im_bgr = r.plot()
|
38 |
im_rgb = im_bgr[..., ::-1] # Convert BGR to RGB
|
39 |
|
40 |
# im_rgb = Image.fromarray(im_rgb)
|
41 |
|
42 |
return im_rgb
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
|
|
|
|
45 |
|
46 |
|
47 |
iface = gr.Interface(fn=response, inputs="image", outputs="image")
|
|
|
1 |
import gradio as gr
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
+
from ultralyticsplus import YOLO, render_result
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
model = YOLO('best (1).pt')
|
10 |
|
|
|
12 |
print(image)
|
13 |
results = model(image)
|
14 |
for i, r in enumerate(results):
|
15 |
+
|
16 |
# Plot results image
|
17 |
+
im_bgr = r.plot()
|
18 |
im_rgb = im_bgr[..., ::-1] # Convert BGR to RGB
|
19 |
|
20 |
# im_rgb = Image.fromarray(im_rgb)
|
21 |
|
22 |
return im_rgb
|
23 |
|
24 |
+
def yoloV8_func(image: gr.Image = None,
|
25 |
+
image_size: gr.Slider = 640,
|
26 |
+
conf_threshold: gr.Slider = 0.4,
|
27 |
+
iou_threshold: gr.Slider = 0.50):
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
# Perform object detection on the input image using the YOLOv8 model
|
33 |
+
results = model.predict(image,
|
34 |
+
conf=conf_threshold,
|
35 |
+
iou=iou_threshold,
|
36 |
+
imgsz=image_size)
|
37 |
+
|
38 |
+
# Print the detected objects' information (class, coordinates, and probability)
|
39 |
+
box = results[0].boxes
|
40 |
+
print("Object type:", box.cls)
|
41 |
+
print("Coordinates:", box.xyxy)
|
42 |
+
print("Probability:", box.conf)
|
43 |
+
|
44 |
+
# Render the output image with bounding boxes around detected objects
|
45 |
+
render = render_result(model=model, image=image, result=results[0], rect_th = 4, text_th = 4)
|
46 |
+
return render
|
47 |
+
|
48 |
+
|
49 |
+
inputs = [
|
50 |
+
gr.Image(type="filepath", label="Input Image"),
|
51 |
+
gr.Slider(minimum=320, maximum=1280, value=640,
|
52 |
+
step=32, label="Image Size"),
|
53 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.25,
|
54 |
+
step=0.05, label="Confidence Threshold"),
|
55 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45,
|
56 |
+
step=0.05, label="IOU Threshold"),
|
57 |
+
]
|
58 |
+
|
59 |
+
|
60 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
61 |
+
|
62 |
+
title = "YOLOv8 Custom Object Detection by Uyen Nguyen"
|
63 |
+
|
64 |
+
|
65 |
+
examples = [['one.jpg', 900, 0.5, 0.8],
|
66 |
+
['two.jpg', 1152, 0.05, 0.05],
|
67 |
+
['three.jpg', 1024, 0.25, 0.25],
|
68 |
+
['four.jpg', 832, 0.3, 0.3]]
|
69 |
+
|
70 |
+
yolo_app = gr.Interface(
|
71 |
+
fn=yoloV8_func,
|
72 |
+
inputs=inputs,
|
73 |
+
outputs=outputs,
|
74 |
+
title=title,
|
75 |
+
examples=examples,
|
76 |
+
cache_examples=True,
|
77 |
+
)
|
78 |
|
79 |
+
# Launch the Gradio interface in debug mode with queue enabled
|
80 |
+
yolo_app.launch(debug=True, share=True)
|
81 |
|
82 |
|
83 |
iface = gr.Interface(fn=response, inputs="image", outputs="image")
|