def process_3d(self, data_dir): new_data_dir = '{}_new'.format(data_dir.rstrip('/')) if os.path.exists(new_data_dir): shutil.rmtree(new_data_dir) os.makedirs(new_data_dir) for filename in FileHelper.list_dir(data_dir): if not ImageHelper.is_img(filename) or 'depth' in filename: Log.info('Image Path: {}'.format(os.path.join(data_dir, filename))) continue file_path = os.path.join(data_dir, filename) img = io.imread(file_path) kpts = self.detect_face(img) if kpts is None: Log.info('Invliad face detected in {}'.format(file_path)) continue depth = np.array(io.imread(os.path.join(data_dir, filename.replace('rgb', 'depth')))) face_depth, kpts = self.align_face([np.array(img), np.array(depth)], kpts) if face_depth is None: Log.info('Invliad face detected in {}'.format(file_path)) continue ImageHelper.save(ImageHelper.rgb2bgr(face_depth[0]), os.path.join(new_data_dir, filename)) ImageHelper.save(ImageHelper.rgb2bgr(face_depth[1]), os.path.join(new_data_dir, filename.replace('rgb', 'depth')))
def vis_bboxes(self, image_in, bboxes_list, name='default', sub_dir='bbox'): """ Show the diff bbox of individuals. """ base_dir = os.path.join(self.configer.get('project_dir'), DET_DIR, sub_dir) if isinstance(image_in, Image.Image): image = ImageHelper.rgb2bgr(ImageHelper.to_np(image_in)) else: image = image_in.copy() if not os.path.exists(base_dir): log.error('Dir:{} not exists!'.format(base_dir)) os.makedirs(base_dir) img_path = os.path.join( base_dir, name if ImageHelper.is_img(name) else '{}.jpg'.format(name)) for bbox in bboxes_list: image = cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2) cv2.imwrite(img_path, image)
def __read_json(self, root_dir, json_path): item_list = [] for item in JsonHelper.load_file(json_path): img_path = os.path.join(root_dir, item['image_path']) if not os.path.exists(img_path) or not ImageHelper.is_img(img_path): Log.error('Image Path: {} is Invalid.'.format(img_path)) exit(1) item_list.append((img_path, '.'.join(item['image_path'].split('.')[:-1]))) Log.info('There are {} images..'.format(len(item_list))) return item_list
def __init__(self, test_dir=None, aug_transform=None, img_transform=None, configer=None): super(DefaultLoader, self).__init__() self.configer = configer self.aug_transform = aug_transform self.img_transform = img_transform self.item_list = [(os.path.join(test_dir, filename), '.'.join(filename.split('.')[:-1])) for filename in FileHelper.list_dir(test_dir) if ImageHelper.is_img(filename)]
def __read_list(self, root_dir, list_path): item_list = [] with open(list_path, 'r') as f: for line in f.readlines()[0:]: filename = line.strip().split()[0] img_path = os.path.join(root_dir, filename) if not os.path.exists(img_path) or not ImageHelper.is_img( img_path): Log.error('Image Path: {} is Invalid.'.format(img_path)) exit(1) item_list.append( (img_path, '.'.join(filename.split('.')[:-1]))) Log.info('There are {} images..'.format(len(item_list))) return item_list
def process(self, data_dir): new_data_dir = '{}_new'.format(data_dir.rstrip('/')) if os.path.exists(new_data_dir): shutil.rmtree(new_data_dir) os.makedirs(new_data_dir) for filename in FileHelper.list_dir(data_dir): if not ImageHelper.is_img(filename): Log.info('Image Path: {}'.format(os.path.join(data_dir, filename))) continue file_path = os.path.join(data_dir, filename) img = io.imread(file_path) kpts = self.detect_face(img) if kpts is None: Log.info('Invliad face detected in {}'.format(file_path)) continue face, kpts = self.align_face(img, kpts) cv2.imwrite(os.path.join(new_data_dir, filename), ImageHelper.rgb2bgr(face))