600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Python调用海康威视网络摄像头进行远程人脸识别

Python调用海康威视网络摄像头进行远程人脸识别

时间:2023-08-29 19:46:12

相关推荐

Python调用海康威视网络摄像头进行远程人脸识别

Python调用网络摄像头

RTSP (Real Time Streaming Protocol),是一种语法和操作类似 HTTP 协议,专门用于音频和视频的应用层协议。 和 HTTP 类似,RTSP 也使用 URL 地址。摄像机传输数据用的是码流,高清网络摄像机产品编码器都会产生两个编码格式,称为主码流和子码流,即双码流技术。双码流能实现本地和远程传输的两种不同的带宽码流需求,本地传输可以用主码流,能获得更清晰的存储录像,远程传输就因为带宽限制的原因,而使用子码流来获得流畅的图像和录像。不同摄像头厂家的RTSP协议地址有所不同,查找到各大厂家的协议格式如下:

海康:

rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream#1) username 用户名,常用 admin#2) password 密码,常用 12345#3) ip 摄像头IP,如 192.0.0.64#4) port端口号,默认为 554,可以不写#5) codec视频编码模式,有 h264、MPEG-4、mpeg4 等,可以不写#6) channel 通道号,起始为1,例如通道1,则为 ch1#7) subtype 码流类型,主码流为 main,辅码流为 sub

大华:

rtsp://[username]:[password]@[ip]:[port]/cam/realmonitor?[channel=1]&[subtype=1] #1) username、password、ip、port 同上#2) channel 通道号,起始为1,例如通道2,则为 channel=2#3) subtype 码流类型,主码流为0(即 subtype=0),辅码流为1(即 subtype=1)

宇视:

rtsp://{用户名}:{密码}@{ip}:{port}/video1/2/3,#1)video1/2/3表示主码流,子码流,三码流(可以不用)#2)其他一样

Python调用海康威视网络摄像头并实时显示的例程如下:

import cv2cap = cv2.VideoCapture("rtsp://admin:a123456789@121.248.50.30/h264/ch1/main/av_stream")while True:ret, frame = cap.read()cv2.imshow("Video", frame)cv2.waitKey(1)

画面显示效果良好,传输25fps 1920×1080的复合流相比海康威视官方监视平台约有1s的时延。

调用网络摄像头进行人脸检测与人脸识别

调用Python模块face_recognition进行基于网络摄像头的人脸识别功能开发,face_recognition基于dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率,但对亚洲人脸的识别准确率尚待提升。

face_recognition模块能够基于本地摄像头实现人脸识别并流畅地显示画面,但网络摄像头添加了人脸检测和人脸识别功能后,画面延迟和卡顿现象十分严重,解决措施:1. 降低人脸检测和人脸识别的频率,每0.2秒进行一次人脸检测,每2秒进行一次人脸识别;2. 采用多线程,将远程视频画面的获取与人脸检测识别分开处理,效果有所改善。

最终代码:

import osimport cv2import queueimport threadingimport face_recognitionimport numpy as npq = queue.Queue()# 线程1获取网络摄像头图像def Receive():print("Start Reveive")cap = cv2.VideoCapture("rtsp://admin:a123456789@121.248.50.30/h264/ch1/main/av_stream")ret, frame = cap.read()q.put(frame)while ret:ret, frame = cap.read()q.put(frame)# 线程2进行人脸检测识别并显示def Display():print("Start Displaying")# 加载人脸数据库并学习database_path = "./Face_Database/"filenames = os.listdir(database_path)image_set = []known_face_names = []known_face_encodings = []for filename in filenames:image_set.append(face_recognition.load_image_file(database_path + filename))known_face_names.append(filename.split(".")[0])for image in image_set:known_face_encodings.append(face_recognition.face_encodings(image)[0])# 检测识别人脸及显示face_locations = []face_names = []count = 0while True:if not q.empty():count += 1frame = q.get()small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)rgb_small_frame = small_frame[:, :, ::-1]# 每0.2秒进行一次人脸检测if count % 5 == 0:face_locations = face_recognition.face_locations(rgb_small_frame)# 每2秒进行一次人脸识别if count % 50 == 0:face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:matches = pare_faces(known_face_encodings, face_encoding, tolerance=0.48)name = "Unknown"face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[int(best_match_index)]face_names.append(name)# 显示人脸定位框及姓名for (top, right, bottom, left), name in zip(face_locations, face_names):top *= 4right *= 4bottom *= 4left *= 4cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)cv2.imshow('Video', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakif __name__ == '__main__':p1 = threading.Thread(target=Receive)p2 = threading.Thread(target=Display)p1.start()p2.start()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。