Spaces:
Runtime error
Runtime error
Commit
Β·
4f32028
1
Parent(s):
590205e
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,66 @@
|
|
1 |
-
|
2 |
-
import torch
|
3 |
-
|
4 |
-
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
5 |
-
#from transformers import pipeline
|
6 |
-
|
7 |
-
from PIL import Image
|
8 |
-
import matplotlib.pyplot as plt
|
9 |
-
import matplotlib.patches as patches
|
10 |
-
|
11 |
-
import io
|
12 |
-
from random import choice
|
13 |
-
|
14 |
-
|
15 |
-
image_processor_tiny = AutoImageProcessor.from_pretrained("foduucom/object_detection")
|
16 |
-
model_tiny = AutoModelForObjectDetection.from_pretrained("foduucom/object_detection")
|
17 |
-
|
18 |
import gradio as gr
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
|
85 |
-
gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
|
86 |
-
|
87 |
-
with gr.Row():
|
88 |
-
input_image = gr.Image(label="Input image", type="pil")
|
89 |
-
output_image = gr.Image(label="Output image with predicted instances", type="pil")
|
90 |
-
|
91 |
-
gr.Examples(['samples/1.jpeg', 'samples/2.JPG'], inputs=input_image)
|
92 |
-
|
93 |
-
gr.HTML("""<br/>""")
|
94 |
-
gr.HTML("""<h4 style="color:navy;">3. Set a threshold value (default to 0.9)</h4>""")
|
95 |
-
|
96 |
-
threshold = gr.Slider(0, 1.0, value=0.9, label='threshold')
|
97 |
-
|
98 |
-
gr.HTML("""<br/>""")
|
99 |
-
gr.HTML("""<h4 style="color:navy;">4. Then, click "Infer" button to predict object instances.</h4>""")
|
100 |
-
|
101 |
-
send_btn = gr.Button("Infer")
|
102 |
-
send_btn.click(fn=infer, inputs=[input_image, model, threshold], outputs=[output_image])
|
103 |
-
|
104 |
-
gr.HTML("""<br/>""")
|
105 |
-
gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
|
106 |
-
gr.HTML("""<ul>""")
|
107 |
-
gr.HTML("""<li><a href="https://huggingface.co/docs/transformers/model_doc/yolos" target="_blank">Hugging Face Transformers - YOLOv8n</a>""")
|
108 |
-
gr.HTML("""</ul>""")
|
109 |
-
|
110 |
-
|
111 |
-
#demo.queue()
|
112 |
-
demo.launch(debug=True)
|
113 |
-
|
114 |
-
|
115 |
-
### EOF ###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Images
|
8 |
+
torch.hub.download_url_to_file('https://huggingface.co/spaces/foduucom/table-extraction-yolov8/resolve/main/test/table1.jpg', 'document1.jpg')
|
9 |
+
torch.hub.download_url_to_file('https://huggingface.co/spaces/foduucom/table-extraction-yolov8/resolve/main/test/table2.jpg', 'document2.jpg')
|
10 |
+
torch.hub.download_url_to_file('https://huggingface.co/spaces/foduucom/table-extraction-yolov8/resolve/main/test/table3.jpg', 'document3.jpg')
|
11 |
+
|
12 |
+
def yolov8_inference(
|
13 |
+
image: gr.inputs.Image = None,
|
14 |
+
model_path: gr.inputs.Dropdown = None,
|
15 |
+
image_size: gr.inputs.Slider = 640,
|
16 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
17 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
18 |
+
):
|
19 |
+
"""
|
20 |
+
YOLOv8 inference function
|
21 |
+
Args:
|
22 |
+
image: Input image
|
23 |
+
model_path: Path to the model
|
24 |
+
image_size: Image size
|
25 |
+
conf_threshold: Confidence threshold
|
26 |
+
iou_threshold: IOU threshold
|
27 |
+
Returns:
|
28 |
+
Rendered image
|
29 |
+
"""
|
30 |
+
model = YOLO(model_path)
|
31 |
+
model.overrides['conf'] = conf_threshold
|
32 |
+
model.overrides['iou']= iou_threshold
|
33 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
34 |
+
model.overrides['max_det'] = 1000
|
35 |
+
image = read_image(image)
|
36 |
+
results = model.predict(image)
|
37 |
+
render = render_result(model=model, image=image, result=results[0])
|
38 |
+
|
39 |
+
return render
|
40 |
+
|
41 |
+
|
42 |
+
inputs = [
|
43 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
44 |
+
gr.inputs.Dropdown(["foduucom/object_detection"],
|
45 |
+
default="foduucom/object_detection", label="Model"),
|
46 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
47 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
48 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
49 |
+
]
|
50 |
+
|
51 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
52 |
+
title = "YOLOobjectdetection: Efficient ObjectDetection"
|
53 |
+
|
54 |
+
description = "π YoloTableExtract is a powerful space that utilizes YOLOv8s for accurate table detection and extraction. Whether tables are bordered or borderless, this space can effectively identify and extract them from images. For further assistance and support related to documentation or data-related issues, feel free to contact [email protected]. If you find this space helpful, please show your appreciation by liking it. β€οΈππΌ"
|
55 |
+
examples = [['1.jpeg', "foduucom/object_detection", 640, 0.25, 0.45], ['2.JPG', "foduucom/object_detection", 640, 0.25, 0.45]]
|
56 |
+
demo_app = gr.Interface(
|
57 |
+
fn=yolov8_inference,
|
58 |
+
inputs=inputs,
|
59 |
+
outputs=outputs,
|
60 |
+
title=title,
|
61 |
+
description=description,
|
62 |
+
examples=examples,
|
63 |
+
cache_examples=True,
|
64 |
+
theme='huggingface',
|
65 |
+
)
|
66 |
+
demo_app.launch(debug=True, enable_queue=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|