|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | import ctypes |
4 | | -import time |
5 | 4 | import sys |
6 | 5 |
|
7 | 6 | import cv |
8 | | -import array |
9 | 7 | import quirc |
10 | 8 |
|
11 | 9 | # Create window for image showing |
|
23 | 21 | # And the font for text drawing |
24 | 22 | font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 2, 2, 0, 1, 8) |
25 | 23 |
|
26 | | -# Initialize all required quirc structures |
27 | | -obj = quirc.api.new() |
28 | | -quirc.api.resize(obj, *size) |
29 | | - |
30 | | -code = quirc.api.structures.Code() |
31 | | -data = quirc.api.structures.Data() |
| 24 | +# Initialize QR decoder |
| 25 | +decoder = quirc.Decoder(*size) |
32 | 26 |
|
33 | 27 | while True: |
34 | 28 | # Query new frame |
|
37 | 31 | # Make a grayscale copy |
38 | 32 | cv.CvtColor(frame, grayscale, cv.CV_BGR2GRAY) |
39 | 33 |
|
40 | | - # Create a buffer for recognition |
41 | | - buffer = quirc.api.begin(obj, *size) |
42 | | - |
43 | | - # Fill it with a pixels data |
44 | | - buf = array.array('B', grayscale.tostring()) |
45 | | - ctypes.memmove(buffer, buf.buffer_info()[0], size[0]*size[1]) |
46 | | - |
47 | | - quirc.api.end(obj) |
48 | | - |
49 | | - for i in range(quirc.api.count(obj)): |
50 | | - # Extract all QR codes |
51 | | - quirc.api.extract(obj, i, code) |
52 | | - try: |
53 | | - quirc.api.decode(code, data) |
54 | | - except quirc.DecodeException: |
55 | | - continue |
| 34 | + for code in decoder.decode(grayscale.tostring()): |
56 | 35 |
|
57 | 36 | # Draw a countours for each QR code |
58 | | - cv.Line(frame, (code.corners[0].x, code.corners[0].y), (code.corners[1].x, code.corners[1].y), (0, 255, 0)) |
59 | | - cv.Line(frame, (code.corners[1].x, code.corners[1].y), (code.corners[2].x, code.corners[2].y), (0, 255, 0)) |
60 | | - cv.Line(frame, (code.corners[2].x, code.corners[2].y), (code.corners[3].x, code.corners[3].y), (0, 255, 0)) |
61 | | - cv.Line(frame, (code.corners[3].x, code.corners[3].y), (code.corners[0].x, code.corners[0].y), (0, 255, 0)) |
| 37 | + # TODO: replace with a cv.PolyLine |
| 38 | + cv.Line(frame, (code.corners[0][0], code.corners[0][1]), (code.corners[1][0], code.corners[1][1]), (0, 255, 0)) |
| 39 | + cv.Line(frame, (code.corners[1][0], code.corners[1][1]), (code.corners[2][0], code.corners[2][1]), (0, 255, 0)) |
| 40 | + cv.Line(frame, (code.corners[2][0], code.corners[2][1]), (code.corners[3][0], code.corners[3][1]), (0, 255, 0)) |
| 41 | + cv.Line(frame, (code.corners[3][0], code.corners[3][1]), (code.corners[0][0], code.corners[0][1]), (0, 255, 0)) |
62 | 42 |
|
63 | 43 | # And a decoded text for each one |
64 | | - cv.PutText(frame, ctypes.string_at(data.payload, data.payload_len), (code.corners[3].x, code.corners[3].y+14), font, (0, 0, 255)) |
| 44 | + cv.PutText(frame, code.text, (code.corners[3][0], code.corners[3][1]+14), font, (0, 0, 255)) |
65 | 45 |
|
66 | 46 | # Show this image in the window |
67 | 47 | cv.ShowImage('window', frame) |
68 | 48 |
|
69 | 49 | # Wait for key pressing |
70 | 50 | key = cv.WaitKey(5) |
71 | 51 | if key > 0: |
72 | | - # And if there any key, do not forgeting to clean up memory! |
73 | | - quirc.api.destroy(obj) |
74 | | - |
75 | | - # Cya! |
| 52 | + # If any key presses, exit |
76 | 53 | sys.exit() |
0 commit comments