123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- import pickle
- import numpy as np
- import face_recognition
- import os
- from flask import Flask, render_template, request, redirect, send_file
- #import shutil
- import cv2
-
- app = Flask(__name__)
- app.config["IMAGE_UPLOADS"] = "C:/Users/Bizgaze/PycharmProjects/face_recogniction/People"
- datasetPath = "/opt/bizgaze/events.bizgaze.app/wwwroot/_files/1/Gallery/"
- peoplePath = "/opt/bizgaze/events.bizgaze.app/wwwroot/_files/People/"
- @app.route('/', methods=['GET'])
- def home():
- return render_template('index.html')
-
-
- @app.route('/Display', methods=['GET', "POST"])
- def Display():
- return render_template('Display.html')
-
-
- @app.route("/upload", methods=["GET", "POST"])
- def upload():
- if request.method == "POST":
-
- if request.files:
-
- image = request.files["image"]
- try:
- image.save(os.path.join(
- app.config["IMAGE_UPLOADS"], image.filename))
- except IsADirectoryError:
- return render_template('index.html')
- # image.save(os.path.join(
- # app.config["IMAGE_UPLOADS"], image.filename))
-
- print("Image saved")
-
- return redirect(request.url)
-
- return 'ok'
-
-
- @app.route('/predict', methods=["GET", "POST"])
- def predict():
- print('starting')
- def saveEncodings(encs, names, fname="encodings.pickle"):
- print('encoding')
- """
- 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):
- """
- 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 = "./output/" + name
- path1 = "./output/" + name
- if os.path.exists(path):
- pass
- else:
- os.mkdir(path)
- 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=peoplePath, saveLocation="./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:
- pass
- known_names.append(name)
-
- for loc in locs:
- top, right, bottom, left = loc
-
- # Show Image
- #cv2.rectangle(image, (left, top), (right, bottom), color=(255, 0, 0), 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("./known_encodings.pickle")
-
- for root, dirs, files in os.walk(datasetPath, 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, "0",name)
-
- # Show Image
- # cv2.rectangle(image, (left, top), (right, bottom), color=(255, 0, 0), thickness=2)
- # # cv2.imshow("Image", image)
- # cv2.waitKey(1)
- # cv2.destroyAllWindows()
-
-
- def main():
- """
- Main Function.
-
- Returns
- -------
- None.
-
- """
-
- processKnownPeopleImages()
- processDatasetImages()
- # shutil.make_archive('./Images', 'zip','./output')
- # p='./Images.zip'
- # return send_file(p,as_attachment=True)
-
-
- # import pandas as pd
- # q = pd.DataFrame(p)
- # x = q
- # # x.drop(x.columns[0], axis=1, inplace=True)
- # df = x.groupby([0], as_index=False).count()
- # z = df[0].str.split('/', expand=True)
-
-
- # for i, group in z.groupby([2]):
- # group.drop(group.columns[2], axis=1, inplace=True)
- #group.to_csv(f'./output1/{i}.csv', index=False, sep='/', header=False)
-
-
- ##############################csv creation code ##############################
- import pandas as pd
- q = pd.DataFrame(p)
- m = q
- #print(m)
- # x.drop(x.columns[Unnam], axis=1, inplace=True)
- df = m.groupby([0], as_index=False).count()
- first_column_name = df.columns[0]
-
- # Rename the first column
- df.rename(columns={first_column_name: 'col'}, inplace=True)
- #print(df)
- z = df['col'].str.split('/', expand=True)
-
- z['ImagePath'] = z[3]
-
- result = z.drop([0,1,3], axis=1)
- result.rename({result.columns[-1]: 'test'}, axis=1, inplace=True)
- # print(result)
- result.to_csv('results1.csv')
- import pandas as pd
- import os
- c = []
- for root, dirs, files in os.walk(datasetPath, topdown=False):
- for name in files:
- # print(name)
- L = os.path.join(root, name)
- c.append(L)
- df = pd.DataFrame(c)
- #print('seconfdf')
-
- first_column_name = df.columns[0]
-
- # Rename the first column
- df.rename(columns={first_column_name: 'col'}, inplace=True)
- print(df)
- df1 = df['col'].str.split("/", expand=True)
- df1.rename({df1.columns[-2]: 'abc'}, axis=1, inplace=True)
- #print('this is df1')
- #print(df1)
- df1.rename({df1.columns[-1]: 'test'}, axis=1, inplace=True)
- merge = pd.merge(df1, result, on='test', how='left')
- merge.to_csv('merge.csv')
- mergesplit = merge.loc[:,'test'].str.split(".", expand=True)
- mergesplit.rename({mergesplit.columns[-2]: 'ImageName'}, axis=1, inplace=True)
- mergesplit = mergesplit.loc[:,'ImageName' ]
-
- merge.rename({merge.columns[-1]: 'Matched'}, axis=1, inplace=True)
- merge['EventName'] = merge['abc']
- merge['Imagepath']="/_files/1/Gallery/"+merge['EventName']+'/'+ + merge['test']
-
- frames = [merge, mergesplit]
-
- r = pd.concat(frames, axis=1, join='inner')
- r=r.iloc[:,3:]
- #print(r)
- r.to_csv('path.csv', index=False)
- #r.to_json(r'./matched.json', orient="records")
- column_list = ['Matched','Imagepath', 'ImageName', 'EventName']
- r[column_list].to_json('matched.json', orient="records")
-
- #############################################################################################
-
-
-
- # merge.rename({merge.columns[-3]: 'ImagePath'}, axis=1, inplace=True)
- #
- # # print(merge)
- # merge1 = merge.iloc[:, -2]
- # merge12= merge.iloc[:, -3]
- #
- # # merge1.rename({merge1.columns[-1]: 'abc'}, axis=1, inplace=True)
- # merge2 = merge.iloc[:, -1].str.split(".", expand=True)
- # merge2.rename({merge2.columns[-1]: 'drop'}, axis=1, inplace=True)
- # #merge2.rename({merge2.columns[-2]: 'ImageName'}, axis=1, inplace=True)
- # print('this is merge1')
- # print(merge1)
- # print('this is merge2')
- # print(merge2)
- # mergefinal = pd.concat([merge1, merge2], axis=1, join='inner')
- # # print(mergefinal)
- # # print('-----------------')
- #
- # mergefinal.drop(columns=mergefinal.columns[-1], axis=1, inplace=True)
- # # print(mergefinal)
- # # print('--------------------------------------------------------------------------------')
- # # mergefinal.rename({mergefinal.columns[-1]: 'ImageName'}, axis=1, inplace=True)
- # # print('this is filename')
- # # print(mergefinal)
- # #mergefinal.rename({mergefinal.columns[-2]: 'EventName'}, axis=1, inplace=True)
- # # print('this is foldername')
- # # print(mergefinal)
- #
- # frames = [mergefinal, merge12]
- #
- # r = pd.concat(frames, axis=1, join='inner')
- #
- #
- # r.to_csv('Imagepath1.csv', index=False)
- # r.to_json('Imagepath1.json', orient="records")
- # import shutil
- # import os
- # # import shutil module
- # import shutil
- #
- # # import os module
- # import os
- # #################### move code#############
- # # base path
- # base_path = 'C:\\Users\\Bizgaze\\PycharmProjects\\face_recogniction\\move'
- # import os
- # dir_list = []
- # rootdir = 'C:\\Users\\Bizgaze\\PycharmProjects\\face_recogniction\\Dataset'
- # for file in os.listdir(rootdir):
- # d = os.path.join(rootdir, file)
- # if os.path.isdir(d):
- # dir_list.append(d)
- #
- # # list of directories we want to move.
- # # dir_list = ['test2', 'test4', 'test5', 'does_not_exist']
- #
- # # path to destination directory
- # # dest = os.path.join(base_path, 'dest')
- #
- # print("Before moving directories:")
- # print(os.listdir(base_path))
- #
- # # traverse each directory in dir_list
- # for dir_ in dir_list:
- #
- # # create path to the directory in the
- # # dir_list.
- # source = os.path.join(base_path, dir_)
- #
- # # check if it is an existing directory
- # if os.path.isdir(source):
- # # move to destination path
- # shutil.move(source, base_path)
- #
- # print("After moving directories:")
- # print(os.listdir(base_path))
-
- print("Completed")
-
- if __name__ == "__main__":
- main()
-
- # return render_template('index.html')
- p = './matched.json'
- return send_file(p,as_attachment=True)
-
- # return 'ALL IMAGES MATCHED'
-
-
- @app.route('/json')
- def json():
- p = './matched.json'
- return send_file(p,as_attachment=True)
-
-
- if __name__ == "__main__":
- app.run(host="0.0.0.0",port=8081)
-
|