This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.colab import drive | |
drive.mount('/content/drive/') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tf.keras.layers.Dense(1024, activation='relu'), | |
tf.keras.layers.Dropout(0.75), | |
tf.keras.layers.Dense(1, activation='sigmoid') ]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install --upgrade google-cloud-vision |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/content/drive/My Drive/ML/Misc/vision_service_account.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
OlderNewer