lep1 commited on
Commit
5876d8c
1 Parent(s): 29c70aa

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +112 -0
app (1).py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Reference
3
+ - https://docs.streamlit.io/library/api-reference/layout
4
+ - https://github.com/CodingMantras/yolov8-streamlit-detection-tracking/blob/master/app.py
5
+ - https://huggingface.co/keremberke/yolov8m-valorant-detection/tree/main
6
+ - https://docs.ultralytics.com/usage/python/
7
+ """
8
+ import time
9
+ import PIL
10
+
11
+ import streamlit as st
12
+ import torch
13
+ from ultralyticsplus import YOLO, render_result
14
+
15
+ from convert import convert_to_braille_unicode, parse_xywh_and_class
16
+
17
+
18
+ def load_model(model_path):
19
+ """load model from path"""
20
+ model = YOLO(model_path)
21
+ return model
22
+
23
+
24
+ def load_image(image_path):
25
+ """load image from path"""
26
+ image = PIL.Image.open(image_path)
27
+ return image
28
+
29
+
30
+ # title
31
+ st.title("Braille Pattern Detection")
32
+
33
+ # sidebar
34
+ st.sidebar.header("Detection Config")
35
+
36
+ conf = float(st.sidebar.slider("Class Confidence", 10, 75, 15)) / 100
37
+ iou = float(st.sidebar.slider("IoU Threshold", 10, 75, 15)) / 100
38
+
39
+ model_path = "snoop2head/yolov8m-braille"
40
+
41
+ try:
42
+ model = load_model(model_path)
43
+ model.overrides["conf"] = conf # NMS confidence threshold
44
+ model.overrides["iou"] = iou # NMS IoU threshold
45
+ model.overrides["agnostic_nms"] = False # NMS class-agnostic
46
+ model.overrides["max_det"] = 1000 # maximum number of detections per image
47
+
48
+ except Exception as ex:
49
+ print(ex)
50
+ st.write(f"Unable to load model. Check the specified path: {model_path}")
51
+
52
+ source_img = None
53
+
54
+ source_img = st.sidebar.file_uploader(
55
+ "Choose an image...", type=("jpg", "jpeg", "png", "bmp", "webp")
56
+ )
57
+ col1, col2 = st.columns(2)
58
+
59
+ # left column of the page body
60
+ with col1:
61
+ if source_img is None:
62
+ default_image_path = "./images/alpha-numeric.jpeg"
63
+ image = load_image(default_image_path)
64
+ st.image(
65
+ default_image_path, caption="Example Input Image", use_column_width=True
66
+ )
67
+ else:
68
+ image = load_image(source_img)
69
+ st.image(source_img, caption="Uploaded Image", use_column_width=True)
70
+
71
+ # right column of the page body
72
+ with col2:
73
+ with st.spinner("Wait for it..."):
74
+ start_time = time.time()
75
+ try:
76
+ with torch.no_grad():
77
+ res = model.predict(
78
+ image, save=True, save_txt=True, exist_ok=True, conf=conf
79
+ )
80
+ boxes = res[0].boxes # first image
81
+ res_plotted = res[0].plot()[:, :, ::-1]
82
+
83
+ list_boxes = parse_xywh_and_class(boxes)
84
+
85
+ st.image(res_plotted, caption="Detected Image", use_column_width=True)
86
+ IMAGE_DOWNLOAD_PATH = f"runs/detect/predict/image0.jpg"
87
+
88
+ except Exception as ex:
89
+ st.write("Please upload image with types of JPG, JPEG, PNG ...")
90
+
91
+
92
+ try:
93
+ st.success(f"Done! Inference time: {time.time() - start_time:.2f} seconds")
94
+ st.subheader("Detected Braille Patterns")
95
+ for box_line in list_boxes:
96
+ str_left_to_right = ""
97
+ box_classes = box_line[:, -1]
98
+ for each_class in box_classes:
99
+ str_left_to_right += convert_to_braille_unicode(
100
+ model.names[int(each_class)]
101
+ )
102
+ st.write(str_left_to_right)
103
+ except Exception as ex:
104
+ st.write("Please try again with images with types of JPG, JPEG, PNG ...")
105
+
106
+ with open(IMAGE_DOWNLOAD_PATH, "rb") as fl:
107
+ st.download_button(
108
+ "Download object-detected image",
109
+ data=fl,
110
+ file_name="image0.jpg",
111
+ mime="image/jpg",
112
+ )