1、增加功能一:保存视频
2、增加功能二:信息提示 cv.putText()函数运用
# 算法还是比较古老,容易识别错误。
# 1 加入库
import cv2
import dlib
# 增加功能二:信息提示 cv.putText()函数运用
def show_info(frame, tracking_state):
# 两个定位点,在所在点写相应的内容。
pos1 = (20, 40)
pos2 = (20, 80)
cv2.putText(frame, "'1' : reset ", pos1, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255)) # 最后几个参数是有关字体的。
# 根据状态,显示不同的信息
if tracking_state is True:
cv2.putText(frame, "tracking now ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0))
else:
cv2.putText(frame, "no tracking ...", pos2, cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0))
# 2 主函数
def main():
# 3 打开摄像头
capture = cv2.VideoCapture(0)
# 4 基于dlib库获取人脸检测器
detector = dlib.get_frontal_face_detector()
# 5 基于dlib库实时跟踪
tractor = dlib.correlation_tracker()
# 6 跟踪状态
tracking_state = False
# 增加功能一:保存视频
# frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
w = int(capture.get(3)) # 获取视频的width
# frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
h = int(capture.get(4)) # 获取视频的height
# frame_fps = capture.get(cv2.CAP_PROP_FPS) # 这里有错误,现在还不太懂
# # 设置视频格式
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# output = cv2.VideoWriter('record.avi', fourcc, 30.0, (int(frame_width), int(frame_height)), True) # 表示是彩色,可以
# output = cv2.VideoWriter('record.avi', fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True) # 输出错误
# output= cv2.VideoWriter(args.video_output, fourcc, int(frame_fps), (int(frame_width), int(frame_height)), False) # 输出错误
output = cv2.VideoWriter(r"C:\Users\xiaohao\Desktop\record.mp4", fourcc, 15.0, (w, h))
# 7 循环读取每一帧
while True:
ret, frame = capture.read()
# 显示提示信息,在循环中。
show_info(frame, tracking_state)
# 8 如果没有跟踪, 启动跟踪器
if tracking_state is False:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dets = detector(gray, 1) # 返回检测到的人脸
if len(dets) > 0:
tractor.start_track(frame, dets[0])
tracking_state = True
# 9 正在跟踪,实时获取人脸的位置,显示
if tracking_state is True:
tractor.update(frame) # 跟新画面
position = tractor.get_position() # 获取人脸的坐标
cv2.rectangle(frame,(int(position.left()), int(position.top())), (int(position.right()), int(position.bottom())), (0,255,0), 3)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
# 恢复重置
if key == ord('1'):
tracking_state = False
cv2.imshow("face tracking", frame)
# 保存视频
output.write(frame)
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
output = cv2.VideoWriter("record.mp4", fourcc, int(frame_fps), (int(frame_width), int(frame_height)), True)
图片的长和宽可以正常获得(int(frame_width), int(frame_height)),就是帧值没有确定int(frame_fps),可以自己输入数据。具体原因,渴望大佬留言帮助。