import sensor,time,image,math
#设置摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)#分辨率,像素点
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False)
sensor.skip_frames(time=1000)
clock=time.clock()#解释一下原因:创建里一个计时器对象,用于测试帧率
while(True):
clock.tick()#记录当前的时间戳,配合clock.fps()可以输出处理频率
img=sensor.snapshot()
for i in img.find_apriltags():
img.draw_rectangle(i.rect(),color=(255,0,0))
img.draw_cross(i.cx(),i.cy(),color=(0,255,0))
degress=180*i.rotation()/math.pi#i.rotation是弧度制下的角度
print(i.id(),degress
使用一个靶子知道位置等各种信息
import sensor,image
#设置摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=1000)
sensor.set_auto_whitebal(False)#颜色识别记得关闭
sensor.set_auto_gain(False)
#先找到图形然后识别颜色
while(True):
img=sensor.snapshot()
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,\
r_min = 2, r_max = 100, r_step = 2):#返回的对象包含什么信息
'''
x_margin:圆心 x 坐标的合并容差,如果两个圆的 x 坐标相差小于此值,可能会被合并。
r_min:检测圆的最小半径
r_step:循环中的步长,单位对应的是像素的大小(和实际距离成一定的比例)
'''
area = (c.cx()-c.r(), c.cy()-c.r(), 2*c.r(), 2*c.r())
statistics=img.get_statistics(roi=area)
#红色的阈值范围
if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:
img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
else:
img.draw_rectangle(area, color = (255, 255, 255))
#unindent does not match any outer indentation level缩进错误
简单的可以做到找到一个绿色的圆形的类似功能
import sensor, image, time
from image import SEARCH_EX, SEARCH_DS
# 颜色跟踪阈值(L Min, L Max, A Min, A Max, B Min, B Max)
# 下面的阈值跟踪一般红色/绿色的东西。你不妨调整他们...
thresholds = [(30, 100, 15, 127, 15, 127),
(30, 100, -64, -8, -32, 32),
(0, 15, 0, 40, -80, -20)]
# 不要超过16个颜色阈值
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
# 只有比“pixel_threshold”多的像素和多于“area_threshold”的区域才被
# 下面的“find_blobs”返回。 如果更改相机分辨率,
# 请更改“pixels_threshold”和“area_threshold”。 “merge = True”合并图像中所有重叠的色块。
templates = ["0.pgm", "1.pgm", "2.pgm", "6.pgm"] #保存多个模板
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
#img.draw_rectangle(blob.rect())
#img.draw_cross(blob.cx(), blob.cy())
#print(blob.code())
img = img.to_grayscale()#前面是色块检测,后面是模板检测,注意模板检测要用灰度图像
for t in templates:
template = image.Image(t)#创建对象
#对每个模板遍历进行模板匹配
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
#find_template(template, threshold, [roi, step, search]),threshold中
#的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
#注意roi的大小要比模板图片大,比frambuffer小。
#把匹配到的图像标记出来
if r:
img.draw_rectangle(r, color=0)
print(blob.code(), t) #打印模板名字
#如果为红色, blob.code()==1; 如果为绿色, blob.code==2.
#如果为数字0, t=="0.pgm"; 如果为数字1, t=="1.pgm".
#print(clock.fps())
模板和颜色综合运用,可以实现更多精确的功能,比如找到一个绿色的数字’‘1’'等,记得颜色识别和模板检测有灰度处理这一步转换
记录人脸,录入人脸的模板
import sensor,image,time,machine
#设置摄像头
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.set_windowing(90,1112)#设定一个区域,整一下背景大小
sensor.skip_frames(n=20)#刚开始跳十张照片,等待摄像头稳定
sensor.skip_frames(time=3000)#给3s给人反应过来
#设置红蓝灯的引脚
LED_RED_PIN=1
LED_BLUE_PIN=3
n=20#拍20张照片
num=1#第NO.1个人
while(n):
machine.LED("LED_RED").on()
machine.LED("LED_BLUE").off()
sensor.skip_frames(time=3000)#准备三秒
#红灯亮表示开始
img=sensor.snapshot()
img.save("")#保存的路径
machine.LED("LED_BLUE").on()
machine.LED("LED_RED").off()
#蓝灯亮表示保存成功
sensor.skip_frames(time=3000)
#更新n
n-=1
人脸检测,特征点检测,寻找特征点差异最小的输出
import sensor, time, image
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.B128X128)
sensor.set_windowing((92,112))
sensor.skip_frames(10)
sensor.skip_frames(time = 5000) #等待5s
NUM_SUBJECTS = 6 #图像库中不同人数,一共6人
NUM_SUBJECTS_IMGS = 20 #每人有20张样本图片
# 拍摄当前人脸。
img = sensor.snapshot()
#img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height()))#参数表示的是roi
#d0为当前人脸的lbp特征
img = None
pmin = 999999
num=0
#一个很简单的比较大小的方法)
def min(pmin, a, s):
global num
if a<pmin:
pmin=a
num=s
return pmin
for s in range(1, NUM_SUBJECTS+1):
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
img = image.Image("singtown/s%d/%d.pgm"%(s, i))
d1 = img.find_lbp((0, 0, img.width(), img.height()))
#d1为第s文件夹中的第i张图片的lbp特征
dist += image.match_descriptor(d0, d1)#计算d0 d1即样本图像与被检测人脸的特征差异度。
print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
print(pmin)
print(num) # num为当前最匹配的人的编号。
下课!