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 )
相关推荐
无忧智库8 分钟前
某矿山井下人员精准定位与AI行为安全识别管控系统建设方案(WORD)
人工智能·安全
CTA量化套保9 分钟前
一个账户跑多个期货策略:仓位与报单隔离思路
python·区块链
机汇五金_10 分钟前
影响交换机箱体使用寿命的几个关键因素
运维·服务器·网络·python
子午11 分钟前
基于DeepSeek的酒店客房管理系统~Python+DeepSeek智能问答+Vue3+Web网站系统
开发语言·前端·python
湘美书院--湘美谈教育13 分钟前
湘美谈教育AI经验集锦:有些东西,它们很难蒸馏
大数据·人工智能·深度学习·机器学习
HyperAI超神经16 分钟前
深度估计准确率冲上0.9,Meta提出VLM³,论证视觉模型天生会学3D,以Qwen3-VL-4B为基础实现多任务的统一建模
人工智能·3d·大模型·多模态·空间推理·3d感知·3d理解
编程大师哥16 分钟前
最高效的 IO 并发方案
linux·网络·python
Hello:CodeWorld17 分钟前
Dify 从入门到实战:部署、模型对接与企业级 AI 应用开发全教程
人工智能·python·架构·ai编程
本地化文档22 分钟前
black-docs-l10n
python·github·gitcode·sphinx
AllData公司负责人24 分钟前
大模型赋能AllData数据中台,系列升级|通过联合智谱大模型与Chat2DB开源项目,建设Text2SQL生产场景全新体验的数据源平台!
数据库·人工智能·text2sql·数据中台·数据源·chat2db·智谱大模型