Exemple #1
0
    def onClientProcessData(self, data):
        filename_path = Path( data[0] )

        image = cv2.imread( str(filename_path) )
        if image is None:
            print ( 'Failed to extract %s, reason: cv2.imread() fail.' % ( str(filename_path) ) )
        else:
            if self.type == 'rects':
                rects = self.e.extract_from_bgr (image)
                return [str(filename_path), rects]

            elif self.type == 'landmarks':
                rects = data[1]   
                landmarks = self.e.extract_from_bgr (image, rects)                    
                return [str(filename_path), landmarks]

            elif self.type == 'final':     
                result = []
                faces = data[1]
                
                if self.debug:
                    debug_output_file = '{}_{}'.format( str(Path(str(self.output_path) + '_debug') / filename_path.stem),  'debug.png')
                    debug_image = image.copy()
                    
                for (face_idx, face) in enumerate(faces):         
                    output_file = '{}_{}{}'.format(str(self.output_path / filename_path.stem), str(face_idx), '.png')
                    
                    rect = face[0]
                    image_landmarks = np.array(face[1])

                    if self.debug:
                        facelib.LandmarksProcessor.draw_rect_landmarks (debug_image, rect, image_landmarks, self.image_size, self.face_type)

                    if self.face_type == FaceType.MARK_ONLY:                        
                        face_image = image
                        face_image_landmarks = image_landmarks
                    else:
                        image_to_face_mat = facelib.LandmarksProcessor.get_transform_mat (image_landmarks, self.image_size, self.face_type)       
                        face_image = cv2.warpAffine(image, image_to_face_mat, (self.image_size, self.image_size), cv2.INTER_LANCZOS4)
                        face_image_landmarks = facelib.LandmarksProcessor.transform_points (image_landmarks, image_to_face_mat)
                    
                    cv2.imwrite(output_file, face_image)

                    DFLPNG.embed_data(output_file, face_type = FaceType.toString(self.face_type),
                                                   landmarks = face_image_landmarks.tolist(),
                                                   yaw_value = facelib.LandmarksProcessor.calc_face_yaw (face_image_landmarks),
                                                   pitch_value = facelib.LandmarksProcessor.calc_face_pitch (face_image_landmarks),
                                                   source_filename = filename_path.name,
                                                   source_rect=  rect,
                                                   source_landmarks = image_landmarks.tolist()
                                        )  
                        
                    result.append (output_file)
                    
                if self.debug:
                    cv2.imwrite(debug_output_file, debug_image )
                    
                return result       
        return None
    def upgradeToFaceSamples(samples):
        sample_list = []

        for s in tqdm(samples, desc="Loading"):

            s_filename_path = Path(s.filename)
            if s_filename_path.suffix != '.png':
                print("%s is not a png file required for training" %
                      (s_filename_path.name))
                continue

            dflpng = DFLPNG.load(str(s_filename_path),
                                 print_on_no_embedded_data=True)
            if dflpng is None:
                continue

            sample_list.append(
                s.copy_and_set(sample_type=SampleType.FACE,
                               face_type=FaceType.fromString(
                                   dflpng.get_face_type()),
                               shape=dflpng.get_shape(),
                               landmarks=dflpng.get_landmarks(),
                               yaw=dflpng.get_yaw_value()))

        return sample_list
Exemple #3
0
    def onClientProcessData(self, data):
        filepath = Path(data[0])

        try:
            if filepath.suffix != '.png':
                raise Exception(
                    "%s is not a png file required for sort_final" %
                    (filepath.name))

            dflpng = DFLPNG.load(str(filepath), print_on_no_embedded_data=True)
            if dflpng is None:
                raise Exception("")

            bgr = cv2.imread(str(filepath))
            if bgr is None:
                raise Exception("Unable to load %s" % (filepath.name))

            gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
            gray_masked = (gray * LandmarksProcessor.get_image_hull_mask(
                bgr.shape, dflpng.get_landmarks())[:, :, 0]).astype(np.uint8)
            sharpness = estimate_sharpness(gray_masked)
            hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
        except Exception as e:
            print(e)
            return [1, [str(filepath)]]

        return [0, [str(filepath), sharpness, hist, dflpng.get_yaw_value()]]
