Skip to content

Instantly share code, notes, and snippets.

@malefs
Forked from samim23/forward.py
Created October 13, 2022 07:54
Show Gist options
  • Save malefs/700aff9d8682baae99a50535d762f0c4 to your computer and use it in GitHub Desktop.
Save malefs/700aff9d8682baae99a50535d762f0c4 to your computer and use it in GitHub Desktop.

Revisions

  1. @samim23 samim23 revised this gist Jan 9, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion forward.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,9 @@
    # 4. Create a directory "kidframe" next to this python file, put your extracted video frames inside.
    # 5. Make sure to have a directory called "out" next to it. Inside "out" a second directory analogue to first ("kidframe")-
    # 6. Run: python forward.py
    # 7. Grab your rendered frames at "out/kidframe/xxx0001.jpg". ;-)
    # 7. Grab your rendered frames at "out/kidframe/xxx0001.jpg".
    # 8. Recombine frames with FFMPEG, e.g:
    # cat *.jpg | ffmpeg -f image2pipe -r 25 -vcodec mjpeg -i - -vcodec libx264 out.mp4

    import tensorflow as tf
    import skimage.transform
  2. @samim23 samim23 revised this gist Jan 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions forward.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # "Colorizing B/W Movies with Neural Nets",
    # Network/Code Created by Ryan Dahl, hacked by samim.io to work with movies
    # BACKGROUND: http://tinyclouds.org/colorize/
    # DEMO: https://www.youtube.com/watch?v=_MJU8VK2PI4
    # USAGE:
    # 1. Download TensorFlow model from: http://tinyclouds.org/colorize/
  3. @samim23 samim23 revised this gist Jan 9, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion forward.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    # "Colorizing B/W Movies with Neural Nets",
    # Network/Code Created by Ryan Dahl, hacked by samim.io to work with movies
    # DEMO: https://www.youtube.com/watch?v=_MJU8VK2PI4
    # USAGE:
    # 1. Download TensorFlow VGG16 model from: http://tinyclouds.org/colorize/
    # 1. Download TensorFlow model from: http://tinyclouds.org/colorize/
    # 2. Use FFMPEG or such to extract frames from video.
    # 3. Make sure your images are 224x224 pixels dimension. You can use imagemagicks "mogrify", here some useful commands:
    # mogrify -resize 224x224 *.jpg
  4. @samim23 samim23 created this gist Jan 9, 2016.
    67 changes: 67 additions & 0 deletions forward.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    # "Colorizing B/W Movies with Neural Nets",
    # Network/Code Created by Ryan Dahl, hacked by samim.io to work with movies
    # USAGE:
    # 1. Download TensorFlow VGG16 model from: http://tinyclouds.org/colorize/
    # 2. Use FFMPEG or such to extract frames from video.
    # 3. Make sure your images are 224x224 pixels dimension. You can use imagemagicks "mogrify", here some useful commands:
    # mogrify -resize 224x224 *.jpg
    # mogrify -gravity center -background black -extent 224x224 *.jpg
    # mogrify -colorspace sRGB -type TrueColor *.jpg
    # 4. Create a directory "kidframe" next to this python file, put your extracted video frames inside.
    # 5. Make sure to have a directory called "out" next to it. Inside "out" a second directory analogue to first ("kidframe")-
    # 6. Run: python forward.py
    # 7. Grab your rendered frames at "out/kidframe/xxx0001.jpg". ;-)

    import tensorflow as tf
    import skimage.transform
    from skimage.io import imsave, imread
    import os
    from os import listdir, path
    from os.path import isfile, join

    def get_directory(folder):
    foundfile = []

    for path, subdirs, files in os.walk(folder):
    for name in files:
    found = os.path.join(path, name)
    if name.endswith('.jpg'):
    foundfile.append(found)
    break

    foundfile.sort()
    return foundfile

    def load_image(path):
    img = imread(path)
    # crop image from center
    short_edge = min(img.shape[:2])
    yy = int((img.shape[0] - short_edge) / 2)
    xx = int((img.shape[1] - short_edge) / 2)
    crop_img = img[yy : yy + short_edge, xx : xx + short_edge]
    # resize to 224, 224
    img = skimage.transform.resize(crop_img, (224, 224))
    # desaturate image
    return (img[:,:,0] + img[:,:,1] + img[:,:,2]) / 3.0

    with open("colorize.tfmodel", mode='rb') as f:
    fileContent = f.read()

    graph_def = tf.GraphDef()
    graph_def.ParseFromString(fileContent)
    grayscale = tf.placeholder("float", [1, 224, 224, 1])
    tf.import_graph_def(graph_def, input_map={ "grayscale": grayscale }, name='')

    images = get_directory("kidframes")

    for image in images:
    print image
    shark_gray = load_image(image).reshape(1, 224, 224, 1)

    with tf.Session() as sess:
    inferred_rgb = sess.graph.get_tensor_by_name("inferred_rgb:0")
    inferred_batch = sess.run(inferred_rgb, feed_dict={ grayscale: shark_gray })
    filename = "out/"+image
    imsave(filename, inferred_batch[0])
    print "saved " + filename
    #sess.close()