获取错误:创建qr scann时,“tuple”对象没有属性width

2024-05-20 22:56:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道,答案是有的,但我对python还不熟悉,不知道如何解决这个问题。你知道吗

我正在创建二维码扫描仪,并从以下位置复制代码: https://pastebin.com/C4r2uNCC

我得到了这些错误。 我插上了网络摄像头。这不是问题所在。你知道吗

[ 2076.921537] usb 1-4: Product: USB2.0 Camera
[ 2077.018998] USB Video Class driver (1.1.1)

我的代码:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Author : Jimmyromanticdevil
# QRbar-cv
#
# Proc of consept :
# this is just simple code from part of my work for QRcode & Barcode scanner with webcame stuff
# i am using Opencv for realtime track the image & zbar for decode the image
#
# Dependency :
#
#
# Some of Good Refrensi stuff :
#   https://github.com/jayrambhia/Install-OpenCV
#   http://nwlinux.com/install-qtqr-in-ubuntu-10-04-lucid-using-apt-get/
#   http://zbar.sourceforge.net/

import cv2 as cv  # Use OpenCV-2.4.3
import zbar


def scanner_procces(frame, set_zbar):
    set_width = 100.0 / 100
    set_height = 90.0 / 100

    coord_x = int(frame.width * (1 - set_width) / 2)
    coord_y = int(frame.height * (1 - set_height) / 2)
    width = int(frame.width * set_width)
    height = int(frame.height * set_height)

    get_sub = cv.GetSubRect(frame, (coord_x + 1, coord_y + 1, width - 1, height - 1))

    cv.Rectangle(frame, (coord_x, coord_y), (coord_x + width, coord_y + height), (255, 0, 0))

    cm_im = cv.CreateImage((get_sub.width, get_sub.height), cv.IPL_DEPTH_8U, 1)
    cv.ConvertImage(get_sub, cm_im)
    image = zbar.Image(cm_im.width, cm_im.height, 'Y800', cm_im.tostring())

    set_zbar.scan(image)
    for symbol in image:
        print()
        '\033[1;32mResult : %s symbol "%s" \033[1;m' % (symbol.type, symbol.data)

    cv.ShowImage("QR Koodi Skanneri", frame)
    # cv.ShowImage("webcame2", get_sub)
    cv.WaitKey(10)


if __name__ == "__main__":
    # set up our stuff
    cv.namedWindow("QR Koodi Scanneri", cv.WINDOW_AUTOSIZE)
    capture = cv.VideoCapture(-1)
    set_zbar = zbar.ImageScanner()
    while True:
        frame = capture.read()
        scanner_procces(frame, set_zbar)

和日志:

Traceback (most recent call last):
  File "kassakone_scanner.py", line 56, in <module>
    scanner_procces(frame, set_zbar)
  File "kassakone_scanner.py", line 26, in scanner_procces
    coord_x = int(frame.width * (1 - set_width) / 2)
AttributeError: 'tuple' object has no attribute 'width'

Tags: inimageforgetcmwidthframecv