Exemple #4
0
def add_landmarks_debug_images(input_path):
    print("Adding landmarks debug images...")

    for filepath in tqdm(Path_utils.get_image_paths(input_path),
                         desc="Processing",
                         ascii=True):
        filepath = Path(filepath)

        img = cv2_imread(str(filepath))

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath), print_on_no_embedded_data=True)
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath), print_on_no_embedded_data=True)
        else:
            print("%s is not a dfl image file" % (filepath.name))
            continue

        if not (dflimg is None or img is None):
            face_landmarks = dflimg.get_landmarks()
            LandmarksProcessor.draw_landmarks(img, face_landmarks)

            output_file = '{}{}'.format(
                str(Path(str(input_path)) / filepath.stem), '_debug.jpg')
            cv2_imwrite(output_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
Exemple #5
0
def sort_by_face_pitch(input_path):
    io.log_info("Sorting by face pitch...")
    img_list = []
    trash_img_list = []
    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Loading"):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        if dflimg is None:
            io.log_err("%s is not a dfl image file" % (filepath.name))
            trash_img_list.append([str(filepath)])
            continue

        pitch_yaw_roll = dflimg.get_pitch_yaw_roll()
        if pitch_yaw_roll is not None:
            pitch, yaw, roll = pitch_yaw_roll
        else:
            pitch, yaw, roll = LandmarksProcessor.estimate_pitch_yaw_roll(
                dflimg.get_landmarks())

        img_list.append([str(filepath), pitch])

    io.log_info("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True)

    return img_list, trash_img_list
Exemple #6
0
def sort_by_face(input_path):

    print ("Sorting by face similarity...")

    img_list = []
    for filepath in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"):
        filepath = Path(filepath)
        
        if filepath.suffix != '.png':
            print ("%s is not a png file required for sort_by_face" % (filepath.name) ) 
            continue
        
        dflpng = DFLPNG.load (str(filepath), print_on_no_embedded_data=True)
        if dflpng is None:
            continue
        
        img_list.append( [str(filepath), dflpng.get_landmarks()] )
        

    img_list_len = len(img_list)
    for i in tqdm ( range(0, img_list_len-1), desc="Sorting"):
        min_score = float("inf")
        j_min_score = i+1
        for j in range(i+1,len(img_list)):

            fl1 = img_list[i][1]
            fl2 = img_list[j][1]
            score = np.sum ( np.absolute ( (fl2 - fl1).flatten() ) )

            if score < min_score:
                min_score = score
                j_min_score = j
        img_list[i+1], img_list[j_min_score] = img_list[j_min_score], img_list[i+1]

    return img_list
Exemple #7
0
        def process_data(self, data):
            filepath = Path(data[0])

            try:
                if filepath.suffix == '.png':
                    dflimg = DFLPNG.load(str(filepath))
                elif filepath.suffix == '.jpg':
                    dflimg = DFLJPG.load(str(filepath))
                else:
                    dflimg = None

                if dflimg is None:
                    self.log_err("%s is not a dfl image file" %
                                 (filepath.name))
                    return [1, [str(filepath)]]

                bgr = cv2_imread(str(filepath))
                if bgr is None:
                    raise Exception("Unable to load %s" % (filepath.name))

                gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
                sharpness = estimate_sharpness(
                    gray) if self.include_by_blur else 0
                pitch, yaw, roll = LandmarksProcessor.estimate_pitch_yaw_roll(
                    dflimg.get_landmarks())

                hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
            except Exception as e:
                self.log_err(e)
                return [1, [str(filepath)]]

            return [0, [str(filepath), sharpness, hist, yaw]]
Exemple #8
0
def sort_by_hist_dissim(input_path):
    io.log_info("Sorting by histogram dissimilarity...")

    img_list = []
    trash_img_list = []
    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Loading"):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        image = cv2_imread(str(filepath))

        if dflimg is not None:
            face_mask = LandmarksProcessor.get_image_hull_mask(
                image.shape, dflimg.get_landmarks())
            image = (image * face_mask).astype(np.uint8)

        img_list.append([
            str(filepath),
            cv2.calcHist([cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)], [0], None,
                         [256], [0, 256]), 0
        ])

    img_list = HistDissimSubprocessor(img_list).run()

    io.log_info("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True)

    return img_list, trash_img_list
Exemple #9
0
def sort_by_origname(input_path):
    io.log_info("Sort by original filename...")

    img_list = []
    trash_img_list = []
    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Loading"):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        if dflimg is None:
            io.log_err("%s is not a dfl image file" % (filepath.name))
            trash_img_list.append([str(filepath)])
            continue

        img_list.append([str(filepath), dflimg.get_source_filename()])

    io.log_info("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(1))
    return img_list, trash_img_list
Exemple #10
0
def convert_png_to_jpg_file(filepath):
    filepath = Path(filepath)

    if filepath.suffix != '.png':
        return
    dflpng = DFLPNG.load(str(filepath), print_on_no_embedded_data=True)
    if dflpng is None:
        return

    dfl_dict = dflpng.getDFLDictData()

    img = cv2_imread(str(filepath))
    new_filepath = str(filepath.parent / (filepath.stem + '.jpg'))
    cv2_imwrite(new_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 85])

    DFLJPG.embed_data(new_filepath,
                      face_type=dfl_dict.get('face_type', None),
                      landmarks=dfl_dict.get('landmarks', None),
                      yaw_value=dfl_dict.get('yaw_value', None),
                      pitch_value=dfl_dict.get('pitch_value', None),
                      source_filename=dfl_dict.get('source_filename', None),
                      source_rect=dfl_dict.get('source_rect', None),
                      source_landmarks=dfl_dict.get('source_landmarks', None))

    filepath.unlink()
Exemple #11
0
    def onClientProcessData(self, data):
        filename_path = Path(data)

        files_processed = 1
        faces_processed = 0
            
        output_filename_path = self.output_path / (filename_path.stem + '.png')

        if self.converter.get_mode() == ConverterBase.MODE_FACE and filename_path.stem not in self.alignments.keys():                    
            if not self.debug:
                print ( 'no faces found for %s, copying without faces' % (filename_path.name) )
                shutil.copy ( str(filename_path), str(output_filename_path) )
        else:
            image = (cv2_imread(str(filename_path)) / 255.0).astype(np.float32)

            if self.converter.get_mode() == ConverterBase.MODE_IMAGE:
                image = self.converter.convert_image(image, None, self.debug)
                if self.debug:
                    for img in image:
                        cv2.imshow ('Debug convert', img )
                        cv2.waitKey(0)
                faces_processed = 1
            elif self.converter.get_mode() == ConverterBase.MODE_IMAGE_WITH_LANDMARKS:
                if filename_path.suffix == '.png':
                    dflimg = DFLPNG.load( str(filename_path), throw_on_no_embedded_data=True )
                elif filename_path.suffix == '.jpg':
                    dflimg = DFLJPG.load ( str(filename_path), throw_on_no_embedded_data=True )
                else:
                    raise Exception ("%s is not a dfl image file" % (filename_path.name) ) 
            
                image_landmarks = dflimg.get_landmarks()
                        
                image = self.converter.convert_image(image, image_landmarks, self.debug)
                if self.debug:
                    for img in image:
                        cv2.imshow ('Debug convert', img )
                        cv2.waitKey(0)
                faces_processed = 1
            elif self.converter.get_mode() == ConverterBase.MODE_FACE:
                faces = self.alignments[filename_path.stem]
                for face_num, image_landmarks in enumerate(faces):
                    try:
                        if self.debug:
                            print ( '\nConverting face_num [%d] in file [%s]' % (face_num, filename_path) )
                        
                        image = self.converter.convert_face(image, image_landmarks, self.debug)     
                        if self.debug:
                            for img in image:
                                cv2.imshow ('Debug convert', (img*255).astype(np.uint8) )
                                cv2.waitKey(0)
                    except Exception as e:
                        print ( 'Error while converting face_num [%d] in file [%s]: %s' % (face_num, filename_path, str(e)) )
                        traceback.print_exc()
                faces_processed = len(faces)
                    
            if not self.debug:
                cv2_imwrite (str(output_filename_path), (image*255).astype(np.uint8) )
            
            
        return (files_processed, faces_processed)
Exemple #12
0
def sort_by_face_dissim(input_path):

    print ("Sorting by face dissimilarity...")

    img_list = []
    for filepath in tqdm( Path_utils.get_image_paths(input_path), desc="Loading"):
        filepath = Path(filepath)
        
        if filepath.suffix != '.png':
            print ("%s is not a png file required for sort_by_face_dissim" % (filepath.name) ) 
            continue
        
        dflpng = DFLPNG.load (str(filepath), print_on_no_embedded_data=True)
        if dflpng is None:
            continue
        
        img_list.append( [str(filepath), dflpng.get_landmarks(), 0 ] )
        
    img_list_len = len(img_list)
    for i in tqdm( range(0, img_list_len-1), desc="Sorting"):
        score_total = 0
        for j in range(i+1,len(img_list)):
            if i == j:
                continue
            fl1 = img_list[i][1]
            fl2 = img_list[j][1]
            score_total += np.sum ( np.absolute ( (fl2 - fl1).flatten() ) )

        img_list[i][2] = score_total

    print ("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True)

    return img_list
Exemple #13
0
def convert_png_to_jpg_file(filepath):
    filepath = Path(filepath)

    if filepath.suffix != '.png':
        return

    dflpng = DFLPNG.load(str(filepath))
    if dflpng is None:
        io.log_err("%s is not a dfl image file" % (filepath.name))
        return

    dfl_dict = dflpng.getDFLDictData()

    img = cv2_imread(str(filepath))
    new_filepath = str(filepath.parent / (filepath.stem + '.jpg'))
    cv2_imwrite(new_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 85])

    DFLJPG.embed_data(new_filepath,
                      face_type=dfl_dict.get('face_type', None),
                      landmarks=dfl_dict.get('landmarks', None),
                      source_filename=dfl_dict.get('source_filename', None),
                      source_rect=dfl_dict.get('source_rect', None),
                      source_landmarks=dfl_dict.get('source_landmarks', None))

    filepath.unlink()
Exemple #14
0
        def process_data(self, data):        
            filepath = Path(data[0])     

            try:
                if filepath.suffix == '.png':
                    dflimg = DFLPNG.load( str(filepath) )
                elif filepath.suffix == '.jpg':
                    dflimg = DFLJPG.load( str(filepath) )
                else:
                    dflimg = None
                    
                if dflimg is None:
                    self.log_err("%s is not a dfl image file" % (filepath.name))
                    return [ 1, [str(filepath)] ]
                
                bgr = cv2_imread(str(filepath))
                if bgr is None:
                    raise Exception ("Unable to load %s" % (filepath.name) ) 
                    
                gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)        
                gray_masked = ( gray * LandmarksProcessor.get_image_hull_mask (bgr.shape, dflimg.get_landmarks() )[:,:,0] ).astype(np.uint8)
                sharpness = estimate_sharpness(gray_masked)
                pitch, yaw = LandmarksProcessor.estimate_pitch_yaw ( dflimg.get_landmarks() )
                
                hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
            except Exception as e:
                self.log_err (e)
                return [ 1, [str(filepath)] ]
                
            return [ 0, [str(filepath), sharpness, hist, yaw ] ]
Exemple #15
0
def extract_fanseg(input_dir, device_args={}):
    multi_gpu = device_args.get('multi_gpu', False)
    cpu_only = device_args.get('cpu_only', False)

    input_path = Path(input_dir)
    if not input_path.exists():
        raise ValueError('Input directory not found. Please ensure it exists.')

    paths_to_extract = []
    for filename in Path_utils.get_image_paths(input_path):
        filepath = Path(filename)
        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        if dflimg is not None:
            paths_to_extract.append(filepath)

    paths_to_extract_len = len(paths_to_extract)
    if paths_to_extract_len > 0:
        io.log_info("Performing extract fanseg for %d files..." %
                    (paths_to_extract_len))
        data = ExtractSubprocessor([
            ExtractSubprocessor.Data(filename) for filename in paths_to_extract
        ],
                                   'fanseg',
                                   multi_gpu=multi_gpu,
                                   cpu_only=cpu_only).run()
Exemple #16
0
def recover_original_aligned_filename(input_path):
    io.log_info("Recovering original aligned filename...")

    files = []
    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Processing"):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        if dflimg is None:
            io.log_err("%s is not a dfl image file" % (filepath.name))
            continue

        files += [[filepath, None, dflimg.get_source_filename(), False]]

    files_len = len(files)
    for i in io.progress_bar_generator(range(files_len), "Sorting"):
        fp, _, sf, converted = files[i]

        if converted:
            continue

        sf_stem = Path(sf).stem

        files[i][1] = fp.parent / (sf_stem + '_0' + fp.suffix)
        files[i][3] = True
        c = 1

        for j in range(i + 1, files_len):
            fp_j, _, sf_j, converted_j = files[j]
            if converted_j:
                continue

            if sf_j == sf:
                files[j][1] = fp_j.parent / (sf_stem + ('_%d' %
                                                        (c)) + fp_j.suffix)
                files[j][3] = True
                c += 1

    for file in io.progress_bar_generator(files, "Renaming", leave=False):
        fs, _, _, _ = file
        dst = fs.parent / (fs.stem + '_tmp' + fs.suffix)
        try:
            fs.rename(dst)
        except:
            io.log_err('fail to rename %s' % (fs.name))

    for file in io.progress_bar_generator(files, "Renaming"):
        fs, fd, _, _ = file
        fs = fs.parent / (fs.stem + '_tmp' + fs.suffix)
        try:
            fs.rename(fd)
        except:
            io.log_err('fail to rename %s' % (fs.name))
Exemple #17
0
    def upgradeToFaceSamples ( samples ):
        sample_list = []
        
        for s in io.progress_bar_generator(samples, "Loading"):
            s_filename_path = Path(s.filename)
            try:
                if s_filename_path.suffix == '.png':
                    dflimg = DFLPNG.load ( str(s_filename_path) )
                elif s_filename_path.suffix == '.jpg':
                    dflimg = DFLJPG.load ( str(s_filename_path) )
                else:
                    dflimg = None
                    
                if dflimg is None:
                    print ("%s is not a dfl image file required for training" % (s_filename_path.name) ) 
                    continue
                    
                pitch, yaw = LandmarksProcessor.estimate_pitch_yaw ( dflimg.get_landmarks() )

                sample_list.append( s.copy_and_set(sample_type=SampleType.FACE,
                                                   face_type=FaceType.fromString (dflimg.get_face_type()),
                                                   shape=dflimg.get_shape(), 
                                                   landmarks=dflimg.get_landmarks(),
                                                   pitch=pitch,
                                                   yaw=yaw) )
            except:
                print ("Unable to load %s , error: %s" % (str(s_filename_path), traceback.format_exc() ) )
                
        return sample_list 
Exemple #18
0
def sort_by_hist_dissim(input_path):
    print("Sorting by histogram dissimilarity...")

    img_list = []
    for filepath in tqdm(Path_utils.get_image_paths(input_path),
                         desc="Loading",
                         ascii=True):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath), print_on_no_embedded_data=True)
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath), print_on_no_embedded_data=True)
        else:
            print("%s is not a dfl image file" % (filepath.name))
            continue

        image = cv2.imread(str(filepath))
        face_mask = LandmarksProcessor.get_image_hull_mask(
            image.shape, dflimg.get_landmarks())
        image = (image * face_mask).astype(np.uint8)

        img_list.append([
            str(filepath),
            cv2.calcHist([cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)], [0], None,
                         [256], [0, 256]), 0
        ])

    img_list = HistDissimSubprocessor(img_list).process()

    print("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True)

    return img_list
Exemple #19
0
def sort_by_face_pitch(input_path):
    print("Sorting by face pitch...")
    img_list = []
    for filepath in tqdm(Path_utils.get_image_paths(input_path),
                         desc="Loading",
                         ascii=True):
        filepath = Path(filepath)

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath), print_on_no_embedded_data=True)
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath), print_on_no_embedded_data=True)
        else:
            print("%s is not a dfl image file" % (filepath.name))
            continue

        pitch, yaw = LandmarksProcessor.estimate_pitch_yaw(
            dflimg.get_landmarks())

        img_list.append([str(filepath), pitch])

    print("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True)

    return img_list
Exemple #20
0
def sort_by_hist_dissim(input_path):
    print("Sorting by histogram dissimilarity...")

    img_list = []
    for filename_path in tqdm(Path_utils.get_image_paths(input_path),
                              desc="Loading"):
        image = cv2.imread(filename_path)

        dflpng = DFLPNG.load(str(filename_path))
        if dflpng is not None:
            face_mask = LandmarksProcessor.get_image_hull_mask(
                image, dflpng.get_landmarks())
            image = (image * face_mask).astype(np.uint8)

        img_list.append([
            filename_path,
            cv2.calcHist([cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)], [0], None,
                         [256], [0, 256]), 0
        ])

    img_list = HistDissimSubprocessor(img_list).process()

    print("Sorting...")
    img_list = sorted(img_list, key=operator.itemgetter(2), reverse=True)

    return img_list
