告别卡顿!手把手教你将TUM RGBD的tgz包转成30Hz流畅bag(附Python脚本详解)
2026/6/9 7:08:58
一、 引言
virtualenv或venv)。opencv-python/opencv-contrib-python):图像处理、视频捕获、基础人脸检测。face_recognition库简介:基于dlib的高层封装,提供人脸定位、特征点检测、编码生成、识别比对功能。dlib库简介:强大的机器学习库,face_recognition的底层依赖。dlib和face_recognition:在树莓派上安装的特殊性(编译时间长、资源消耗大)。NumPy:数值计算。Pillow(PIL):图像处理。imutils:简化 OpenCV 操作。dlib/face_recognition默认)在精度和速度上的平衡。face_recognition库封装了预训练模型。.npy文件或数据库)。face_recognition.face_locations在当前帧中查找所有人脸位置。face_recognition.face_encodings提取特征向量。face_recognition.compare_faces或计算欧氏距离,将当前人脸特征与数据库中的特征进行比对。# 导入必要的库 import face_recognition import cv2 import numpy as np # 初始化摄像头 video_capture = cv2.VideoCapture(0) # 加载已知人脸数据库 (示例:已知人脸编码列表和对应名字列表) known_face_encodings = [...] # 加载之前保存的编码 known_face_names = [...] # 对应的名字 while True: # 抓取一帧视频 ret, frame = video_capture.read() # 将图像从 BGR 颜色 (OpenCV 使用) 转换为 RGB 颜色 (face_recognition 使用) rgb_frame = frame[:, :, ::-1] # 查找图像中所有人脸和人脸编码 face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # 遍历当前帧中的每个人脸 for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # 检查人脸是否与已知人脸匹配 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # 或者计算距离 # face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) # best_match_index = np.argmin(face_distances) # if face_distances[best_match_index] < tolerance_threshold: # name = known_face_names[best_match_index] # 使用第一个匹配结果 (简单示例) if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] # 在图像上绘制人脸边界框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 在边界框下方绘制名字标签 cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # 显示结果图像 cv2.imshow('Video', frame) # 按 'q' 键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭窗口 video_capture.release() cv2.destroyAllWindows()(注:此为核心流程示例,实际应用中需考虑数据库加载、错误处理、性能调优等)
dlib的 HOG 模型)。haarcascade_frontalface_default.xml检测 + 小尺寸模型编码)。face_recognitionGitHub 仓库与文档dlib官方文档