import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
templates = ["/2zuo.pgm","/2you.pgm","/6zuo.pgm","/6you.pgm"] #保存多个模板
#加载模板图片
sensor.set_vflip(True) #设置图像水平方向的翻转
sensor.set_hmirror(True) #设置图像垂直方向的翻转
while (True):
img = sensor.snapshot()
for t in templates:
template = image.Image(t)
#对每个模板遍历进行模板匹配
r = img.find_template(template, 0.60, step=4, search=SEARCH_EX)#,roi=(0, 0, 160, 160))
if r :
if t==2zuo.pgm or t==2you.pgm :
templates = ["/2zuo.pgm","/2you.pgm"]
if t==6zuo.pgm or t==6you.pgm :
templates = ["/6zuo.pgm","/6you.pgm"]
while True :
img = sensor.snapshot()
for t in templates:
template = image.Image(t)
#对每个模板遍历进行模板匹配
r = img.find_template(template, 0.60, step=4, search=SEARCH_EX)#,roi=(0, 0, 160, 160))
if r:
img.draw_rectangle(r)
print(t) #打印模板名字
#print(r) #输出识别到的目标的坐标(x,y,w,h)这个高度中间是64,r[0]就是x的坐标
if r[0]<64 :
print("左边")
if r[0]>64 :
print("右边")if t==2zuo.pgm or t==2you.pgm:改为
if t=="2zuo.pgm" or t=="2you.pgm":