Exemple #21
0
def add_landmarks_debug_images(input_path):
    io.log_info("Adding landmarks debug images...")

    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Processing"):
        filepath = Path(filepath)

        img = cv2_imread(str(filepath))

        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None

        if dflimg is None:
            io.log_err("%s is not a dfl image file" % (filepath.name))
            continue

        if img is not None:
            face_landmarks = dflimg.get_landmarks()
            LandmarksProcessor.draw_landmarks(img, face_landmarks)

            output_file = '{}{}'.format(
                str(Path(str(input_path)) / filepath.stem), '_debug.jpg')
            cv2_imwrite(output_file, img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
Exemple #22
0
    def onClientProcessData(self, data):
        filename_path = Path( data[0] )

        dflpng = DFLPNG.load( str(filename_path), print_on_no_embedded_data=True )        
        if dflpng is not None:
            image = cv2.imread( str(filename_path) )
            image = ( image * \
                      LandmarksProcessor.get_image_hull_mask (image, dflpng.get_landmarks()) \
                     ).astype(np.uint8)
            return [ str(filename_path), estimate_sharpness( image ) ]
        else:
            return [ str(filename_path), 0 ]
Exemple #23
0
def get_pitch_yaw_roll(input_path, r=0.05):
    import os
    import numpy as np
    import cv2
    from shutil import copyfile
    from pathlib import Path
    from utils import Path_utils
    from utils.DFLPNG import DFLPNG
    from utils.DFLJPG import DFLJPG
    from facelib import LandmarksProcessor
    from joblib import Subprocessor
    import multiprocessing
    from interact import interact as io
    from imagelib import estimate_sharpness
    io.log_info("Sorting by face yaw...")
    img_list = []
    trash_img_list = []
    for filepath in io.progress_bar_generator(
            Path_utils.get_image_paths(input_path), "Loading"):
        filepath = Path(filepath)
        if filepath.suffix == '.png':
            dflimg = DFLPNG.load(str(filepath))
        elif filepath.suffix == '.jpg':
            dflimg = DFLJPG.load(str(filepath))
        else:
            dflimg = None
        if dflimg is None:
            io.log_err("%s is not a dfl image file" % (filepath.name))
            trash_img_list.append([str(filepath)])
            continue
        pitch, yaw, roll = LandmarksProcessor.estimate_pitch_yaw_roll(
            dflimg.get_landmarks())
        img_list.append([str(filepath), pitch, yaw, roll])

    img_list.sort(key=lambda item: item[1])
    with open(os.path.join(input_path, "_pitch_yaw_roll.csv"), "w") as f:
        for i in img_list:
            f.write("%s,%f,%f,%f\n" %
                    (os.path.basename(i[0]), i[1], i[2], i[3]))

    import cv
    width = 800
    img = cv.cv_new((width, width))
    xs = [i[1] for i in img_list]
    ys = [i[2] for i in img_list]
    cs = [(128, 128, 128)] * len(xs)
    rs = [int(r * width / 2)] * len(xs)
    cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs)
    cs = [(0xcc, 0x66, 0x33)] * len(xs)
    rs = [2] * len(xs)
    cv.cv_scatter(img, xs, ys, [-1, 1], [-1, 1], cs, rs)
    cv.cv_save(img, os.path.join(input_path, "_pitch_yaw_roll.bmp"))
    return img_list
