GithubHelp home page GithubHelp logo

eric-canas / qreader Goto Github PK

View Code? Open in Web Editor NEW
120.0 3.0 10.0 39.73 MB

Robust and Straight-Forward solution for reading difficult and tricky QR codes within images in Python. Powered by YOLOv8

License: MIT License

Python 80.35% Jupyter Notebook 19.65%
computer-vision easy-to-use image-processing qrcode qrcode-reader qrcode-scanner object-detection python qr pip

qreader's Introduction

Hi there, I'm Eric Cañas

GitHub Linkedin StackOverflow GoogleScholar Gmail ShutterStock Sponsors

I'm a Computer Science and Computer Vision Engineer who specially loves to develop Open Source libraries for Python and JavaScript during my leisure time. I have special enthusiasm for Computer Vision problems and High Performance Computing. Lately, I'm becoming very interested in Web Applications.

From 2018 to 2021 I worked as a Researcher at the Computer Vision Center. Developing tools for Analysis and Dissection on CNNs and Deep Learning Super-Resolution for Microscopy (MSIM). During 2021, I moved to the Department of Applied Physics at the University of Barcelona, where besides Deep Learning I worked writing C++ DLLs with OpenMP. From 2022, I started to work as Freelance Computer Vision Engineer for Early-Stage Start Ups. I'm trying to build something great.

I studied a Computer Sciences Bachelor's Degree, where I ranked 1st of my promotion, and a Master's Degree on Artificial Intelligence.

I always develop my libraries under MIT License. Openly accessible for everyone's benefit.

Feel free to check my Portfolio.

🗂️ Highlighted Projects

💻 Language 🔬 Principal Field 🚀 Projects
Python Data Gathering, Data Science, Deep Learning & Crawling Social-Media-Crawler
Machine Learning, Sockets & Robotics Baby-Robot
Computer Vision Libraries Pic2World QReader QRDet
Libraries USBMonitor CircularDict QuadrilateralFitter
Utilities JPEG-MetaFilter
JavaScript Machine Learning for Web & Tensorflow.js Drums-app Makeup-Hub
Computer Vision Libraries Homography.js
React.js & Serverless Websites My Portfolio

🛠️ My favorite tools

Languages

Frameworks & Libraries

Databases

Web Technologies

Bootstrap Sass Node.js Gatsby.js Contentful

Others

📝 Publications

[1] E. Canas, A. M. García, A. Garrell and C. Angulo. Initial Test of "BabyRobot" Behaviour on a Teleoperated Toy Substitution: Improving the Motor Skills of Toddlers. Proceedings of the 2022 ACM/IEEE International Conference on Human-Robot Interaction. pp. 708–712. Sapporo, Hokkaido, Japan. March 2022.

[2] F. Lumbreras, R. Bola, E. Canas, S. Lumbreras, E. Martín-Badosa and M. Montes-Usategui. SCORED: Super-resolution Confocal Microscopy Enhanced by Deep-Learning. ATTRACT Final Conference. September 2020.

📺 On the Media

  1. 📰 An Interview about Baby Robot was featured in TechXplore, and echoed in Spanish, French, Dutch, Armenian, Polish, Swedish, Chinese and Russian magazines. It positioned the preprint as the most discussed paper about Human-Computer Interaction of the week in the news.
  2. ▶️ Drums-app was featured on the TensorFlow.js Community "Show & Tell" #9

🐼 About me

  1. 💻 I use to code in Python, but since I discovered TensorFlow.js I'm very interested in building web tools.
  2. ❣️ I love Pytorch, not only for Deep Learning purposes, but as the ultra-doped version of NumPy. It's like having the power of CUDA but in 5 minutes of coding.
  3. 🤖 I dream with building a system for automating every household task that is not yet automated.

:octocat: How I arrived here

  1. 🔍 Before to discover how amazing Computer Sciences were [2015] I studied Plastic Arts [2011-2013], and Scenic Arts [2013-2015]. My life changed just the day I discovered a Java tutorial.
    1. 📚 Three months later, I decided to learn on my own all the mathematical background I lacked, and to enroll on a Computer Sciences Bachelor's Degree. This is the right day I became a truly nerd 🤓.
  2. 🏡 I have been working remotely since 2018.
  3. 🚋 In 2021 I decided to move from Barcelona to a quiet coastal town. Now I feel closer to nirvana. I'm not returning to a crowded city anymore 👋.

