- 论坛徽章:
- 0
|
videoCapture = cv2.VideoCapture(args.video)
fps = int(videoCapture.get(5))
# get size
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
#special format, I420-avi, MJPG-mp4
videoWriter = cv2.VideoWriter('oto_other.mp4', cv2.VideoWriter_fourcc(*'XVID'), fps, size)
#read
success, frame = videoCapture.read()
while success :
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# The frame has the face in it
rects = detector(rgb_frame, 1)
if(len(rects) > 0):
print("The frame has face...")
# return the star face landmarks
rgb_frame, rgb_landmark = getlmface(rgb_frame, args.pic)
if(len(rgb_landmark) > 0):
print("The frame has star face...")
rep_pic = getclose(face_orientation(rgb_frame, rgb_landmark), posedict)
im2, landmarks2 = read_im_and_landmarks(rep_pic)
# get the affix matrix
rgb_landmark = numpy.matrix(rgb_landmark)
#im2 = im2[:, :, ::-1]
#print(landmarks2[ALIGN_POINTS], rgb_landmark[ALIGN_POINTS])
M = transformation_from_points(rgb_landmark[ALIGN_POINTS], landmarks2[ALIGN_POINTS])
mask = get_face_mask(im2, landmarks2)
warped_mask = warp_im(mask, M, rgb_frame.shape)
combined_mask = numpy.max([get_face_mask(rgb_frame, rgb_landmark), warped_mask], axis=0)
warped_im2 = warp_im(im2, M, rgb_frame.shape)
warped_corrected_im2 = correct_colours(rgb_frame, warped_im2, rgb_landmark)
output_im = rgb_frame * (1.0 - combined_mask) + warped_corrected_im2 * combined_mask
#cv2.imwrite("outputbef.png", output_im)
frame = output_im[:, :, ::-1]
#cv2.imwrite("outputaf.png", frame)
else:
frame = rgb_frame[:, :, ::-1]
# show video
#cv2.imshow("Oto Video", frame)
# delay
cv2.waitKey(int(1000/int(fps)))
# generate video
videoWriter.write(numpy.uint8(frame))
# next frame
success, frame = videoCapture.read()
代码比较长,没有全贴上来 |
|