每天都学一点

基于海康摄像头进行人脸识别

24 04月
作者:林健|分类:未分类

硬件设备:使用的普通的海康ip摄像头。这里摄像头只用来获取视频流,人脸识别部分用代码处理。


代码部分:使用到了Python库opencv(用来获取把摄像头的视频流拿到代码中处理)。face_recognition 人脸识别库。 


我们用的是海康摄像头通过局域网ip访问,当然也适用于其他型号摄像头,(更换source既rtsp地址)也适用于视频的人脸识别,(把source改为 视频文件的路径)。


海康ip摄像头的rtsp地址 :


rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream


username: 用户名。例如admin。

password: 密码。例如12345。

ip: 为设备IP。例如 192.0.0.64。

port: 端口号默认为554,若为默认可不填写。

codec:有h264、MPEG-4、mpeg4这几种。

channel: 通道号,起始为1。例如通道1,则为ch1。

subtype: 码流类型,主码流为main,辅码流为sub。参考


人脸识别模块使用的是python的一个人脸识别库,face_recognition


github地址 https://github.com/ageitgey/face_recognition


代码中把已知人物信息照片的路径放到filepath即可,注意如果不是jpg格式的需要在代码中更改成其他格式的。


另外如果名字是中文的还需要再进行修改,可以参考我博客中另一篇文章https://blog.csdn.net/Nirvana_6174/article/details/81411842


# -*- coding: utf-8 -*-

# 摄像头头像识别

import face_recognition

import cv2

 

from os import listdir

 

source = "×××××" #摄像头的rtsp地址

cam = cv2.VideoCapture(source)

 

filepath='../face_photos'  #已知人脸图片文件夹 注意 如果会员图片后缀不是jpg 需要进行修改

filename_list=listdir(filepath)

known_face_names=[]

known_face_encodings=[]

a=0

 

for filename in filename_list:#依次读入列表中的内容

    a+=1

    if filename.endswith('jpg'):# 后缀名'jpg'匹对

        known_face_names.append(filename[:-4])#把文件名字的后四位.jpg去掉获取人名

        file_str=filepath+'/'+filename

        a_images=face_recognition.load_image_file(file_str)

        #print(file_str)

        a_face_encoding = face_recognition.face_encodings(a_images)[0]

        known_face_encodings.append(a_face_encoding)

print(known_face_names,a)

 

face_locations = []

face_encodings = []

face_names = []

process_this_frame = True

 

while(cam.isOpened()):

    # 读取摄像头画面

    ret, frame = cam.read()

    if not ret:

        #等同于 if ret is not none

        break

 

    # 改变摄像头图像的大小,图像小,所做的计算就少

    small_frame = cv2.resize(frame, (0, 0), fx=0.33, fy=0.33)

 

    # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。

    rgb_small_frame = small_frame[:, :, ::-1]

 

    # Only process every other frame of video to save time

    if process_this_frame:

        # 根据encoding来判断是不是同一个人,是就输出true,不是为flase

        face_locations = face_recognition.face_locations(rgb_small_frame)

        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

 

        face_names = []

        for face_encoding in face_encodings:

            # 默认为unknown

            matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.48)

            #阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6

            #print(matches)

            name = "Unknown"

 

            # if match[0]:

            #     name = "michong"

            # If a match was found in known_face_encodings, just use the first one.

            if True in matches:

                first_match_index = matches.index(True)

                name = known_face_names[first_match_index]

 

            face_names.append(name)

 

    process_this_frame = not process_this_frame

 

    # 将捕捉到的人脸显示出来

    for (top, right, bottom, left), name in zip(face_locations, face_names):

        # Scale back up face locations since the frame we detected in was scaled to 1/4 size

        #由于我们检测到的帧被缩放到1/4大小,所以要缩小面位置

        top *= 3

        right *= 3

        bottom *= 3

        left *= 3

 

        # 矩形框

        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

       

        #引入ft2中的字体

        #加上标签

        cv2.rectangle(frame, (left, bottom - 20), (right, bottom), (0, 0, 255), cv2.FILLED)

        font = cv2.FONT_HERSHEY_DUPLEX

        cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)

 

        #frame = ft.draw_text(frame,(left + 6, bottom - 6), name, 1.0, (255, 255, 255))

        #def draw_text(self, image, pos, text, text_size, text_color)

    # Display

 

    cv2.imshow('monitor', frame)

    if cv2.waitKey(1) & 0xFF == 27:

        break

 

cam.release()

cv2.destroyAllWindows()

 


在运行的时候会出现卡顿的状况,需要对海康摄像头配置进行改进,适当的降低清晰度码流等。


这是我的配置




看个人情况,这些慢慢调试就好。


 


汉字标框问题已解决,可以到我博客里面看,以及使用该模块做的智能签到系统,和各个人脸识别模块的代码。


其他人脸识别模块介绍   https://blog.csdn.net/Nirvana_6174/article/details/89599441


如有问题,或有什么建议可加群:894243022或发邮箱[email protected] 


使用本文章或代码还请声明。


    浏览7 评论0
    返回
    目录
    返回
    首页
    教你分分钟搞定Docker私有仓库Registry ESXI6.7通过命令行直接升级到7.0教程

    发表评论