Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?
@0uts1de

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

More than 1 year has passed since last update.

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)

6
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0uts1de

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
6
Help us understand the problem. What is going on with this article?