sayedM commited on
Commit
f22bcea
·
1 Parent(s): a2639ac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import supervision as sv
2
+ import gradio as gr
3
+ from ultralytics import YOLO
4
+ import sahi
5
+
6
+
7
+
8
+
9
+
10
+ # Images
11
+ sahi.utils.file.download_from_url(
12
+ "https://www.erbanotizie.com/wp-content/uploads/2014/01/Casello.jpg",
13
+ "ocr1.jpg",
14
+ )
15
+ sahi.utils.file.download_from_url(
16
+ "https://media-cdn.tripadvisor.com/media/photo-s/15/1d/03/18/receipt.jpg",
17
+ "ocr2.jpg",
18
+ )
19
+ sahi.utils.file.download_from_url(
20
+ "https://upload.forumfree.net/i/ff11450850/b5ef33b7-01da-4055-9ece-089b2a35a193.jpg",
21
+ "ocr3.jpg",
22
+ )
23
+
24
+
25
+
26
+
27
+ annotatorbbox = sv.BoxAnnotator()
28
+ annotatormask=sv.MaskAnnotator()
29
+
30
+
31
+ def yolov8_inference(
32
+ image: gr.inputs.Image = None,
33
+ model_name: gr.inputs.Dropdown = None,
34
+ image_size: gr.inputs.Slider = 320,
35
+ conf_threshold: gr.inputs.Slider = 0.25,
36
+ iou_threshold: gr.inputs.Slider = 0.45,
37
+ ):
38
+
39
+
40
+ model = YOLO("best_Receipt.pt")
41
+
42
+ results = model(image,conf=conf_threshold,iou=iou_threshold ,imgsz=320)[0]
43
+ detections = sv.Detections.from_yolov8(results)
44
+ annotated_image = annotatorbbox.annotate(scene=image, detections=detections)
45
+ annotated_image = annotatormask.annotate(scene=annotated_image, detections=detections)
46
+
47
+
48
+
49
+
50
+ return annotated_image
51
+
52
+ image_input = gr.inputs.Image() # Adjust the shape according to your requirements
53
+
54
+ inputs = [
55
+ gr.inputs.Image(label="Input Image"),
56
+ gr.Slider(
57
+ minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
58
+ ),
59
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
60
+ ]
61
+
62
+ outputs = gr.Image(type="filepath", label="Output Image")
63
+ title = "YOLOv8 Segmentation Demo Receipt"
64
+ examples = [
65
+ ["ocr1.jpg", 0.6, 0.45],
66
+ ["ocr2.jpg", 0.25, 0.45],
67
+ ["ocr3.jpg", 0.25, 0.45],
68
+ ]
69
+ demo_app = gr.Interface(examples=examples,
70
+ fn=yolov8_inference,
71
+ inputs=inputs,
72
+ outputs=outputs,
73
+ title=title,
74
+ cache_examples=True,
75
+ theme="default",
76
+ )
77
+ demo_app.launch(debug=False, enable_queue=True)