'I'm trying to create a qr code scanner through a webcam

In a training project (web application) I want to implement a function for reading a qr code through a webcam. In open sources, I found the code just for this case, but I ran into an error that I can’t overcome ... maybe someone can tell me what to do? thanks in advance

upd: webcam not starts working at all. I click a button in my application and immediately get a 500 error

            # set up camera object
            cap = cv2.VideoCapture(0)
            # инициализируем детектор QRCode cv2
            detector = cv2.QRCodeDetector()
            while True:
                _, img = cap.read()
                # обнаружить и декодировать
                data, bbox, _ = detector.detectAndDecode(img)
                # проверяем, есть ли на изображении QRCode
                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, 0), thickness=2)
                    if data:
                        print("[+] QR Code detected, data:", data)
                # отобразить результат
                cv2.imshow("img", img)
                if cv2.waitKey(1) == ord("q"):
                    break
            cap.release()
            cv2.destroyAllWindows() 

Mistake:

    data, bbox, _ = detector.detectAndDecode(img)
cv2.error: OpenCV(4.5.5) /io/opencv/modules/objdetect/src/qrcode.cpp:29: error: (-215:Assertion failed) !img.empty() in function 'checkQRInputImage'


Solution 1:[1]

There is a similar error here: CV2 Image Error: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

You should check if the img you get actually exist, because the function throwing your error return a none value instead of the expected array if the image does not exist.

if img is None:
    continue

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LittlePanic404