Openmv identifies two-dimensional code, color recognition and serial communication

Openmv recognizes TWO-DIMENSIONAL code, color recognition and serial port communication
Objective Two-dimensional code recognition color recognition serial port communication complete code

purpose
1. Specify the content of the QR code: “XXX + XXX”, identify, return and send the corresponding serial number to the serial port; 2. Identify the color of materials in order according to the content of the two-dimensional code
3. Turn on the green light when standby, turn on the blue light when performing the action, and turn off the LED when the action is over.
Two-dimensional code recognition

while(True):
    img = sensor.snapshot()
    img.lens_corr(1.8)
    for code in img.find_qrcodes():
        output_str="%s" % code.payload()
		#output_str is QR code content

Attached (Forage TWO-DIMENSIONAL code generator)
Color identification

#Color Threshold
green_threshold   = (73, 96, -79, -22, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)
blue_threshold   = (22, 67, 9, 127, -128, -54)

blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
for b in blobs:
	img.draw_rectangle(b[0:4]) # rect
    #Mark the target color area with a rectangle
    img.draw_cross(b[5], b[6]) # cx, cy
    # draw a cross in the center of the target color area
    print(b[8]) # b[8] is the color code, red=2,green=1,blue=4

A serial port communication

if uart.any():# determine if data is received
	getrx = uart.readline()# read data
	uart.write('abc')#Send data

The complete code

import sensor, image, time, pyb
import ujson
from pyb import UART

green_threshold   = (73, 96, -79, -22, -128, 127)
#(0, 100, -128, -25, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)
#(41, 61, 42, 127, -128, 127)
blue_threshold   = (22, 67, 9, 127, -128, -54)
#(15, 100, -128, 127, -128, -41)
#red=2,green=1,blue=4
getrx = 0
getcmd = 0
renum = 0
colornum = 0
ptrposition = 0
sensor.reset()# Initialize the camera
sensor.set_pixformat(sensor.RGB565)# formatted as RGB565.
sensor.set_framesize(sensor.QQVGA) # Use QQVGA for faster speed
sensor.skip_frames(time = 2000) # Skip 2000s, make the new settings take effect, and adjust the white balance automatically
sensor.set_auto_gain(False) # Turn off auto gain. The default is on, in the color recognition, make sure to turn off the white balance.
sensor.set_auto_whitebal(False)
# Turn off white balance. White balance is on by default. In color recognition, be sure to turn off white balance.
clock = time.clock() # Track frame rate
led = pyb.LED(1) # Red LED = 1, Green LED = 1, Blue LED = 2, IR LEDs = 4.
uart = UART(3, 115200, timeout_char = 1000)
led.on()

def Rec_NUM1(lista):
    if (lista[0]=='1' and lista[1]=='2' and lista[2]=='3'):
        return 1
    elif (lista[0]=='1' and lista[1]=='3' and lista[2]=='2'):
        return 2
    elif (lista[0]=='2' and lista[1]=='1' and lista[2]=='3'):
        return 3
    elif (lista[0]=='2' and lista[1]=='3' and lista[2]=='1'):
        return 4
    elif (lista[0]=='3' and lista[1]=='1' and lista[2]=='2'):
        return 5
    elif (lista[0]=='3' and lista[1]=='2' and lista[2]=='1'):
        return 6

def Rec_NUM2(lista):
    if (lista[4]=='1' and lista[5]=='2' and lista[6]=='3'):
        return 1
    elif (lista[4]=='1' and lista[5]=='3' and lista[6]=='2'):
        return 2
    elif (lista[4]=='2' and lista[5]=='1' and lista[6]=='3'):
        return 3
    elif (lista[4]=='2' and lista[5]=='3' and lista[6]=='1'):
        return 4
    elif (lista[4]=='3' and lista[5]=='1' and lista[6]=='2'):
        return 5
    elif (lista[4]=='3' and lista[5]=='2' and lista[6]=='1'):
        return 6

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    if uart.any():
        led.off()
        getrx = uart.readline()
        time.sleep(150)     #Time delay 150ms
        led = pyb.LED(2)
        led.on()
        getcmd = int(getrx)
        print(getcmd)
      #  print(img.find_qrcodes())
    img = sensor.snapshot()# 从The light-sensitive chip acquires an image
    img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
    blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
    if(getcmd==2):
        for code in img.find_qrcodes():
            output_str="%s" % code.payload() #Method 1
            renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
            uart.write(ujson.dumps(renum))
            getcmd = 0
            led.off()
    if blobs and getcmd==3:
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            #Mark the target color area with a rectangle
            img.draw_cross(b[5], b[6]) # cx, cy
            # Draw a cross in the center of the target color area
            #print(b[5], b[6], b[8])
            #uart.write(ujson.dumps(b[8]))
            #transfer color serial number to race serial number
            if b[8]==1:
                colornum=2
            elif b[8]==2:
                colornum=1
            elif b[8]==4:
                colornum=3
            print('colornum=',colornum,'output_str[ptrposition]=',output_str[ptrposition])
            #If the task code corresponds to
            if (int(output_str[ptrposition]) == colornum):
                uart.write('t')
                ptrposition+=1
                if ptrposition==4:
                    ptrposition+=1
                getcmd=0

Read More: