#带显示功能
import sensor, image, time, pyb, os
from fpioa_manager import fm, board_info
from Maix import GPIO
from display import SPIDisplay
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False) # 关闭自动增益
sensor.set_auto_whitebal(False) # 关闭自动白平衡
# 初始化SD卡
uos.mount(sdcard, '/sd')
# 初始化LED灯
led = pyb.LED(1)
# 初始化串口
uart3 = pyb.UART(3, 115200)
# 初始化显示屏
lcd = SPIDisplay(width=132, height=132, bgr=True)
lcd.init()
# 阈值设置
THRESHOLD = 50
# 录入人脸图片并计算LBP特征
def capture_face(index):
if not os.path.exists('/sd/{}'.format(index)):
os.mkdir('/sd/{}'.format(index))
for i in range(10):
img = sensor.snapshot()
img = img.resize(128, 128) # 调整图像大小
lcd.display(img) # 显示调整大小后的图像
lbp = img.find_lbp((0, 0, img.width(), img.height()))
lbp.save('/sd/{}/face_{}.lbp'.format(index, i))
led.on()
time.sleep(100)
led.off()
time.sleep(900)
# 接收指令并录入人脸
def receive_and_capture():
while True:
data = uart3.read()
if data:
cmd = data.decode()[0]
if cmd.isdigit() and int(cmd) in range(1, 7):
capture_face(cmd)
elif cmd == 'r':
face_recognition()
# 启动人脸识别
def face_recognition():
img = sensor.snapshot()
img = img.resize(128, 128) # 调整图像大小
lcd.display(img) # 显示调整大小后的图像
lbp = img.find_lbp((0, 0, img.width(), img.height()))
min_diff = 999999
recognized = None
for folder in range(1, 7):
for i in range(10):
template_file = '/sd/{}/face_{}.lbp'.format(folder, i)
if os.path.exists(template_file):
template = image.load(template_file)
diff = image.match_descriptors(lbp, template)
if diff < min_diff:
min_diff = diff
recognized = folder
if min_diff < THRESHOLD:
uart3.write('{}\n'.format(recognized))
else:
uart3.write('0\n')
# 显示识别结果
img.draw_string(10, 10, "Recognizing...", color=(255), scale=2)
if recognized:
img.draw_string(10, 40, "Match Found!", color=(255), scale=2)
else:
img.draw_string(10, 40, "No Match Found", color=(255), scale=2)
lcd.display(img) # 显示带有识别结果的图像
# 主循环
def main():
while True:
data = uart3.read()
if data:
cmd = data.decode()[0]
if cmd == 'r':
face_recognition()
elif cmd.isdigit() and int(cmd) in range(1, 7):
capture_face(cmd)
if __name__ == '__main__':
main()
#带显示功能
#import sensor, image, time, pyb, os
#from fpioa_manager import fm, board_info
#from Maix import GPIO
#from display import SPIDisplay
import time, os, pyb
import display
import sensor, image, machine
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
#sensor.set_framesize(sensor.QVGA)
#sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # 128x160大小的特定液晶屏。
sensor.skip_frames(time=200)
sensor.set_auto_gain(False) # 关闭自动增益
sensor.set_auto_whitebal(False) # 关闭自动白平衡
# 初始化SD卡
#os.mount(sdcard, '/sd')
# 初始化LED灯
led = pyb.LED(1)
# 初始化串口
uart3 = pyb.UART(3, 115200)
# 初始化显示屏
lcd = display.SPIDisplay( width=132, height=132,bgr=True)
#lcd = SPIDisplay(width=132, height=132, bgr=True)
#lcd.init()
print("start ok")
# 阈值设置
THRESHOLD = 2600
# 录入人脸图片并计算LBP特征
def capture_face(index):
if not '{}'.format(index) in os.listdir():
os.mkdir('{}'.format(index)) # 新建一个新的文件夹
print("mkdir ok")
for i in range(10):
led.on()
time.sleep_ms(100)
img = sensor.snapshot()
lcd.write(img) # 拍照并显示图像。
img.save('/{}/face_{}.pgm'.format(index, i))
print('/{}/face_{}.pgm'.format(index, i))
led.off()
time.sleep_ms(900)
# 接收指令并录入人脸
def receive_and_capture():
while True:
data = uart3.read()
if data:
cmd = data.decode()[0]
if cmd.isdigit() and int(cmd) in range(1, 7):
capture_face(cmd)
elif cmd == 'r':
face_recognition()
# 启动人脸识别
def face_recognition():
img = sensor.snapshot()
lcd.write(img) # 拍照并显示图像。
lbp = img.find_lbp((0, 0, img.width(), img.height()))
min_diff = 999999
recognized = None
img_temp = img
for folder in range(1, 7):
for i in range(10):
if '{}'.format(folder) in os.listdir():
template_file = '/{}/face_{}.pgm'.format(folder, i)
img = image.Image(template_file)
template = img.find_lbp((0, 0, img.width(), img.height()))
diff = image.match_descriptor(lbp, template)
if diff < min_diff:
min_diff = diff
recognized = folder
print(min_diff,recognized)
img_temp = img
else:
print("no dir:"+template_file)
if min_diff < THRESHOLD:
uart3.write('{}\n'.format(recognized))
print('{}\n'.format(recognized))
img_temp.draw_string(5, 120, "Match Found!", color=(255), scale=1)
else:
uart3.write('0\n')
print('0\n')
img_temp.draw_string(5, 120, "No Match Found", color=(255), scale=1)
# 显示识别结果
lcd.write(img_temp) # 显示带有识别结果的图像
# 主循环
def main():
img = sensor.snapshot()
lcd.write(img) # 拍照并显示图像。
while True:
data = uart3.read()
if data:
cmd = data.decode()[0]
if cmd == 'r':
face_recognition()
elif cmd.isdigit() and int(cmd) in range(1, 7):
capture_face(cmd)
if __name__ == '__main__':
main()
引用: JOEYCH 发表于 2024-10-20 13:42 实时性好吗?
一秒以内识别完成,速度还可以