qreader's People

Contributors

eric-canas avatar michaelcurrie avatar scito avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

qreader's Issues

Micro QR support

I have tried running it on micro QRs but couldn't get it to decode.
Are you planning to add micro QR / ArUco / AprilTags support anytime soon?
Micro_QR_Example

detector = QReader()
img = cv2.cvtColor(cv2.imread('Micro_QR_Example.png'), cv2.COLOR_BGR2RGB)
QRs = detector.detect_and_decode(image=img)
for QR in QRs:
    print(QR)

returns None

Error loading Yolov7 weight

I have installed QReader using Python 3.8 on Ubuntu 20.04. When I run the test script using the example provided, I get a runtime error -
FileNotFoundError: [Errno 2] No such file or directory: '/test-example/venv/lib/python3.8/site-packages/.yolov7_qrdetl0nxf.tml'. Stack trace shows when attempt_load is called in model_utils.py which then calls attempt_download(w) in experimental.py which calls wget.download in google_utils.py.

Please help as this works really well on my development machine which is Windows 11 and Python 3.10 and does a neat job in detecting QR codes that OpenCV and PyZbar fail to do so.

Thanks!

Error executing example

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.8/site-packages/qreader.py", line 100, in detect_and_decode
    bboxes = self.detect(image=image)
  File "/home/user/.local/lib/python3.8/site-packages/qreader.py", line 39, in detect
    detections = self.detector.detect(image=image, return_confidences=True, as_float=False)
  File "/home/user/.local/lib/python3.8/site-packages/qrdet.py", line 50, in detect
    dets = self.model.detect(image)
  File "/home/user/.local/lib/python3.8/site-packages/yolov7_package/model_utils.py", line 187, in detect
    img_inf = cv2.resize(img, self.img_size)
SystemError: new style getargs format but argument is not a tuple

I have this error pop up when I execute the example. I'm using torch 2.0, and the yolo7 the one it installed by default
Any ideas whats going on here?

Nice work

@Eric-Canas Hello, this is excellent work. I understand that you didn't use barcode data during training, which is not very helpful for my current project. My project involves detecting and decoding barcodes, so I was wondering if you could share your training code. I would greatly appreciate it! My email is [email protected].

TypeError due to exchanged bbox, found

Exception has occurred: TypeError
cannot unpack non-iterable bool object
File "QReader/qreader.py", line 71, in decode
x1, y1, x2, y2 = bbox
File "QReader/qreader.py", line 105, in detect_and_decode
return self.decode(image=image, bbox=bbox)
File "QReader/main_test.py", line 14, in
qreader_out = qreader_reader.detect_and_decode(img, False)

Reproduce with:

from qreader import QReader
import cv2
from pyzbar.pyzbar import decode

if __name__ == '__main__':
    # Initialize the three tested readers (QRReader, OpenCV and pyzbar)
    qreader_reader, cv2_reader, pyzbar_reader = QReader(), cv2.QRCodeDetector(), decode

    for img_path in (['documentation/resources/logo_square.png']):
        # Read the image
        img = cv2.imread(img_path)

        # Try to decode the QR code with the three readers
        qreader_out = qreader_reader.detect_and_decode(img, False)
        cv2_out = cv2_reader.detectAndDecode(img=img)[0]
        pyzbar_out = pyzbar_reader(image=img)
        # Read the content of the pyzbar output
        pyzbar_out = pyzbar_out[0].data.decode('utf-8') if len(pyzbar_out) > 0 else ""

        # Print the results
        print(f"Image: {img_path} -> QReader: {qreader_out}. OpenCV: {cv2_out}. pyzbar: {pyzbar_out}.")

ImportError due to possible circular import

    from qreader import QReader
ImportError: cannot import name 'QReader' from partially initialized module 'qreader' (most likely due to a circular import)

I am using the example code on an example image:

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("./image.png"), cv2.COLOR_BGR2RGB)

# Use the detect_and_decode function to get the decoded QR data
decoded_text = qreader.detect_and_decode(image=image)

