Commit
·
6edc5c0
1
Parent(s):
f69deff
Most likely final
Browse files- app.py +35 -18
- flagged/image/tmpyu6iq1kf.png +0 -0
- flagged/log.csv +0 -2
app.py
CHANGED
@@ -3,11 +3,15 @@ from PIL import Image
|
|
3 |
import gradio as gr
|
4 |
from ultralytics import YOLO
|
5 |
import cv2
|
|
|
6 |
|
7 |
# Load the YOLO model
|
8 |
m_raw_model = YOLO("M-Raw.pt")
|
9 |
n_raw_model = YOLO("N-Raw.pt")
|
10 |
s_raw_model = YOLO("S-Raw.pt")
|
|
|
|
|
|
|
11 |
|
12 |
# Class to syllable map
|
13 |
class_mapping = {0: 'Baybayin Character', 1: 'a', 2: 'b', 3: 'ba', 4: 'be', 5: 'bi', 6: 'bo', 7: 'bu', 8: 'd', 9: 'da', 10: 'di', 11: 'do', 12: 'du', 13: 'e', 14: 'g', 15: 'ga', 16: 'gi', 17: 'go', 18: 'gu', 19: 'ha', 20: 'he', 21: 'hi', 22: 'ho', 23: 'hu', 24: 'i', 25: 'k', 26: 'ka', 27: 'ki', 28: 'ko', 29: 'ku', 30: 'l', 31: 'la', 32: 'le', 33: 'li', 34: 'lo', 35: 'lu', 36: 'm', 37: 'ma', 38: 'me', 39: 'mi', 40: 'mo', 41: 'mu', 42: 'n', 43: 'na', 44: 'ng', 45: 'nga', 46: 'ngi', 47: 'ngo', 48: 'ngu', 49: 'ni', 50: 'no', 51: 'nu', 52: 'o', 53: 'p', 54: 'pa', 55: 'pe', 56: 'pi', 57: 'po', 58: 'pu', 59: 'r', 60: 'ra', 61: 're', 62: 'ri', 63: 'ro', 64: 'ru', 65: 's', 66: 'sa', 67: 'se', 68: 'si', 69: 'so', 70: 'su', 71: 't', 72: 'ta', 73: 'te', 74: 'ti', 75: 'to', 76: 'tu', 77: 'u', 78: 'w', 79: 'wa', 80: 'we', 81: 'wi', 82: 'y', 83: 'ya', 84: 'yi', 85: 'yo', 86: 'yu'}
|
@@ -20,12 +24,13 @@ pageTitle = "Baybayin Instance Detection"
|
|
20 |
msgWarning = "This demo was created by Adriel Amoguis and Miguel Flores for PCSC 2023"
|
21 |
desc = """
|
22 |
<center>
|
23 |
-
Take a picture of some Baybayin text and upload it in the Image Box on the left. You
|
|
|
24 |
</center>
|
25 |
"""
|
26 |
|
27 |
|
28 |
-
def snap(upload,
|
29 |
|
30 |
# if webcam is not None:
|
31 |
# image = webcam
|
@@ -36,8 +41,8 @@ def snap(upload, model, conf, iou):
|
|
36 |
image = upload
|
37 |
|
38 |
# If no model selected, use M-Raw
|
39 |
-
if model == None:
|
40 |
-
|
41 |
|
42 |
# Run the selected model
|
43 |
results = None
|
@@ -47,6 +52,12 @@ def snap(upload, model, conf, iou):
|
|
47 |
results = n_raw_model(image, conf=conf, iou=iou)
|
48 |
elif model == "S-Raw":
|
49 |
results = s_raw_model(image, conf=conf, iou=iou)
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Convert the results list into an output image
|
52 |
result = results[0]
|
@@ -60,39 +71,45 @@ def snap(upload, model, conf, iou):
|
|
60 |
probs = result.boxes.conf.cpu().numpy()
|
61 |
boxes = result.boxes.xyxy.cpu().numpy()
|
62 |
|
63 |
-
print(classes)
|
64 |
-
print(probs)
|
65 |
-
print(boxes)
|
66 |
|
67 |
# print(f"Detected {classes} with {probs:.2f} confidence.")
|
|
|
68 |
for i in range(len(boxes)):
|
69 |
x1, y1, x2, y2 = boxes[i]
|
70 |
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
71 |
-
cv2.rectangle(image, (x1, y1), (x2, y2), (
|
72 |
-
cv2.putText(image, f"{class_mapping[int(classes[i])]} {probs[i]:.2f}", (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (
|
|
|
|
|
73 |
|
74 |
-
#
|
75 |
-
#
|
76 |
-
|
77 |
-
# Sort the labels by their x-value first and then by their y-value
|
78 |
-
# print(labels)
|
79 |
|
80 |
return image
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
|
83 |
demo = gr.Interface(
|
84 |
snap,
|
85 |
[
|
86 |
# gr.Webcam(type="numpy", label="Webcam"),
|
87 |
gr.Image(source="upload", type="numpy", label="Baybayin Image"),
|
88 |
-
|
89 |
gr.Slider(0, 1, value=0.6, label="Classifier Confidence Threshold"),
|
90 |
gr.Slider(0, 1, value=0.7, label="IoU Threshold")],
|
91 |
-
|
|
|
92 |
title="Baybayin Instance Detection",
|
93 |
article=msgWarning,
|
94 |
description=desc
|
95 |
).queue()
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
-
demo.launch()
|
|
|
3 |
import gradio as gr
|
4 |
from ultralytics import YOLO
|
5 |
import cv2
|
6 |
+
import os
|
7 |
|
8 |
# Load the YOLO model
|
9 |
m_raw_model = YOLO("M-Raw.pt")
|
10 |
n_raw_model = YOLO("N-Raw.pt")
|
11 |
s_raw_model = YOLO("S-Raw.pt")
|
12 |
+
m_pre_model = YOLO("M-Pre.pt")
|
13 |
+
n_pre_model = YOLO("N-Pre.pt")
|
14 |
+
s_pre_model = YOLO("S-Pre.pt")
|
15 |
|
16 |
# Class to syllable map
|
17 |
class_mapping = {0: 'Baybayin Character', 1: 'a', 2: 'b', 3: 'ba', 4: 'be', 5: 'bi', 6: 'bo', 7: 'bu', 8: 'd', 9: 'da', 10: 'di', 11: 'do', 12: 'du', 13: 'e', 14: 'g', 15: 'ga', 16: 'gi', 17: 'go', 18: 'gu', 19: 'ha', 20: 'he', 21: 'hi', 22: 'ho', 23: 'hu', 24: 'i', 25: 'k', 26: 'ka', 27: 'ki', 28: 'ko', 29: 'ku', 30: 'l', 31: 'la', 32: 'le', 33: 'li', 34: 'lo', 35: 'lu', 36: 'm', 37: 'ma', 38: 'me', 39: 'mi', 40: 'mo', 41: 'mu', 42: 'n', 43: 'na', 44: 'ng', 45: 'nga', 46: 'ngi', 47: 'ngo', 48: 'ngu', 49: 'ni', 50: 'no', 51: 'nu', 52: 'o', 53: 'p', 54: 'pa', 55: 'pe', 56: 'pi', 57: 'po', 58: 'pu', 59: 'r', 60: 'ra', 61: 're', 62: 'ri', 63: 'ro', 64: 'ru', 65: 's', 66: 'sa', 67: 'se', 68: 'si', 69: 'so', 70: 'su', 71: 't', 72: 'ta', 73: 'te', 74: 'ti', 75: 'to', 76: 'tu', 77: 'u', 78: 'w', 79: 'wa', 80: 'we', 81: 'wi', 82: 'y', 83: 'ya', 84: 'yi', 85: 'yo', 86: 'yu'}
|
|
|
24 |
msgWarning = "This demo was created by Adriel Amoguis and Miguel Flores for PCSC 2023"
|
25 |
desc = """
|
26 |
<center>
|
27 |
+
Take a picture of some Baybayin text and upload it in the Image Box on the left. You can choose the confidence threshold and the IoU threshold using the sliders. <br />
|
28 |
+
<img src="https://qph.cf2.quoracdn.net/main-qimg-a5ae0e74f5d0ef9980e7b1c59fdf2014-lq" width="60%">
|
29 |
</center>
|
30 |
"""
|
31 |
|
32 |
|
33 |
+
def snap(upload, conf, iou, show_conf=False):
|
34 |
|
35 |
# if webcam is not None:
|
36 |
# image = webcam
|
|
|
41 |
image = upload
|
42 |
|
43 |
# If no model selected, use M-Raw
|
44 |
+
# if model == None:
|
45 |
+
model = "M-Raw"
|
46 |
|
47 |
# Run the selected model
|
48 |
results = None
|
|
|
52 |
results = n_raw_model(image, conf=conf, iou=iou)
|
53 |
elif model == "S-Raw":
|
54 |
results = s_raw_model(image, conf=conf, iou=iou)
|
55 |
+
elif model == 'M-Pre':
|
56 |
+
results = m_pre_model(image, conf=conf, iou=iou)
|
57 |
+
elif model == 'N-Pre':
|
58 |
+
results = n_pre_model(image, conf=conf, iou=iou)
|
59 |
+
elif model == 'S-Pre':
|
60 |
+
results = s_pre_model(image, conf=conf, iou=iou)
|
61 |
|
62 |
# Convert the results list into an output image
|
63 |
result = results[0]
|
|
|
71 |
probs = result.boxes.conf.cpu().numpy()
|
72 |
boxes = result.boxes.xyxy.cpu().numpy()
|
73 |
|
74 |
+
# print(classes)
|
75 |
+
# print(probs)
|
76 |
+
# print(boxes)
|
77 |
|
78 |
# print(f"Detected {classes} with {probs:.2f} confidence.")
|
79 |
+
words = []
|
80 |
for i in range(len(boxes)):
|
81 |
x1, y1, x2, y2 = boxes[i]
|
82 |
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
83 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), int(image.shape[1] * 0.001))
|
84 |
+
if show_conf: cv2.putText(image, f"{class_mapping[int(classes[i])]} {probs[i]:.2f}", (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), int(image.shape[1] * 0.001))
|
85 |
+
else: cv2.putText(image, f"{class_mapping[int(classes[i])]}", (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), int(image.shape[1] * 0.00075))
|
86 |
+
# words.append([(x1,y1), (x2,y2), class_mapping[int(classes[i])]])
|
87 |
|
88 |
+
# Sort the words by top to bottom, left to right
|
89 |
+
# words = sorted(words, key=lambda x: (x[0][1], x[0][0]))
|
90 |
+
# words = [word[2] for word in words]
|
|
|
|
|
91 |
|
92 |
return image
|
93 |
+
|
94 |
+
# Get the samples array
|
95 |
+
samples_dir = "sample_images"
|
96 |
+
samples = os.listdir(samples_dir)
|
97 |
+
samples = [os.path.join(samples_dir, sample) for sample in samples]
|
98 |
|
99 |
demo = gr.Interface(
|
100 |
snap,
|
101 |
[
|
102 |
# gr.Webcam(type="numpy", label="Webcam"),
|
103 |
gr.Image(source="upload", type="numpy", label="Baybayin Image"),
|
104 |
+
# gr.Radio(["M-Raw", "S-Raw", "N-Raw", "M-Pre", "S-Pre", "N-Pre"], label="Model", value="M-Raw"),
|
105 |
gr.Slider(0, 1, value=0.6, label="Classifier Confidence Threshold"),
|
106 |
gr.Slider(0, 1, value=0.7, label="IoU Threshold")],
|
107 |
+
# gr.Checkbox(label="Show Confidence Scores", value=False),
|
108 |
+
[gr.Image(type="numpy", label="Detected Baybayin")], #gr.Textbox(lines=5, label="Detected Latin Syllabes")],
|
109 |
title="Baybayin Instance Detection",
|
110 |
article=msgWarning,
|
111 |
description=desc
|
112 |
).queue()
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
+
demo.launch(share=True)
|
flagged/image/tmpyu6iq1kf.png
DELETED
Binary file (407 kB)
|
|
flagged/log.csv
DELETED
@@ -1,2 +0,0 @@
|
|
1 |
-
image,model,Classifier Confidence Threshold,IoU Threshold,output,flag,username,timestamp
|
2 |
-
/Users/adrielamoguis/Documents/Academics/De La Salle University/Publications/Baybayin OCR - PCSC/Baybayin-Instance-Detection/flagged/image/tmpyu6iq1kf.png,N-Raw,0.6,0.7,,,,2023-03-23 19:23:28.934630
|
|
|
|
|
|