1
6

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.

OpenCVとZbarでバーコード・QRコード認識(Python)

Last updated at Posted at 2018-08-22

#OpenCVとZbarでバーコード・QRコード認識(Python)

自分用の備忘録兼バックアップ的なもの


##環境

  • OS : Ubuntu16.04LTS
  • IDE : PyCharm Community Edition
  • Python : 2.7

##Pythonライブラリ
覚えてる分だけ

  • Pillow
  • zbar
  • opencv

ライブラリのインストール時の問題はStack Overflowにお世話になった


##Zbar導入

pipでインストールしようとしてもエラーを吐く

sudo apt install python-zbar

これでインストールできる


##ソース

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

scanner = zbar.ImageScanner()

scanner.parse_config('enable')

cap = cv2.VideoCapture(0)

captured = False

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)

    cv2.imshow("frame", gray_img)

    for symbol in image:
        #print('%s' % symbol.data)
        f = open('barcode.txt', 'w')
        f.write('%s' % symbol.data)
        f.close()
        captured = True

    if captured:
        break

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

cap.release()

cv2.destroyAllWindows()

##結果

barcode.txt
4909411076634

一番手こずったのは環境構築(Zbar)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?