2 Barcode recognition2.1 experimental goals2.2 experimental procedure2.3 experimental results2.4 experiment summary
This lesson focuses on the color recognition function, which frames items of the same color according to the LAB value of the color.
The reference code path for this experiment is :CanMV\05-AI\find_barcodes.py
The factory firmware of the module has integrated the AI vision algorithm module. If you have downloaded other firmware, please burn it back to the factory firmware before doing the experiment.
Import the relevant libraries and initialize the camera and LCD display, here setting the camera color to grayscale.
ximport sensor, image, time, math, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565) #GRAYSCALE
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
The type name of the conversion barcode is String.
xxxxxxxxxx
def barcode_name(code):
if(code.type() == image.EAN2):
return "EAN2"
if(code.type() == image.EAN5):
return "EAN5"
if(code.type() == image.EAN8):
return "EAN8"
if(code.type() == image.UPCE):
return "UPCE"
if(code.type() == image.ISBN10):
return "ISBN10"
if(code.type() == image.UPCA):
return "UPCA"
if(code.type() == image.EAN13):
return "EAN13"
if(code.type() == image.ISBN13):
return "ISBN13"
if(code.type() == image.I25):
return "I25"
if(code.type() == image.DATABAR):
return "DATABAR"
if(code.type() == image.DATABAR_EXP):
return "DATABAR_EXP"
if(code.type() == image.CODABAR):
return "CODABAR"
if(code.type() == image.CODE39):
return "CODE39"
if(code.type() == image.PDF417):
return "PDF417"
if(code.type() == image.CODE93):
return "CODE93"
if(code.type() == image.CODE128):
return "CODE128"
Create a new while loop, the camera captured the picture for barcode analysis, if there is a barcode in the image, print the barcode information, and frame the location of the barcode in green.
xxxxxxxxxx
while(True):
clock.tick()
img = sensor.snapshot()
fps = clock.fps()
codes = img.find_barcodes()
for code in codes:
img.draw_rectangle(code.rect())
print_args = (barcode_name(code), code.payload(), (180 * code.rotation()) / math.pi, code.quality(), fps)
print("Barcode %s, Payload \"%s\", rotation %f (degrees), quality %d, FPS %f" % print_args)
img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
lcd.display(img)
Connect the K210 module to the computer through the microUSB data cable, CanMV IDE click the connect button, after the connection is completed click the Run button to run the routine code. You can also download the code as main.py and run it in the K210 module.
Wait for the system initialization to complete, the LCD displays the camera screen, and when the barcode is captured with the camera, the barcode will be framed out, and the serial terminal at the bottom of the IDE will be displayed.
This experiment can identify multiple types of barcodes, you can also search for barcode generation tools on the Internet, generate barcodes with custom information for barcodes, or click IDE's tools - > machine vision - > barcode generator, you can pop up the browser page search tool.
test photo: