Spaces:
Sleeping
Sleeping
nehulagrawal
commited on
Commit
•
968696e
1
Parent(s):
a589bba
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
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 |
+
def yolov8_inference(
|
8 |
+
image: gr.inputs.Image = None,
|
9 |
+
model_path: gr.inputs.Dropdown = None,
|
10 |
+
image_size: gr.inputs.Slider = 640,
|
11 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
12 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
13 |
+
):
|
14 |
+
"""
|
15 |
+
YOLOv8 inference function
|
16 |
+
Args:
|
17 |
+
image: Input image
|
18 |
+
model_path: Path to the model
|
19 |
+
image_size: Image size
|
20 |
+
conf_threshold: Confidence threshold
|
21 |
+
iou_threshold: IOU threshold
|
22 |
+
Returns:
|
23 |
+
Rendered image
|
24 |
+
"""
|
25 |
+
model = YOLO(model_path)
|
26 |
+
model.overrides['conf'] = conf_threshold
|
27 |
+
model.overrides['iou']= iou_threshold
|
28 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
29 |
+
model.overrides['max_det'] = 1000
|
30 |
+
image = read_image(image)
|
31 |
+
results = model.predict(image)
|
32 |
+
render = render_result(model=model, image=image, result=results[0])
|
33 |
+
|
34 |
+
return render
|
35 |
+
|
36 |
+
|
37 |
+
inputs = [
|
38 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
39 |
+
gr.inputs.Dropdown(["foduucom/pan-card-detection"],
|
40 |
+
default="foduucom/pan-card-detection", label="Model"),
|
41 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
42 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
43 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
44 |
+
]
|
45 |
+
|
46 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
47 |
+
title = "pancard : pancard Detection in Images"
|
48 |
+
|
49 |
+
examples = [['sample/1.jpeg', 'foduucom/pan-card-detection', 640, 0.25, 0.45], ['sample/2.jpg', 'foduucom/pan-card-detection', 640, 0.25, 0.45]]
|
50 |
+
demo_app = gr.Interface(
|
51 |
+
fn=yolov8_inference,
|
52 |
+
inputs=inputs,
|
53 |
+
outputs=outputs,
|
54 |
+
title=title,
|
55 |
+
description=description,
|
56 |
+
examples=examples,
|
57 |
+
cache_examples=True,
|
58 |
+
theme='huggingface',
|
59 |
+
)
|
60 |
+
demo_app.launch(debug=True, enable_queue=True)
|