예제 #1
0
 def test_resize(self):
     l = cv2.imread(lenna_path)
     m = cv2.imread(mandrill_path)
     imgs = IMG.size2x([l, m])
     self.assertEqual(imgs[0].shape, (512, 512, 3))
     self.assertEqual(imgs[1].shape, (512, 512, 3))
     self.assertEqual(IMG.resize(l, -1).shape, (256, 256, 3))
     self.assertEqual(IMG.resize(l, 2).shape, (512, 512, 3))
     self.assertEqual(IMG.resize(l, 1.5).shape, (384, 384, 3))
     self.assertEqual(IMG.resize(l, 0.5).shape, (128, 128, 3))
예제 #2
0
파일: predict.py 프로젝트: ka10ryu1/duotone
def predict(model, data, batch, org_shape, rate, gpu):
    """
    推論実行メイン部
    [in]  model:     推論実行に使用するモデル
    [in]  data:      分割(IMG.split)されたもの
    [in]  batch:     バッチサイズ
    [in]  org_shape: 分割前のshape
    [in]  rate:      出力画像の拡大率
    [in]  gpu:       GPU ID
    [out] img:       推論実行で得られた生成画像
    """

    # dataには圧縮画像と分割情報が含まれているので、分離する
    comp, size = data
    imgs = []
    st = time.time()
    # バッチサイズごとに学習済みモデルに入力して画像を生成する
    for i in range(0, len(comp), batch):
        x = IMG.imgs2arr(comp[i:i + batch], gpu=gpu)
        y = model.predictor(x)
        imgs.extend(IMG.arr2imgs(to_cpu(y.array)))

    print('exec time: {0:.2f}[s]'.format(time.time() - st))
    # 生成された画像を分割情報をもとに結合する
    buf = [
        np.vstack(imgs[i * size[0]:(i + 1) * size[0]]) for i in range(size[1])
    ]
    img = np.hstack(buf)
    # 出力画像は入力画像の2倍の大きさになっているので半分に縮小する
    img = IMG.resize(img, 1 / rate)
    # 結合しただけでは画像サイズがオリジナルと異なるので切り取る
    return img[:org_shape[0], :org_shape[1]]
예제 #3
0
 def on_modified(self, event):
     path, name, ext = super().on_modified(event)
     if ('jpg' in ext.lower()):
         print('duotone:{0}'.format(os.path.join(self.copy, path)))
         time.sleep(1)
         # 学習モデルを入力画像ごとに実行する
         with chainer.using_config('train', False):
             img = IMG.resize(cv2.imread(path, IMG.getCh(3)), self.rate)
             img = predict(self.model, IMG.splitSQ(img, self.size),
                           self.batch, img.shape, self.gpu)
             cv2.imwrite(os.path.join(self.copy, name), img)
예제 #4
0
def stackImages(imgs, rate):
    """
    推論実行で得られた 画像のリストを結合してサイズを調整する
    [in]  imgs:    入力画像リスト
    [in]  rate: リサイズする倍率
    [out] 結合画像
    """

    img = np.hstack([
        cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) if len(img.shape) < 3 else img
        for img in imgs
    ])
    return IMG.resize(img, rate)
예제 #5
0
파일: predict.py 프로젝트: ka10ryu1/duotone
def main(args):
    # jsonファイルから学習モデルのパラメータを取得する
    net, unit, ch, size, layer, sr, af1, af2 = GET.modelParam(args.param)
    # 学習モデルを生成する
    if net == 0:
        from Lib.network import JC_DDUU as JC
    else:
        from Lib.network2 import JC_UDUD as JC

    model = L.Classifier(
        JC(n_unit=unit, n_out=1, rate=sr, actfun1=af1, actfun2=af2))

    # load_npzのpath情報を取得し、学習済みモデルを読み込む
    load_path = F.checkModelType(args.model)
    try:
        chainer.serializers.load_npz(args.model, model, path=load_path)
    except:
        import traceback
        traceback.print_exc()
        print(F.fileFuncLine())
        exit()

    # GPUの設定
    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()
    else:
        model.to_intel64()

    # 高圧縮画像の生成
    org_imgs = [
        cv2.imread(name, IMG.getCh(ch)) for name in args.jpeg
        if IMG.isImgPath(name)
    ]
    imgs = []
    # 学習モデルを入力画像ごとに実行する
    for i, ei in enumerate(org_imgs):
        with chainer.using_config('train', False):
            img = predict(model, IMG.splitSQ(ei, size), args.batch, ei.shape,
                          sr, args.gpu)

        imgs.append(IMG.resize(img, args.out_rate))

    for img in imgs:
        cv2.imshow('view', img)
        cv2.waitKey()
        # 生成結果を保存する
        name = F.getFilePath(args.out_path, 'comp-' + str(i * 10 + 1).zfill(3),
                             '.jpg')
        print('save:', name)
        cv2.imwrite(name, img)
예제 #6
0
def main(args):
    # jsonファイルから学習モデルのパラメータを取得する
    net, unit, ch, size, layer, sr, af1, af2 = GET.modelParam(args.param)
    # 学習モデルを生成する
    model = L.Classifier(
        JC(n_unit=unit, n_out=ch, rate=sr, actfun1=af1, actfun2=af2))

    # load_npzのpath情報を取得し、学習済みモデルを読み込む
    load_path = F.checkModelType(args.model)
    try:
        chainer.serializers.load_npz(args.model, model, path=load_path)
    except:
        import traceback
        traceback.print_exc()
        print(F.fileFuncLine())
        exit()

    # GPUの設定
    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()
    else:
        model.to_intel64()

    # 画像の生成
    if args.image == '':
        x, _ = create(args.other_path, args.human_path, args.background_path,
                      args.obj_size, args.img_size, args.obj_num, 1, 1)
    elif IMG.isImgPath(args.image):
        x = cv2.imread(args.image, IMG.getCh(0))
        w, h = x.shape[:2]
        x = np.array(x).reshape(1, w, h, -1)
    else:
        print('input image path is not found:', args.image)
        exit()

    # 学習モデルを実行する
    with chainer.using_config('train', False):
        img = IMG.resize(predict(model, x, args.batch, args.gpu), 1 / sr)

    # 生成結果を保存する
    name = F.getFilePath(args.out_path, 'predict', '.jpg')
    print('save:', name)
    img = np.hstack([x[0], img])
    cv2.imwrite(name, img)
    cv2.imshow(name, img)
    cv2.waitKey()