¿Cómo puedo leer los códigos qr utilizando CV2 dado su CV2 la ruptura de tuplas?

0

Pregunta

Yo estoy siguiendo un tutorial para conseguir un lector de códigos qr de trabajo en python, pero me estoy quedando en la siguiente error al ejecutarlo:

Ha producido la excepción: error OpenCV(4.5.4) :-1: error: (-5:Bad argument) en función de la 'línea' Resolución de sobrecarga error:

  • No se puede analizar 'pt1'. Secuencia del elemento con índice 0 tiene un mal tipo
  • No se puede analizar 'pt1'. Secuencia del elemento con índice 0 tiene un mal tipo Archivo "C:\Users\me\project\qrreader.py" la línea 18, en cv2.línea(img, tupla(bbox[i][0]), tupla(bbox[(i+1) % len(bbox)][0]), de color=(255,

El script es el siguiente

import cv2

# set up camera object
cap = cv2.VideoCapture(0)

# QR code detection object
detector = cv2.QRCodeDetector()

while True:
    # get the image
    _, img = cap.read()
    # get bounding box coords and data
    data, bbox, _ = detector.detectAndDecode(img)
    
    # if there is a bounding box, draw one, along with the data
    if(bbox is not None):
        for i in range(len(bbox)):
            cv2.line(img, tuple(bbox[i][0]), tuple(bbox[(i+1) % len(bbox)][0]), color=(255,
                     0, 255), thickness=2)
        cv2.putText(img, data, (int(bbox[0][0][0]), int(bbox[0][0][1]) - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, (0, 255, 0), 2)
        if data:
            print("data found: ", data)
    # display the image preview
    cv2.imshow("code detector", img)
    if(cv2.waitKey(1) == ord("q")):
        break
# free camera object and exit

Este script está en todos los tutoriales que hay, aparentemente, pero parece que han roto con opencv 4.5.2 cambios como lo que yo puedo decir, pero me parece que no puede arreglarlo.

Si no es una tupla, lo que hace la función de línea requieren?

computer-vision cv2 opencv python
2021-11-22 20:07:52
1

Mejor respuesta

1

Su bbox es un 3-dimensiones de la matriz con la forma (1,4,2). Le sugiero que simplificarlo transformando a una matriz 2D. Echarlo a int, numpy matrices tienen el astype método. Por último, un tuple todavía se requiere por cv2.line, por lo tanto mantenga como está.

He aquí una posible solución fragmento:

    # if there is a bounding box, draw one, along with the data
    if bbox is not None:
        bb_pts = bbox.astype(int).reshape(-1, 2)
        num_bb_pts = len(bb_pts)
        for i in range(num_bb_pts):
            cv2.line(img,
                     tuple(bb_pts[i]),
                     tuple(bb_pts[(i+1) % num_bb_pts]),
                     color=(255, 0, 255), thickness=2)
        cv2.putText(img, data,
                    (bb_pts[0][0], bb_pts[0][1] - 10),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.5, (0, 255, 0), 2)

Numpy documentación: remodelar, astype.

2021-11-23 13:25:33

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................