Update app.py
Browse files
app.py
CHANGED
@@ -4,65 +4,61 @@ from sahi.prediction import ObjectPrediction
|
|
4 |
from sahi.utils.cv import visualize_object_predictions, read_image
|
5 |
from ultralyticsplus import YOLO, render_result
|
6 |
|
|
|
7 |
image_path = [
|
8 |
['test/web form.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45],
|
9 |
['test/web form2.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45]
|
10 |
]
|
11 |
|
12 |
-
|
13 |
def yolov8_inference(
|
14 |
-
image
|
15 |
-
model_path
|
16 |
-
image_size
|
17 |
-
conf_threshold
|
18 |
-
iou_threshold
|
19 |
):
|
20 |
-
|
21 |
-
YOLOv8 inference function
|
22 |
-
Args:
|
23 |
-
image: Input image
|
24 |
-
model_path: Path to the model
|
25 |
-
image_size: Image size
|
26 |
-
conf_threshold: Confidence threshold
|
27 |
-
iou_threshold: IOU threshold
|
28 |
-
Returns:
|
29 |
-
Rendered image
|
30 |
-
"""
|
31 |
model = YOLO(model_path)
|
32 |
-
model.overrides
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
|
|
43 |
inputs = [
|
44 |
-
gr.
|
45 |
-
gr.
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
]
|
51 |
|
52 |
-
outputs = gr.
|
53 |
-
|
|
|
54 |
|
55 |
-
|
|
|
56 |
fn=yolov8_inference,
|
57 |
inputs=inputs,
|
58 |
outputs=outputs,
|
59 |
title=title,
|
60 |
examples=image_path,
|
61 |
cache_examples=False,
|
62 |
-
theme=
|
63 |
)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
tab_names=['Image inference']
|
68 |
-
).queue().launch()
|
|
|
4 |
from sahi.utils.cv import visualize_object_predictions, read_image
|
5 |
from ultralyticsplus import YOLO, render_result
|
6 |
|
7 |
+
# your example images
|
8 |
image_path = [
|
9 |
['test/web form.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45],
|
10 |
['test/web form2.jpg', 'foduucom/web-form-ui-field-detection', 640, 0.25, 0.45]
|
11 |
]
|
12 |
|
|
|
13 |
def yolov8_inference(
|
14 |
+
image, # will be a filepath string
|
15 |
+
model_path, # string
|
16 |
+
image_size, # int
|
17 |
+
conf_threshold, # float
|
18 |
+
iou_threshold # float
|
19 |
):
|
20 |
+
# load and configure the model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
model = YOLO(model_path)
|
22 |
+
model.overrides.update({
|
23 |
+
'conf': conf_threshold,
|
24 |
+
'iou': iou_threshold,
|
25 |
+
'agnostic_nms': False,
|
26 |
+
'max_det': 1000
|
27 |
+
})
|
28 |
+
|
29 |
+
# read & run
|
30 |
+
img = read_image(image)
|
31 |
+
results = model.predict(img)
|
32 |
+
rendered = render_result(model=model, image=img, result=results[0])
|
33 |
+
return rendered
|
34 |
|
35 |
+
# define components using the new API
|
36 |
inputs = [
|
37 |
+
gr.Image(type="filepath", label="Input Image"),
|
38 |
+
gr.Dropdown(
|
39 |
+
choices=["foduucom/web-form-ui-field-detection"],
|
40 |
+
value="foduucom/web-form-ui-field-detection",
|
41 |
+
label="Model"
|
42 |
+
),
|
43 |
+
gr.Slider(320, 1280, step=32, value=640, label="Image Size"),
|
44 |
+
gr.Slider(0.0, 1.0, step=0.05, value=0.25, label="Confidence Threshold"),
|
45 |
+
gr.Slider(0.0, 1.0, step=0.05, value=0.45, label="IOU Threshold"),
|
46 |
]
|
47 |
|
48 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
49 |
+
|
50 |
+
title = "Web-Form UI Field Detection"
|
51 |
|
52 |
+
# single-tab interface
|
53 |
+
interface = gr.Interface(
|
54 |
fn=yolov8_inference,
|
55 |
inputs=inputs,
|
56 |
outputs=outputs,
|
57 |
title=title,
|
58 |
examples=image_path,
|
59 |
cache_examples=False,
|
60 |
+
theme="huggingface"
|
61 |
)
|
62 |
|
63 |
+
if __name__ == "__main__":
|
64 |
+
interface.launch()
|
|
|
|