代码:
import sensor, image, time, network, usocket, sys, pyb
SSID ='OPENMV_AP' # Network SSID
KEY ='1234567890' # wifi密码(必须为10字符)
HOST = '' # 使用第一个可用的端口
PORT = 8080 # 任意非特权端口
# 在AP模式下启动wlan模块。
wlan = network.WINC(mode=network.WINC.MODE_AP)
wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)
# 配置WIFI并传输视频流
def start_streaming(s):
# 重置传感器
sensor.reset()
# 设置传感器设置
sensor.set_contrast(1)
sensor.set_brightness(1)
sensor.set_saturation(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.RGB565)
print ('Waiting for connections..')
client, addr = s.accept()
# 将客户端套接字超时设置为2秒
client.settimeout(2.0)
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
# 从客户端读取请求
data = client.recv(1024)
# 应该在这里解析客户端请求
# 发送多部分head
client.send("HTTP/1.1 200 OK\r\n" \
"Server: OpenMV\r\n" \
"Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \
"Cache-Control: no-cache\r\n" \
"Pragma: no-cache\r\n\r\n")
# FPS clock
clock = time.clock()
# 开始人脸识别
facial_identification()
# 开始分辨人脸
temp = Distinguish_faces()
if (temp == 0):
# 开始流媒体图像
#注:禁用IDE预览以增加流式FPS。
while (True):
clock.tick() # 跟踪snapshots()之间经过的毫秒数。
frame = sensor.snapshot()
cframe = frame.compressed(quality=35)
header = "\r\n--openmv\r\n" \
"Content-Type: image/jpeg\r\n"\
"Content-Length:"+str(cframe.size())+"\r\n\r\n"
client.send(header)
client.send(cframe)
print(clock.fps())
# 直到接受到信号(WIFI接受的信号/延时信号)在退出视频流传输
# return 0
else:
return 0
# 人脸识别
def facial_identification():
# 感光元件设置
sensor.set_contrast(3)
sensor.set_gainceiling(16)
# HQVGA and GRAYSCALE are the best for face tracking.
# HQVGA和灰度对于人脸识别效果最好
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
# 注意人脸识别只能用灰度图哦
# 加载Haar算子
# 默认情况下,这将使用所有阶段,更低的satges更快,但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)
# image.HaarCascade(path, stages=Auto)加载一个haar模型。haar模型是二进制文件,
# 这个模型如果是自定义的,则引号内为模型文件的路径;也可以使用内置的haar模型,
# 比如“frontalface” 人脸模型或者“eye”人眼模型。
# stages值未传入时使用默认的stages。stages值设置的小一些可以加速匹配,但会降低准确率。
print(face_cascade)
# FPS clock
clock = time.clock()
while (True):
clock.tick()
# 拍摄一张照片
img = sensor.snapshot()
# Find objects.
# Note: Lower scale factor scales-down the image more and detects smaller objects.
# Higher threshold results in a higher detection rate, with more false positives.
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35)
# image.find_features(cascade, threshold=0.5, scale=1.5),thresholds越大,
# 匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。
# h = None # 用于判断是否识别到人脸
# 在找到的目标上画框,标记出来
for r in objects:
img.draw_rectangle(r) # 在找到的目标上画框,标记出来
# h = 1
# return h
return 0
# 分辨人脸
def Distinguish_faces():
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others)
sensor.set_windowing((92, 112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time=5000) # 等待5s
# SUB = "s1"
NUM_SUBJECTS = 3 # 图像库中不同人数,一共3人
NUM_SUBJECTS_IMGS = 10 # 每人有20张样本图片
# 拍摄当前人脸。
img = sensor.snapshot()
# img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height()))
# d0为当前人脸的lbp特征
img = None
pmin = 15000
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为当前最匹配的人的编号。
if (num == 1 or num == 2 or num == 3):
# 使电磁失效(待补充)
print("认证成功")
else:
num = 0
return int(num)
# 主函数
# def main_s():
while (True):
# 创建服务器套接字
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
try:
# Bind and listen
s.bind([HOST, PORT])
s.listen(5)
# 设置服务器套接字超时
# 注意:由于WINC FW bug,如果客户端断开连接,服务器套接字必须
# 关闭并重新打开。在这里使用超时关闭并重新创建套接字。
s.settimeout(3)
start_streaming(s)
except OSError as e:
s.close()
print("socket error: ", e)
#sys.print_exception(e)
# 开始运行
# main_s()
运行结果:
Waiting for connections..
socket error: -13
Waiting for connections..
Connected to 192.168.1.100:44620
{"width":24, "height":24, "n_stages":25, "n_features":2913, "n_rectangles":6383}