Пример #1
0
def test_progress_iter():
    with capture_output(display=False) as captured:
        for i in display.ProgressBar(5):
            out = captured.stdout
            nt.assert_in('{0}/5'.format(i), out)
    out = captured.stdout
    nt.assert_in('5/5', out)
Пример #2
0
def progress(n, width=50):
    """Display progress bar for long running operations.

    :param n: total number of steps to completion
    :param width: width of the progress bar (only for the text version)

    >>> import arlpy
    >>> progress = arlpy.utils.progress(100)
    >>> for j in range(100):
            next(progress)
    """
    if _notebook:
        import IPython.display as _ipyd
        p = _ipyd.ProgressBar(total=n)
        did = str(_uuid.uuid4())
        _ipyd.display(p, display_id=did)
        for j in range(1, n):
            p.progress = j
            _ipyd.update_display(p, display_id=did)
            yield j
        _ipyd.update_display(_ipyd.HTML(''), display_id=did)
        yield None
    else:
        _sys.stdout.write('%s|\n' % ('-' * width))
        _sys.stdout.flush()
        c = 0
        for j in range(n):
            c1 = int(width * (j + 1) / n)
            if c1 > c:
                _sys.stdout.write('>' * (c1 - c))
                c = c1
                if c == width:
                    _sys.stdout.write('\n')
                _sys.stdout.flush()
            yield j
Пример #3
0
def test_progress_iter():
    with capture_output(display=False) as captured:
        for i in display.ProgressBar(5):
            out = captured.stdout
            assert "{0}/5".format(i) in out
    out = captured.stdout
    assert "5/5" in out
Пример #4
0
def test_progress():
    p = display.ProgressBar(10)
    assert "0/10" in repr(p)
    p.html_width = "100%"
    p.progress = 5
    assert (p._repr_html_() ==
            "<progress style='width:100%' max='10' value='5'></progress>")
Пример #5
0
def test_progress():
    p = display.ProgressBar(10)
    nt.assert_in('0/10', repr(p))
    p.html_width = '100%'
    p.progress = 5
    nt.assert_equal(
        p._repr_html_(),
        "<progress style='width:100%' max='10' value='5'></progress>")
def face_aligner(face_image_paths,
                 images_directory_output=None,
                 output_size=256,
                 limit_num_faces_=None,
                 limit_num_files_=None):
    def write_faces_to_disk(directory, faces):
        print("writing faces to disk...")
        if os.path.exists(directory):
            shutil.rmtree(directory)
        print('creating output directory: %s' % (directory))
        os.mkdir(directory)
        for i in range(faces.shape[0]):
            cv2.imwrite(''.join([directory, "%03d.jpg" % i]),
                        faces[i, :, :, ::-1])
        print("wrote %d faces" % (faces.shape[0]))

    if images_directory_output and images_directory_output[-1] != '/':
        images_directory_output += '/'

    faces = []

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(
        '/Users/ost437/Documents/OneDrive/workspace/WorkspaceLiClipse/semi-adversarial-networks/shape_predictor_68_face_landmarks.dat'
    )

    max_val = len(
        face_image_paths) if limit_num_files_ is None else limit_num_files_
    pb = display.ProgressBar(max_val)
    pb.display()

    face_counter = 0
    for img_idx, img_file_path in enumerate(face_image_paths):
        # load the input image, resize it, and convert it to grayscale
        image = cv2.imread(img_file_path)

        if image is None:
            continue

        image = image[:, :, ::-1]  # BGR to RGB
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # detect faces in the grayscale image
        rects = detector(gray, 1)

        if len(rects) > 0:
            # loop over the face detections
            for (i, rect) in enumerate(rects):
                ######### Align with facial features detector #########

                shape = predictor(gray, rect)  # get facial features
                shape = np.array([(shape.part(j).x, shape.part(j).y)
                                  for j in range(shape.num_parts)])

                # center and scale face around mid point between eyes
                center_eyes = shape[27].astype(np.int)
                eyes_d = np.linalg.norm(shape[36] - shape[45])
                face_size_x = int(eyes_d * 2.)
                if face_size_x < 50: continue

                # rotate to normalized angle
                d = (shape[45] - shape[36]
                     ) / eyes_d  # normalized eyes-differnce vector (direction)
                a = np.rad2deg(np.arctan2(d[1], d[0]))  # angle
                ###scale_factor = float(output_size) / float(face_size_x * 2.)  # scale to fit in output_size
                scale_factor = float(output_size) / float(
                    face_size_x)  # scale to fit in output_size
                # rotation (around center_eyes) + scale transform
                M = np.append(cv2.getRotationMatrix2D(
                    (center_eyes[0], center_eyes[1]), a, scale_factor),
                              [[0, 0, 1]],
                              axis=0)
                # apply shift from center_eyes to middle of output_size
                M1 = np.array([[1., 0., -center_eyes[0] + output_size / 2.],
                               [0., 1., -center_eyes[1] + output_size / 2.],
                               [0, 0, 1.]])
                # concatenate transforms (rotation-scale + translation)
                M = M1.dot(M)[:2]
                # warp
                try:
                    face = cv2.warpAffine(image,
                                          M, (output_size, output_size),
                                          borderMode=cv2.BORDER_REPLICATE)
                except:
                    continue
                face_counter += 1
                face = cv2.resize(face, (output_size, output_size))
                faces.append(face)
        pb.progress = img_idx + 1

        if limit_num_faces_ is not None and faces.shape[0] > limit_num_faces_:
            break
        if limit_num_files_ is not None and img_idx >= limit_num_files_:
            break

    faces = np.asarray(faces)

    if images_directory_output:
        write_faces_to_disk(images_directory_output, faces)
    return faces