Closed SikkeyHuang closed 5 years ago
I cannot get correct result from the python code, but when I use the command like ' autocolorize grayscale.png -o colorized.png' I get the correct results
My code is following:
import cv2 import os import autocolorize
path2 = 'input/' path3 = 'output/' files= os.listdir(path2)
classifier = autocolorize.load_default_classifier()
for ff in files: gray_image = cv2.imread(path2+ff) rgb_image = autocolorize.colorize(gray_image, classifier=classifier) cv2.imwrite(path3+rgb_image,rgb_image)
How do you make out with it ?
You need to use the data reading and writing method in the code:
import cv2
import os
import autocolorize
import numpy as np
import torch
import time
from tqdm import tqdm
def load(path, dtype=np.float64):
"""
Loads an image from file.
Parameters
----------
path : str
Path to image file.
dtype : np.dtype
Defaults to ``np.float64``, which means the image will be returned as a
float with values between 0 and 1. If ``np.uint8`` is specified, the
values will be between 0 and 255 and no conversion cost will be
incurred.
"""
import skimage.io
im = skimage.io.imread(path)
if dtype == np.uint8:
return im
elif dtype in {np.float16, np.float32, np.float64}:
return im.astype(dtype) / 255
else:
raise ValueError('Unsupported dtype')
def save(path, im):
"""
Saves an image to file.
If the image is type float, it will assume to have values in [0, 1].
Parameters
----------
path : str
Path to which the image will be saved.
im : ndarray (image)
Image.
"""
from PIL import Image
if im.dtype == np.uint8:
pil_im = Image.fromarray(im)
else:
pil_im = Image.fromarray((im * 255).astype(np.uint8))
pil_im.save(path)
path2 = '/home/rpf/tgp/Colorization/colorization_valset/celebahq_val_256/'
path3 = '/home/rpf/tgp/Colorization/colorization_valset/celebahq_val_256_results/'
files = os.listdir(path2)
import caffe
caffe.set_mode_gpu()
classifier = autocolorize.load_default_classifier()
time_costs = []
skip = 5
for ff in tqdm(files):
skip -= 1
gray_image = load(path2 + ff)
# torch.cuda.synchronize()
start = time.time()
rgb_image = autocolorize.colorize(gray_image, classifier=classifier)
# torch.cuda.synchronize()
end = time.time()
cost = (end - start) * 1000
if skip < 0:
time_costs.append(cost)
save(path3 + ff, rgb_image)
print(np.average(time_costs))
I cannot get correct result from the python code, but when I use the command like ' autocolorize grayscale.png -o colorized.png' I get the correct results
My code is following:
import cv2 import os import autocolorize
path2 = 'input/' path3 = 'output/' files= os.listdir(path2)
classifier = autocolorize.load_default_classifier()
for ff in files: gray_image = cv2.imread(path2+ff) rgb_image = autocolorize.colorize(gray_image, classifier=classifier) cv2.imwrite(path3+rgb_image,rgb_image)