Skip to content

Instantly share code, notes, and snippets.

!pip install --upgrade google-cloud-vision
@alonlavian
alonlavian / faceCrop.py
Created April 21, 2019 11:37
find and crop faces in an image using google vision API
def detect_face(face_file, max_results=4):
client = vision.ImageAnnotatorClient()
content = face_file.read()
image = types.Image(content=content)
return client.face_detection(image=image, max_results=max_results).face_annotations
def crop_faces(image_to_crop, cropped_photos_dir):
with open(image_to_crop, 'rb') as image:
faces = detect_face(image, 8)
@alonlavian
alonlavian / dropout.py
Created April 19, 2019 12:35
an example of adding a dropout layer to a CNN
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.75),
tf.keras.layers.Dense(1, activation='sigmoid') ])
@alonlavian
alonlavian / ResNet50Binary.py
Created April 3, 2019 07:58
Using ResNet50 as a base model for transfer learning
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.layers import Dense, Activation, Flatten, Dropout
from tensorflow.keras.models import Sequential, Model
HEIGHT = 300
WIDTH = 300
base_model = ResNet50(weights='imagenet',
include_top=False,
input_shape=(HEIGHT, WIDTH, 3))
@alonlavian
alonlavian / mountDriveToColab.py
Created April 2, 2019 09:38
mounts google drive to /content/drive/
from google.colab import drive
drive.mount('/content/drive/')
@alonlavian
alonlavian / ImageDataGenerator.py
Last active April 2, 2019 11:13
example of using ImageDataGenerator for creating a binary training set
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(
'/content/drive/My Drive/Training/',
target_size=(300, 300),
batch_size=10,
class_mode='binary')