2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jetson nanoでQRコードを読み取るのにzbarlightを使ってみた

Last updated at Posted at 2019-09-12

この記事について

QRコードを認識するのにpyzbarを使うより、zbarlightを使ったほうが速いというお話

zbarlightのインストール

bash
$ apt-get install libzbar0 libzbar-dev
$ pip install zbarlight  # you can also use setuptools directly

zbarlightより速いfastzbarlightというものもあるそうですが、jetson nanoで何故かエラーが出てしまったので諦めました。

opencvでカメラを読み込む

opencv
GST_STR = 'nvarguscamerasrc \
    ! video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)30/1 \
    ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx \
    ! videoconvert \
    ! appsink'
cap = cv2.VideoCapture(GST_STR, cv2.CAP_GSTREAMER)

これでpicameraを使うことができます。

opencvで読み込んだ画像をzbarで用いる

opencv
import cv2
import zbarlight
from PIL import Image

GST_STR = 'nvarguscamerasrc \
    ! video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)30/1 \
    ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx \
    ! videoconvert \
    ! appsink'
cap = cv2.VideoCapture(GST_STR, cv2.CAP_GSTREAMER)

while True:
    ret, img = cap.read()
    if ret != True:
        break
    im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    PIL_data = Image.fromarray(im_rgb)
    codes = zbarlight.scan_codes('qrcode',PIL_data)
    print(f'QR codes: {codes}')

cap.release()

zbarlightはPILで使われている画像データ形式を用いるので、opencv -> PILの変換をする必要があります。
opencvはBGRなので、RGBにしてから変換します。

まとめ

pyzbarよりもzbarlightのほうが速いのでzbarlightを使おう。(fastzbarlight入れられた人は教えてください。)

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?