def preprocess(self, frame, rois, landmarks, headposes): assert len(frame.shape) == 4, "Frame shape should be [1, c, h, w]" inputs = cut_rois(frame, rois) output = [] for input, roi, landmark, headpose in zip(inputs, rois, landmarks, headposes): boxLeft = self.createEyeBoundingBox(landmark.left_eye[0], landmark.left_eye[1]) boxRight = self.createEyeBoundingBox(landmark.right_eye[0], landmark.right_eye[1]) midLeft = (landmark.left_eye[0] + landmark.left_eye[1]) / 2 midRight = (landmark.right_eye[0] + landmark.right_eye[1]) / 2 scaledBoxLeft = self.BoundingBox(boxLeft.position * roi.size, boxLeft.size * roi.size) scaledBoxRight = self.BoundingBox(boxRight.position * roi.size, boxRight.size * roi.size) imgLeft = resize_input(cut_roi(input, scaledBoxLeft), self.input_shape_left) imgRight = resize_input(cut_roi(input, scaledBoxRight), self.input_shape_right) output.append( self.PreProcessResult(imgLeft, imgRight, midLeft, midRight, headpose)) return output
def preprocess(self, frame, rois): inputs = cut_rois(frame, rois) inputs = [ resize_input(input, self.input_shape, self.nchw_layout) for input in inputs ] return inputs
def preprocess(self, frame): # prepare the frames to fit openvino's requirement of nchw assert len(frame.shape) == 4, "Frame shape should be [1, c, h, w]" assert frame.shape[0] == 1 assert frame.shape[1] == 3 input = resize_input(frame, self.input_shape) return input
def preprocess(self, frame, rois, landmarks): image = frame.copy() inputs = cut_rois(image, rois) self._align_rois(inputs, landmarks) inputs = [ resize_input(input, self.input_shape, self.nchw_layout) for input in inputs ] return inputs
def preprocess(self, frame, rois, landmarks): assert len(frame.shape) == 4, "Frame shape should be [1, c, h, w]" inputs = cut_rois(frame, rois) self._align_rois(inputs, landmarks) inputs = [resize_input(input, self.input_shape) for input in inputs] return inputs
def preprocess(self, frame): assert len(frame.shape) == 4, "Frame shape should be [1, c, h, w]" assert frame.shape[0] == 1 assert frame.shape[1] == 3 input = resize_input(frame, self.input_shape) return input
def preprocess(self, frame): self.input_size = frame.shape return resize_input(frame, self.input_shape)
def main(args): if not os.path.isfile(args.input): print("Error : {} file doesn't exist".format(args.input), file=sys.stderr) exit(1) start = time.time() gpu_ids = args.gpu prefs = { "titsize": args.bsize, "aursize": args.asize, "nipsize": args.nsize, "vagsize": args.vsize, "hairsize": args.hsize } if args.cpu: gpu_ids = None elif gpu_ids is None: gpu_ids = [0] if not args.gif: # Read image file = open(args.input, "rb") image_bytes = bytearray(file.read()) np_image = np.asarray(image_bytes, dtype=np.uint8) image = cv2.imdecode(np_image, cv2.IMREAD_COLOR) # See if image loaded correctly if image is None: print("Error : {} file is not valid".format(args.input), file=sys.stderr) exit(1) # Preprocess if args.overlay: original_image = image.copy() image = utils.crop_input(image, args.overlay[0], args.overlay[1], args.overlay[2], args.overlay[3]) elif args.auto_resize: image = utils.resize_input(image) elif args.auto_resize_crop: image = utils.resize_crop_input(image) elif args.auto_rescale: image = utils.rescale_input(image) # See if image has the correct shape after preprocessing if image.shape != (512, 512, 3): print("Error : image is not 512 x 512, got shape: {}".format( image.shape), file=sys.stderr) exit(1) # Process if args.n_runs is None or args.n_runs == 1: result = process(image, gpu_ids, prefs) if args.overlay: result = utils.overlay_original_img(original_image, result, args.overlay[0], args.overlay[1], args.overlay[2], args.overlay[3]) cv2.imwrite(args.output, result) else: base_output_filename = utils.strip_file_extension( args.output, ".png") def process_one_image(i): result = process(image, gpu_ids, prefs) if args.overlay: result = utils.overlay_original_img( original_image, result, args.overlay[0], args.overlay[1], args.overlay[2], args.overlay[3]) cv2.imwrite(base_output_filename + "%03d.png" % i, result) if args.cpu: pool = ThreadPool(args.n_cores) pool.map(process_one_image, range(args.n_runs)) pool.close() pool.join() else: for i in range(args.n_runs): process_one_image(i) else: # Read images gif_imgs = imageio.mimread(args.input) print("Total {} frames in the gif!".format(len(gif_imgs))) # Preprocess if args.auto_resize: gif_imgs = [utils.resize_input(img) for img in gif_imgs] elif args.auto_resize_crop: gif_imgs = [utils.resize_crop_input(img) for img in gif_imgs] elif args.auto_rescale: gif_imgs = [utils.rescale_input(img) for img in gif_imgs] # Process if args.n_runs is None or args.n_runs == 1: process_gif_wrapper( gif_imgs, args.output if args.output != "output.png" else "output.gif", gpu_ids, prefs, args.n_cores) else: base_output_filename = utils.strip_file_extension( args.output, ".gif") if args.output != "output.png" else "output" for i in range(args.n_runs): process_gif_wrapper(gif_imgs, base_output_filename + "%03d.gif" % i, gpu_ids, prefs, args.n_cores) end = time.time() duration = end - start # Done print("Done! We have taken", round(duration, 2), "seconds") # Exit sys.exit()