Spaces:
Runtime error
Runtime error
andytonglove
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import cv2 as cv
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
|
7 |
+
# 定义二维码检测函数
|
8 |
+
def detect_qr_code(image):
|
9 |
+
# 初始化 WeChatQRCode 检测器
|
10 |
+
qrcode_detector = cv.wechat_qrcode_WeChatQRCode(
|
11 |
+
"model/detect.prototxt",
|
12 |
+
"model/detect.caffemodel",
|
13 |
+
"model/sr.prototxt",
|
14 |
+
"model/sr.caffemodel",
|
15 |
+
)
|
16 |
+
|
17 |
+
# 检测并解码二维码
|
18 |
+
result = qrcode_detector.detectAndDecode(image)
|
19 |
+
seen_qrcodes = set()
|
20 |
+
|
21 |
+
# 创建副本以绘制检测框
|
22 |
+
debug_image = copy.deepcopy(image)
|
23 |
+
for text in result[0]:
|
24 |
+
if text:
|
25 |
+
seen_qrcodes.add(text)
|
26 |
+
|
27 |
+
debug_image = draw_tags(debug_image, result, 0, len(seen_qrcodes), len(result[0]))
|
28 |
+
|
29 |
+
return debug_image, list(seen_qrcodes)
|
30 |
+
|
31 |
+
|
32 |
+
# 定义绘制标签的函数
|
33 |
+
def draw_tags(image, qrcode_result, elapsed_time, total_qrcodes, current_qrcodes):
|
34 |
+
for i in range(len(qrcode_result[0])):
|
35 |
+
text = qrcode_result[0][i]
|
36 |
+
corner = qrcode_result[1][i]
|
37 |
+
|
38 |
+
corner_01 = (int(corner[0][0]), int(corner[0][1]))
|
39 |
+
corner_02 = (int(corner[1][0]), int(corner[1][1]))
|
40 |
+
corner_03 = (int(corner[2][0]), int(corner[2][1]))
|
41 |
+
corner_04 = (int(corner[3][0]), int(corner[3][1]))
|
42 |
+
|
43 |
+
# 各边勾画
|
44 |
+
cv.line(
|
45 |
+
image,
|
46 |
+
(corner_01[0], corner_01[1]),
|
47 |
+
(corner_02[0], corner_02[1]),
|
48 |
+
(255, 0, 0),
|
49 |
+
2,
|
50 |
+
)
|
51 |
+
cv.line(
|
52 |
+
image,
|
53 |
+
(corner_02[0], corner_02[1]),
|
54 |
+
(corner_03[0], corner_03[1]),
|
55 |
+
(255, 0, 0),
|
56 |
+
2,
|
57 |
+
)
|
58 |
+
cv.line(
|
59 |
+
image,
|
60 |
+
(corner_03[0], corner_03[1]),
|
61 |
+
(corner_04[0], corner_04[1]),
|
62 |
+
(0, 255, 0),
|
63 |
+
2,
|
64 |
+
)
|
65 |
+
cv.line(
|
66 |
+
image,
|
67 |
+
(corner_04[0], corner_04[1]),
|
68 |
+
(corner_01[0], corner_01[1]),
|
69 |
+
(0, 255, 0),
|
70 |
+
2,
|
71 |
+
)
|
72 |
+
|
73 |
+
# 文本
|
74 |
+
cv.putText(
|
75 |
+
image,
|
76 |
+
str(text),
|
77 |
+
(corner_01[0], corner_01[1] - 10),
|
78 |
+
cv.FONT_HERSHEY_SIMPLEX,
|
79 |
+
0.75,
|
80 |
+
(0, 255, 0),
|
81 |
+
2,
|
82 |
+
cv.LINE_AA,
|
83 |
+
)
|
84 |
+
|
85 |
+
# 处理时间
|
86 |
+
cv.putText(
|
87 |
+
image,
|
88 |
+
"Elapsed Time:" + "{:.1f}".format(elapsed_time * 1000) + "ms",
|
89 |
+
(10, 30),
|
90 |
+
cv.FONT_HERSHEY_SIMPLEX,
|
91 |
+
0.8,
|
92 |
+
(0, 255, 0),
|
93 |
+
2,
|
94 |
+
cv.LINE_AA,
|
95 |
+
)
|
96 |
+
cv.putText(
|
97 |
+
image,
|
98 |
+
"Total QR Codes: " + str(total_qrcodes),
|
99 |
+
(10, 60),
|
100 |
+
cv.FONT_HERSHEY_SIMPLEX,
|
101 |
+
0.8,
|
102 |
+
(0, 255, 0),
|
103 |
+
2,
|
104 |
+
cv.LINE_AA,
|
105 |
+
)
|
106 |
+
cv.putText(
|
107 |
+
image,
|
108 |
+
"Current QR Codes: " + str(current_qrcodes),
|
109 |
+
(10, 90),
|
110 |
+
cv.FONT_HERSHEY_SIMPLEX,
|
111 |
+
0.8,
|
112 |
+
(0, 255, 0),
|
113 |
+
2,
|
114 |
+
cv.LINE_AA,
|
115 |
+
)
|
116 |
+
|
117 |
+
return image
|
118 |
+
|
119 |
+
|
120 |
+
# 创建 Gradio 接口
|
121 |
+
interface = gr.Interface(
|
122 |
+
fn=detect_qr_code,
|
123 |
+
inputs=gr.Image(type="numpy", label="Upload an image"),
|
124 |
+
outputs=[
|
125 |
+
gr.Image(type="numpy", label="Annotated Image"),
|
126 |
+
gr.Textbox(label="Detected QR Codes"),
|
127 |
+
],
|
128 |
+
title="QR Code Detector",
|
129 |
+
description="Upload an image to detect QR codes in it using OpenCV and WeChatQRCode.",
|
130 |
+
)
|
131 |
+
|
132 |
+
# 启动 Gradio 接口
|
133 |
+
interface.launch()
|