Extracting text from QR Codes

OpenMV related project discussion.
kaz
Posts: 3
Joined: Tue Feb 13, 2018 7:39 am

Extracting text from QR Codes

Postby kaz » Tue Feb 13, 2018 8:30 am

Hello there,

Can anyone help me with my simple project? I'm very new on the OpenMV M7. I just need to move a mini servo depending on the text read on the QR Code.

I've used the sample QR Code and the text on the QR is printed on the "payload". How can i extract the payload and use it as a variable to move the servo by a certain degrees?

Code: Select all

import sensor, image, time
from pyb import Servo

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color = (255, 0, 0))
        print(code)
    if (code == "LEFT"): 
    	s1 = pyb.Servo(1)
        s1.angle(45)
I'm pretty sure that the last if statement doesn't work. How do I proceed with this? Your help is greatly appreciated.
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Tue Feb 13, 2018 12:39 pm

Do:

Code: Select all

codes = img.find_qrcodes()
for code in codes:
        img.draw_rectangle(code.rect(), color = (255, 0, 0))
        print(code)
if codes:
    if code[0].payload == "LEFT":
        s1 = pyb.Servo(1)
        s1.angle(45)
This extracts the text for the first QR code seen.

The find QR codes method returns a list of objects, so, I put the list in a variable above, then look at the first object in the list and get the payload of it. I check to make sure the list is not empty first however.
Nyamekye,
kaz
Posts: 3
Joined: Tue Feb 13, 2018 7:39 am

Re: Extracting text from QR Codes

Postby kaz » Wed Feb 14, 2018 7:11 am

kwagyeman wrote:
Tue Feb 13, 2018 12:39 pm
Do:

Code: Select all

codes = img.find_qrcodes()
for code in codes:
        img.draw_rectangle(code.rect(), color = (255, 0, 0))
        print(code)
if codes:
    if code[0].payload == "LEFT":
        s1 = pyb.Servo(1)
        s1.angle(45)
This extracts the text for the first QR code seen.

The find QR codes method returns a list of objects, so, I put the list in a variable above, then look at the first object in the list and get the payload of it. I check to make sure the list is not empty first however.
Thanks for the help but I've come up into an error while trying the code you provided me. The error that appeared says "AttributeError: 'int' object has no attribute 'payload'.

Can you help me again? Here's the full code, and while it displays the error it highlights the line of the code: if code[0].payload == "LEFT":

Code: Select all

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.8)
    print(clock.fps())
    codes = img.find_qrcodes()
    for code in codes:
            img.draw_rectangle(code.rect(), color = (255, 0, 0))
            print(code)
    if codes:
        if code[0].payload == "LEFT":
            s1 = pyb.Servo(1)
            s1.angle(45)
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Wed Feb 14, 2018 11:59 am

Sorry, I meant .payload().

The parens are important.
Nyamekye,
kaz
Posts: 3
Joined: Tue Feb 13, 2018 7:39 am

Re: Extracting text from QR Codes

Postby kaz » Sun Feb 18, 2018 7:50 am

kwagyeman wrote:
Wed Feb 14, 2018 11:59 am
Sorry, I meant .payload().

The parens are important.
Thank you very much. I've tested it before going, it works as expected. One more thing, I've printed some QR codes but the OpenMV Cam doesn't immediately detect it unless on some certain conditions like when the background is white. Also can I use lists like the ones I've seen on online references to read multiple payloads?

like:

Code: Select all

my_list = ["LEFT", "RIGHT"]
and replace the if codes with:

Code: Select all

if code.payload() == "my_list"
will it not cause errors? I'm not at home currently for the next few days and I wanna know if my idea would be functional. Thanks again!
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Sun Feb 18, 2018 2:00 pm

Qr codes require the background to be white. The scanning algorithm looks for certain attributes about the area around a QR code before decoding them. In particular, you absolutely need to have enough of a white border around the codes or the algorithm ignores the codes.

As for your second question... You're asking how to compare against a list? This is a python question. Just a tip, if something seems like a core language problem google for it and the answer will be available on stack overflow. Just about anything you need to know about python has an answer out there. Anyway, do this:

Code: Select all

if code.payload() in my_list:
Yes, python has a method to do what you want built-in.
Nyamekye,
Seaha
Posts: 5
Joined: Tue Dec 24, 2019 9:23 am

Re: Extracting text from QR Codes

Postby Seaha » Tue Dec 24, 2019 10:34 am

Hello Friends

I am pretty new to this camera and have big problems to get this code running.
I am copy your Code and included the Parens but getting always the Error
AttributeError: 'int' object has no attribute 'payload'

I tryed to figure the error out for Hours but its not working

Code: Select all

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.8)
    print(clock.fps())
    codes = img.find_qrcodes()
    for code in codes:
            img.draw_rectangle(code.rect(), color = (255, 0, 0))
            print(code)
    if codes:
        if code[0].payload() == "LEFT":
            s1 = pyb.Servo(1)
            s1.angle(45)
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Tue Dec 24, 2019 11:01 am

It's codes[0] not code[0].

Accessing code[0] makes no sense. That variable isn't in scope at that time in your code. However, since MP doesn't garbage cleanup variables quickly you don't get an error about accessing an out of scope variable... But, you are doing that.
Nyamekye,
alexia30
Posts: 1
Joined: Fri Dec 27, 2019 5:42 am
Location: Amsterdam,Netherlands
Contact:

Re: Extracting text from QR Codes

Postby alexia30 » Fri Dec 27, 2019 5:51 am

Thanks for this perfect code, It works without any error.
Whimsical Exhibits the best exhibition stand design company.http://www.whimsicalexhibits.eu/
Seaha
Posts: 5
Joined: Tue Dec 24, 2019 9:23 am

Re: Extracting text from QR Codes

Postby Seaha » Fri Dec 27, 2019 6:06 am

Okay that works thanks but i have another Question i am playing arround a bit and try to figure out how i can acces maybe the second character of the payload i tryied differnt things but always with an error message.
On the internet i read something like this print(a[1])

and i tryed to do that with

if codes[0].payload([1]) == "LEFT":
but then i get this errormessage

TyperError: function takes 1 positional arguments but 2 were given

What does this mean
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Fri Dec 27, 2019 9:05 am

codes[0].payload()[1]

Will return 1 character. If you want 4 characters starting from position 1 do:

codes[0].payload()[1:4]

Note that these are general python questions you are asking. If you Google how python strings work you'd be able to solve these more quickly.

...

The payload method returns a string. It doesn't take any arguments. The index operation is on the returned string.
Nyamekye,
Seaha
Posts: 5
Joined: Tue Dec 24, 2019 9:23 am

Re: Extracting text from QR Codes

Postby Seaha » Sun Feb 16, 2020 3:46 pm

I have a weird problem i use my code and hooked everything up to comunicate to an arduino uno. If i use the uart example in openmw it works that it outputs everything to the arduino serial monitor.
Now i have a more complex sketch in arduino were the given numbers read out by openmv make a servo move. But it is now only showing weird characters in the serial monitor if the openmv sends the numbers. Also it looks like that the arduino is also sending stuff to the openmv because i can see text that should be outputed in the arduino serial monitor in the serial monitor from openmv. Could you explane this
User avatar
kwagyeman
Posts: 3715
Joined: Sun May 24, 2015 2:10 pm

Re: Extracting text from QR Codes

Postby kwagyeman » Sun Feb 16, 2020 5:48 pm

Code?
Nyamekye,

Return to “Project Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests