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(1000)
img = sensor.snapshot()
#相机拍照显示红色
led = pyb.LED(1)
#亮灯
led.on()
#亮灯3ms
time.sleep_ms(3)
led.off()
for t in templates:
template = image.Image(t)
#对每个模板遍历进行模板匹配
r = img.find_template(template, 0.60,roi=(110,90,160,100), step=4, search=SEARCH_EX) #, roi1=(10, 0, 60, 60))
if r:
img.draw_rectangle(r)
print(t) #打印模板名字
#准备保存以时间命名的图片
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())你同一秒检测出多个图片,文件名一样,不就覆盖了?
是的,那有没有一种方法可以快速达到OCR功能的,在每个字符位置建立独立的roi进行for循环搜素吗,那有没有带位置矫正功能的