add key verification
Browse files- .gitignore +2 -0
- app.py +18 -12
- test.jpeg → example1.jpeg +0 -0
- loading.gif → example2.jpeg +2 -2
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
.DS_Store
|
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from types import SimpleNamespace
|
| 2 |
import gradio as gr
|
| 3 |
from PIL import Image
|
|
@@ -12,11 +13,15 @@ from utils import custom_plot
|
|
| 12 |
|
| 13 |
model = YOLO("YOLOV8s_Barcode_Detection.pt")
|
| 14 |
reader = easyocr.Reader(['en'])
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
| 18 |
if input_img is None:
|
| 19 |
-
return None, "
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Perform object detection on an image
|
| 22 |
result = model(input_img, imgsz=(1280))[0]
|
|
@@ -77,6 +82,7 @@ def process_image(input_img, progress=gr.Progress()):
|
|
| 77 |
pred_text += cur_box[1]
|
| 78 |
else:
|
| 79 |
break
|
|
|
|
| 80 |
if len(pred_text) != 15:
|
| 81 |
res = []
|
| 82 |
res = [SimpleNamespace(text=pred_text)] if res else []
|
|
@@ -84,35 +90,35 @@ def process_image(input_img, progress=gr.Progress()):
|
|
| 84 |
texts.append(res[0].text if res else None)
|
| 85 |
|
| 86 |
output_text = '\n'.join([t for t in texts if isinstance(t, str)])
|
| 87 |
-
yield loading_img, output_text
|
| 88 |
results_img = custom_plot(
|
| 89 |
result, font_size=40, pil=True,
|
| 90 |
barcode_texts=texts,
|
| 91 |
)
|
| 92 |
-
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
# Defining the Gradio Interface
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# Barcode")
|
| 99 |
-
gr.Markdown("
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column():
|
| 103 |
-
input_view = gr.Image(type="pil", label="
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
gr.Examples(examples=["
|
| 107 |
with gr.Column():
|
| 108 |
-
output_view = gr.Image(type="pil", label="
|
| 109 |
with gr.Column():
|
| 110 |
-
text_output = gr.Textbox(label="
|
| 111 |
|
| 112 |
# Wire up the button
|
| 113 |
btn.click(
|
| 114 |
fn=process_image,
|
| 115 |
-
inputs=input_view,
|
| 116 |
outputs=[output_view, text_output]
|
| 117 |
)
|
| 118 |
|
|
|
|
| 1 |
+
import hashlib
|
| 2 |
from types import SimpleNamespace
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
|
|
|
| 13 |
|
| 14 |
model = YOLO("YOLOV8s_Barcode_Detection.pt")
|
| 15 |
reader = easyocr.Reader(['en'])
|
| 16 |
+
key_hash = "6734e0268423441620ada6c66bf76c255500139962741833ab33185274b8a010"
|
| 17 |
+
hash = lambda x: hashlib.sha256(x.encode()).hexdigest()
|
| 18 |
|
| 19 |
+
|
| 20 |
+
def process_image(input_img, activation_key, progress=gr.Progress()):
|
| 21 |
if input_img is None:
|
| 22 |
+
return None, "請上傳圖片後再進行辨識。"
|
| 23 |
+
if hash(activation_key) != key_hash:
|
| 24 |
+
return None, "金鑰錯誤,請確認後再試一次。"
|
| 25 |
|
| 26 |
# Perform object detection on an image
|
| 27 |
result = model(input_img, imgsz=(1280))[0]
|
|
|
|
| 82 |
pred_text += cur_box[1]
|
| 83 |
else:
|
| 84 |
break
|
| 85 |
+
# HACK
|
| 86 |
if len(pred_text) != 15:
|
| 87 |
res = []
|
| 88 |
res = [SimpleNamespace(text=pred_text)] if res else []
|
|
|
|
| 90 |
texts.append(res[0].text if res else None)
|
| 91 |
|
| 92 |
output_text = '\n'.join([t for t in texts if isinstance(t, str)])
|
|
|
|
| 93 |
results_img = custom_plot(
|
| 94 |
result, font_size=40, pil=True,
|
| 95 |
barcode_texts=texts,
|
| 96 |
)
|
| 97 |
+
return results_img, output_text
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
# Defining the Gradio Interface
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
gr.Markdown("# Barcode")
|
| 104 |
+
gr.Markdown("請用下方 example 圖片測試條碼辨識功能,或上傳您自己的圖片。")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column():
|
| 108 |
+
input_view = gr.Image(type="pil", label="輸入圖片")
|
| 109 |
+
activation_key = gr.Textbox(label="金鑰")
|
| 110 |
+
btn = gr.Button("辨識", variant="primary")
|
| 111 |
|
| 112 |
+
gr.Examples(examples=[["example1.jpeg", ""], ["example2.jpeg", ""]], inputs=input_view)
|
| 113 |
with gr.Column():
|
| 114 |
+
output_view = gr.Image(type="pil", label="辨識結果")
|
| 115 |
with gr.Column():
|
| 116 |
+
text_output = gr.Textbox(label="條碼內容")
|
| 117 |
|
| 118 |
# Wire up the button
|
| 119 |
btn.click(
|
| 120 |
fn=process_image,
|
| 121 |
+
inputs=[input_view, activation_key],
|
| 122 |
outputs=[output_view, text_output]
|
| 123 |
)
|
| 124 |
|
test.jpeg → example1.jpeg
RENAMED
|
File without changes
|
loading.gif → example2.jpeg
RENAMED
|
File without changes
|