Code Monkey home page Code Monkey logo

Comments (2)

hermosayhl avatar hermosayhl commented on August 27, 2024

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 ?

from autocolorize.

guangpintao avatar guangpintao commented on August 27, 2024

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))

from autocolorize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.