Skip to content

Commit 08eb81c

Browse files
committed
Example for recognition QR codes in the OpenCV-based video stream
1 parent 554b9f0 commit 08eb81c

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

examples/decode_opencv.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import ctypes
4+
import time
5+
import sys
6+
7+
import cv
8+
import array
9+
import quirc
10+
11+
# Create window for image showing
12+
cv.NamedWindow('window', cv.CV_WINDOW_AUTOSIZE)
13+
capture = cv.CaptureFromCAM(0)
14+
15+
# Capture first frame to determine window size
16+
frame = cv.QueryFrame(capture)
17+
# Here it is
18+
size = cv.GetSize(frame)
19+
20+
# Prepare additional object for grayscale version of the image
21+
grayscale = cv.CreateImage(size, 8, 1)
22+
23+
# And the font for text drawing
24+
font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 2, 2, 0, 1, 8)
25+
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()
32+
33+
while True:
34+
# Query new frame
35+
frame = cv.QueryFrame(capture)
36+
37+
# Make a grayscale copy
38+
cv.CvtColor(frame, grayscale, cv.CV_BGR2GRAY)
39+
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
56+
57+
# 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))
62+
63+
# 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))
65+
66+
# Show this image in the window
67+
cv.ShowImage('window', frame)
68+
69+
# Wait for key pressing
70+
key = cv.WaitKey(5)
71+
if key > 0:
72+
# And if there any key, do not forgeting to clean up memory!
73+
quirc.api.destroy(obj)
74+
75+
# Cya!
76+
sys.exit()

examples/decode_pil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
if __name__ == '__main__':
1616
try:
1717
for code in quirc.decode(Image.open(sys.argv[1])):
18-
print code['text']
18+
print code.text
1919
except IndexError:
2020
print 'Usage: %s /path/to/qr/qr.image.jpg' % sys.argv[0]

quirc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77

88
import api
99
from base import decode, Decoder
10+
from api.exceptions import DecodeException

0 commit comments

Comments
 (0)