コード例 #1
0
def change_avatar(token):
    try:
        vprint("Processes: ", app.processes)
        vprint("Predictors: ", app.predictors)
        IMG_SIZE = 256
        ava_f = request.files['avatar']
        if ava_f is not None:
            ava_g = ava_f.read()
            ava_np = np.fromstring(ava_g, np.uint8)
            ava_h = cv2.imdecode(ava_np, cv2.IMREAD_COLOR)
            if token in app.processes:
                predictor = app.processes[token]['predictor']
                if ava_h.ndim == 2:
                    ava_h = np.tile(ava_h[..., None], [1, 1, 3])
                ava_h = ava_h[..., :3][..., ::-1]
                ava_h = resize(ava_h, (IMG_SIZE, IMG_SIZE))
                vprint('set_source_image')
                predictor.set_source_image(ava_h)
                vprint('finished set_source_image')
                vprint('reset_frames')
                predictor.reset_frames()
                vprint('finished reset_frames')
                return avatar_response(status=afy_flask_avatar_status.SUCCESS)
            return avatar_response(status=afy_flask_avatar_status.NO_PREDICTOR,
                                   error="Predictor not available")
        return avatar_response(
            status=afy_flask_avatar_status.INPUT_IMAGE_ERROR,
            error="Invalid image / image corrupted")
    except Exception as e:
        if app.verbose:
            traceback.print_exc()
        return avatar_response(error=str(e))
コード例 #2
0
def is_new_frame_better(source, driving, predictor):
    global avatar_kp
    global display_string

    if avatar_kp is None:
        display_string = "No face detected in avatar."
        return False

    if predictor.get_start_frame() is None:
        display_string = "No frame to compare to."
        return True

    driving_smaller = resize(driving, (128, 128))[..., :3]
    new_kp = predictor.get_frame_kp(driving)

    if new_kp is not None:
        new_norm = (np.abs(avatar_kp - new_kp)**2).sum()
        old_norm = (np.abs(avatar_kp -
                           predictor.get_start_frame_kp())**2).sum()

        out_string = "{0} : {1}".format(int(new_norm * 100),
                                        int(old_norm * 100))
        display_string = out_string
        log(out_string)

        return new_norm < old_norm
    else:
        display_string = "No face found!"
        return False
コード例 #3
0
def load_stylegan_avatar():
    url = "https://thispersondoesnotexist.com/image"
    r = requests.get(url, headers={'User-Agent': "My User Agent 1.0"}).content

    image = np.frombuffer(r, np.uint8)
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    image = resize(image, (IMG_SIZE, IMG_SIZE))

    return image
コード例 #4
0
ファイル: cam_fomm.py プロジェクト: SkywalkerAtlas/avatarify
def load_stylegan_avatar():
    img_id = np.random.randint(0, 50000)
    print('select waifu {}'.format(img_id))
    url = "http://thiswaifudoesnotexist.net/v2/example-{}.jpg".format(img_id)
    r = requests.get(url, headers={'User-Agent': "My User Agent 1.0"}).content

    image = np.frombuffer(r, np.uint8)
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    image = resize(image, (IMG_SIZE, IMG_SIZE))

    return image
コード例 #5
0
def load_images(IMG_SIZE=256):
    avatars = []
    filenames = []
    images_list = sorted(glob.glob(f'{opt.avatars}/*'))
    for i, f in enumerate(images_list):
        if f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.png'):
            img = cv2.imread(f)
            if img.ndim == 2:
                img = np.tile(img[..., None], [1, 1, 3])
            img = img[..., :3][..., ::-1]
            img = resize(img, (IMG_SIZE, IMG_SIZE))
            avatars.append(img)
            filenames.append(f)
    return avatars, filenames
コード例 #6
0
def predict(token):
    try:
        vprint("Processes: ", app.processes)
        vprint("Predictors: ", app.predictors)
        IMG_SIZE = 256
        frame_proportion = 0.9
        frame_offset_x = 0
        frame_offset_y = 0
        img = request.files['image']
        if img is not None:
            img_g = img.read()
            img_np = np.fromstring(img_g, np.uint8)
            frame = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
            if token in app.processes:
                predictor = app.processes[token]['predictor']
                frame = frame[..., ::-1]
                frame_orig = frame.copy()
                frame, lrudwh = crop(frame,
                                     p=frame_proportion,
                                     offset_x=frame_offset_x,
                                     offset_y=frame_offset_y)
                frame_lrudwh = lrudwh
                frame = resize(frame, (IMG_SIZE, IMG_SIZE))[..., :3]
                out = predictor.predict(frame)
                if out is not None:
                    out = cv2.cvtColor(out, cv2.COLOR_BGR2RGB)
                    _, out = cv2.imencode('.jpg', out)
                    out = out.tobytes()
                    out = base64.b64encode(out).decode("utf-8")
                    return predict_response(
                        status=afy_flask_predict_status.SUCCESS, image=out)
                return predict_response(
                    status=afy_flask_predict_status.SUCCESS)
            return predict_response(
                status=afy_flask_predict_status.NO_PREDICTOR,
                error="Predictor not available")
        return predict_response(
            status=afy_flask_predict_status.INPUT_IMAGE_ERROR,
            error="Invalid image / image corrupted")
    except Exception as e:
        if app.verbose:
            traceback.print_exc()
        return predict_response(error=str(e))
コード例 #7
0
            tt.tic()

            ret, frame = cap.read()
            if not ret:
                log("Can't receive frame (stream end?). Exiting ...")
                break

            frame = frame[..., ::-1]
            frame_orig = frame.copy()

            frame, lrudwh = crop(frame,
                                 p=frame_proportion,
                                 offset_x=frame_offset_x,
                                 offset_y=frame_offset_y)
            frame_lrudwh = lrudwh
            frame = resize(frame, (IMG_SIZE, IMG_SIZE))[..., :3]

            if find_keyframe:
                if is_new_frame_better(avatar, frame, predictor):
                    log("Taking new frame!")
                    green_overlay = True
                    predictor.reset_frames()

            timing['preproc'] = tt.toc()

            if passthrough:
                out = frame
            elif is_calibrated:
                tt.tic()
                out = predictor.predict(frame)
                if out is None: