Skip to content

Commit

Permalink
Added gif generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesara Rathnayake committed Jul 10, 2015
1 parent 4b6d3fd commit c3e690a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ Easy to configure Python program that make use of [Google's DeepDream](https://g
usage: deepdreamer.py [-h] [--zoom {true,false}] [--scale SCALE]
[--dreams DREAMS] [--itern ITERN] [--octaves OCTAVES]
[--octave-scale OCTAVE_SCALE] [--layers LAYERS]
[--clip {true,false}] [--list-layers]
[--network {bvlc_googlenet, googlenet_place205}]
[--clip {true,false}]
[--network {bvlc_googlenet,googlenet_place205}]
[--gif {true,false}] [--reverse {true,false}]
[--duration DURATION] [--loop {true,false}]
[--list-layers]
[image]
positional arguments:
Expand All @@ -42,6 +45,12 @@ optional arguments:
dream octave scale (default: 1.4)
--layers LAYERS dream layers (default: inception_4c/output)
--clip {true,false} clip dreams (default: true)
--network {bvlc_googlenet,googlenet_place205}
choose the network to use (default: bvlc_googlenet)
--gif {true,false} make a gif (default: false)
--reverse {true,false}
make a reverse gif (default: false)
--duration DURATION gif frame duration in seconds (default: 0.1)
--loop {true,false} enable gif loop (default: false)
--list-layers list layers
--network choose the network to use (default: googlenet_place205)
```
24 changes: 23 additions & 1 deletion deepdreamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def main():
"--network", choices=['bvlc_googlenet', 'googlenet_place205'],
default='bvlc_googlenet',
help="choose the network to use (default: bvlc_googlenet)")
parser.add_argument(
"--gif", choices=["true", "false"], default="false",
help="make a gif (default: false)")
parser.add_argument(
"--reverse", choices=["true", "false"], default="false",
help="make a reverse gif (default: false)")
parser.add_argument(
"--duration", type=float, default=0.1,
help="gif frame duration in seconds (default: 0.1)")
parser.add_argument(
"--loop", choices=["true", "false"], default="false",
help="enable gif loop (default: false)")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("image", nargs="?")
group.add_argument(
Expand All @@ -54,11 +66,21 @@ def main():
clip = True
if args.clip == "false":
clip = False
gif = False
if args.gif == "true":
gif = True
reverse = False
if args.reverse == "true":
reverse = True
loop = False
if args.loop == "true":
loop = True
deepdream(
args.image, zoom=zoom, scale_coefficient=args.scale,
irange=args.dreams, iter_n=args.itern, octave_n=args.octaves,
octave_scale=args.octave_scale, end=args.layers, clip=clip,
network=args.network)
network=args.network, gif=gif, reverse=reverse,
duration=args.duration, loop=loop)
except Exception as e:
print("Error: {}".format(e))
sys.exit(2)
Expand Down
19 changes: 18 additions & 1 deletion deepdreamer/deepdreamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
from caffe import Classifier
from images2gif import writeGif
from scipy.ndimage import affine_transform, zoom
from PIL.Image import fromarray as img_fromarray, open as img_open

Expand Down Expand Up @@ -115,7 +116,8 @@ def list_layers(network="bvlc_googlenet"):
def deepdream(
img_path, zoom=True, scale_coefficient=0.05, irange=100, iter_n=10,
octave_n=4, octave_scale=1.4, end="inception_4c/output", clip=True,
network="bvlc_googlenet"):
network="bvlc_googlenet", gif=False, reverse=False, duration=0.1,
loop=False):
img = np.float32(img_open(img_path))
s = scale_coefficient
h, w = img.shape[:2]
Expand All @@ -125,14 +127,29 @@ def deepdream(
net = Classifier(
NET_FN, PARAM_FN, mean=CAFFE_MEAN, channel_swap=CHANNEL_SWAP)

img_pool = [img_path,]

print("Dreaming...")
for i in xrange(irange):
img = _deepdream(
net, img, iter_n=iter_n, octave_n=octave_n,
octave_scale=octave_scale, end=end, clip=clip)
img_fromarray(np.uint8(img)).save("{}_{}.jpg".format(
img_path, i))
if gif:
img_pool.append("{}_{}.jpg".format(img_path, i))
print("Dream {} saved.".format(i))
if zoom:
img = affine_transform(
img, [1-s, 1-s, 1], [h*s/2, w*s/2, 0], order=1)
if gif:
print("Creating gif...")
frames = None
if reverse:
frames = [img_open(f) for f in img_pool[::-1]]
else:
frames = [img_open(f) for f in img_pool]
writeGif(
"{}.gif".format(img_path), frames, duration=duration,
repeat=loop)
print("gif created.")

0 comments on commit c3e690a

Please sign in to comment.