pmax=0
num=1
n=1
loop_flag=1
SSID ='OPENMV_AP' # Network SSID
KEY ='1234567890' # Network key (must be 10 chars)
HOST = '' # Use first available interface
PORT = 8080 # Arbitrary non-privileged port
wlan = network.WINC(mode=network.WINC.MODE_AP)
wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
# try:
# Bind and listen
s.bind([HOST, PORT])
s.listen(5)
# Set server socket timeout
# NOTE: Due to a WINC FW bug, the server socket must be closed and reopened if
# the client disconnects. Use a timeout here to close and re-create the socket.
#s.settimeout(10)
print ('Waiting for connections..')
client, addr = s.accept()
# set client socket timeout to 2s
client.settimeout(2.0)
print ('Connected to ' + addr[0] + ':' + str(addr[1]))
# Read request from client
data = client.recv(1024)
# Should parse client request here
# Send multipart header
client.send("HTTP/1.1 200 OK\r\n" \
"Server: OpenMV\r\n" \
"Content-Type: image/jpeg\r\n\r\n")
while loop_flag:
#加了histeq子自适应直方图均衡,当人脸填满整个屏幕的时候可以提高一点点识别准确度.histeq(adaptive=True, clip_limit=3)
#找图片中人脸
#gamma_corr用于修正图像中色彩,数值越高图像越亮
faces = sensor.snapshot().gamma_corr(contrast=1.5).find_features(image.HaarCascade("frontalface"))
#仅当图片中有人脸(正脸)才裁切脸部分,并进行特征点比对判断
print("Gain %f dB, Exposure %d us" % \
(sensor.get_gain_db(), sensor.get_exposure_us()))
if faces:
pyb.LED(3).on()
pyb.LED(3).off()
loop_flag=0
#获取image中人像部分largest_face的roi
largest_face = max(faces, key = lambda f: f[3] * f[3])
img=sensor.get_fb().crop(roi=largest_face)#裁剪人脸部分保存到img
kpts1=img.find_keypoints(max_keypoints=90, threshold=0, scale_factor=1.3)
#img.draw_keypoints(kpts)#画出此时的目标特征,没有调用那个定义的函数
img.draw_keypoints(kpts1)
#下面进行特征点比对
kpts2=None
for s in range(1, NUM_SUBJECTS+1):
match_count = int(0)
angle_count=0
for i in range(2, NUM_SUBJECTS_IMGS+1):
kpts2=image.load_descriptor("/keypoints/s%s/%s.orb"%(s,i))
#匹配当前找到的特征和最初的目标特征的相似度
# #threshold阈值设置匹配的准确度,用来过滤掉有歧义的匹配。这个值越小,准确度越高。阈值范围0~100,默认70
#filter_outliers默认关闭。
#对于ORB描述符来说,这个函数返回的是kptmatch对象。
match_count+=image.match_descriptor(kpts1, kpts2).count()
#match=image.match_descriptor(kpts1, kpts2)
#if isinstance(match,kptmatch):+
# match_count+=match.count()
#angle_count+=image.match_descriptor(kpts1, kpts2).theta()
print("Average match_count for subject %d: %d"%(s, match_count/NUM_SUBJECTS_IMGS))
pmax=find_max(pmax, match_count/NUM_SUBJECTS_IMGS, s)#match_count越大,被检测人脸与此样本更相似更匹配。
#print("pmax:%d"%pmax)
if pmax<9:#和三个人都差距都较大,返回0
#buneng
#uart.write(str(0))
print("unknown person !") # face_num保存s中当前最匹配的人的编号。
pyb.LED(1).on()
sensor.reset()
# Set sensor settings
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.snapshot().save("unknow person/%s.bmp" % (num) )
num+=1
pyb.LED(1).off()
else:
#uart.write(str(face_num))#发送只能是字符,而不能是数字,所以需要格式转换str(b)
print("The most similar person is:%d"%face_num) # face_num保存s中当前最匹配的人的编号。
pyb.LED(2).on()
sensor.skip_frames(time = 1000)
pyb.LED(2).off()
else:
print("find no face")
frame = image.Image("unknow person/%s.bmp" % (n), copy_to_fb=True)
cframe = frame.compressed(quality=35)
client.send(cframe)
client.close()
n+=1
sensor.skip_frames(time = 2000)
@k1zq 在 [OSError: 32,代码运行时的错误,求大佬指点](/post/19979) 中说:
> client.send(cframe)
>

大佬帮帮忙,真不知道为什么,第一张图还能查看,第二张的时候帧缓存区就图失真了
我是从SD卡里读取图片然后通过WiFi传输,同时进行人脸识别
你这个代码的结构也不是例子里的啊。
https://book.openmv.cc/example/14-WiFi-Shield/mjpeg-streamer-ap.html
你的代码要实现的功能是什么?
我是想实现通过特征点检测人脸,不认识的人脸拍照保存到SD卡的unknown person 文件夹里,然后在通过将图片读取的帧缓存区WiFi图传,在网页上看,结果运行到第一张图片的时候,第一张图可以在网页上看到,然后两秒钟左右就出现这个错误了,第二张图片在帧缓存区是失真的
我没看懂你的逻辑,将不认识的人脸保存到SD卡,然后再读取SD卡中的图片通过Wi-Fi传输?
对的,认识的人脸会输出人脸编号,不认识的会输出不认识,然后拍照保存到SD卡里,然后WiFi图传,我代码的意思是运行人脸识别的同时WiFi图传,然后可以在网页上看到,但现在只能看到一张,好像帧缓存区会卡住,还有什么别的方法WiFi图传吗?教程上的是将帧缓存区的图像压缩然后传输
没必要保存到SD卡里,然后再“读取”啊。直接就是img。
你的错误就是代码的整个架构是不对的,只返回一个图片。你用上面链接的代码改。