简体   繁体   English

Python - Opencv 读取 qr 图像

[英]Python - Opencv to read qr images

I am using the following program to detect a qr code within an image (there may be other noise in it)我正在使用以下程序来检测图像中的二维码(其中可能存在其他噪声)

    image = cv2.imread('/content/Screen Shot 2021-11-10 at 3.30.16 PM.png')
 
qrCodeDetector = cv2.QRCodeDetector()
 
decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
 
if points is not None:
 
    nrOfPoints = len(points)
 
    for i in range(nrOfPoints):
        nextPointIndex = (i+1) % nrOfPoints
        cv2.line(image, tuple(points[i][0]), tuple(points[nextPointIndex][0]), (255,0,0), 5)
 
    print(decodedText)    
 
    cv2_imshow(image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
     
 
else:
    print("QR code not detected")

However, when I run this in jupyter notebook (change cv2_imshow to cv2.imshow, this runs on) I get the following error:但是,当我在 jupyter notebook 中运行它时(将 cv2_imshow 更改为 cv2.imshow,这会继续运行)我收到以下错误:

error: OpenCV(4.5.4-dev) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type

I am confused as to why I cannot run it on my computer in an ide like Jupiter or spyder.我很困惑为什么我不能在像 Jupiter 或 spyder 这样的 ide 中在我的计算机上运行它。 I updated multiple things like OpenCV yesterday.我昨天更新了很多东西,比如 OpenCV。 Additionally, It seems like this error shouldn't occur as the documentation shows that tuples are used.此外,似乎不应发生此错误,因为文档显示使用了元组。 Any suggestions?有什么建议? Thanks谢谢

You are getting an error: " index 0 has a wrong type ", because cv2.line expects integer type, and the type your are passing is float32 .您收到错误消息:“ index 0 has a wrong type ”,因为cv2.line需要整数类型,而您传递的类型是float32

points[i][0] is a NumPy array with dtype = float32 . points[i][0]是一个dtype = float32的 NumPy 数组。
You can convert the NumPy array type to int32 using astype :您可以使用astype将 NumPy 数组类型转换为int32

tuple(points[i][0].astype('int32'))

There other issues that I tried to fix...我试图解决的其他问题......

Here is the complete code:这是完整的代码:

import cv2

image = cv2.imread('qrcode_stackoverflow.com.png')

qrCodeDetector = cv2.QRCodeDetector()
 
decodedText, points, _ = qrCodeDetector.detectAndDecode(image)

# Remove redundant dimensions - points.shape is (1, 4, 2), and we want it to be (4, 2)
# The first dimension may be above 1 when there are multiple QR codes (not sure about it).
points = points[0]
 
if points is not None:
 
    #nrOfPoints = len(points)
    nrOfPoints = points.shape[0]
 
    for i in range(nrOfPoints):
        nextPointIndex = (i+1) % nrOfPoints
        cv2.line(image, tuple(points[i, :].astype('int32')), tuple(points[nextPointIndex, :].astype('int32')), (255,0,0), 5)
 
    print(decodedText)
 
    cv2.imshow('image', image) #cv2_imshow(image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()   

else:
    print("QR code not detected")

Input image " qrcode_stackoverflow.com.png ":输入图像“ qrcode_stackoverflow.com.png ”:
在此处输入图片说明

Output image:输出图像:
在此处输入图片说明

Output text:输出文本:
https://stackoverflow.com/questions/69934887/python-opencv-to-read-qr-images

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM