前言
openMVCam M7实现人眼的识别以及轨迹记录 此篇仅为菜鸡个人笔记 若存在错误希望各位大佬指正
一、什么是openMV?
OpenMV是一个开源,低成本,功能强大的机器视觉模块。
本文全程使用openMVCamM7为基础配合openMV的IDE进行学习
openMVCamM7 使用ARM CortexM7处理器,216 MHz ,512KB RAM,2 MB flash. 所有的 I/O 引脚输出 3.3V 并且 5V 耐受。
二、使用步骤
1.安装好openMV IDE
下载地址:https://openmv.io/pages/download.
这个安装很简单不做记录(注意选择和自己电脑相符的下载地址)
2.连接openMV与IDE
用数据线连接openMV和电脑
在设备管理器(右键 开始 )里可以看见端口
然后在IDE 里点击连接
三、基本思路
1、流程图
2、基本思路
利用openmv对瞳孔的识别
四、代码示例
openMV可使用MicroPython进行程序编写,以下为MicroPython代码注释
import sensor,time,image,lcd,pyb
import | 导入所需要使用的第三方库 |
---|---|
sensor | 感光元件(摄像头) |
time | 记录时间 |
image | 处理图像 |
lcd | lcd屏幕(openMV可通过长脚引母连接lcd屏幕) |
pyb | I/O引脚 |
#以上功能仅为当前项目中所使用到的功能,这些库能做的远远不止这些
sensor.reset() #相机传感器初始化
sensor.set_contrast(3)#设置相机图像对比度
sensor.set_gainceiling(16)#设置相机图像增益上限
sensor.set_framesize(sensor.VGA)#设置相机模块的帧大小
sensor.set_windowing((220, 190, 200, 100))#将相机的分辨率设置为当前分辨率的子分辨率
sensor.set_pixformat(sensor.GRAYSCALE)#设置相机模块的像素模式
lcd.init()#LCD屏幕初始化
p6=pyb.Pin("P6",pyb.Pin.OUT_PP)#设置引脚6为输出
p6.high()#p6输出高电频
eyes_cascade = image.HaarCascade("eye", stages=24)#利用HaarCascade加载haar-eye模块
n = 20 #设置照片张数
x1 = 0
while (n):
clock.tick() #
img0 = sensor.snapshot() #将当前图像保存为img0
eyes = img0.find_features(eyes_cascade, threshold=0.5, scale=1.5) #加载
lcd.display(sensor.snapshot())#设置lcd屏
for e in eyes:
iris = img0.find_eye(e)
#img0.draw_rectangle(e)
img0.draw_cross(iris[0], iris[1])
img0.draw_circle(iris[0], iris[1], 20, color=(255,0,0),fill = False)
img0.save("singtown/s%s.pgm" % n )
img = image.Image("data.pgm")
img.draw_circle(iris[0], iris[1], 1, color=(255,0,0),fill = True)
if (n % 2) == 0:
if x1 != 0:
x0=x1
y0=y0
x0=iris[0]
y0=iris[1]
img.draw_line(x0, y0, x1, y1, color=(255,0,0) ,thickness=1)
else:
x0=iris[0]
y0=iris[1]
else:
x1=iris[0]
y1=iris[1]
img.draw_line(x0, y0, x1, y1, color=(255,0,0) ,thickness=1)
if n==20:
img.draw_circle(iris[0], iris[1], 1, color=(0,0,255),fill = True)
if n==0:
img.draw_circle(iris[0], iris[1], 1, color=(0,255,0),fill = True)
break
img.save("data.pgm")#保存当前笔迹
n=n-1
print(clock.fps())输出fps
p6.low()#关闭灯光
以下为源码
# Tris_Detation - By: Ash、Scum - 周二 8月 4 2020
import sensor,time,image,lcd,pyb
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((220, 190, 200, 100))
sensor.set_pixformat(sensor.GRAYSCALE)
lcd.init()
p6=pyb.Pin("P6",pyb.Pin.OUT_PP)
p6.high()
eyes_cascade = image.HaarCascade("eye", stages=24)
print(eyes_cascade)
clock = time.clock()
n = 20
x1 = 0
while (n):
clock.tick()
img0 = sensor.snapshot()
eyes = img0.find_features(eyes_cascade, threshold=0.5, scale=1.5)
lcd.display(sensor.snapshot())
for e in eyes:
iris = img0.find_eye(e)
#img0.draw_rectangle(e)
img0.draw_cross(iris[0], iris[1])
img0.draw_circle(iris[0], iris[1], 20, color=(255,0,0),fill = False)
img0.save("singtown/s%s.pgm" % n )
img = image.Image("data.pgm")
img.draw_circle(iris[0], iris[1], 1, color=(255,0,0),fill = True)
if (n % 2) == 0:
if x1 != 0:
x0=x1
y0=y0
x0=iris[0]
y0=iris[1]
img.draw_line(x0, y0, x1, y1, color=(255,0,0) ,thickness=1)
else:
x0=iris[0]
y0=iris[1]
else:
x1=iris[0]
y1=iris[1]
img.draw_line(x0, y0, x1, y1, color=(255,0,0) ,thickness=1)
if n==20:
img.draw_circle(iris[0], iris[1], 1, color=(0,0,255),fill = True)
if n==0:
img.draw_circle(iris[0], iris[1], 1, color=(0,255,0),fill = True)
break
img.save("data.pgm")
n=n-1
print(clock.fps())
p6.low()
代码可以实现自动识别人眼并自动标识瞳孔中心以及瞳孔
并将保存图片保存在sd卡中为pgm文件(用ps打开)
与此同时记录每一次瞳孔位置将其轨迹在空白图片中(空白图片需手动更新)
五、视频演示
链接: https://www.bilibili.com/video/BV1YV411y7rD/.
https://www.bilibili.com/video/BV1YV411y7rD/
自制眼动轨迹仪演示