1.视频
通过视频每一帧的时间戳保存人脸图片
cap.get(cv2.CAP_PROP_POS_MSEC)/1000
vidio_path='/home/lqs/Documents/retinaface_lightweight.pytorch-master/1.mp4'
cap = cv2.VideoCapture(vidio_path) # open video
while True:
# read frame
ret,frame=cap.read()
if not ret: # if the camera over return false
break
start=time.time()
boxes, landms, scores = detector(frame)
if any(scores) and boxes[0][0]:
end=time.time()
time1 = round(end - start, 3) # 保留一位小数 2为两位
boxes = boxes.astype(np.int)
landms = landms.astype(np.int)
for i in range(len(boxes)):
text = "{:.4f}".format(scores[i])
faces=[boxes[i][0],boxes[i][1],boxes[i][2],boxes[i][3]]
face_roi=align_process(frame,faces,landms[i],(112,112))
a=round(cap.get(cv2.CAP_PROP_POS_MSEC)/1000,2)
cv2.imwrite(output1_path+str(a)+'.jpg',face_roi)
cv2.rectangle(frame, (boxes[i][0], boxes[i][1]), (boxes[i][2], boxes[i][3]), (0, 0, 255), 2)
cx = boxes[i][0]
cy = boxes[i][1] + 12
cv2.putText(frame, text, (cx, cy),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
cv2.putText(frame, 'Cost: {}ms'.format(time1),
(10, 25), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.75, (0, 23, 255), 1)
cv2.putText(frame,
'FPS: {:2.2f}'.format(1 / time1) if time1 > 0 else 'FPS: --',
(10, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.75, (0, 23, 255), 1)
# landms
cv2.circle(frame, (landms[i][0][0], landms[i][0][1]), 1, (0, 0, 255), 4)
cv2.circle(frame, (landms[i][1][0], landms[i][1][1]), 1, (0, 255, 255), 4)
cv2.circle(frame, (landms[i][2][0], landms[i][2][1]), 1, (255, 0, 255), 4)
cv2.circle(frame, (landms[i][3][0], landms[i][3][1]), 1, (0, 255, 0), 4)
cv2.circle(frame, (landms[i][4][0], landms[i][4][1]), 1, (255, 0, 0), 4)
cv2.imshow('video',frame)
if ord('q')==cv2.waitKey(40):
break
else:
print('no face')
continue
# release resource
cv2.destroyAllWindows()
cap.release
----------------------------------------
# the offcial code
import cv2
def vidread(path):
cap = cv2.VideoCapture(path)
frames = []
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
frames.append(frame)
else:
break
cap.release()
return frames
# save video
def vidwrite(path, frames):
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter(path, fourcc, 30.0, (frames[0].shape[1], frames[0].shape[0]))
for frame in frames:
out.write(frame)
out.release()
2.摄像头
通过CSI摄像头每一帧的系统时间保存图片
#import time 获取绝对时间年月日时分秒
#a=time.strftime('%Y%m%d%H%M%S', time.localtime())
#print(a)
import cv2,os,time
import argparse
import numpy as np
from align_faces import align_process
from retinaface import RetinaDetector
#设置gstreamer管道参数
def gstreamer_pipeline(
capture_width=1280, #摄像头预捕获的图像宽度
capture_height=720, #摄像头预捕获的图像高度
display_width=1280, #窗口显示的图像宽度
display_height=720, #窗口显示的图像高度
framerate=60, #捕获帧率
flip_method=0, #是否旋转图像
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
if __name__ == "__main__":
vidio_path='/home/z/Documents/retinaface_lightweight.pytorch-master/1.mp4'
output_path = '/home/z/Documents/retinaface_lightweight.pytorch-master/retainmp4_result/'
output1_path = '/home/z/Documents/retinaface_lightweight.pytorch-master/retainmp4_roi_result/'
os.makedirs(os.path.dirname(output_path),exist_ok=True)
os.makedirs(os.path.dirname(output1_path),exist_ok=True)
capture_width = 1280
capture_height = 720
display_width = 1280
display_height = 720
framerate = 60
flip_method = 0
# 创建管道
print(gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method))
#管道与视频流绑定
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
detector = RetinaDetector()
if cap.isOpened():
window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
# 逐帧显示
while cv2.getWindowProperty("CSI Camera", 0) >= 0:
ret_val, img = cap.read()
if not ret_val: # if the camera over return false
break
# 图像太大需要调整
height, width = img.shape[0:2]
print("height=",height,"width=",width)
if width > 800:
new_width = 640
new_height = int(new_width/width*height)
img = cv2.resize(img, (new_width, new_height))
print("new_height=",new_height,"new_width=",new_width)
cv2.imshow("CSI Camera", img)
#print("img.shape=",img.shape)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:# ESC键退出
break
#print("img.shape=",img.shape)
#释放资源
cap.release()
cv2.destroyAllWindows()
else:
print("打开摄像头失败")