Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QR code detecting in python with OpenCV raises UnicodeDecodeError: 'utf-8' codec can't decode byte #23728

Closed
4 tasks done
MKraemer85 opened this issue Jun 2, 2023 · 6 comments · Fixed by #24350
Closed
4 tasks done
Assignees
Labels

Comments

@MKraemer85
Copy link

MKraemer85 commented Jun 2, 2023

System Information

OpenCV python version: 4.7.0
Operating System: Windows 10
Python Version: Python 3.10.5

Detailed description

Related to this Question:
https://stackoverflow.com/questions/76362026/qr-code-detecting-in-python-with-opencv-raises-unicodedecodeerror-utf-8-codec

As I'm trying to detect an qr code and the qr code values, I get this error:

Traceback (most recent call last):
  File "C:\Users\m7073\Repos\Chronos_New\invoice_extraction\qr_code_scan.py", line 128, in <module>
    qrcode.set_qr_values()
  File "C:\Users\m7073\Repos\Chronos_New\invoice_extraction\qr_code_scan.py", line 73, in set_qr_values
    text, points, straight_qrcode = detect.detectAndDecode(img)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 157: invalid start byte

Steps to reproduce

import cv2
img = cv2.imread(f"page_1.png")
detect = cv2.QRCodeDetector()
text, points, straight_qrcode = detect.detectAndDecode(img)

page_1

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)
@MKraemer85 MKraemer85 added the bug label Jun 2, 2023
@cpoerschke
Copy link
Contributor

Looking into this I enjoyed learning more about QR codes and python:

Code snippet:

try:
    text, points, straight_qrcode = detect.detectAndDecode(img)
except UnicodeDecodeError as ex:
    print(ex)
    print(ex.object)
    print(ex.object.decode('ISO-8859-1').encode('UTF-8'))

These two issues sound related:

@MKraemer85
Copy link
Author

Hi cpoerschke,
I will try that in like a week or so.
I'm ill for an week now and can't try it out right now, sadly.
But thanks for the suggestion, I will reply back when I can try that.

@MKraemer85
Copy link
Author

I've switched to zxingcpp and this way it works:

import cv2
img = cv2.imread(f"./qr_codes/page_1.png")
detect = cv2.QRCodeDetector()
for result in zxingcpp.read_barcodes(img):
print(result.text)

@flauer7
Copy link

flauer7 commented Jul 11, 2023

zxingcpp seems way more reliable in this usecase... thanks @MKraemer85

otherwise if you wish to stick with cv2,
try: data, bbox, rectifiedImage = cv2.QRCodeDetector().detectAndDecode(image) except UnicodeDecodeError as ex: data = ex.object.decode('ISO-8859-1')
In my case it was the letter "à" that generates a UnicodeDecodeError

@MKraemer85
Copy link
Author

a cool, good to know @flauer7 . Thanks for the input

@dkurt
Copy link
Member

dkurt commented Oct 4, 2023

Fixed by this PR: #24350

asmorkalov pushed a commit that referenced this issue Oct 12, 2023
Encode QR code data to UTF-8 #24350

### Pull Request Readiness Checklist

**Merge with extra**: opencv/opencv_extra#1105

resolves #23728

This is first PR in a series. Here we just return a raw Unicode. Later I will try expand QR codes decoding methods to use ECI assignment number and return a string with proper encoding, not only UTF-8 or raw unicode.

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
IskXCr pushed a commit to Haosonn/opencv that referenced this issue Dec 20, 2023
Encode QR code data to UTF-8 opencv#24350

### Pull Request Readiness Checklist

**Merge with extra**: opencv/opencv_extra#1105

resolves opencv#23728

This is first PR in a series. Here we just return a raw Unicode. Later I will try expand QR codes decoding methods to use ECI assignment number and return a string with proper encoding, not only UTF-8 or raw unicode.

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue Jan 4, 2024
Encode QR code data to UTF-8 opencv#24350

### Pull Request Readiness Checklist

**Merge with extra**: opencv/opencv_extra#1105

resolves opencv#23728

This is first PR in a series. Here we just return a raw Unicode. Later I will try expand QR codes decoding methods to use ECI assignment number and return a string with proper encoding, not only UTF-8 or raw unicode.

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue May 29, 2024
Encode QR code data to UTF-8 opencv#24350

### Pull Request Readiness Checklist

**Merge with extra**: opencv/opencv_extra#1105

resolves opencv#23728

This is first PR in a series. Here we just return a raw Unicode. Later I will try expand QR codes decoding methods to use ECI assignment number and return a string with proper encoding, not only UTF-8 or raw unicode.

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants