Skip to content

Instantly share code, notes, and snippets.

@makoto
Created October 3, 2017 07:51
Show Gist options
  • Save makoto/c30637fe990c9f0a703ca8d607355fb3 to your computer and use it in GitHub Desktop.
Save makoto/c30637fe990c9f0a703ca8d607355fb3 to your computer and use it in GitHub Desktop.
import io
import datetime
import time
import picamera
from PIL import Image
import zbar
import cv2
import numpy as np
while True:
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
# camera.start_preview()
# time.sleep(1)
camera.capture(stream, format='jpeg')
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
pil = Image.open(stream)
imgGauss = cv2.GaussianBlur(np.asarray(pil), (3,3), 0)
gray = cv2.cvtColor(imgGauss, cv2.COLOR_RGB2GRAY)
fm = cv2.Laplacian(gray, cv2.CV_64F).var()
# print "%d" % fm
if fm < 100:
continue
#
#########################################
#
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
pil = pil.convert('L')
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
del(image)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment