<jupyter_start><jupyter_text>Neural AutoTaggingauthor :<NAME><jupyter_code>from keras.models import Sequential
from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate
from keras.models import Model
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.merge import Concatenate
from keras.layers.core import Lambda, Flatten, Dense
from keras.initializers import glorot_uniform
from keras.engine.topology import Layer
from keras import backend as K
K.set_image_data_format('channels_first')
import cv2
import os
import numpy as np
from numpy import genfromtxt
import pandas as pd
import tensorflow as tf
from utility import *
from inception_blocks_v2 import *
from pathlib import Path
import pickle

%matplotlib inline
%load_ext autoreload
%autoreload 2

np.set_printoptions(threshold=np.nan)

font = cv2.FONT_HERSHEY_COMPLEX_SMALL<jupyter_output>Using TensorFlow backend.<jupyter_text>The FaceNet model takes a lot of data and a long time to train. So following common practice in applied deep learning settings, let's just load weights that someone else has already trained. The network architecture follows the Inception model from Szegedy et al..The key things you need to know are:This network uses 96x96 dimensional RGB images as its input. Specifically, inputs a face image (or batch of  mm  face images) as a tensor of shape  (m,nC,nH,nW)=(m,3,96,96)(m,nC,nH,nW)=(m,3,96,96) It outputs a matrix of shape  (m,128)(m,128)  that encodes each input face image into a 128-dimensional vectorRun the cell below to create the model for face images<jupyter_code># facial recognization model
FRmodel = faceRecoModel(input_shape=(3, 96, 96))
# printing the total no of parameters 
print("Total Params:", FRmodel.count_params())<jupyter_output>Total Params: 3743280<jupyter_text>we will supply the images of the person whom you want to tag in photos.... our facial recognization model will output an 128 dimensional vector which is the encoding of the face... we will then go to each an every photograph and crop out the face part of each and every person in the image... if we can find a match between these faces and those stored in our database then we will tag those person in the image other wise we will leave the person as it is in the photo<jupyter_code># this function resizes images to so that it can be used as an input to the model
def resize_img(image):
    image = cv2.resize(image, (96,96))
    return image
#initialize the database
def init_database():
    my_file = Path("database/user.pickle")
    if my_file.is_file():
        # file exists
        try:
            pickle_in = open("database/user.pickle","rb")
            user_db = pickle.load(pickle_in)
            pickle_in.close()
        except EOFError:
            user_db = {}
    else:
        #file does not exists
        # make a file and load it
        user_db = {}
    return user_db
# adding a user to the data base by taking snapshot from the webcam
def add_new_user_webcam(user_db):
    # taking name as input 
    name = input('enter the name of the user \n')
    #initializing the haar cascade
    face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(1)
    counter = 0
    while(True):
        ret,image= cap.read() 
        gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.3,5)
        if len(faces) == 0:
            # be in the loop until a face is found
            continue
        for (x,y,w,h) in faces:
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_gray = resize_img(roi_gray)
            cv2.imwrite('images/user.'+str(name) +'.jpg',roi_gray)
            # as the face is detected we will now break from the internal loop
            break
        #breaking from the external while loop
        break
    cap.release()
    cv2.destroyAllWindows()
    
    #extracting the features from the image
    encoding = img_to_encoding("images/user." + str(name)+'.jpg', FRmodel)
    user_db[name] = encoding
    #saving the database
    pickle_out = open("database/user.pickle","wb")
    pickle.dump(user_db, pickle_out)
    pickle_out.close()
    print('user added to the database sucessfully from the webcam.....')
# this funcion adds a new user to the database from the disk
def add_new_user_disk(user_db):
    face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
    for filename in os.listdir("upload_pic_disk"):
        name = filename.split(".")[0]
        img_name = 'upload_pic_disk/' + str(name)+".jpg"
        img = cv2.imread(img_name)
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.3,5)
        for (x,y,w,h) in faces:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_gray = resize_img(roi_gray)
            cv2.imwrite('images/user.'+str(name) +'.jpg',roi_gray)
        #extracting the features from the image
        encoding = img_to_encoding("images/user." + str(name)+'.jpg', FRmodel)    
        user_db[name] = encoding
        #saving the database
        pickle_out = open("database/user.pickle","wb")
        pickle.dump(user_db, pickle_out)
        pickle_out.close()
    print('user added to the database sucessfully from the disk.....')
#this function deletes user from the images folder and cleares the database 
def delete_user():
    #deletes the images from the picture folder
    filelist = [ f for f in os.listdir("images") if f.endswith(".jpg") ]
    for f in filelist:
        os.remove(os.path.join("images", f))
    #deletes the pickle file from the database folder
    filelist = [ f for f in os.listdir("database") if f.endswith(".pickle") ]
    for f in filelist:
        os.remove(os.path.join("database", f))
    # clearing the user data base from the current program memory
    user_db = {}
    print('all the user data deleted from the data base')
    return user_db
# this function calculates the minimum distance between two persons and judges whose pic it is 

def cal_distance(encoding, user_db):
    min_dis = 100
    name = ""
    for key in user_db.keys():
        dist = np.linalg.norm(encoding -user_db[key] )
        if dist < min_dis:
            min_dis = dist
            name = key
    if min_dis > 0.7:
        name= "none"
    return name
# this function takes every picture in the picture folder and will try to tag the persons in the photograph
def tag_pictures():
    face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
    counter = 0
    factor = 2
    for filename in os.listdir("pictures"):
        img = cv2.imread(os.path.join("pictures",filename))
        #reshaping the image to match the input to the model
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.3,5)
        counter = counter +1
        print('image no : ', counter )
        face  = 1
        for (x,y,w,h) in faces:
            
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
            roi_gray = gray[y:y+h, x:x+w]
            original_height, original_width = roi_gray.shape[:2]
            resized_image = cv2.resize(roi_gray, (int(original_height*factor), int(original_width*factor)), interpolation=cv2.INTER_CUBIC )
            roi_gray =cv2.resize(resized_image, (96,96))
            
            file_name = str(face) +'.jpg'
            face = face +1 
            cv2.imwrite(file_name, roi_gray)
            #calculating the encoding for the images
            encoding = img_to_encoding(file_name, FRmodel)
            name = cal_distance(encoding , user_db)
            cv2.putText(img,str(name),(x,y+h), font,2,(255,255,255),2)
            file_name = 'tagged_photos/' + str(filename)
            cv2.imwrite(file_name, img)
            cv2.destroyAllWindows()
# initializing the database using the custom function created above
user_db = init_database()
# add a new user to the data base
print("enter 1 to take photo from camera ")
print("enter 2 to load the image from the disk. photo of the person must be present in the folder upload_pic_disk")
choice = input("enter your choice :  ")

# converting str choice into integer
choice = int(choice)
if choice == 1:
    add_new_user_webcam(user_db)
elif choice == 2:
    add_new_user_disk(user_db)
else:
    print('you have entered a wrong choice... read the instructions properly... ')
 tag_pictures()
## finally deleting all the user database from the disk
user_db = delete_user()
user_db<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09342915811088295, 'fraction_numerical': 0.01597079625827059, 'mean_word_length': 3.2250602409638556, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '355766', 'license': 'MIT', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2701, 'n_words': 870, 'path': 'Untitled9.ipynb', 'repo_name': 'adibyte95/Neural-AutoTagging', 'size': '8772'}