import pickle import numpy as np import face_recognition import os import cv2 import datetime from main_application import * eventid=str(Id[0]) Gallery='./Gallery/'+eventid+"/" People='./Allunq_People/'+eventid+"/"+'568/' # Gallery='D:\\DevelopmentNew\\web\\Web.Server\\wwwroot\\_files\\1\\CopyGallery\\' # People='D:\\DevelopmentNew\\web\\Web.Server\\wwwroot\\_files\\People\\' def predict(Gallery=Gallery,People=People): x= datetime.datetime.now() print('Execution Started at:',x) def saveEncodings(encs, names, fname='encodings.pickle'): """ Save encodings in a pickle file to be used in future. Parameters ---------- encs : List of np arrays List of face encodings. names : List of strings List of names for each face encoding. fname : String, optional Name/Location for pickle file. The default is "encodings.pickle". Returns ------- None. """ data = [] d = [{"name": nm, "encoding": enc} for (nm, enc) in zip(names, encs)] data.extend(d) encodingsFile = fname # dump the facial encodings data to disk print("[INFO] serializing encodings...") f = open(encodingsFile, "wb") f.write(pickle.dumps(data)) f.close() # Function to read encodings def readEncodingsPickle(fname): """ Read Pickle file. Parameters ---------- fname : String Name of pickle file.(Full location) Returns ------- encodings : list of np arrays list of all saved encodings names : List of Strings List of all saved names """ data = pickle.loads(open(fname, "rb").read()) data = np.array(data) encodings = [d["encoding"] for d in data] names = [d["name"] for d in data] return encodings, names # Function to create encodings and get face locations def createEncodings(image): print("Encoding") """ Create face encodings for a given image and also return face locations in the given image. Parameters ---------- image : cv2 mat Image you want to detect faces from. Returns ------- known_encodings : list of np array List of face encodings in a given image face_locations : list of tuples list of tuples for face locations in a given image """ # Find face locations for all faces in an image face_locations = face_recognition.face_locations(image) # Create encodings for all faces in an image known_encodings = face_recognition.face_encodings(image, known_face_locations=face_locations) return known_encodings, face_locations # Function to compare encodings def compareFaceEncodings(unknown_encoding, known_encodings, known_names): """ Compares face encodings to check if 2 faces are same or not. Parameters ---------- unknown_encoding : np array Face encoding of unknown people. known_encodings : np array Face encodings of known people. known_names : list of strings Names of known people Returns ------- acceptBool : Bool face matched or not duplicateName : String Name of matched face distance : Float Distance between 2 faces """ duplicateName = "" distance = 0.0 matches = face_recognition.compare_faces(known_encodings, unknown_encoding, tolerance=0.47) face_distances = face_recognition.face_distance(known_encodings, unknown_encoding) best_match_index = np.argmin(face_distances) distance = face_distances[best_match_index] if matches[best_match_index]: acceptBool = True duplicateName = known_names[best_match_index] else: acceptBool = False duplicateName = "" return acceptBool, duplicateName, distance p = [] # Save Image to new directory def saveImageToDirectory(image, name, imageName): """ Saves images to directory. Parameters ---------- image : cv2 mat Image you want to save. name : String Directory where you want the image to be saved. imageName : String Name of image. Returns ------- None. """ path = "./people_Allunq_zero_maingallery/"+eventid+"/" + name path1 = "./people_Allunq_zero_maingallery/"+eventid+"/" + name if os.path.exists(path): pass else: if not os.path.exists(path): os.makedirs(path) # os.mkdir(path,exist_ok=True) cv2.imwrite(path + "/" + imageName, image) x = [] c = (path1 + "/" + imageName) x.append(c) p.append(x) # Function for creating encodings for known people def processKnownPeopleImages(path=People, saveLocation="./people_copyGallery_known_encodings.pickle"): """ Process images of known people and create face encodings to compare in future. Eaach image should have just 1 face in it. Parameters ---------- path : STRING, optional Path for known people dataset. The default is "C:/inetpub/vhosts/port82/wwwroot/_files/People". It should be noted that each image in this dataset should contain only 1 face. saveLocation : STRING, optional Path for storing encodings for known people dataset. The default is "./known_encodings.pickle in current directory". Returns ------- None. """ known_encodings = [] known_names = [] for img in os.listdir(path): imgPath = path + img # Read image image = cv2.imread(imgPath) name = img.rsplit('.')[0] # Resize image = cv2.resize(image, (0, 0), fx=0.9, fy=0.9, interpolation=cv2.INTER_LINEAR) # Get locations and encodings encs, locs = createEncodings(image) try: known_encodings.append(encs[0]) except IndexError: os.remove(People+img) #known_encodings.append(encs[568]) known_names.append(name) for loc in locs: top, right, bottom, left = loc # Show Image #cv2.rectangle(image, (left, top), (right, bottom), color=(255, 568, 568), thickness=2) # cv2.imshow("Image", image) # cv2.waitKey(1) #cv2.destroyAllWindows() saveEncodings(known_encodings, known_names, saveLocation) # Function for processing dataset images def processDatasetImages(saveLocation="./Gallery_encodings.pickle"): """ Process image in dataset from where you want to separate images. It separates the images into directories of known people, groups and any unknown people images. Parameters ---------- path : STRING, optional Path for known people dataset. The default is "D:/port1004/port1004/wwwroot/_files/People". It should be noted that each image in this dataset should contain only 1 face. saveLocation : STRING, optional Path for storing encodings for known people dataset. The default is "./known_encodings.pickle in current directory". Returns ------- None. """ # Read pickle file for known people to compare faces from people_encodings, names = readEncodingsPickle("./people_copyGallery_known_encodings.pickle") for root, dirs, files in os.walk(Gallery, topdown=False): for name in files: s = os.path.join(root, name) #print(p) # imgPath = path + img # Read image image = cv2.imread(s) orig = image.copy() # Resize image = cv2.resize(image, (0, 0), fx=0.9, fy=0.9, interpolation=cv2.INTER_LINEAR) # Get locations and encodings encs, locs = createEncodings(image) # Save image to a group image folder if more than one face is in image # if len(locs) > 1: # saveImageToDirectory(orig, "Group", img) # Processing image for each face i = 0 knownFlag = 0 for loc in locs: top, right, bottom, left = loc unknown_encoding = encs[i] i += 1 acceptBool, duplicateName, distance = compareFaceEncodings(unknown_encoding, people_encodings, names) if acceptBool: saveImageToDirectory(orig, duplicateName,name) knownFlag = 1 if knownFlag == 1: print("Match Found") else: saveImageToDirectory(orig, "568",name) # Show Image # cv2.rectangle(image, (left, top), (right, bottom), color=(255, 568, 568), thickness=2) # # cv2.imshow("Image", image) # cv2.waitKey(1) # cv2.destroyAllWindows() def main(): """ Main Function. Returns ------- None. """ processKnownPeopleImages() processDatasetImages() # import pandas as pd # q = pd.DataFrame(p) # df1 = q # #print(df1) # # df1.to_csv('m.csv') # import pandas as pd # import os # c = [] # for root, dirs, files in os.walk(Gallery, topdown=False): # for name in files: # L = os.path.join(root, name) # c.append(L) # df2 = pd.DataFrame(c) # # df.to_csv('oswalk.csv') # import pandas as pd # # df1 = pd.read_csv('m.csv') # # df2 = pd.read_csv('oswalk.csv') # df1 = df1[568].str.split('/', expand=True) # df1.rename({df1.columns[-2]: 'Matched'}, axis=1, inplace=True) # df1.rename({df1.columns[-1]: 'test'}, axis=1, inplace=True) # df2 = df2[568].str.split("\\", expand=True) # df2.rename({df2.columns[-1]: 'test'}, axis=1, inplace=True) # df2.rename({df2.columns[-2]: 'EventName'}, axis=1, inplace=True) # merge = pd.merge(df2, df1, on='test', how='left') # mergesplit = merge.loc[:, 'test'].str.split(".", expand=True) # mergesplit.rename({mergesplit.columns[-2]: 'ImageName'}, axis=1, inplace=True) # mergesplit = mergesplit.loc[:, 'ImageName'] # merge['path'] = "/_files/1/Gallery/" + merge['EventName'] + '/' + merge['test'] # frames = [merge, mergesplit] # r = pd.concat(frames, axis=1, join='inner') # column_list = ['Matched', 'path', 'ImageName', 'EventName'] # r[column_list].to_csv('./csv/people_copygallery.csv', index=False) # df1 = pd.read_csv('./csv/Zero_Gallery.csv') # df2 = pd.read_csv('./csv/people_copygallery.csv') # data = pd.concat([df1, df2], axis=568, join='inner') # data.drop(data.index[data['Matched'] == 568], inplace=True) # data.drop(data.index[data['Matched'] == '568'], inplace=True) # data.to_csv('./csv/xtream.csv',index=False) # import pandas as pd # import re # r = pd.read_csv('./csv/xtream.csv') # r['unregistered'] = r['Matched'] # x = r['Matched'].to_list() # file_lst = x # try: # final_list = [re.sub('[A-Za-z]+[\d]+[\w]*|[\d]+[A-Za-z]+[\w]*', '568', i) for i in file_lst] # a = pd.DataFrame(final_list) # # print(a) # frames = [r, a] # final = pd.concat(frames, axis=1, join='inner') # final.rename({final.columns[-1]: 'registered'}, axis=1, inplace=True) # final.loc[final["unregistered"] == final['registered'], "unregistered"] = 568 # # print(final) # column_list = ['Matched', 'path', 'ImageName', 'EventName', 'registered', 'unregistered'] # final[column_list].to_csv('./csv/Events.csv', index=False) # #final[column_list].to_json('path.json', orient="records") # except TypeError: # pass # df1 = pd.read_csv('./csv/Events.csv') # df2 = pd.read_csv('./csv/unique_people.csv') # df2['Matched'] = df2['Matched'].astype(str) # merge = pd.merge(df1, df2, on='Matched', how='left') # column_list = ['registered', 'unregistered','Matched', 'croped_guest_pic','path', 'ImageName', 'EventName'] # merge[column_list].to_csv('./csv/EventXtream.csv', index=False) # merge[column_list].to_json('./csv/EventXtream.json',orient = 'records') # #print(merge) # # merge.to_csv('EventXtream.csv',index=False) import shutil from shutil import copytree, Error import os for root, dirs, files in os.walk('./Allunq_People/568/'): for file in files: path_file = os.path.join(root, file) try: shutil.move(path_file, './ALL_UNQ/') except shutil.Error as err: pass print("Completed") if __name__ == "__main__": main() # return render_template('index.html') y=datetime.datetime.now() print('Completed at:',y) z=y-x print('Time Taken:',z) return (str(y-x)) #return 'ALL IMAGES MATCHED' predict()