5、机器码识别实验
About 2 min
例程代码
import sensor, image, time, math, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
#sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
tag_families = 0
tag_families |= image.TAG16H5 # comment out to disable this family
tag_families |= image.TAG25H7 # comment out to disable this family
tag_families |= image.TAG25H9 # comment out to disable this family
tag_families |= image.TAG36H10 # comment out to disable this family
tag_families |= image.TAG36H11 # comment out to disable this family (default family)
tag_families |= image.ARTOOLKIT # comment out to disable this family
def family_name(tag):
if(tag.family() == image.TAG16H5):
return "TAG16H5"
if(tag.family() == image.TAG25H7):
return "TAG25H7"
if(tag.family() == image.TAG25H9):
return "TAG25H9"
if(tag.family() == image.TAG36H10):
return "TAG36H10"
if(tag.family() == image.TAG36H11):
return "TAG36H11"
if(tag.family() == image.ARTOOLKIT):
return "ARTOOLKIT"
while(True):
clock.tick()
img = sensor.snapshot()
#img = img.resize(280, 195)
#img = img.resize(292, 210)
for tag in img.find_apriltags(families=tag_families):
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
lcd.display(img)
#print(clock.fps())
实验准备
- 通过usb线将K210与电脑连接。
- 准备一张机器码。
- 打开CanMV IDE,执行上面的例程代码。
实验结果
- 等待系统初始化完成后,LCD显示摄像头画面,用摄像头拍摄机器码,识别的类型包括:TAG16H5,TAG25H7, TAG25H9, TAG36H10, TAG36H11, ARTOOLKIT。由于K210处理能力有限,处理AprilTag机器码需要大量储存空间和算力,所以目前无法设置全屏分辨率大小画面。可以看到机器码被框出来。
- 并且在IDE底部的串行终端打印出机器码的信息。
例程代码讲解
- 导入相关库,并初始化摄像头和LCD显示屏。
import sensor, image, time, math, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
- 设置要识别的机器码家族成员,如果不需要识别哪个就注释掉那一行。这里默认是TAG36H11家
族。
- 设置要识别的机器码家族成员,如果不需要识别哪个就注释掉那一行。这里默认是TAG36H11家
tag_families = 0
tag_families |= image.TAG16H5 # comment out to disable this family
tag_families |= image.TAG25H7 # comment out to disable this family
tag_families |= image.TAG25H9 # comment out to disable this family
tag_families |= image.TAG36H10 # comment out to disable this family
tag_families |= image.TAG36H11 # comment out to disable this family (default
family)
tag_families |= image.ARTOOLKIT # comment out to disable this family
- 新建一个函数用来将家族名转化成字符串。
def family_name(tag):
if(tag.family() == image.TAG16H5):
return "TAG16H5"
if(tag.family() == image.TAG25H7):
return "TAG25H7"
if(tag.family() == image.TAG25H9):
return "TAG25H9"
if(tag.family() == image.TAG36H10):
return "TAG36H10"
if(tag.family() == image.TAG36H11):
return "TAG36H11"
if(tag.family() == image.ARTOOLKIT):
return "ARTOOLKIT"
- 新建一个while循环,调用find_apriltags函数查找图像中的机器码,找到之后框出来并且打印相关信息。
while(True):
clock.tick()
img = sensor.snapshot()
#img = img.resize(280, 195)
#img = img.resize(292, 210)
for tag in img.find_apriltags(families=tag_families):
img.draw_rectangle(tag.rect(), color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
lcd.display(img)
#print(clock.fps())