LoginSignup
16
17

More than 5 years have passed since last update.

Raspberry Pi 専用カメラでQRコードを取得する

Last updated at Posted at 2019-01-08

内容

・Raspberry Pi の専用カメラを使ってQRコード内の情報を取得します。
・コマンドラインで取得する方法を試します。
・Python のプログラムで取得する方法も試します。

環境

実際に用いた環境は以下です。
・本体 Raspberry Pi 3B
・OS Raspbian Stretch Lite 2018-11-13 Kernel version 4.14
・カメラ Raspberry Pi Camera V2

コマンドラインで取得

・最初に zbar-tools をインストールします。
・raspistill で撮影・保管した画像ファイルから zbar-tools でQRコードを読み込み、情報を表示します。
・撮影する画像はWXGA(1280X800)程度の大きさで十分です。(サイズが小さい方が処理も早い。)

詳細は以下を参照ください。

raspistillにいろいろなオプションをつけて写真を撮影してみる
https://www.kabegiwablog.com/entry/2018/07/15/183846

ZBar でバーコード読み取り
http://99blues.dyndns.org/blog/2010/12/zbar/

$ sudo apt-get install zbar-tools
(zbar-tools のインストール)

$ raspistill -o image.jpg && zbarimg image.jpg
QR-Code:09CAB423-633D-1801-99CD-001C4DEAA59E
scanned 1 barcode symbols from 1 images in 3.8 seconds
(QRを撮影し、画像ファイルに保管します。)

$ raspistill -t 1 -w 800 -h 600 -o image.jpg && zbarimg image.jpg
QR-Code:09CAB423-633D-1801-99CD-001C4DEAA59E
scanned 1 barcode symbols from 1 images in 0.39 seconds
(画像ファイルのQRコードの情報を表示します。)

Python プログラムで取得

Python プログラム開発は以下の処理手順で行います。
・最初に python-opencv python-zbar をインストールします。
・OpenCV で処理し易いように /dev/video0 としてアクセスできるようにします。
・/etc/modules に bcm2835-v4l2 を登録しておくと毎回登録する必要がない
・プログラムを実行し、QRをスキャンすると表示(重複表示はしないように)

詳細は以下を参照ください。

Pythonで画像処理をするならOpenCVがオススメ!
https://www.sejuku.net/blog/54272

【python】zbarでバーコードを読み取る(画像→数列) 【お家IT#17】
http://motojapan.hateblo.jp/entry/2018/03/05/090402

OpenCVとZbarでバーコード・QRコード認識(Python)
https://qiita.com/Oside/items/67372ca26f2d9ff77458

ラズパイとカメラモジュールでIPカメラ
http://yagitsawa.github.io/2017/06/17/raspberrypi-camera-rtsp-server/

$ sudo apt-get install python-opencv python-zbar
(必要なライブラリィのインストール)

$ ls /dev/video*
ls: cannot access '/dev/video*': No such file or directory
$ sudo modprobe bcm2835-v4l2
$ ls /dev/video*
/dev/video0

$ sudo nano /etc/modules
bcm2835-v4l2
(Raspberry Pi の専用カメラをビデオデバイスとして認識させる)

$ python qrTest.py
09CAB423-633D-1801-99CD-001C4DEAA59E
http://www.soft-village.com/
09CAB423-633D-1801-99CD-001C4DEAA59E
^CTraceback (most recent call last):
  File "qrTest.py", line 19, in <module>
    scanner.scan(image)
KeyboardInterrupt
(Python のプログラムで連続してQRコードを読み込ませた実行結果)

Python のコード

qrTest.py
# -*- coding: utf-8 -*-
import cv2
import zbar

scanner = zbar.ImageScanner()
scanner.parse_config('enable')
cap = cv2.VideoCapture(0)
captured = False
oldQr = ""
qr = ""

while True:

    ret, frame = cap.read()
    gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    rows, cols = gray_img.shape[:2]
    image = zbar.Image(cols, rows, 'Y800', gray_img.tostring())
    scanner.scan(image)

    for symbol in image:
        qr = str(symbol.data)

    if oldQr != qr:
       print qr
       oldQr = qr

    if cv2.waitKey(1) == 27:
        break

cap.release()

参照情報

QR Code Scanner compatible for RPi 3 ( https://lb.raspberrypi.org/forums/viewtopic.php?t=178411#p1173647 )
Raspberry Pi のカメラモジュールの使い方 ( https://www.mztn.org/rpi/rpi23.html )
OpenCVとZbarでバーコード・QRコード認識(Python) ( https://qiita.com/Oside/items/67372ca26f2d9ff77458 )
Python zbar.ImageScanner() Examples ( https://www.programcreek.com/python/example/98487/zbar.ImageScanner )

16
17
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
16
17