-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicam.py
55 lines (46 loc) · 1.3 KB
/
picam.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
48
49
50
51
52
53
54
55
#!/usr/bin/python
from time import sleep
import picamera
import io
import cv2
import numpy as np
import zbar
from PIL import Image
def cam_setup():
cam = picamera.PiCamera()
cam.hflip = True
cam.vflip = True
cam.start_preview()
cam.preview_fullscreen = True
sleep(2) # Camera setup delay
return cam
def process_stream(stream):
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
img = cv2.imdecode(data, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY, dstCn=0)
pil = Image.fromarray(gray)
width, height = pil.size
raw = pil.tostring()
image = zbar.Image(width, height, 'Y800', raw)
return image
def scan(camera, stream, scanner):
camera.capture(stream, format='jpeg', use_video_port=True)
image = process_stream(stream)
scanner.scan(image)
for symbol in image:
if symbol.data != "None":
print symbol.data # qr code data
print symbol.location # pixel values of contour surrounding the qr code
stream.seek(0)
stream.truncate()
def main():
stream = io.BytesIO()
scanner = zbar.ImageScanner()
scanner.parse_config('enable')
camera = cam_setup()
while(True):
scan(camera, stream, scanner)
camera.stop_preview()
camera.close()
if __name__ == '__main__':
main()