TypeError: slice indices must be integers or None or have an __index__ method

Hello.

Always getting the following error:

in __deep_detect_and_decode_with_resize
cropped_img = image[y1:y2, x1:x2]
TypeError: slice indices must be integers or None or have an index method

with the following code:

from qreader import QReader
import cv2


qreader = QReader()

image = cv2.imread("test_mobile.jpeg")

decoded_text = qreader.detect_and_decode(image=image)

If i turn off deep_search, the error goes away.

Problem with loading weights

Using the example code in the readme.md on my workstation has shown that QReader succeeds where other python codes fail (congrats on that!). However, when moving the same small code into a venv on the server where it is meant to be included into a django project (and where hence django is installed), it fails by suddenly landing in django-land (after line 1124 in serialization.py) although django is not invoked in the example code.

This is confusing and kind of above my pay grade...

Traceback (most recent call last):
  File "/webapps/roggu/roggu/rogguapp/q1.py", line 15, in <module>
    detector = QReader()
  File "/webapps/roggu/lib/python3.10/site-packages/qreader.py", line 26, in __init__
    self.detector = QRDetector()
  File "/webapps/roggu/lib/python3.10/site-packages/qrdet.py", line 28, in __init__
    self.model = Yolov7Detector(weights=_WEIGHTS_PATH, img_size=None, agnostic_nms=True, traced=False)
  File "/webapps/roggu/lib/python3.10/site-packages/yolov7_package/model_utils.py", line 147, in __init__
    self.model = attempt_load([weights], map_location=self.device)  # load FP32 model
  File "/webapps/roggu/lib/python3.10/site-packages/yolov7_package/models/experimental.py", line 242, in attempt_load
    ckpt = torch.load(w, map_location=map_location)  # load
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 789, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 1131, in _load
    result = unpickler.load()
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 1124, in find_class
    return super().find_class(mod_name, name)
  File "/webapps/roggu/roggu/rogguapp/models.py", line 3, in <module>
    class Beleg(models.Model):
  File "/webapps/roggu/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/webapps/roggu/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/webapps/roggu/lib/python3.10/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/webapps/roggu/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/webapps/roggu/lib/python3.10/site-packages/django/conf/__init__.py", line 72, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Downloading weights library in real time

This is a issue while using AWS lambda as it doesn't allow write permissions on /var/lang/lib/python3.11/site-packages/qrdet/.model'. Can we change the directory of download to a custom path? Like i want to change it to /tmp
[Errno 30] Read-only file system: '/var/lang/lib/python3.11/site-packages/qrdet/.model'

Unable to use pyinstaller with QReader

Hi, I found QReader to be the best solution to catch hard to read QR codes, now I can't live without it.
The problem is I tried tu use pyinstaller with it but no success. I use Python 3.9 on Windows 10. My example code is:

main.py


import cv2
import qreader
imgread = cv2.imread('imgqr.png')
det = qreader.QReader().detect_and_decode(image=imgread)
print(det)

It works with no issues inside PyCharm, but when I try to run the exe made with pyinstaller I get this:

