请在import time, sensor, image,pyb
from image import SEARCH_EX, SEARCH_DS
from pyb import RTC
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
#SEARCH_DS两个需要的部分,而不把image模块全部引入。
# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(3)
sensor.set_gainceiling(8)
# Max resolution for template matching with SEARCH_EX is QVGA
sensor.set_framesize(sensor.QVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
templates = ["/0.pgm", "/1.pgm", "/2.pgm", "/3.pgm","/4.pgm", "/5.pgm", "/6.pgm", "/7.pgm","/8.pgm", "/9.pgm"] #保存多个模板
#加载模板图片
clock = time.clock()
# Run template matching
while (True):
clock.tick()
#每1s触发一次相机
time.sleep_ms(500)
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
#img = sensor.snapshot()
#相机拍照显示红色
led = pyb.LED(1)
#亮灯
led.on()
#亮灯3ms
time.sleep_ms(3)
led.off()
result=[]
for t1 in templates:
template = image.Image(t1)
#对每个模板遍历进行模板匹配
r1 = img.find_template(template, 0.65,roi=(57,110,35,50), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r1:
img.draw_rectangle(r1)
print(t1) #打印模板名字
for t2 in templates:
template = image.Image(t2)
#对每个模板遍历进行模板匹配
r2 = img.find_template(template, 0.65,roi=(92,110,35,50), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r2:
img.draw_rectangle(r2)
print(t2) #打印模板名字
for t3 in templates:
template = image.Image(t3)
#对每个模板遍历进行模板匹配
r3 = img.find_template(template, 0.65,roi=(118,110,35,50), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r3:
img.draw_rectangle(r3)
#print(t3) #打印模板名字
for t4 in templates:
template = image.Image(t4)
#对每个模板遍历进行模板匹配
r4 = img.find_template(template, 0.65,roi=(150,110,35,50), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r4:
img.draw_rectangle(r4)
print(t4) #打印模板名字
for t5 in templates:
template = image.Image(t5)
#对每个模板遍历进行模板匹配
r5 = img.find_template(template, 0.65,roi=(179,110,35,50), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r5:
img.draw_rectangle(r5)
print(t5) #打印模板名字
#准备保存以时间命名的图片
rtc = pyb.RTC()
dateTime = rtc.datetime()
year = str(dateTime[0])
month = '%02d' % dateTime[1]
day = '%02d' % dateTime[2]
hour = '%02d' % dateTime[4]
minute = '%02d' % dateTime[5]
second = '%02d' % dateTime[6]
subSecond = str(dateTime[7])
sensor.snapshot().save(year+month+hour+minute+second+".jpg")
#保存照片显示绿色
# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
led1 = pyb.LED(2)
#亮灯
led1.on()
#亮灯3ms
time.sleep_ms(3)
led1.off()
print(year+month+hour+minute+second)
# print(clock.fps())
![0_1646900317503_01421.bmp](正在上传 93%) 这里粘贴代码