Exemple #24
0
    def load_fanseg_mask(self):
        if self.fanseg_mask_exist:
            filepath = Path(self.filename)
            if filepath.suffix == '.png':
                dflimg = DFLPNG.load ( str(filepath) )
            elif filepath.suffix == '.jpg':
                dflimg = DFLJPG.load ( str(filepath) )
            else:
                dflimg = None
            return dflimg.get_fanseg_mask()

        return None
Exemple #25
0
def dfl_load_img(path):
    from pathlib import Path
    from utils.DFLPNG import DFLPNG
    from utils.DFLJPG import DFLJPG
    filepath = Path(path)
    if filepath.suffix == '.png':
        dflimg = DFLPNG.load(str(filepath))
    elif filepath.suffix == '.jpg':
        dflimg = DFLJPG.load(str(filepath))
    else:
        dflimg = None
    if dflimg is None:
        io.log_err("%s is not a dfl image file" % (filepath.name))
    return dflimg
Exemple #26
0
def convert_png_to_jpg_file (filepath):
    filepath = Path(filepath)
    
    if filepath.suffix != '.png': 
        return  
    dflpng = DFLPNG.load (str(filepath), print_on_no_embedded_data=True)
    if dflpng is None: 
        return
    
    dfl_dict = dflpng.getDFLDictData()
    
    img = cv2.imread (str(filepath))
    new_filepath = str(filepath.parent / (filepath.stem + '.jpg'))
    cv2.imwrite ( new_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 85])
    DFLJPG.embed_data( new_filepath, **dfl_dict )
    filepath.unlink()
