def main(): os.makedirs('original', exist_ok=True) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 while cap.isOpened(): ret, frame = cap.read() t = time.time() if ret is True: # Display the resulting frame count += 1 print(count) cv2.imwrite("original/{}.png".format(count), frame) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if count == args.number: # only take 400 photos break elif cv2.waitKey(1) & 0xFF == ord('q'): break else: break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def real_time(): # TensorFlow if a.style == "rococo": graph = load_graph('frozen_models/frozen_rococo.pb') elif a.style == "ukiyo": graph = load_graph('frozen_models/frozen_ukiyo.pb') elif a.style == "vangogh": graph = load_graph('frozen_models/frozen_vg.pb') elif a.style == "fauvism": graph = load_graph('frozen_models/frozen_fauvism.pb') else: print("ERROR al seleccionar el ESTILO") image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV cap = cv2.VideoCapture(0) fps = video.FPS().start() while True: # Obtenemos el frame. ret, frame = cap.read() # Se reduce el tamaño del frame a uno procesable por pix2pix frame_resize = resize_out(frame) # Se aplica pre procesamiento del frame. gray_image = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) gaussian_image = cv2.GaussianBlur(gray_image, (3, 3), 0) # Se extraen los bordes. edge = 255 - auto_canny(gaussian_image) edge_color = edge_color = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR) black_image = np.zeros(edge.shape, np.uint8) # Se genera la predicción. combined_image = np.concatenate([edge, black_image], axis=1) image_rgb = cv2.cvtColor( combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([frame_resize, edge_color, image_bgr], axis=1) cv2.imshow('Tiempo real', image_normal) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() sess.close() cap.release() cv2.destroyAllWindows()
def detect_from_id(video_path, csv_file_result, dim="2D"): result_npy_path = "../output/landmarks/{}/".format( video_path.split("/")[-1][:-4]) result_img_path = "../output/landmarks/frames/{}/".format( video_path.split("/")[-1][:-4]) if not os.path.exists(result_npy_path): os.makedirs(result_npy_path) os.makedirs(result_img_path) print(result_npy_path) cap = cv2.VideoCapture(video_path) fps = video.FPS().start() count = 0 LE = LandmarkExtractor(dim=dim) if cap.isOpened() == False: print("Error opening video stream") while cap.isOpened(): t = time.time() ret, frame = cap.read() for result in csv_file_result: if result["frame"] == count: print(result["face_box"]) landmarks = LE.landmark_extractor(frame, [result["face_box"]]) if not os.path.exists(os.path.join(result_npy_path, str(count))): os.mkdir(os.path.join(result_npy_path, str(count))) np.save( os.path.join(result_npy_path, str(count), str(result["ID"])), landmarks) print("npy saved at ", result_npy_path, str(count), str(result["ID"])) cv2.imwrite( os.path.join(result_img_path, "{}_{}.png".format(count, result["ID"])), LE.draw_landmark(frame, [result["face_box"]], landmarks)) print("Saved {} video {} frame".format( video_path.split('/')[-1], count)) count += 1 fps.update() print('[INFO] {} frame elapsed time: {:.2f}'.format( count, time.time() - t)) if count == 5000: break elif cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() cap.release() cv2.destroyAllWindows()
def detect_only_vid(video_path, dim="2D"): # video_path = "parasite-trailer1-result.avi" # LS: Landmarks and Images result_npy_path = "../output/landmarks/{}".format( video_path.split("/")[-1][:-4]) result_img_path = "../output/landmarks/frames/{}".format( video_path.split("/")[-1][:-4]) if not os.path.exists(result_npy_path): os.makedirs(result_npy_path) os.makedirs(result_img_path) print(result_npy_path) cap = cv2.VideoCapture(video_path) fps = video.FPS().start() count = 0 FD = FaceDetector() LE = LandmarkExtractor(dim=dim) if cap.isOpened() == False: print("Error opening video stream") while cap.isOpened(): t = time.time() ret, frame = cap.read() faces = FD.detect_face(frame) if len(faces) is not 0: landmarks = LE.landmark_extractor(frame, faces) np.save( os.path.join(result_npy_path, "{}".format(count), landmarks)) print(faces) cv2.imwrite(os.path.join(result_img_path, "{}.png".format(count)), LE.draw_landmark(frame, faces, landmarks)) print("Saved {} video {} frame".format( video_path.split('/')[-1], count)) count += 1 fps.update() print('[INFO] {} frame elapsed time: {:.2f}'.format( count, time.time() - t)) if count == 3000: break elif cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() cap.release() cv2.destroyAllWindows()
def real_time(): # TensorFlow if a.style == "rococo": graph = load_graph('frozen_models/frozen_rococo.pb') elif a.style == "ukiyo": graph = load_graph('frozen_models/frozen_ukiyo.pb') elif a.style == "vangogh": graph = load_graph('frozen_models/frozen_vg.pb') elif a.style == "fauvism": graph = load_graph('frozen_models/frozen_fauvism.pb') else: print("ERROR: Select the correct STYLE") image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV cap = cv2.VideoCapture(0) fps = video.FPS().start() while True: # Getting the actual frame ret, frame = cap.read() # Reducing the image size to 256x256 frame_resize = resize_out(frame) # Pre-processing of the frame gray_image = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) gaussian_image = cv2.GaussianBlur(gray_image, (3, 3), 0) # Extracting edges edge = 255 - auto_canny(gaussian_image) edge_color = edge_color = cv2.cvtColor(edge, cv2.COLOR_GRAY2BGR) black_image = np.zeros(edge.shape, np.uint8) # Generating predictions combined_image = np.concatenate([edge, black_image], axis=1) image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([frame_resize, edge_color, image_bgr], axis=1) cv2.imshow('Real time', image_normal) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() sess.close() cap.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) os.makedirs('landmarks', exist_ok=True) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 while cap.isOpened(): ret, frame = cap.read() frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) t = time.time() # Perform if there is a face detected if len(faces) >= 0: # Display the resulting frame count += 1 print(count) #cv2.imwrite("original/{}.png".format(count), frame) #cv2.imwrite("landmarks/{}.png".format(count), black_image) cv2.imshow('a', frame_resize) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if count == args.number: # only take 400 photos break elif cv2.waitKey(1) & 0xFF == ord('q'): break else: print("No face detected") fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() frame_count = 0 image_number = 0 while cap.isOpened( ): #if specified num of frames is too great, this will return false and break the while loop ret, frame = cap.read() t = time.time() if ret is True: # Display the resulting frame frame_count += 1 print(frame_count) if frame_count >= args.start_frame: image_number += 1 cv2.imwrite(f'{args.output}/{image_number}.png', frame) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if frame_count == (args.number + args.start_frame): #default is 400 break elif cv2.waitKey(1) & 0xFF == ord('q'): break else: break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): # OpenCV #cap = cv2.VideoCapture(args.video_source) cap = cv2.VideoCapture('b.mov') fps = video.FPS().start() # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib) #while True: while cap.isOpened(): ret, frame = cap.read() # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) print(frame_resize.shape) out.write(frame_resize) # read image image = frame_resize image = resize(image) cv2.imshow('a', frame_resize) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output.avi', fourcc, 20.0, (1024,512)) cap = cv2.VideoCapture('VID_20190829_225300.mp4') fps = video.FPS().start() c1=0 while(cap.isOpened()): c1+=1 print(c1) ret, frame = cap.read() if ret==True: # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction combined_image = np.concatenate([resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) image_landmark=image_landmark.astype('uint8') image_normal = image_normal.astype('uint8') if c1==300: cv2.imwrite('frame.png',image_landmark) cv2.imwrite('normal.png',image_normal) out.write(image_normal) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break sess.close() cap.release() out.release() cv2.destroyAllWindows()
def main(): # OpenCV #cap = cv2.VideoCapture(args.video_source) cap = cv2.VideoCapture('b.mov') fps = video.FPS().start() # ---- init PRN os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu # GPU number, -1 for CPU prn = PRN(is_dlib=args.isDlib) #while True: while cap.isOpened(): ret, frame = cap.read() # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) # read image image = frame_resize image = resize(image) [h, w, c] = image.shape if c > 3: image = image[:, :, :3] # the core: regress position map if args.isDlib: max_size = max(image.shape[0], image.shape[1]) if max_size > 1000: image = rescale(image, 1000. / max_size) image = (image * 255).astype(np.uint8) st = time() pos = prn.process(image) # use dlib to detect face print('process', time() - st) else: if image.shape[0] == image.shape[1]: image = resize(image, (256, 256)) pos = prn.net_forward( image / 255.) # input image has been cropped to 256x256 else: box = np.array([0, image.shape[1] - 1, 0, image.shape[0] - 1 ]) # cropped with bounding box pos = prn.process(image, box) image = image / 255. if pos is None: cv2.imshow('a', frame_resize) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break continue if args.is3d or args.isMat or args.isPose or args.isShow: # 3D vertices vertices = prn.get_vertices(pos) if args.isFront: save_vertices = frontalize(vertices) else: save_vertices = vertices.copy() save_vertices[:, 1] = h - 1 - save_vertices[:, 1] #colors = prn.get_colors(image, vertices) #write_obj_with_colors(os.path.join('', 'webcam' + '.obj'), save_vertices, prn.triangles, colors) #if args.is3d: # # corresponding colors # colors = prn.get_colors(image, vertices) # # if args.isTexture: # if args.texture_size != 256: # pos_interpolated = resize(pos, (args.texture_size, args.texture_size), preserve_range = True) # else: # pos_interpolated = pos.copy() # texture = cv2.remap(image, pos_interpolated[:,:,:2].astype(np.float32), None, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT,borderValue=(0)) # if args.isMask: # vertices_vis = get_visibility(vertices, prn.triangles, h, w) # uv_mask = get_uv_mask(vertices_vis, prn.triangles, prn.uv_coords, h, w, prn.resolution_op) # uv_mask = resize(uv_mask, (args.texture_size, args.texture_size), preserve_range = True) # texture = texture*uv_mask[:,:,np.newaxis] # #write_obj_with_texture(os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, texture, prn.uv_coords/prn.resolution_op)#save 3d face with texture(can open with meshlab) # else: # True # #write_obj_with_colors(os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) #save 3d face(can open with meshlab) # #if args.isDepth: # depth_image = get_depth_image(vertices, prn.triangles, h, w, True) # depth = get_depth_image(vertices, prn.triangles, h, w) # #imsave(os.path.join(save_folder, name + '_depth.jpg'), depth_image) # #sio.savemat(os.path.join(save_folder, name + '_depth.mat'), {'depth':depth}) # #if args.isKpt or args.isShow: # # get landmarks # kpt = prn.get_landmarks(pos) # #np.savetxt(os.path.join(save_folder, name + '_kpt.txt'), kpt) # #if args.isPose or args.isShow: # # estimate pose # camera_matrix, pose = estimate_pose(vertices) #write_obj_with_colors(os.path.join(save_folder, name + '.obj'), save_vertices, prn.triangles, colors) rendering_cc = mesh.render.render_grid(save_vertices, prn.triangles, 900, 900) a = np.transpose(rendering_cc, axes=[1, 0, 2]) dim = rendering_cc.shape[0] i_t = np.ones([dim, dim, 3], dtype=np.float32) for i in range(dim): i_t[i] = a[dim - 1 - i] i_t = i_t / 255 #imsave('webcam.png', i_t) #kpt = prn.get_landmarks(pos) #cv2.imshow('frame', image) #cv2.imshow('a',i_t/255) #cv2.imshow('sparse alignment', np.concatenate([image, i_t], axis=1)) cv2.imshow('sparse alignment', i_t) cv2.imshow('vedio', image) #cv2.imshow('sparse alignment', np.concatenate([plot_kpt(image, kpt), i_t], axis=1)) #cv2.imshow('dense alignment', plot_vertices(image, vertices)) #cv2.imshow('pose', plot_pose_box(image, camera_matrix, kpt)) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV # print(args.video_source) cap = cv2.VideoCapture(args.video_source) # print(cap) ret, frame = cap.read() # print(frame) if frame is None: cap = cv2.VideoCapture(int(args.video_source)) # vcapture = cv2.VideoCapture(video_path) # length = int(vcapture.get(cv2.CAP_PROP_FRAME_COUNT)) # width = int(vcapture.get(cv2.CAP_PROP_FRAME_WIDTH)) # height = int(vcapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) # fps = vcapture.get(cv2.CAP_PROP_FPS) fps1 = cap.get(cv2.CAP_PROP_FPS) # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. # Define the fps to be equal to 10. Also frame size is passed. out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps1, (3 * CROP_SIZE, CROP_SIZE)) # out = cv2.VideoWriter('outpy_trial2.mp4',cv2.VideoWriter_fourcc(*'MP4V'), # fps1, (3*CROP_SIZE, CROP_SIZE)) fps = video.FPS().start() counter = 0 while True: # for i in range(10): ret, frame = cap.read() # frame=frame[150:-200,:,:] # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction combined_image = np.concatenate( [resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor( combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) image_all = np.concatenate( [resize(frame_resize), resize(black_image), image_bgr], axis=1) if args.display_landmark == 0: cv2.imshow('frame', image_normal) # Write the frame into the file 'output.avi' out.write(image_normal) else: cv2.imshow('frame', image_all) # Write the frame into the file 'output.avi' out.write(image_all) counter = counter + 1 fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.release() out.release() cv2.destroyAllWindows()
def main(): # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV cap = cv2.VideoCapture(args.video_source) fps = video.FPS().start() while True: ret, frame = cap.read() # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction combined_image = np.concatenate([resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) image_all = np.concatenate([resize(frame_resize), resize(black_image), image_bgr], axis=1) if args.display_landmark == 0: cv2.imshow('frame', image_normal) else: cv2.imshow('frame', image_all) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.release() cv2.destroyAllWindows()
def main(): #os.makedirs('original') #os.makedirs('landmarks') cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 idx_f = 0 while cap.isOpened(): start_time = time.time() rrr = float(idx_f + 0.5) / 5000.0 frame_no = int(rrr * float(cap.get(cv2.CAP_PROP_FRAME_COUNT))) cap.set(1, frame_no) ret, frame = cap.read() idx_f += 1 if frame is None: continue if frame.shape[0] == 0 or frame.shape[1] == 1 or frame.shape[2] == 0: continue # if idx_f % 30 > 0: # continue # print("GO!!!",cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES),cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT),idx_f) print("done %0.2f" % (float(100.0 * cap.get(cv2.CAP_PROP_POS_FRAMES)) / float(cap.get(cv2.CAP_PROP_FRAME_COUNT)))) # frame_resize = cv2.resize(frame, None, fx=1.0 / DOWNSAMPLE_RATIO, fy=1.0 / DOWNSAMPLE_RATIO) try: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imwrite("test.jpg", gray) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) t = time.time() #print("len faces ", len(faces)) # Perform if there is a face detected if len(faces) != 3: print("No face detected") continue faces_sorted = sorted(faces, key=lambda f: f.left() + 0.5 * f.width()) face = faces_sorted[1] facel, facer = faces_sorted[0], faces_sorted[2] detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] black_image = np.zeros(frame.shape, np.uint8) whole_face = reshape_for_polyline( landmarks[0:17] + list(reversed(landmarks[22:27])) + list(reversed(landmarks[17:22]))) jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) # paint cv2.fillPoly(black_image, [whole_face], (255, 255, 255)) cv2.fillPoly(black_image, [left_eye], (255, 0, 0)) cv2.fillPoly(black_image, [right_eye], (255, 0, 0)) cv2.fillPoly(black_image, [lower_nose], (255, 255, 0)) cv2.fillPoly(black_image, [outer_lip], (0, 0, 255)) cv2.fillPoly(black_image, [inner_lip], (0, 255, 0)) cv2.polylines(black_image, [left_eyebrow], False, (255, 0, 255), 4) cv2.polylines(black_image, [right_eyebrow], False, (255, 0, 255), 4) cv2.polylines(black_image, [nose_bridge], False, (0, 255, 255), 4) #color = (255, 255, 255) #thickness = 3 #cv2.polylines(black_image, [jaw], False, color, thickness) #cv2.polylines(black_image, [left_eyebrow], False, color, thickness) #cv2.polylines(black_image, [right_eyebrow], False, color, thickness) #cv2.polylines(black_image, [nose_bridge], False, color, thickness) #cv2.polylines(black_image, [lower_nose], True, color, thickness) #cv2.polylines(black_image, [left_eye], True, color, thickness) #cv2.polylines(black_image, [right_eye], True, color, thickness) #cv2.polylines(black_image, [outer_lip], True, color, thickness) #cv2.polylines(black_image, [inner_lip], True, color, thickness) minp = np.array(landmarks).min(axis=0) maxp = np.array(landmarks).max(axis=0) #cx, cy = ctr[0], ctr[1] cx, cy = 0.5 * (minp[0] + maxp[0]), 0.5 * (minp[1] + maxp[1]) min_x, min_y = np.min(np.array(landmarks)[:, 0]), np.min( np.array(landmarks)[:, 1]) max_x, max_y = np.max(np.array(landmarks)[:, 0]), np.max( np.array(landmarks)[:, 1]) w, h = max_x - min_x, max_y - min_y size = max(w, h) * 3.5 * (0.9 + 0.2 * random()) s2 = size * 2 if size < 100: continue x1, x2 = int(cx - s2 / 2.0), int(cx + s2 / 2.0) y1, y2 = int(cy - size / 2.0), int(cy + size / 2.0) x1, x2 = facel.left(), facer.left() + facer.width() s2 = x2 - x1 size = s2 / 2.0 y1, y2 = int(cy - size / 2.0), int(cy + size / 2.0) #margin = 512-size #x1m, y1m = x1, y1 #if margin > 0: # x1m, y1m = int(x1-margin/2), int(y1-margin/2) frame = frame[y1:y2, x1:x2, :] black_image = black_image[y1:y2, x1:x2, :] frame2 = cv2.resize(frame, (1024, 512), interpolation=cv2.INTER_LANCZOS4) black_image2 = cv2.resize(black_image, (1024, 512), interpolation=cv2.INTER_LANCZOS4) # Display the resulting frame count += 1 #cv2.imwrite("original/{}.png".format(count), frame) #cv2.imwrite("landmarks/{}.png".format(count), black_image) cv2.imwrite("combined/frame%05d.png" % (10000 + count), np.concatenate([black_image2, frame2], axis=1)) fps.update() #print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) except: print('oops') dt = 1.0 - time.time() + start_time #if dt > 0: # time.sleep(dt) if count == args.number: # only take 400 photos break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release()
def main(): # fourcc = cv2.VideoWriter_fourcc(*'XVID') fourcc = cv2.VideoWriter_fourcc(*'MP4V') writer = None if writer is None: print("Starting video writer") writer = cv2.VideoWriter("./out.mp4", fourcc, 30.0, (CROP_SIZE*2, CROP_SIZE)) if writer.isOpened(): print("Writer succesfully opened") else: writer = None print("Writer opening failed") else: print("Stopping video writer") writer.release() writer = None # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV # cap = cv2.VideoCapture(args.video_source) cap = cv2.VideoCapture(args.video_dir) fps = video.FPS().start() mean3DShape, blendshapes, mesh, idxs3D, idxs2D = utils.load3DFaceModel("candide.npz") projectionModel = models.OrthographicProjectionBlendshapes(blendshapes.shape[0]) while True: ret, frame = cap.read() if frame is None: break # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) #get frame face label # faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) # for face in faces: # detected_landmarks = predictor(gray, face).parts() # landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] # # jaw = reshape_for_polyline(landmarks[0:17]) # left_eyebrow = reshape_for_polyline(landmarks[22:27]) # right_eyebrow = reshape_for_polyline(landmarks[17:22]) # nose_bridge = reshape_for_polyline(landmarks[27:31]) # lower_nose = reshape_for_polyline(landmarks[30:35]) # left_eye = reshape_for_polyline(landmarks[42:48]) # right_eye = reshape_for_polyline(landmarks[36:42]) # outer_lip = reshape_for_polyline(landmarks[48:60]) # inner_lip = reshape_for_polyline(landmarks[60:68]) # # color = (255, 255, 255) # thickness = 3 # # cv2.polylines(black_image, [jaw], False, color, thickness) # cv2.polylines(black_image, [left_eyebrow], False, color, thickness) # cv2.polylines(black_image, [right_eyebrow], False, color, thickness) # cv2.polylines(black_image, [nose_bridge], False, color, thickness) # cv2.polylines(black_image, [lower_nose], True, color, thickness) # cv2.polylines(black_image, [left_eye], True, color, thickness) # cv2.polylines(black_image, [right_eye], True, color, thickness) # cv2.polylines(black_image, [outer_lip], True, color, thickness) # cv2.polylines(black_image, [inner_lip], True, color, thickness) shapes2D = getFaceKeypoints(frame, detector, predictor) if shapes2D is None: continue # 3D model parameter initialization modelParams = projectionModel.getInitialParameters(mean3DShape[:, idxs3D], shapes2D[0][:, idxs2D]) # 3D model parameter optimization modelParams = NonLinearLeastSquares.GaussNewton(modelParams, projectionModel.residual, projectionModel.jacobian, ( [mean3DShape[:, idxs3D], blendshapes[:, :, idxs3D]], shapes2D[0][:, idxs2D]), verbose=0) drawProjectedShape(black_image, [mean3DShape, blendshapes], projectionModel, mesh, modelParams, lockedTranslation) # generate prediction combined_image = np.concatenate([resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) if args.display_landmark == 0: cv2.imshow('frame', image_normal) else: cv2.imshow('frame', image_landmark) if writer is not None: writer.write(image_normal) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break writer.release() fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) os.makedirs('landmarks', exist_ok=True) if args.webcam: cap = WebcamVideoStream(0).start() else: cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 frame_count = 0 ret = True while ret is True: frame_count += 1 print("Frame:", frame_count) ret, frame = cap.read() if args.zoom > 1: o_h, o_w, _ = frame.shape frame = cv2.resize(frame, None, fx=args.zoom, fy=args.zoom) h, w, _ = frame.shape off_h, off_w = int((h - o_h) / 2), int((w - o_w) / 2) frame = frame[off_h:h - off_h, off_w:w - off_w, :] for _ in range(args.n): if args.dmax > 0 and args.dmin > 0: down_scale = np.random.uniform(args.dmin, args.dmax) else: down_scale = args.downsample down = 1 / down_scale frame_resize = cv2.resize(frame, None, fx=down, fy=down) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame_resize.shape, np.uint8) t = time.time() # Perform if there is a face detected if len(faces) == 1: # Display the resulting frame count += 1 print(count) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[int(p.x), int(p.y)] for p in detected_landmarks] color = (255, 255, 255) thickness = 3 if args.points: jaw = landmarks[0:17] left_eyebrow = landmarks[22:27] right_eyebrow = landmarks[17:22] nose_bridge = landmarks[27:31] lower_nose = landmarks[30:35] left_eye = landmarks[42:48] right_eye = landmarks[36:42] outer_lip = landmarks[48:60] inner_lip = landmarks[60:68] for part in [ jaw, left_eyebrow, right_eyebrow, nose_bridge, lower_nose, left_eye, right_eye, outer_lip, inner_lip ]: for x, y in part: cv2.circle(black_image, (x, y), 1, (255, 255, 255), -1) else: jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) cv2.imwrite("original/{}_{}.png".format(count, round(down, 3)), frame) cv2.imwrite( "landmarks/{}_{}.png".format(count, round(down, 3)), black_image) if args.show: cv2.imshow( "Capturing Train Data", np.concatenate([black_image, frame_resize], axis=1)) key = cv2.waitKey(1) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) else: print("No face detected") if count == args.num: break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) os.makedirs('landmarks', exist_ok=True) mean3DShape, blendshapes, mesh, idxs3D, idxs2D = utils.load3DFaceModel("candide.npz") projectionModel = models.OrthographicProjectionBlendshapes(blendshapes.shape[0]) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 while cap.isOpened(): ret, frame = cap.read() if frame is None: break frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) t = time.time() # Perform if there is a face detecte shapes2D = getFaceKeypoints(frame, detector, predictor) if shapes2D is None: continue if len(shapes2D) == 1: # 3D model parameter initialization modelParams = projectionModel.getInitialParameters(mean3DShape[:, idxs3D], shapes2D[0][:, idxs2D]) # 3D model parameter optimization modelParams = NonLinearLeastSquares.GaussNewton(modelParams, projectionModel.residual, projectionModel.jacobian, ( [mean3DShape[:, idxs3D], blendshapes[:, :, idxs3D]], shapes2D[0][:, idxs2D]), verbose=0) drawProjectedShape(black_image, [mean3DShape, blendshapes], projectionModel, mesh, modelParams, lockedTranslation) # Display the resulting frame count += 1 print(count) cv2.imwrite("original/{}.png".format(count), frame) cv2.imwrite("landmarks/{}.png".format(count), black_image) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if count == args.number: # only take 400 photos break elif cv2.waitKey(1) & 0xFF == ord('q'): break else: print("No face detected") fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') sess = tf.Session(graph=graph) # OpenCV cap = cv2.VideoCapture(args.video_source) print(cap) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, cap.get(cv2.CAP_PROP_FPS), (1024 * 3, 1024)) ret, frame = cap.read() if frame is None: cap = cv2.VideoCapture(int(args.video_source)) fps = video.FPS().start() counter = 0 if RepresentsInt(args.video_source): while True: # for i in range(10): ret, frame = cap.read() # frame=frame[150:-200,:,:] # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction combined_image = np.concatenate( [resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor( combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) image_all = np.concatenate( [resize(frame_resize), resize(black_image), image_bgr], axis=1) if args.save_video == 1: out.write(image_all) # if args.display_landmark == 0: # cv2.imshow('frame', image_normal) # else: # cv2.imshow('frame', image_all) if RepresentsInt(args.video_source) == False: cv2.imshow('frame', image_all) cv2.imwrite('/tmp/image%09d.jpg' % counter, image_all) cv2.imwrite('/tmp/face/gen/image%09d.jpg' % counter, image_bgr) cv2.imwrite('/tmp/face/face/image%09d.jpg' % counter, resize(black_image)) cv2.imwrite('/tmp/face/input/image%09d.jpg' % counter, resize(frame_resize)) if len(faces) > 0: cv2.imwrite('/tmp/face/mix/image%09d.jpg' % counter, image_bgr) else: cv2.imwrite('/tmp/face/mix/image%09d.jpg' % counter, resize(frame_resize)) counter = counter + 1 fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.release() out.release() cv2.destroyAllWindows() else: while (cap.isOpened()): # for i in range(10): ret, frame = cap.read() if ret == True: # frame=frame[150:-200,:,:] # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[ p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO ] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction combined_image = np.concatenate( [resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor( combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate( [resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate( [resize(black_image), image_bgr], axis=1) image_all = np.concatenate( [resize(frame_resize), resize(black_image), image_bgr], axis=1) out.write(image_all) # if args.display_landmark == 0: # cv2.imshow('frame', image_normal) # else: # cv2.imshow('frame', image_all) if RepresentsInt(args.video_source) == False: cv2.imshow('frame', image_all) cv2.imwrite('/tmp/image%09d.jpg' % counter, image_all) cv2.imwrite('/tmp/face/gen/image%09d.jpg' % counter, image_bgr) cv2.imwrite('/tmp/face/face/image%09d.jpg' % counter, resize(black_image)) cv2.imwrite('/tmp/face/input/image%09d.jpg' % counter, resize(frame_resize)) if len(faces) > 0: cv2.imwrite('/tmp/face/mix/image%09d.jpg' % counter, image_bgr) else: cv2.imwrite('/tmp/face/mix/image%09d.jpg' % counter, resize(frame_resize)) counter = counter + 1 fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break else: break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.release() out.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) os.makedirs('landmarks', exist_ok=True) try: cap = cv2.VideoCapture(args.filename) except: print("error") fps = video.FPS().start() print("starting") print(cap.isOpened()) count = 0 prevt = time.time() while cap.isOpened(): ret, frame_raw = cap.read() if not ret: break prevt = time.time() (height, width, channels) = frame_raw.shape frame = frame_raw[0:height - 200, 200:width - 200] frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) t = time.time() (x, y, w, h) = (0, 0, 0, 0) # Perform if there is a face detected if len(faces) == 1: for face in faces: (x, y, w, h) = rect_to_bb(face) detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) padd = int(80 / DOWNSAMPLE_RATIO) cropo = frame[(y - padd) * DOWNSAMPLE_RATIO:(y) * DOWNSAMPLE_RATIO + (padd + h) * DOWNSAMPLE_RATIO, (x - padd) * DOWNSAMPLE_RATIO:(x) * DOWNSAMPLE_RATIO + (padd + w) * DOWNSAMPLE_RATIO] cropl = black_image[(y - padd) * DOWNSAMPLE_RATIO:(y) * DOWNSAMPLE_RATIO + (padd + h) * DOWNSAMPLE_RATIO, (x - padd) * DOWNSAMPLE_RATIO:(x) * DOWNSAMPLE_RATIO + (padd + w) * DOWNSAMPLE_RATIO] if (y - padd <= 0 or x - padd <= 0): continue crop_original = cropo crop_landmarks = cropl cv2.imwrite("original/{}.png".format(count + 1), crop_original) cv2.imwrite("landmarks/{}.png".format(count + 1), crop_landmarks) # Display the resulting frame padd = int(140 / DOWNSAMPLE_RATIO) # print(count) if (y - padd <= 0 or x - padd <= 0): continue cropo = frame[(y - padd) * DOWNSAMPLE_RATIO:(y) * DOWNSAMPLE_RATIO + (padd + h) * DOWNSAMPLE_RATIO, (x - padd) * DOWNSAMPLE_RATIO:(x) * DOWNSAMPLE_RATIO + (padd + w) * DOWNSAMPLE_RATIO] cropl = black_image[(y - padd) * DOWNSAMPLE_RATIO:(y) * DOWNSAMPLE_RATIO + (padd + h) * DOWNSAMPLE_RATIO, (x - padd) * DOWNSAMPLE_RATIO:(x) * DOWNSAMPLE_RATIO + (padd + w) * DOWNSAMPLE_RATIO] cv2.imwrite("original/{}.png".format(count), cropo) cv2.imwrite("landmarks/{}.png".format(count), cropl) fps.update() progress(count, args.number) # print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) cv2.imshow("image", np.concatenate([cropo, cropl])) count += 2 if count == args.number: # only take 400 photos break elif cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): os.makedirs('original', exist_ok=True) os.makedirs('landmarks', exist_ok=True) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 while cap.isOpened(): ret, frame = cap.read() frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) t = time.time() # Perform if there is a face detected if len(faces) == 1: for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) color = (255, 255, 255) thickness = 3 cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # Display the resulting frame count += 1 print(count) cv2.imwrite("original/{}.png".format(count), frame) cv2.imwrite("landmarks/{}.png".format(count), black_image) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if count == args.number: # only take 400 photos break # elif cv2.waitKey(1) & 0xFF == ord('q'): # break else: print("No face detected") fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release()
def main(args): input_src, shuffle, max_num_images, min_w, min_h, max_w, max_h = args.input_src, args.shuffle, args.max_num_images, args.min_dim, args.min_dim, args.max_dim, args.max_dim output_dir, out_w, out_h, pct_test, save_mode, save_ext = args.output_dir, args.w, args.h, args.pct_test, args.save_mode, args.save_ext num_per, frac, frac_vary, max_ang_rot, max_stretch, centered = args.num_per, args.frac, args.frac_vary, args.max_ang_rot, args.max_stretch, args.centered action, target_face_image, face_crop, face_crop_lerp, landmarks_path, hed_model_path = args.action, args.target_face_image, args.face_crop, args.face_crop_lerp, args.landmarks_path, args.hed_model_path #os.system('rm -rf %s'%output_dir) # get list of actions actions = action.split(',') if False in [a in allowable_actions for a in actions]: raise Exception('one of your actions does not exist') # initialize face_processing if needed if 'face' in actions: initialize_face_processing(landmarks_path) target_encodings = get_encodings( target_face_image) if target_face_image else None # initialize photosketch if needed if 'sketch' in actions: photosketch_processing.setup(args.photosketch_model_path) # initialize esrgan if needed if 'upsample' in actions: esrgan_processing.setup(args.esrgan_model_path) # initialize SSS if needed if 'sss' in actions: sss_processing.setup(args.sss_model_path) # setup output directories if output_dir != 'None': trainA_dir, trainB_dir, testA_dir, testB_dir = setup_output_dirs( output_dir, save_mode, pct_test > 0) # initialize input ext = os.path.splitext(input_src)[1] is_movie = ext.lower() in ['.mp4', '.mov', '.avi'] if is_movie: cap = cv2.VideoCapture(input_src) fps = video.FPS().start() num_images = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) pct_frames = list(np.linspace(0, 1, num_images)) all_frames = get_frame_indexes(max_num_images, num_images, shuffle) else: images = sorted([ f for f in os.listdir(input_src) if os.path.isfile(os.path.join(input_src, f)) ]) num_images = len(images) all_frames = get_frame_indexes(max_num_images, num_images, shuffle) # training/test split training = [1] * len(all_frames) * num_per if pct_test > 0: n_test = int(len(all_frames) * num_per * pct_test) test_per = 1.0 / pct_test test_idx = [int(test_per * (i + 1) - 1) for i in range(n_test)] for t in test_idx: training[t] = 0 # iterate through each input print("Iterating through %d input images" % len(all_frames)) for k, idx_frame in tqdm(enumerate(all_frames)): if is_movie: pct_frame = pct_frames[idx_frame] frame = int(pct_frame * num_images) cap.set(1, frame) ret, img = cap.read() frame_name = 'frame%06d' % frame img = cv2pil(img) else: img_path = images[idx_frame] frame_name = os.path.splitext(img_path)[0] full_image_path = os.path.join(input_src, img_path) img = Image.open(full_image_path).convert("RGB") # skip images which are too small or too big if img.width < min_w or img.height < min_h: continue if img.width > max_w or img.height > max_h: continue # first crop around face if requested if face_crop is not None: jx, jy, jw, jh = get_crop_around_face(img, target_encodings, out_w / out_h, face_crop, face_crop_lerp) img = img.crop((jx, jy, jx + jw, jy + jh)) # preprocess/augment and produce input images imgs0, imgs1 = augmentation(img, num_per, out_w, out_h, frac, frac_vary, max_ang_rot, max_stretch, centered), [] # process each input image to make output for img0 in imgs0: img = img0 for a in actions: if a == 'segment': img = segment(img) elif a == 'colorize': colors = [[255, 255, 255], [0, 0, 0], [127, 0, 0], [0, 0, 127], [0, 127, 0]] img = quantize_colors(img, colors) elif a == 'trace': img = trace(img) elif a == 'hed': img = hed_processing.run_hed(img, hed_model_path) elif a == 'sketch': img = photosketch_processing.sketch(img) elif a == 'simplify': img = simplify(img, hed_model_path) elif a == 'face': img = extract_face(img, target_encodings) elif a == 'sss': img = sss_processing.run_sss(img) elif a == 'upsample': img = esrgan_processing.upsample(img) img = img.resize( (int(img.width / 2), int(img.height / 2)), resample=Image.BICUBIC) # go from 4x to 2x elif a == 'none' or a == '': pass imgs1.append(img) # save the images for i, (img0, img1) in enumerate(zip(imgs0, imgs1)): out_name = 'f%05d%s_%s.%s' % (idx_frame, '_%02d' % i if num_per > 1 else '', frame_name, save_ext) is_train = training[num_per * k + i] if save_mode == 'combined': output_dir = trainA_dir if is_train else testA_dir img2 = Image.new('RGB', (out_w * 2, out_h)) img2.paste(img1.convert('RGB'), (0, 0)) img2.paste(img0.convert('RGB'), (out_w, 0)) img2.save(os.path.join(output_dir, out_name), quality=97) else: if output_dir == 'None': img1.convert('RGB').save(full_image_path, quality=97) else: outputA_dir = trainA_dir if is_train else testA_dir img1.convert('RGB').save(os.path.join( outputA_dir, out_name), quality=97) if save_mode == 'split': outputB_dir = trainB_dir if is_train else testB_dir img0.convert('RGB').save(os.path.join( outputB_dir, out_name), quality=97)
choices=[0, 1], help='0 shows the normal input and 1 the facial landmark.') parser.add_argument('--landmark-model', dest='face_landmark_shape_file', type=str, help='Face landmark model file.') parser.add_argument('--tf-model', dest='frozen_model_file', type=str, help='Frozen TensorFlow model file.') args = parser.parse_args() avatar = AvatarProcessor(args.frozen_model_file) cap = cv2.VideoCapture(args.video_source) fps = video.FPS().start() while True: ret, frame = cap.read() ret_img = avatar.process_frame(frame) cv2.imshow('frame', ret_img) fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()
def main(): os.makedirs(ORIGIN_DIR, exist_ok=True) os.makedirs(DEST_DIR, exist_ok=True) cap = cv2.VideoCapture(args.filename) fps = video.FPS().start() count = 0 while cap.isOpened(): ret, frame = cap.read() frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame.shape, np.uint8) t = time.time() # Perform if there is a face detected if len(faces) == 1: for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks] jaw = reshape_for_polyline(landmarks[0:17]) uni_eyebrow = reshape_for_polyline(landmarks[17:27]) right_jaw_to_eyebrow = reshape_for_polyline( [landmarks[0], landmarks[17]]) left_jaw_to_eyebrow = reshape_for_polyline( [landmarks[16], landmarks[26]]) # jaw, connect jaw to unibrow, unibrow left to right, connect right unibrow to jaw #create polygon of bounding points for face shield - 0:16,16-26,26:17,17:0 #jaw already exists jaw_connect_right = reshape_for_polyline( [landmarks[16], landmarks[26]]) unibrow_left_to_right = reshape_for_polyline(landmarks[slice( 27, 16, -1)]) jaw_connect_left = reshape_for_polyline( [landmarks[17], landmarks[0]]) color = (255, 255, 255) thickness = 1 ''' Jaw is dots 0:16, connect 0-17 to link upper right jaw to upper right eyebrow right eyebrow is dots 17:21 left eyebrow is dots 22:26 combined eyebrow is 17:26 connect 16-26 to connect upper left jaw to upper left eyebrow TODO: Currently Grabs from MID-EYEBROW! Need to grab the faceplate, so mid-forehead ''' cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [uni_eyebrow], False, color, thickness) cv2.polylines(black_image, [ jaw, jaw_connect_right, unibrow_left_to_right, jaw_connect_left ], False, color, thickness) #so now black image should have white pixels defining the face polygon #TODO: FIXXXXX cv2.fillPoly(black_image, [ jaw, left_jaw_to_eyebrow, uni_eyebrow, right_jaw_to_eyebrow ], color, thickness) # Display the resulting frame count += 1 print(count) cv2.imwrite(f"{ORIGIN_DIR}/{count}.png", frame) cv2.imwrite(f"{DEST_DIR}/{count}.png", black_image) fps.update() print('[INFO] elapsed time: {:.2f}'.format(time.time() - t)) if count == args.number: # only take 400 photos break elif cv2.waitKey(1) & 0xFF == ord('q'): break else: print("No face detected") fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cap.release() cv2.destroyAllWindows()