-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_decoder.py
47 lines (40 loc) · 1.36 KB
/
qr_decoder.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
45
46
47
"""Decodes camera images to return QR code dimensions."""
from PIL import ImageEnhance
from pyzbar.pyzbar import decode
from src.config import SIMULATION
from src import utils as u
class QRDecoder(object):
def __init__(self):
self.image = None
def input(self, image):
"""Stores the input image."""
self.image = image
def get_dist(self, rect):
return u.fw2d((rect.width + rect.height) / 2)
def angle_to_qr(self, polygon):
"""Gives heading angle to QR code."""
return u.get_alpha(polygon)
def extract_qrs(self, decoded):
"""Extract and return QR code information."""
qrs = []
for code in decoded:
name = code.data.decode('UTF-8')
rect = code.rect
distance = self.get_dist(rect)
polygon = code.polygon
angle = self.angle_to_qr(polygon)
qrs.append((name, rect, distance, angle, polygon))
if not qrs:
qrs = None
return qrs
# TODO: @timed
def output(self):
"""Returns the decoded QR code distance, angle and shape."""
if not SIMULATION:
contrast = ImageEnhance.Contrast(self.image)
contrast = contrast.enhance(1.5)
decoded = decode(contrast)
qrs = self.extract_qrs(decoded)
else:
qrs = None
return qrs