C:\Users\Filip\PycharmProjects\testpyinstallqr\dist\main>main.exe
torchvision\io\image.py:13: UserWarning: Failed to load image Python extension:
torch\_jit_internal.py:839: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x000001EEE768F550>.
  warnings.warn(
torch\_jit_internal.py:839: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x000001EEE768F790>.
  warnings.warn(
Downloading weights...: 100%|████████████████████████████████| 74.8M/74.8M [00:08<00:00, 8.94MiB/s]
Traceback (most recent call last):
  File "main.py", line 14, in <module>
  File "qreader.py", line 26, in __init__
  File "qrdet.py", line 27, in __init__
  File "yolov7_package\model_utils.py", line 147, in __init__
  File "yolov7_package\models\experimental.py", line 242, in attempt_load
  File "torch\serialization.py", line 789, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "torch\serialization.py", line 1131, in _load
    result = unpickler.load()
  File "torch\serialization.py", line 1124, in find_class
    return super().find_class(mod_name, name)
ModuleNotFoundError: No module named 'models'
[1940] Failed to execute script 'main' due to unhandled exception!

I'm a beginner so every help would be greatly appreciated!

Additional Transformation to Increase QR Detection Rate

Hi,

This is very cool work. I'd like to suggest a few additional transformations that you could add to increase the QR decoding rate.

I used your code, and it failed to decode after detecting the QR area. Below are some good transformations that I empirically validated (with dozens of QR codes) that helped increase decoding rate. You can consider adding them to the decode function in https://github.com/Eric-Canas/QReader/blob/main/qreader.py. If you think they add too much overhead, feel free to add a switch for them (i.e., only turn them on upon setting a flag).

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
advanced_gray = cv2.cvtColor(cv2.filter2D(src=image, ddepth=-1, kernel=_SHARPEN_KERNEL), cv2.COLOR_RGB2GRAY)
_, thresholded_gray = cv2.threshold(gray, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU)
_, thresholded_advanced_gray = cv2.threshold(advanced_gray, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
blurred1 = cv2.GaussianBlur(advanced_gray, (3, 3), 0)
blurred2 = cv2.GaussianBlur(gray, (5, 5), 0)
blurred3 = cv2.GaussianBlur(gray, (7, 7), 0)

Also, running decode on all of the variations helped.

Error decode UTF-8 character 'â'

I have a problem when I try using pyzbar to decode a QR image. But I had given result don't match data which I using qrcode make before.
this is my code:

from qreader import QReader
from PIL import Image
import qrcode

image_path = "my_image.png"
data = 'â'
print(f'data = {data}')
img = qrcode.make(data)

img.save(image_path)
img = cv2.imread(image_path)
result = qreader.detect_and_decode(image=img)
print(f"result = {result[0]}")

creating QReader instance produces debugging messages on sdout

This code:

import qreader
qr = qreader.QReader()

produces this output:

Fusing layers...
IDetect.fuse

In a production environment these messages can be really annoying since they don't actually follow the proper logging paradigm, so they'll stick out like a sore thumb. To suppress them I have to do this:

class StdoutSuppressor:
    def __enter__(self):
        self.stdout = sys.stdout
        sys.stdout = self

    def __exit__(self, exception_type, value, traceback):
        sys.stdout = self.stdout
        if exception_type is not None:
            # Do normal exception handling
            raise Exception(f"Got exception: {exception_type} {value} {traceback}")

    def write(self, x):
        pass

import qreader
with StdoutSuppressor:
    qr = qreader.QReader()

Better is if these statements were actually coming from a logging DEBUG streamer which could be disabled via:

import logging
logging.getLogger("qreader").setLevel(logging.INFO)

Thanks!

Improvements in the code

In the latest version, the "QReader.decode" function has changed, now the presence of "detection_result" is required, can it be done as before to only serve the image as input? I have a linear algorithm for finding squares that may contain a QR code and this algorithm works faster than YoloV8 without a GPU

FileNotFoundError: .yolov7_qrdetdo7bgp8r.tmp

Traceback (most recent call last):
File "/home/shubankar/Desktop/barcode/undetected.py", line 4, in
qreader=QReader()
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/qreader.py", line 26, in init
self.detector = QRDetector()
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/qrdet.py", line 27, in init
self.model = Yolov7Detector(weights=_WEIGHTS_PATH, img_size=(640, 640), agnostic_nms=True, traced=False)
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/yolov7_package/model_utils.py", line 147, in init
self.model = attempt_load([weights], map_location=self.device) # load FP32 model
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/yolov7_package/models/experimental.py", line 241, in attempt_load
attempt_download(w)
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/yolov7_package/utils/google_utils.py", line 26, in attempt_download
wget.download(f'https://github.com/WongKinYiu/yolov7/releases/download/v0.1/{file.name}', out=str(file.parent))
File "/home/shubankar/Desktop/envdori/lib/python3.10/site-packages/wget.py", line 506, in download
(fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
File "/usr/lib/python3.10/tempfile.py", line 480, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
File "/usr/lib/python3.10/tempfile.py", line 395, in _mkstemp_inner
fd = _os.open(file, flags, 0o600)
FileNotFoundError: [Errno 2] No such file or directory: '/home/shubankar/desktop/envdori/lib/python3.10/site-packages/.yolov7_qrdetdo7bgp8r.tmp'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.