Skip to content

Instantly share code, notes, and snippets.

@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')
@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 / 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 / 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 / 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)
!pip install --upgrade google-cloud-vision
@alonlavian
alonlavian / credentials.py
Created April 22, 2019 12:34
Add vision credentials to env
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/content/drive/My Drive/ML/Misc/vision_service_account.json"
@alonlavian
alonlavian / face_detect.py
Created April 24, 2019 17:40
face detection annotation with google vision
from google.cloud import vision
from google.cloud.vision import types
def detect_face(face_file, max_results=4):
client = vision.ImageAnnotatorClient()
content = face_file.read()
image = types.Image(content=content)
@alonlavian
alonlavian / face_crop.py
Last active April 28, 2019 19:25
Only the crop part
from PIL import Image
def crop_faces(image_path,image_name,cropped_images_lib):
with open(image_path, 'rb') as image:
faces = detect_face(image, 8)
im = Image.open(image_path)
for idx,face in enumerate(faces):
vects = face.fd_bounding_poly.vertices
@alonlavian
alonlavian / face_crop_library.py
Created April 28, 2019 19:32
crops all jpg in a folder and saves all cropped faces to the destination folder
original_images_dir = os.fsencode('/content/drive/My Drive/Google Photos/2019')
cropped_images_lib = '/content/drive/My Drive/CropMix/'
for path, dirs, files in os.walk(original_images_dir):
for file in files:
image_name = os.fsdecode(file)
if image_name.endswith(".jpg"):
image_path = os.path.join(path,file)
crop_faces(image_path,image_name,cropped_images_lib)