opencv动态识别人脸

复制代码
import cv2
import os
import numpy as np



def take_faces():
    while True:
        key = input('请输入文件夹的名字,姓名拼音的缩写,如果输入Q,程序退出!')
        if key == 'Q':
            break
        # 在faces_dynamic下面创建子文件夹
        os.makedirs('./faces_dymamic/%s' % (key), exist_ok=True)
        take_photo(key)

def load_data():
    listdir = os.listdir('./faces_dymamic')
    #列表生成式
    names = [d for d in listdir if not d.startswith('.')]
    faces  = []
    target = [i for i in range(len(names))]*10
    for dir in names:
        for i in range(1,11):
            gray = cv2.imread('./faces_dymamic/%s/%d.jpg'% (dir,i)) #三维图片
            gray_ = gray[:, :, 0] #二维数组
            faces.append(gray_)
    faces = np.asarray(faces)
    target = np.asarray(target)
    target.sort()  # 排序
    return faces,target,names


def dynamic_recognizer_face(face_recognizer,names):
    cap = cv2.VideoCapture(0)
    #人脸检测
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
    while True:
        flag,frame = cap.read()
        if not flag:
            break
        gray = cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY)
        faces = face_detector.detectMultiScale(gray,minNeighbors=5)
        for x,y,w,h in faces:
            face = gray[y:y + h, x:x + w]
            face = cv2.resize(face, dsize=(64, 64))
            y_,confidence = face_recognizer.predict(face)
            label = names[y_]
            print('这个人是:%s.置信度:%0.1f'%(label,confidence))

            cv2.rectangle(frame,pt1=(x,y),pt2=(x+w,y+h),color=[0,0,255],thickness=2)
            cv2.putText(frame,text=label,
                        org=(x,y-10),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=1.5,
                        color=[0,0,255],thickness=2)
        cv2.imshow('face',frame)
        key = cv2.waitKey(1000//24)
        if key == ord('q'):
            break
    cv2.destroyAllWindows()
    cap.release()

if __name__ == '__main__':
   
    #2、加载数据,返回目标值
    faces,target,names  = load_data()
    #print(faces.shape,target.shape)
    #3、加载人脸识别算法
    #face_recognizer = cv2.face.EigenFaceRecognizer_create()
    #face_recognizer = cv2.face.FisherFaceRecognizer_create()
    face_recognizer = cv2.face.LBPHFaceRecognizer_create()
    #4、算法训练,找到目标值之间的规律
    face_recognizer.train(faces,target)
    #5、动态加载数据
    dynamic_recognizer_face(face_recognizer,names )
相关推荐
会博通·代码搬运工4 分钟前
粮食加工企业档案系统,如何对接 MES/ERP 与质检?——会博通采集与适配实践
人工智能·档案管理·企业数字化·ai赋能·档案系统·知识资产
愤怒火龙果5 分钟前
AI攻防 AIRT03-RAG Exploitation & Vector Database Attacks
数据库·人工智能·网络安全
孟健8 分钟前
Claude Code 接入 DeepSeek V4.1 与 MiniMax M3.1 实测:200+ TPS 下的代码准确率与 Token 账单
人工智能·llm·ai编程
戴西软件15 分钟前
制造研发中的AI视觉与仿真:戴西工程图纸智能审图智能体、戴西3DViz_CAE AI集成与戴西3DViz_CAD AI集成
大数据·运维·人工智能·3d·自动化·制造·rpa
智联视频超融合平台19 分钟前
AI大模型与智能监控的融合:开启安防新时代
人工智能
Pin_Qing19 分钟前
账单没超,请求先被拒了
大数据·开发语言·人工智能
2601_9628857221 分钟前
如何用 Python 计算 VaR 和 CVaR(在险价值)?
开发语言·python
杨杨杨大侠25 分钟前
DeepSeek Harness:连 Agent Loop 都能替换的插件架构
java·人工智能·agent
老李IT笔记25 分钟前
关掉设备上的 AI 扩展:两个键各管一层,配错了等于没配
人工智能·ios·智能手机
李航198329 分钟前
DeepSeekAI建模三 - 实战建模一组柜子
人工智能·python·计算机视觉·ai·ai编程