Skip to content

Instantly share code, notes, and snippets.

@Prasad9
Created October 21, 2017 05:27
Show Gist options
  • Save Prasad9/afb27c9fff38e90220a0cb7db80161b8 to your computer and use it in GitHub Desktop.
Save Prasad9/afb27c9fff38e90220a0cb7db80161b8 to your computer and use it in GitHub Desktop.
Tensorflow framework to rotate images at given start and end angle with total number of images to produce
from math import pi
def rotate_images(X_imgs, start_angle, end_angle, n_images):
X_rotate = []
iterate_at = (end_angle - start_angle) / (n_images - 1)
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (None, IMAGE_SIZE, IMAGE_SIZE, 3))
radian = tf.placeholder(tf.float32, shape = (len(X_imgs)))
tf_img = tf.contrib.image.rotate(X, radian)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for index in range(n_images):
degrees_angle = start_angle + index * iterate_at
radian_value = degrees_angle * pi / 180 # Convert to radian
radian_arr = [radian_value] * len(X_imgs)
rotated_imgs = sess.run(tf_img, feed_dict = {X: X_imgs, radian: radian_arr})
X_rotate.extend(rotated_imgs)
X_rotate = np.array(X_rotate, dtype = np.float32)
return X_rotate
# Start rotation at -90 degrees, end at 90 degrees and produce totally 14 images
rotated_imgs = rotate_images(X_imgs, -90, 90, 14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment