Skip to content

Instantly share code, notes, and snippets.

@jesuscmadrigal
Created April 11, 2024 23:20
Show Gist options
  • Save jesuscmadrigal/284aee7735bac4fd1ec5d62e0c4f543a to your computer and use it in GitHub Desktop.
Save jesuscmadrigal/284aee7735bac4fd1ec5d62e0c4f543a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""FinalCVproject.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Mo2qGnXDtZ44uIoGC_eGSod8NMTxBqr5
"""
!pip install caer
!pip install canaro #for tensorflow
import os
import caer
import canaro
import numpy as np
import cv2 as cv
import gc
from google.colab.patches import cv2_imshow
from google.colab import drive
drive.mount("/content/drive")
proyecto_dir = '/content/drive/MyDrive/Computer Vision/HW3/PRJ/'
dataset_dir = '/content/drive/MyDrive/Computer Vision/HW3/PRJ/Faces/train'
valimg_dir = '/content/drive/MyDrive/Computer Vision/HW3/PRJ/Faces/val/mindy_kaling/2.jpg'
#dataset_dir = '/content/gdrive/My Drive/Proyecto/MIT database/train'
#valimg_dir = '/content/gdrive/My Drive/Proyecto/MIT database/val/0000_00000001.jpg'
#dataset_dir = '/content/gdrive/My Drive/Proyecto/simpsons_dataset/train'
#valimg_dir = '/content/gdrive/My Drive/Proyecto/simpsons_dataset/val/pic1.jpg'
dataset=[]
for i in os.listdir(dataset_dir):
dataset.append(i)
print(dataset)
IMG_SIZE = (80,80)
channels = 1 #only gray needed
"""## **1st approach using haar casacade method**"""
from google.colab import drive
drive.mount('/content/drive')
os.chdir('/content/drive/MyDrive/Computer Vision/HW3/PRJ/')
haar_cascade = cv.CascadeClassifier('/content/drive/MyDrive/Computer Vision/HW3/PRJ/haar_face.xml')
features = []
labels = []
def create_train():
for person in dataset:
path = os.path.join(dataset_dir, person)
label = dataset.index(person)
for img in os.listdir(path):
img_path = os.path.join(path,img)
img_array = cv.imread(img_path)
if img_array is None:
continue
gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)
faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)
for (x,y,w,h) in faces_rect:
faces_roi = gray[y:y+h, x:x+w]
features.append(faces_roi)
labels.append(label)
create_train()
print('Training done ---------------')
features = np.array(features, dtype='object')
labels = np.array(labels)
face_recognizer = cv.face.LBPHFaceRecognizer_create()
# Train the Recognizer on the features list and the labels list
face_recognizer.train(features,labels)
face_recognizer.save('face_trained.yml')
face_recognizer = cv.face.LBPHFaceRecognizer_create()
face_recognizer.read('face_trained.yml')
img = cv.imread(valimg_dir)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv2_imshow(gray)
# Detect the face in the image
faces_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)
for (x,y,w,h) in faces_rect:
faces_roi = gray[y:y+h,x:x+w]
label, confidence = face_recognizer.predict(faces_roi)
print(f'Label = {dataset[label]} with a confidence of {confidence}')
cv.putText(img, str(dataset[label]), (20,20), cv.FONT_HERSHEY_COMPLEX, 1.0, (0,255,0), thickness=2)
cv.rectangle(img, (x,y), (x+w,y+h), (0,255,0), thickness=2)
cv2_imshow(img)
"""## ***2nd approach using deep learing***"""
# Creating a dictionary for each person, sorting it in descending order
person_dict = {}
for person in os.listdir(dataset_dir):
person_dict[person] = len(os.listdir(os.path.join(dataset_dir,person)))
# Sort in descending order
person_dict = caer.sort_dict(person_dict, descending=True)
person_dict
# Getting the first n categories with the most number of images
people = []
count = 0
for i in person_dict:
people.append(i[0])
count += 1
if count >= 5:
break
people
# Create the training data
train = caer.preprocess_from_dir(dataset_dir, people, channels=channels, IMG_SIZE=IMG_SIZE, isShuffle=True)
# Number of training samples
len(train)
# Visualizing the data (OpenCV doesn't display well in Jupyter notebooks)
import matplotlib.pyplot as plt
plt.figure(figsize=(30,30))
plt.imshow(train[0][0], cmap='gray')
plt.show()
"""**Feature extraction**"""
featureSet, labels = caer.sep_train(train, IMG_SIZE=IMG_SIZE)
from tensorflow.keras.utils import to_categorical
# Normalize the featureSet ==> (0,1)
featureSet = caer.normalize(featureSet)
# Converting numerical labels to binary class vectors
labels = to_categorical(labels, len(people))
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(featureSet, labels,test_size=0.2)
del train
del featureSet
del labels
gc.collect()
# Useful variables when training
BATCH_SIZE = 32
EPOCHS = 30
# Image data generator (introduces randomness in network ==> better accuracy)
datagen = canaro.generators.imageDataGenerator()
train_gen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE)
"""**Model Creation**"""
# Create our model (returns a compiled model)
#model = canaro.models.createSimpsonsModel(IMG_SIZE=IMG_SIZE, channels=channels, output_dim=len(people), loss='binary_crossentropy', decay=1e-7, learning_rate=0.001, momentum=0.9,nesterov=True)}
model = canaro.models.createDefaultModel(IMG_SIZE=80, channels=channels, output_dim=len(people))
model.summary()
"""**Training**"""
# Training the model
from tensorflow.keras.callbacks import LearningRateScheduler
from keras import losses
from keras import optimizers
from keras import metrics
callbacks_list = [LearningRateScheduler(canaro.lr_schedule)]
model.compile(loss = 'mean_squared_error',
optimizer = 'Adam', metrics = [metrics.categorical_accuracy])
training = model.fit(train_gen,
steps_per_epoch=len(x_train)//BATCH_SIZE,
epochs=EPOCHS,
validation_data=(x_val,y_val),
validation_steps=len(y_val)//BATCH_SIZE,
callbacks = callbacks_list)
people
"""**Testing**"""
#os.chdir(valset_dir)
#!ls
img = cv.imread(valimg_dir)
img_RGB=cv.cvtColor(img, cv.COLOR_BGR2RGB )
#plt.imshow(img)
plt.imshow(img_RGB)
plt.show()
def prepare(image):
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
image = cv.resize(image, IMG_SIZE)
image = caer.reshape(image, IMG_SIZE, 1)
return image
predictions = model.predict(prepare(img))
#predictions = model.predict(x_val)
# Getting class with the highest probability
print(people[np.argmax(predictions[0])])
#print(predictions)
result = model.evaluate(x=x_val,
y=y_val)
for name, value in zip(model.metrics_names, result):
print(name, value)
#os.chdir('/content/gdrive/My Drive/Proyecto')
#!ls
#img=cv.imread("cat.jpg",1)
#img_grayscale=cv.imread("cat.jpg",0)
#cv2_imshow(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment