“网络摄像头进行人脸识别,输出到直播服务器”
最初的想法是识别网络摄像头中的自己,后来经过几次变动,变成了识别指定文件夹中的人脸图片,如果和网络摄像头中一致,将视频中的人脸标识出来并标注对应的人名,然后输出到直播服务器。实际操作起来需要解决几个问题。
怎样将网络摄像头的视频数据传输到处理服务器。
怎样将视频中的人脸进行定位和识别。
怎样将识别后的视频输出到直播服务器。
通过什么方式观看已经完成人脸识别的视频。
01
—
视频中进行人脸识别的设计
经过几次修改确定了最终的设计,通过网络摄像头的ONVIF/RTSP协议传输视频流,采用Python通过OpenCV获取视频流,然后通过face_recognition对视频中的人脸进行识别,识别完成后输出到直播服务器。
具体设计:
采用OpenCV获取视频流, 通过RTSP协议将摄像头采集的视频传输到视频识别服务器,摄像头可以采用PoE供电。
通过face_recognition对每帧视频进行识别, 通过红色矩形将人脸在图像中进行标注。
通过ffmpeg将标注后的视频流推送至nginx直播服务器, 采用RTMP协议进行传输。
通过ffplay连接nginx直播服务器获取视频,可以通过HLS协议通过浏览器获取视频。
02
—
安装配置、代码和效果
1. 使用网络摄像头的子码流进行网络传输, 主码流用于本地存储卡存储,将码率、帧率、分辨率调低,服务器处理能力有限。
2. 安装face_recognition,会自动安装依赖库dlib。
pip install face_recognition
3. 安装opencv
pip install numpy Matplotlib
pip install opencv-python
4. 安装ffmpeg,下载操作系统对应版本
将服务器视频流发送到直播服务器
5. 安装配置nginx-rtmp
https://github.com/arut/nginx-rtmp-module
https://github.com/illuspas/nginx-rtmp-win32 ,windows使用这个
#./configure--prefix=/usr/local/nginx --add-module=../nginx-rtmp-module-1.2.1--with-http_ssl_module
#make&& make install
#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx-rtmp配置
userroot;
worker_processes 1;
error_loglogs/error.log debug;
events{
worker_connections 1024;
}
rtmp{
server {
listen 1935;
chunk_size 4000;
application live{
live on;
drop_idle_publisher 5s;
}
}
}
http{
server {
listen 8035;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root/data/face/nginx-rtmp-module-1.2.1/;
}
location /control {
rtmp_control all;
}
location /rtmp-publisher {
root/data/face/nginx-rtmp-module-1.2.1/test;
}
location / {
root/data/face/nginx-rtmp-module-1.2.1/test/www;
}
}
}
6. 转码推送到直播服务器的主要python代码
command = ['ffmpeg',
'-y',
# '-re',
# '-rtsp_transport', 'tcp',
'-f','rawvideo',
'-vcodec','rawvideo',
'-pix_fmt','bgr24',
'-s', sizeStr,
'-r',str(fps),
'-i','-',
'-c:v','libx264',
'-pix_fmt','yuv420p',
'-preset','ultrafast',
'-f','flv',
rtmpUrl]
whileTrue:
# 抓取一帧视频
ret, frame = video_capture.read()
# 发现在视频帧所有的脸和face_enqcodings
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
# 在这个视频帧中循环遍历每个人脸
for(top, right, bottom, left), face_encodinginzip(face_locations, face_encodings):
# 看看面部是否与已知人脸相匹配。
fori,vinenumerate(total_face_encoding):
match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)
name ="Unknown"
ifmatch[]:
name = total_image_name[i]
break
# 画出一个框,框住脸
cv2.rectangle(frame, (left, top), (right, bottom), (,,255),2)
# 画出一个带名字的标签,放在框下
cv2.rectangle(frame, (left, bottom -10), (right, bottom), (,,255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left +6, bottom -6), font,0.5, (255,255,255),1)
#通过ffmpeg输出到远程rtmp服务器
# 释放摄像头中的流
video_capture.release()
cv2.destroyAllWindows()
7. 识别效果
领取专属 10元无门槛券
私享最新 技术干货