-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqr_coder.py
44 lines (35 loc) · 1.19 KB
/
qr_coder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# by nyLiao, 2020
import cv2 as cv
from PIL import Image
class qrEncoder(object):
"""QR code encoder."""
def __init__(self):
super(qrEncoder, self).__init__()
def _pil2pil(self, img):
"""Convert to normal PIL image type."""
import io
buffered = io.BytesIO()
img.save(buffered)
return Image.open(buffered)
def enc_str(self, s):
"""Encode string to QR code."""
import qrcode
img = self._pil2pil(qrcode.make(s))
return img
class qrDecoder(object):
"""QR code decoder."""
def __init__(self):
super(qrDecoder, self).__init__()
def process(self, imgarr):
"""Decrpyted image denoiser."""
imgarr_blr = cv.blur(imgarr, ksize=(3, 3))
_, dst = cv.threshold(imgarr_blr, 150, 255, cv.THRESH_BINARY_INV)
return dst
def dec_str(self, imgarr):
"""Detect and decode QR code."""
qrcoder = cv.QRCodeDetector()
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(imgarr)
if codeinfo == '':
imgarr = cv.bitwise_not(imgarr)
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(imgarr)
return codeinfo