LoginSignup
26
29

More than 5 years have passed since last update.

Raspberry Pi 3 QRコードリーダー

Last updated at Posted at 2016-08-23

インストール

  • sudo apt-get install python-zbar

  • sudo pip install pillow

  • sudo apt-get install libopencv-dev

  • sudo apt-get install python-opencv

  • sudo pip install imutils

準備するもの

  • Raspberry Pi 3 Model B (Raspbian JESSIE)

  • Raspberry Pi HD Video Camera Module
    (カメラモジュールのレンズを回して前方に移動し接写を可能とする)

仕様

  • Python ZBarライブラリを使用してQRコードを読み取る

  • Python OpenCVライブラリを使用してノイズ除去のガウスぼかしをしてからラプラシアンフィルターでimageセンサのフォーカスレベルを検出する。

  • QRコードを読み取り可能なスレッショルドを設定する。(本ソースでは100を設定しているが環境によって決定する)

ソースコード

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

結果

連続的に1秒程度の周期でフォーカスレベルを検出し、しきい値以上である場合はQRコードを読み取る。読み取りが実行された場合のタクト時間は2秒程度である。

26
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
29