Exemple #27
0
        def process_data(self, data):
            filepath = Path(data[0])

            if filepath.suffix == '.png':
                dflimg = DFLPNG.load(str(filepath))
            elif filepath.suffix == '.jpg':
                dflimg = DFLJPG.load(str(filepath))
            else:
                dflimg = None

            if dflimg is not None:
                image = cv2_imread(str(filepath))
                return [str(filepath), estimate_sharpness(image)]
            else:
                self.log_err("%s is not a dfl image file" % (filepath.name))
                return [str(filepath), 0]
Exemple #28
0
def remove_fanseg_file(filepath):
    filepath = Path(filepath)

    if filepath.suffix == '.png':
        dflimg = DFLPNG.load(str(filepath))
    elif filepath.suffix == '.jpg':
        dflimg = DFLJPG.load(str(filepath))
    else:
        return

    if dflimg is None:
        io.log_err("%s is not a dfl image file" % (filepath.name))
        return

    dflimg.remove_fanseg_mask()
    dflimg.embed_and_set(str(filepath))
Exemple #29
0
    def upgradeToFaceSamples(samples, silent=False):
        sample_list = []

        for s in (samples if silent else io.progress_bar_generator(
                samples, "Loading")):
            s_filename_path = Path(s.filename)
            try:
                if s_filename_path.suffix == '.png':
                    dflimg = DFLPNG.load(str(s_filename_path))
                elif s_filename_path.suffix == '.jpg':
                    dflimg = DFLJPG.load(str(s_filename_path))
                else:
                    dflimg = None

                if dflimg is None:
                    print("%s is not a dfl image file required for training" %
                          (s_filename_path.name))
                    continue

                landmarks = dflimg.get_landmarks()
                pitch_yaw_roll = dflimg.get_pitch_yaw_roll()
                eyebrows_expand_mod = dflimg.get_eyebrows_expand_mod()

                if pitch_yaw_roll is None:
                    pitch_yaw_roll = LandmarksProcessor.estimate_pitch_yaw_roll(
                        landmarks)

                sample_list.append(
                    s.copy_and_set(
                        sample_type=SampleType.FACE,
                        face_type=FaceType.fromString(dflimg.get_face_type()),
                        shape=dflimg.get_shape(),
                        landmarks=landmarks,
                        ie_polys=dflimg.get_ie_polys(),
                        pitch_yaw_roll=pitch_yaw_roll,
                        eyebrows_expand_mod=eyebrows_expand_mod,
                        source_filename=dflimg.get_source_filename(),
                        fanseg_mask_exist=dflimg.get_fanseg_mask() is not None,
                    ))
            except:
                print("Unable to load %s , error: %s" %
                      (str(s_filename_path), traceback.format_exc()))

        return sample_list
Exemple #30
0
 def process_data(self, data):
     filepath = Path( data[0] )
 
     if filepath.suffix == '.png':
         dflimg = DFLPNG.load( str(filepath) )
     elif filepath.suffix == '.jpg':
         dflimg = DFLJPG.load ( str(filepath) )
     else:
         dflimg = None
         
     if dflimg is not None:
         image = cv2_imread( str(filepath) )
         image = ( image * \
                   LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \
                  ).astype(np.uint8)
         return [ str(filepath), estimate_sharpness( image ) ]
     else:
         self.log_err ("%s is not a dfl image file" % (filepath.name) ) 
         return [ str(filepath), 0 ]