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: 2404
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: 2404
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: 2404
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,

Return to “Project Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests