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

Got error when try to use qreader! #36

Open
Phuc193057 opened this issue Aug 1, 2024 · 18 comments
Open

Got error when try to use qreader! #36

Phuc193057 opened this issue Aug 1, 2024 · 18 comments

Comments

@Phuc193057
Copy link

Hello! I'm trying to use qreader to detect qr code, but i got this error.

How can i fix this?
Screenshot 2024-08-01 110118

@MatteoOrlandini
Copy link

Same for me. I'm using Windows 11.

@Eric-Canas
Copy link
Owner

Hi, I have been testing it,

Seems related to an edge case in quadrilateral-fitter. Happening when QR is not correctly fitting to a quadrilateral (for providing the 4 corners). Could you provide your sample code and/or the image that produced you that error? It is getting difficult to replicate it.

@Phuc193057
Copy link
Author

Phuc193057 commented Aug 4, 2024

i used your sample code and your test image
test_mobile

@Eric-Canas
Copy link
Owner

Wow, that's quite weird :( Then, I don't know why I can't replicate it.

Bug is related with shapely interpreting some coords as a LineString instead of a Polygon. Could you provide which version of shapely do you have installed?

Alternatively, I'll upload a new version of quadrilateral-fitter that should solve this case, but I would like to replicate it to have more information about why does it happen.

I don't know if you are working on an IDE, but it would also get me a lot of information if you can put a breakpoint in code at _qrdet_helpers, line 50. And get me the value of accurate_polygon_xy

Screenshot 2024-08-04 at 10 31 39 Screenshot 2024-08-04 at 10 34 06

If you can't arrive to this value is fine. Sorry for the inconvenience.

@Phuc193057
Copy link
Author

i'm using visual sudio code, when tried detect by qrdet, i got same error, qrdet version is 2.5. but i noticed that when i use qrdet ver 1.4, it worked.
Screenshot 2024-08-04 190635
Screenshot 2024-08-04 190743

@Eric-Canas
Copy link
Owner

Hi!

Yes, it makes sense as quadrilateral-fitting was introduced in 2.0.

Case is that, by some reason a call like shapely.Polygon(coords) is returning a LineString instead of a Polygon. But doesn't matter what kind of coords I try, I'm not able to replicate it to trace the error and introduce a fallback :(

@Phuc193057
Copy link
Author

Hi!
I tried run your code example.ipynb in google colab and got this result.
Screenshot 2024-08-06 124640
Screenshot 2024-08-06 124608
Screenshot 2024-08-06 124622

@Eric-Canas
Copy link
Owner

Eric-Canas commented Aug 6, 2024

Hi!

Yes, I also tried it in the way to detect the error. But in Colab everything seems to be working ok, the same as my system :( Not breaking by the polygon being interpreted as a LineString.

What I would need to know is why that's not happening in your systems :(
I can't find what's the difference :_(

It must be something related with shapely library. Maybe, also with the segmentation that the yolov8 model is retrieving that is used as input for shapely.Polygon

@RaoufiTech
Copy link

I am having this error:
Traceback (most recent call last): File "C:\Users\Said\vscode\lpo.py", line 12, in <module> decoded_text = qreader.detect_and_decode(image=image) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\qreader.py", line 215, in detect_and_decode detections = self.detect(image=image, is_bgr=is_bgr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\qreader.py", line 147, in detect return self.detector.detect(image=image, is_bgr=is_bgr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\qrdet\qrdet.py", line 84, in detect results = _yolo_v8_results_to_dict(results = results[0], image=image) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\qrdet\_qrdet_helpers.py", line 51, in _yolo_v8_results_to_dict quadrilateral_xy = _quadrilateral_fit.fit(simplify_polygons_larger_than=8, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 81, in fit self._initial_guess = self.__find_initial_quadrilateral(max_sides_to_simplify=simplify_polygons_larger_than, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 120, in __find_initial_quadrilateral simplified_polygon = self.__simplify_polygon(polygon=self.convex_hull_polygon, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\vscode\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 317, in __simplify_polygon if max_sides is None or len(polygon.exterior.coords) - 1 <= max_sides: ^^^^^^^^^^^^^^^^ AttributeError: 'GeometryCollection' object has no attribute 'exterior' (.venv) PS C:\Users\Said\vscode>

image:
image

@RaoufiTech
Copy link

Hello
@Eric-Canas When can you fix the issues bro? It doesnt work, i tried older versions, different python versions but I am still getting errors.

@Eric-Canas
Copy link
Owner

Hi, sorry!
I was a bit blocked here because I was unable to reproduce the error.

I have uploaded a solution that should work anyway. Combining what I could deduce from your logs and some solution for repairing unexpected LineStrings and your solution with GeometryCollection.

Could you try to:
pip install --upgrade quadrilateral-fitter ?

Thanks!

@RaoufiTech
Copy link

RaoufiTech commented Aug 24, 2024

Hi
I will test it again, could you try it with those images?
After testing:
PS C:\Users\Said\vscode\src> pip install --upgrade quadrilateral-fitter
Requirement already satisfied: quadrilateral-fitter in c:\users\said\vscode.venv\lib\site-packages (1.9)
Requirement already satisfied: shapely in c:\users\said\vscode.venv\lib\site-packages (from quadrilateral-fitter) (2.0.6)
Requirement already satisfied: numpy in c:\users\said\vscode.venv\lib\site-packages (from quadrilateral-fitter) (1.26.4)
Requirement already satisfied: scipy in c:\users\said\vscode.venv\lib\site-packages (from quadrilateral-fitter) (1.14.1)
PS C:\Users\Said\vscode\src> python test.py
Traceback (most recent call last):
File "C:\Users\Said\vscode\src\test.py", line 12, in
decoded_text = qreader.detect_and_decode(image=image)
File "C:\Users\Said\vscode.venv\lib\site-packages\qreader.py", line 215, in detect_and_decode
detections = self.detect(image=image, is_bgr=is_bgr)
File "C:\Users\Said\vscode.venv\lib\site-packages\qreader.py", line 147, in detect
return self.detector.detect(image=image, is_bgr=is_bgr)
File "C:\Users\Said\vscode.venv\lib\site-packages\qrdet\qrdet.py", line 84, in detect
results = _yolo_v8_results_to_dict(results = results[0], image=image)
File "C:\Users\Said\vscode.venv\lib\site-packages\qrdet_qrdet_helpers.py", line 51, in _yolo_v8_results_to_dict
quadrilateral_xy = _quadrilateral_fit.fit(simplify_polygons_larger_than=8,
File "C:\Users\Said\vscode.venv\lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 104, in fit
self._initial_guess = self.__find_initial_quadrilateral(max_sides_to_simplify=simplify_polygons_larger_than,
File "C:\Users\Said\vscode.venv\lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 143, in __find_initial_quadrilateral
simplified_polygon = self.__simplify_polygon(polygon=self.convex_hull_polygon,
File "C:\Users\Said\vscode.venv\lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 343, in __simplify_polygon
elif isinstance(polygon, self.GeometryCollection):
AttributeError: 'QuadrilateralFitter' object has no attribute 'GeometryCollection'
PS C:\Users\Said\vscode\src>

It is still giving that error.

![2](https://github.com/
barry-business-card-qr-code

bsq
user-attachments/a
712e316588ff0b68a333e4c183f89eb7
ssets/70c6f15e-462c-401c-8c3b-f89c92ec8a33)
2d6dc35caa6ab032d1d9042b951ddd0c
8d5276b768748e2dd930a5726c55477b
0d472e117e6da003a1d5b531561b60a8

@Eric-Canas
Copy link
Owner

Hi!

I tested them in the notebook and seems to work

Screenshot 2024-08-25 at 10 08 49 Screenshot 2024-08-25 at 10 14 27 Screenshot 2024-08-25 at 10 16 42

Could you try it again with the new quadrilateral_fitter 1.10?

@RaoufiTech
Copy link

RaoufiTech commented Aug 25, 2024

Hi
I tested it with the latest version and now I am getting a new type of error:
PS C:\Users\Said\vscode\src> & c:/Users/Said/vscode/.venv/Scripts/python.exe c:/Users/Said/vscode/src/tester.py
Traceback (most recent call last):
File "c:\Users\Said\vscode\src\tester.py", line 12, in
decoded_text = qreader.detect_and_decode(image=image)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\qreader.py", line 215, in detect_and_decode
detections = self.detect(image=image, is_bgr=is_bgr)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\qreader.py", line 147, in detect
return self.detector.detect(image=image, is_bgr=is_bgr)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\qrdet\qrdet.py", line 84, in detect
results = _yolo_v8_results_to_dict(results = results[0], image=image)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\qrdet_qrdet_helpers.py", line 51, in _yolo_v8_results_to_dict
quadrilateral_xy = _quadrilateral_fit.fit(simplify_polygons_larger_than=8,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 105, in fit
self._initial_guess = self.__find_initial_quadrilateral(max_sides_to_simplify=simplify_polygons_larger_than,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 144, in __find_initial_quadrilateral
simplified_polygon = self.__simplify_polygon(polygon=self.convex_hull_polygon,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Said\vscode.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 350, in __simplify_polygon
raise TypeError("Expected Polygon or GeometryCollection, got {type(polygon)}.")
TypeError: Expected Polygon or GeometryCollection, got {type(polygon)}.
PS C:\Users\Said\vscode\src>

Code:
from qreader import QReader
import cv2


# Create a QReader instance
qreader = QReader()

# Get the image that contains the QR code
image = cv2.cvtColor(cv2.imread("images/businesscard-02.png"), cv2.COLOR_BGR2RGB)

# Use the detect_and_decodC:\Users\Said\vscode\src\images\businesscard-02.pnge function to get the decoded QR data
decoded_text = qreader.detect_and_decode(image=image)
print(decoded_text)

businesscard-02.png:
image

btw, i have a question about YOLO, is it possible to fine-tune yolo to be able to detect edge/boundary of a document? I tried using opencv and Canny but they have a difficult time trying to detect the edges when the background color is the same as the document.

@Eric-Canas
Copy link
Owner

Hi,
ok, I have prepared a debug version for you. So let's see If we can find where is your problem coming from.

Please, upgrade quadrilateral-fitter to 1.11, and run some images. It will print all the information about your polygon, so we will see whats wrong with it.

Finetunning YOLO for edge detection can be a bit tricky. You should tag a dataset of edges, and finetune a segmentation model. Then use a similar approach of what I'm doing with quadrilateral-fitter for transforming from a noisy segmentation mask to 4 points

@RaoufiTech
Copy link

RaoufiTech commented Aug 26, 2024

Hi
I tested it again and those are the logs:
(.venv) PS C:\Users\Said\debug> python qrcodereader.py Polygon: LINESTRING (303.515625 0, 365.0390625 0) Polygon Type <class 'shapely.geometry.linestring.LineString'> Polygon Bounds: (303.515625, 0.0, 365.0390625, 0.0) Polygon Centroid: POINT (334.27734375 0) Polygon Convex Hull: LINESTRING (303.515625 0, 365.0390625 0) Polygon is valid: True Traceback (most recent call last): File "C:\Users\Said\debug\qrcodereader.py", line 12, in <module> decoded_text = qreader.detect_and_decode(image=image) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\qreader.py", line 215, in detect_and_decode detections = self.detect(image=image, is_bgr=is_bgr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\qreader.py", line 147, in detect return self.detector.detect(image=image, is_bgr=is_bgr) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\qrdet\qrdet.py", line 84, in detect results = _yolo_v8_results_to_dict(results = results[0], image=image) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\qrdet\_qrdet_helpers.py", line 51, in _yolo_v8_results_to_dict quadrilateral_xy = _quadrilateral_fit.fit(simplify_polygons_larger_than=8, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 105, in fit self._initial_guess = self.__find_initial_quadrilateral(max_sides_to_simplify=simplify_polygons_larger_than, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 144, in __find_initial_quadrilateral simplified_polygon = self.__simplify_polygon(polygon=self.convex_hull_polygon, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Said\debug\.venv\Lib\site-packages\quadrilateral_fitter\quadrilateral_fitter.py", line 357, in __simplify_polygon print(f"Polygon Vertices: {polygon.exterior.coords}") ^^^^^^^^^^^^^^^^ AttributeError: 'LineString' object has no attribute 'exterior' (.venv) PS C:\Users\Said\debug>

image

About the YOLO model, what if I fine tune it for detecting a document instead of it's edges? Will that be a better approach?

@Eric-Canas
Copy link
Owner

Hi!

I uploaded a patch it will likely work, but I hope you don't see this warning:
"Convex Hull of the polygon seems to be a LineString, trying to convert it to a Polygon."

Could you try it with new quadrilateral-fitter==1.12 and show me the logs?

About detection vs segmentation, detection is easier to tag, and will give you always 4 points. But will not remove the background if the document is rotated and will not give you rotation info. If that's fine for you, yes, it will be easier.

@RaoufiTech
Copy link

Hi
It didnt give an error this time but it is failing to detect the qr code even tho the qr code was in a clear image and it should have detected it, and also is there a way to speed up the inference time?:
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants