Update app.py
Browse files
app.py
CHANGED
@@ -2,15 +2,36 @@ import gradio as gr
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
model = YOLO('best (1).pt')
|
7 |
|
8 |
def response(image):
|
9 |
-
|
10 |
-
|
11 |
-
im_bgr = r.plot() # BGR-order numpy array
|
12 |
-
im_rgb = im_bgr[..., ::-1] # Convert BGR to RGB
|
13 |
-
|
14 |
return im_rgb
|
15 |
|
16 |
iface = gr.Interface(fn=response, inputs="image", outputs="image")
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
from PIL import Image
|
4 |
from ultralytics 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 |
|
32 |
def response(image):
|
33 |
+
res = image_preprocess(image)
|
34 |
+
im_rgb = model(res)
|
|
|
|
|
|
|
35 |
return im_rgb
|
36 |
|
37 |
iface = gr.Interface(fn=response, inputs="image", outputs="image")
|