Skip to content

Instantly share code, notes, and snippets.

@Prasad9
Prasad9 / upload.swift
Created August 21, 2016 12:00
Code for storing data in Firebase
let uploadQuestionFirebaseRef = FIRDatabase.database().reference().child(<your_channel_name>)
uploadQuestionFirebaseRef.setValue(<upload_data>)
@Prasad9
Prasad9 / Listener.swift
Last active August 21, 2016 12:15
Swift code for listening to nodes in Firebase
let questionListenerFirebaseRef = FIRDatabase.database().reference().child(<your_channel_name>)
questionListenerFirebaseRef.observeEventType(FIRDataEventType.ChildAdded) { (dataSnapshot: FIRDataSnapshot) in
// Question added. Proceed accordingly.
}
@Prasad9
Prasad9 / resize_images.py
Last active May 9, 2019 23:40
Resize images to a common size
import tensorflow as tf
import matplotlib.image as mpimg
import numpy as np
IMAGE_SIZE = 224
def tf_resize_images(X_img_file_paths):
X_data = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, (None, None, 3))
@Prasad9
Prasad9 / central_scale_images.py
Last active October 20, 2017 20:56
Tensorflow framework code to centrally scale images
def central_scale_images(X_imgs, scales):
# Various settings needed for Tensorflow operation
boxes = np.zeros((len(scales), 4), dtype = np.float32)
for index, scale in enumerate(scales):
x1 = y1 = 0.5 - 0.5 * scale # To scale centrally
x2 = y2 = 0.5 + 0.5 * scale
boxes[index] = np.array([y1, x1, y2, x2], dtype = np.float32)
box_ind = np.zeros((len(scales)), dtype = np.int32)
crop_size = np.array([IMAGE_SIZE, IMAGE_SIZE], dtype = np.int32)
@Prasad9
Prasad9 / translate_images.py
Last active October 21, 2017 07:26
Tensorflow framework code to translate images in all sides
from math import ceil, floor
def get_translate_parameters(index):
if index == 0: # Translate left 20 percent
offset = np.array([0.0, 0.2], dtype = np.float32)
size = np.array([IMAGE_SIZE, ceil(0.8 * IMAGE_SIZE)], dtype = np.int32)
w_start = 0
w_end = int(ceil(0.8 * IMAGE_SIZE))
h_start = 0
h_end = IMAGE_SIZE
@Prasad9
Prasad9 / rotate90_images.py
Created October 20, 2017 21:59
Tensorflow framework code to rotate image at 90, 180 and 270 degrees
def rotate_images(X_imgs):
X_rotate = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (IMAGE_SIZE, IMAGE_SIZE, 3))
k = tf.placeholder(tf.int32)
tf_img = tf.image.rot90(X, k = k)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img in X_imgs:
for i in range(3): # Rotation at 90, 180 and 270 degrees
@Prasad9
Prasad9 / flip_images.py
Last active October 21, 2017 03:41
Tensorflow framework code to flip images left-right, up-down and transpose.
def flip_images(X_imgs):
X_flip = []
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape = (IMAGE_SIZE, IMAGE_SIZE, 3))
tf_img1 = tf.image.flip_left_right(X)
tf_img2 = tf.image.flip_up_down(X)
tf_img3 = tf.image.transpose_image(X)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for img in X_imgs:
@Prasad9
Prasad9 / rotate_images.py
Created October 21, 2017 05:27
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)
@Prasad9
Prasad9 / salt_pepper_noise_images.py
Last active June 3, 2021 16:38
Add salt and pepper noise to images
def add_salt_pepper_noise(X_imgs):
# Need to produce a copy as to not modify the original image
X_imgs_copy = X_imgs.copy()
row, col, _ = X_imgs_copy[0].shape
salt_vs_pepper = 0.2
amount = 0.004
num_salt = np.ceil(amount * X_imgs_copy[0].size * salt_vs_pepper)
num_pepper = np.ceil(amount * X_imgs_copy[0].size * (1.0 - salt_vs_pepper))
for X_img in X_imgs_copy:
@Prasad9
Prasad9 / add_gaussian_noise.py
Last active October 27, 2024 12:52
Python code to add random Gaussian noise on images
import cv2
def add_gaussian_noise(X_imgs):
gaussian_noise_imgs = []
row, col, _ = X_imgs[0].shape
# Gaussian distribution parameters
mean = 0
var = 0.1
sigma = var ** 0.5