first commit

This commit is contained in:
2022-12-12 10:45:51 +05:30
commit 48effe8886
126 changed files with 119396 additions and 0 deletions
+491
View File
@@ -0,0 +1,491 @@
import pickle
import numpy as np
import face_recognition
import os
import cv2
import datetime
import click
@click.command()
@click.argument('eventid', default='')
def predict123(eventid):
original_working_directory = os.getcwd()
new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files\\'
# change to the networked directory
os.chdir(new_networked_directory)
People = "./ALL_UNQ/" + eventid + "/"
Gallery = './guestimage/'+ eventid + "/"
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 = []
def f_CSVwrite():
import pandas as pd
q = pd.DataFrame(p)
#print(q)
m = q
# print(m)
# x.drop(x.columns[Unnam], axis=1, inplace=True)
df = m.groupby([0], as_index=False).count()
z = df[0].str.split('/', expand=True)
z.to_csv('all_people.csv',index=False)
import pandas as pd
df2 = pd.read_csv('./all_people.csv')
df2.rename({df2.columns[-1]: 'test'}, axis=1, inplace=True)
df2.rename({df2.columns[-2]: 'Matched'}, axis=1, inplace=True)
df2 = df2[['Matched', 'test']]
import pandas as pd
import os
c = []
for root, dirs, files in os.walk(Gallery,
topdown=False):
for name in files:
# print(name)
L = os.path.join(root, name)
c.append(L)
df = pd.DataFrame(c)
df1 = df[0].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(df2, df1, on='test', how='left')
merge.rename({merge.columns[-1]: 'EventName'}, axis=1, inplace=True)
# 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')
df2 = r.dropna(subset=['Matched'])
#df2['Matched'] = df2['Matched'].astype(str)
#df2['Matched'] = df2['Matched'].astype(int)
column_list = ['Matched', 'Imagepath', 'ImageName', 'EventName']
df2[column_list].to_csv('all_people_fina123.csv', index=False)
df2[column_list].to_json('all_people_final123.json', orient="records")
# import requests
# import json
# with open('all_people_final123.json', 'r') as json_file:
# json_load = json.load(json_file)
# #url = "https://test.bizgaze.app:8443/apis/v4/bizgaze/integrations/testevents/save"
# payload = json.dumps(json_load).replace("]", "").replace("[", "")
# print(payload)
# headers = {
# 'Authorization': 'stat 7f1007799b1f42999544d0338251bb19',
# 'Content-Type': 'application/json'
# }
# response = requests.request("POST", url, headers=headers, data=payload)
# print("##############################################################")
# print(response.text)
# p.clear()
# 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.
"""
# from pathlib import Path
# a=
# Path('Allunq_People/' + eventid).mkdir(exist_ok=True)
path = original_working_directory+"Allunq_People/" +eventid+'/'+ name
path1 = original_working_directory+"Allunq_People/" +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)
f_CSVwrite()
# Function for creating encodings for known people
def processKnownPeopleImages(path=People, saveLocation="./Zero_gallery_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
print(imgPath)
import pathlib
file = pathlib.Path(str(path+"Thumbs.db"))
if file.exists ():
os.remove(path+"Thumbs.db")
else:
pass
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("./Zero_gallery_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()
# print(imgPath)
# import pathlib
# file = pathlib.Path(str(path+"Thumbs.db"))
# if file.exists ():
# os.remove(path+"Thumbs.db")
# else:
# pass
# 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, 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)
# #print(q)
# m = q
# # print(m)
# # x.drop(x.columns[Unnam], axis=1, inplace=True)
# df = m.groupby([0], as_index=False).count()
# z = df[0].str.split('/', expand=True)
# z.to_csv('all_people.csv',index=False)
# import pandas as pd
# df2 = pd.read_csv('./all_people.csv')
# df2.rename({df2.columns[-1]: 'test'}, axis=1, inplace=True)
# df2.rename({df2.columns[-2]: 'Matched'}, axis=1, inplace=True)
# df2 = df2[['Matched', 'test']]
# import pandas as pd
# import os
# c = []
# for root, dirs, files in os.walk(Gallery,
# topdown=False):
# for name in files:
# # print(name)
# L = os.path.join(root, name)
# c.append(L)
# df = pd.DataFrame(c)
# df1 = df[0].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(df2, df1, on='test', how='left')
# merge.rename({merge.columns[-1]: 'EventName'}, axis=1, inplace=True)
# # 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')
# df2 = r.dropna(subset=['Matched'])
# #df2['Matched'] = df2['Matched'].astype(str)
# #df2['Matched'] = df2['Matched'].astype(int)
# column_list = ['Matched', 'Imagepath', 'ImageName', 'EventName']
# df2[column_list].to_csv('all_people_fina123.csv', index=False)
# df2[column_list].to_json('all_people_final123.json', orient="records")
# 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'
predict123()
+527
View File
@@ -0,0 +1,527 @@
import pickle
import numpy as np
import face_recognition
import os
import cv2
import datetime
import click
import requests
@click.command()
@click.argument('eventid', default='')
def predict456(eventid):
original_working_directory = os.getcwd()
new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files\\'
# change to the networked directory
os.chdir(new_networked_directory)
#People = './ALL_UNQ/' + eventid + "/"
People="./ALL_UNQ/"+ eventid + "/"
#Gallery = './Copy_Gallery/'+ eventid + "/"
Gallery='./1/CopyGallery/'+ eventid + "/"
x= datetime.datetime.now()
print('ALLunq_copy_gallery Running')
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 = []
def f_CSVwrite():
import pandas as pd
q = pd.DataFrame(p)
#print(q)
m = q
# print(m)
# x.drop(x.columns[Unnam], axis=1, inplace=True)
df = m.groupby([0], as_index=False).count()
z = df[0].str.split('/', expand=True)
z.to_csv('zzzzzzzzzzzzz.csv',index=False)
import pandas as pd
df2 = pd.read_csv('./zzzzzzzzzzzzz.csv')
df2.rename({df2.columns[-1]: 'test'}, axis=1, inplace=True)
df2.rename({df2.columns[-2]: 'Matched'}, axis=1, inplace=True)
df2 = df2[['Matched', 'test']]
import pandas as pd
import os
c = []
for root, dirs, files in os.walk(Gallery,
topdown=False):
for name in files:
# print(name)
L = os.path.join(root, name)
c.append(L)
df = pd.DataFrame(c)
df1 = df[0].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(df2, df1, on='test', how='left')
merge.rename({merge.columns[-1]: 'EventName'}, axis=1, inplace=True)
# merge.to_csv('merge.csv')
mergesplit = merge.loc[:, 'test'].str.split(".", expand=True)
mergesplit.rename({mergesplit.columns[-2]: 'ImageName1'}, axis=1, inplace=True)
mergesplit = mergesplit.loc[:, 'ImageName1']
#merge.rename({merge.columns[-1]: 'Matched'}, axis=1, inplace=True)
#merge['EventName'] = merge['abc']
merge['Imagepath'] = "\\_files\\1\\Gallery\\" + merge['EventName'] + '\\' + + merge['test']
merge['Matched']='\\_files\\ALL_UNQ\\'+eventid+'\\'+df2['Matched']+'.jpg'
merge["ImageName"]=df2['Matched']+'.jpg'
frames = [merge, mergesplit]
r = pd.concat(frames, axis=1, join='inner')
df2 = r.dropna(subset=['Matched'])
#df2['Matched'] = df2['Matched'].astype(str)
#df2['Matched'] = df2['Matched'].astype(int)
column_list = ['Matched', 'ImageName','Imagepath', 'ImageName1', 'EventName']
df2[column_list].to_csv('Zero_Gallery123254.csv', index=False)
df2[column_list].to_json('events12554.json', orient="records")
# import json
#
# with open('events.json', 'r') as json_file:
# json_load = json.load(json_file)
#
# print(json_load)
import requests
import json
with open('events12554.json', 'r') as json_file:
json_load = json.load(json_file)
url = "https://eventxstreamnew.bizgaze.com:5443/apis/v4/bizgaze/integrations/testevents/unregistereduser"
payload = json.dumps(json_load).replace("]", "").replace("[", "")
print(payload)
headers = {
'Authorization': 'stat e44ced3eff684aa9b932672ea8406029',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print("##############################################################")
print(response.text)
p.clear()
# 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 = original_working_directory+"/Allunq_CopyGallery/" + name
path1 = original_working_directory+"/Allunq_CopyGallery/" + 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)
f_CSVwrite()
# Function for creating encodings for known people
def processKnownPeopleImages(path=People, saveLocation="./Zero_gallery_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
print(imgPath)
import pathlib
file = pathlib.Path(str(path+"Thumbs.db"))
if file.exists ():
os.remove(path+"Thumbs.db")
else:
pass
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("./Zero_gallery_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, "0",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)
# #print(q)
# m = q
# # print(m)
# # x.drop(x.columns[Unnam], axis=1, inplace=True)
# df = m.groupby([0], as_index=False).count()
# z = df[0].str.split('/', expand=True)
# z.to_csv('zzzzzzzzzzzzz.csv',index=False)
# import pandas as pd
# df2 = pd.read_csv('./zzzzzzzzzzzzz.csv')
# df2.rename({df2.columns[-1]: 'test'}, axis=1, inplace=True)
# df2.rename({df2.columns[-2]: 'Matched'}, axis=1, inplace=True)
# df2 = df2[['Matched', 'test']]
# import pandas as pd
# import os
# c = []
# for root, dirs, files in os.walk(Gallery,
# topdown=False):
# for name in files:
# # print(name)
# L = os.path.join(root, name)
# c.append(L)
# df = pd.DataFrame(c)
# df1 = df[0].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(df2, df1, on='test', how='left')
# merge.rename({merge.columns[-1]: 'EventName'}, axis=1, inplace=True)
# # 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']
# merge['Matched']='\\_files\\ALL_UNQ\\'+eventid+'\\'+df2['Matched']+'.jpg'
# merge["MatchedImageName"]=df2['Matched']
# frames = [merge, mergesplit]
# r = pd.concat(frames, axis=1, join='inner')
# df2 = r.dropna(subset=['Matched'])
# #df2['Matched'] = df2['Matched'].astype(str)
# #df2['Matched'] = df2['Matched'].astype(int)
# column_list = ['Matched','MatchedImageName','Imagepath', 'ImageName', 'EventName']
# df2[column_list].to_csv('Zero_Gallery123254.csv', index=False)
# df2[column_list].to_json('events12554.json', orient="records")
# import json
#
# with open('events.json', 'r') as json_file:
# json_load = json.load(json_file)
#
# print(json_load)
# import requests
# import json
# with open('events12554.json', 'r') as json_file:
# json_load = json.load(json_file)
# #url = "https://eventxstreamnew.bizgaze.com:5443/apis/v4/bizgaze/integrations/testevents/unregistereduser"
# payload = json.dumps(json_load).replace("]", "").replace("[", "")
# print(payload)
# headers = {
# 'Authorization': 'stat e44ced3eff684aa9b932672ea8406029',
# 'Content-Type': 'application/json'
# }
# response = requests.request("POST", url, headers=headers, data=payload)
# print("##############################################################")
# print(response.text)
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'
predict456()
+1
View File
@@ -0,0 +1 @@
[{"Matched":"\\_files\\ALL_UNQ\\100013660000161\\sefsf4556.jpg","ImageName":"sefsf4556.jpg","Imagepath":"\\_files\\1\\Gallery\\100013660000161\\100011460001404.jpg","ImageName1":"100011460001404","EventName":"100013660000161"}]
+117
View File
@@ -0,0 +1,117 @@
#from IPython.core.pylabtools import find_gui_and_backend
#from oswalk import files
#from google.colab.patches import cv2_imshow
import cv2
import mediapipe as mp
import numpy as np
import glob
import click
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5)
@click.command()
@click.argument('eventid', default='')
def cap(eventid):
for files in glob.glob("C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\sepration_crop\\"+eventid+"\\*"):
image = cv2.imread(files)
# Flip the image horizontally for a later selfie-view display
# Also convert the color space from BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# To improve performance
image.flags.writeable = False
# Get the result
results = face_mesh.process(image)
# To improve performance
image.flags.writeable = True
# Convert the color space from RGB to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
img_h, img_w, img_c = image.shape
face_3d = []
face_2d = []
if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
if idx == 1:
nose_2d = (lm.x * img_w, lm.y * img_h)
nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 8000)
x, y = int(lm.x * img_w), int(lm.y * img_h)
# Get the 2D Coordinates
face_2d.append([x, y])
# Get the 3D Coordinates
face_3d.append([x, y, lm.z])
# Convert it to the NumPy array
face_2d = np.array(face_2d, dtype=np.float64)
# Convert it to the NumPy array
face_3d = np.array(face_3d, dtype=np.float64)
# The camera matrix
focal_length = 1 * img_w
cam_matrix = np.array([ [focal_length, 0, img_h / 2],
[0, focal_length, img_w / 2],
[0, 0, 1]])
# The Distance Matrix
dist_matrix = np.zeros((4, 1), dtype=np.float64)
# Solve PnP
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
# Get rotational matrix
rmat, jac = cv2.Rodrigues(rot_vec)
# Get angles
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
# Get the y rotation degree
x = angles[0] * 360
y = angles[1] * 360
# print(y)
# See where the user's head tilting
if y < -20:
text = "Left"
elif y > 20:
text = "Right"
elif x < -20:
text = "Down"
else:
text = "Forward"
#djtillu.append(files)
print(files)
import os
import shutil
shutil.copy2(files, 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\front_face\\'+eventid+"\\")
# Display the nose direction
nose_3d_projection, jacobian = cv2.projectPoints(nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)
p1 = (int(nose_2d[0]), int(nose_2d[1]))
p2 = (int(nose_3d_projection[0][0][0]), int(nose_3d_projection[0][0][1]))
cv2.line(image, p1, p2, (255, 0, 0), 2)
# Add the text on the image
cv2.putText(image, text, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cv2_imshow(image)
#
# if cv2.waitKey(5) & 0xFF == 27:
# pass
cap()
+49
View File
@@ -0,0 +1,49 @@
import os
import click
import shutil
@click.command()
@click.argument('eventid', default='')
def checkfolder(eventid):
original_working_directory = os.getcwd()
new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files\\'
# change to the networked directory
os.chdir(new_networked_directory)
for dirpath, dirnames, files in os.walk('.\\ALL_UNQ\\' + eventid + '/'):
if os.listdir(dirpath)==[]:
print("files not found")
for root, dirs, files in os.walk('C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\unique_1\\' + eventid + '\\'):
for file in files:
path_file = os.path.join(root, file)
shutil.move(path_file, '.\\ALL_UNQ\\' + eventid + "\\")
else:
print("files found")
cmd = "python C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\unique_Allunq.py "+ str(eventid)
os.system(cmd)
checkfolder()
+16
View File
@@ -0,0 +1,16 @@
import requests
import json
with open('C:\\Users\\Bizgaze\\Desktop\\AI\\AI_Events\\csv\\EventXtream.json', 'r') as json_file:
json_load = json.load(json_file)
url = "http://localhost:3088/apis/v4/bizgaze/integrations/json/save/List"
payload1 = json.dumps(json_load)#.replace("]", "").replace("[", "")
print('--------------------------------------------------------------------------')
print(payload1)
headers = {
'Authorization': 'Stat 22cbfadfa548448bb0b55193bc8e99fa',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload1)
print("##############################################################")
print(response.text)
+252
View File
@@ -0,0 +1,252 @@
import os
import click
from flask import Flask, render_template, request, redirect, send_file
import shutil
import glob
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return render_template('index.html')
import datetime
# def crop_Alluq():
# import os
# cmd = "python .\\sepration_crop.py"
# os.system(cmd)
#
# import os
# cmd = "python .\\front_face.py"
# os.system(cmd)
#
# import os
# cmd = "python .\\sepration_cluster.py"
# os.system(cmd)
#
# import os
# cmd = "python .\\unique_1.py"
# os.system(cmd)
#
# import os
# cmd = "python .\\unique_Allunq.py"
# os.system(cmd)
#
# # import os
# # cmd = "python .\\Allunq_copy_gallery.py"
# # os.system(cmd)
#
# import os
# cmd = "python .\\remove.py"
# os.system(cmd)
#
#
# def sync_Alluq_people():
# import os
# cmd = "python .\\Allunq_People.py"
# os.system(cmd)
def crop_Alluq(eventid,original_working_directory):
print("started with images")
import os
cmd = "python "+original_working_directory+"\\sepration_crop.py"+" "+str(eventid)
os.system(cmd)
import os
cmd = "python "+original_working_directory+"\\front_face.py"+" "+str(eventid)
os.system(cmd)
import os
cmd = "python "+original_working_directory+"\\sepration_cluster.py"+" "+str(eventid)
os.system(cmd)
import os
cmd = "python "+original_working_directory+"\\unique_1.py"+" "+str(eventid)
os.system(cmd)
#eventid="789456123"
import os
cmd = "python "+original_working_directory+"\\is_existALLUNQ.py"+" "+str(eventid)
os.system(cmd)
import os
cmd = "python "+original_working_directory+"\\Allunq_copy_gallery.py"+" "+str(eventid)
os.system(cmd)
import os
cmd = "python "+original_working_directory+"\\Allunq_People.py"+" "+str(eventid)
os.system(cmd)
return "ended with images"
# import os
# cmd = "python .\\remove.py"
# os.system(cmd)
def sync_Alluq_people(eventid,original_working_directory):
import os
cmd = "python "+original_working_directory+"\\Allunq_People.py"+" "+str(eventid)
os.system(cmd)
return "ended with images"
def create_dir(eventid,original_working_directory):
# original_working_directory = os.getcwd()
# new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files\\'
# # change to the networked directory
# os.chdir(new_networked_directory)
print(original_working_directory)
from pathlib import Path
Path(original_working_directory+'\\front_face\\' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/sepration_cluster/'+eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/sepration_crop/' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/unique_1/' + eventid).mkdir(exist_ok=True)
Path('ALL_UNQ/' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/output_unique_ALLUNQ/' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/people_Allunq_zero_maingallery/' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/Allunq_People/' + eventid).mkdir(exist_ok=True)
Path(original_working_directory+'/Allunq_CopyGallery/' + eventid).mkdir(exist_ok=True)
@app.route('/eventwise', methods=["GET", "POST"])
def eventwise():
import os
original_working_directory = os.getcwd()
new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files'
# change to the networked directory
os.chdir(new_networked_directory)
eventid= request.args.get('Dataset')
# Id.append(Events)
create_dir(eventid,original_working_directory)
import pathlib
file = pathlib.Path(new_networked_directory+"/"+eventid+"/"+"Thumbs.db")
if file.exists ():
os.remove(new_networked_directory+"/"+eventid+"/"+'Thumbs.db')
else:
pass
x = datetime.datetime.now()
print('Execution Started at:', x)
import os
# path of the directory
for dirpath, dirnames, files in os.walk('1/CopyGallery/' + eventid + '/'):
if os.listdir(dirpath)==[]:
#f = os.path.join(root, name)
# Checking the length of list
#if len((f)) == 0:
print("No files found in the directory.")
print("working on sync_Alluq_people.........")
sync_Alluq_people(eventid,original_working_directory)
else:
print("Some files found in the directory.")
print("working on crop_Alluq.........")
crop_Alluq(eventid,original_working_directory)
return "ended with images"
#
#
# import os
# cmd = "python .\\people_Allunq_zero_maingallery.py"
# os.system(cmd)
# import os
# cmd = "python .\\remove.py"
# os.system(cmd)
# import requests
# import json
# with open('C:\\Users\\Bizgaze\\Desktop\\AI\\AI_Events\\csv\\EventXtream.json', 'r') as json_file:
# json_load = json.load(json_file)
# url = "http://localhost:3088/apis/v4/bizgaze/integrations/json/save"
# payload1 = json.dumps(json_load).replace("]", "").replace("[", "")
# print('--------------------------------------------------------------------------')
# print(payload1)
# headers = {
# 'Authorization': 'Stat a528db7c512f494eab8bfef012c220e0',
# 'Content-Type': 'application/json'
# }
# response = requests.request("POST", url, headers=headers, data=payload1)
# print("##############################################################")
# print(response.text)
# y = datetime.datetime.now()
# print('Completed at:', y)
# z = y - x
# print('Time Taken:', z)
# return render_template('index.html')
# # return 'ALL IMAGES MATCHED'
@app.route('/json')
def json():
p = './path.json'
return send_file(p, as_attachment=True)
if __name__ == "__main__":
app.run(host="0.0.0.0",port=5001,debug=True)
Binary file not shown.
Binary file not shown.
+419
View File
@@ -0,0 +1,419 @@
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()
+41
View File
@@ -0,0 +1,41 @@
# Python program to check if
# a directory contains file
import os
# directoryPath = "Copy_Gallery"
# for root, dirs, files in os.walk(directoryPath):
# for name in files:
# f = os.path.join(root, name)
# print(type(f))
# # if len(f)==568:
# # print("none")
# # else:
# # print("yes")
# Python program to check whether
# the directory empty or not
import os
# path of the directory
path = "Copy_Gallery"
# Getting the list of directories
dir = os.listdir(path)
# Checking if the list is empty or not
if len(dir) == 0:
print("Empty directory")
else:
print("Not empty directory")
import glob
from os import listdir
for f in glob.glob("Copy_Gallery/*"):
if len(os.listdir(f))==0:
print("file not found")
else:
print("file found")
BIN
View File
Binary file not shown.
+88
View File
@@ -0,0 +1,88 @@
import os
import shutil
files = './front_face/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './output_unique_ALLUNQ/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './sepration_cluster/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './sepration_crop/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './unique_1/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './Allunq_People/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
files = './people_Allunq_zero_maingallery/'
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
# files = './unique_1/'
# for root, dirs, files in os.walk(files):
# for f in files:
# os.unlink(os.path.join(root, f))
# for d in dirs:
# shutil.rmtree(os.path.join(root, d))
# files = './Copy_Gallery/'
#
# for root, dirs, files in os.walk(files):
# for f in files:
# os.unlink(os.path.join(root, f))
# for d in dirs:
# shutil.rmtree(os.path.join(root, d))
os.remove('known_encodings.pickle')
os.remove('people_copyGallery_known_encodings.pickle')
os.remove('Zero_gallery_known_encodings.pickle')
+99
View File
@@ -0,0 +1,99 @@
import sys
import os
import dlib
import glob
import time
import uuid
from main_application import *
import click
@click.command()
@click.argument('eventid', default='')
def sep_clust(eventid):
Gallery = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\front_face\\' + eventid + "\\"
start = time.time()
# if len(sys.argv) != 3:
# print("Please specify valid arguments. Call the program like this \npython face_clustering.py -specify input folder- -specify output path-")
# exit()
predictor_path = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\model\\shape_predictor_68_face_landmarks.dat'
face_rec_model_path = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\model\\dlib_face_recognition_resnet_model_v1.dat'
# faces_folder_path = sys.argv[1]
output_folder = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\sepration_cluster\\' + eventid + "\\"
import os
import shutil
files = output_folder
for root, dirs, files in os.walk(files):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
detector = dlib.get_frontal_face_detector() # a detector to find the faces
sp = dlib.shape_predictor(predictor_path) # shape predictor to find face landmarks
facerec = dlib.face_recognition_model_v1(face_rec_model_path) # face recognition model
descriptors = []
images = []
for root, dirs, files in os.walk(Gallery, topdown=False):
for name in files:
f = os.path.join(root, name)
# Load the images from input folder
# for f in glob.glob(os.path.join(faces_folder_path, "*")):
print("Processing file: {}".format(f))
img = dlib.load_rgb_image(f)
# Ask the detector to find the bounding boxes of each face. The 1 in the second argument indicates that we should upsample the image 1 time. This will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
# Now process each face we found.
for k, d in enumerate(dets):
# Get the landmarks/parts for the face in box d.
shape = sp(img, d)
# Compute the 128D vector that describes the face in img identified by shape.
face_descriptor = facerec.compute_face_descriptor(img, shape)
descriptors.append(face_descriptor)
images.append((img, shape))
# Cluster the faces.
labels = dlib.chinese_whispers_clustering(descriptors, 0.40)
num_classes = len(set(labels)) # Total number of clusters
print("Number of clusters: {}".format(num_classes))
for i in range(0, num_classes):
indices = []
class_length = len([label for label in labels if label == i])
for j, label in enumerate(labels):
if label == i:
indices.append(j)
print("Indices of images in the cluster {0} : {1}".format(str(i), str(indices)))
print("Size of cluster {0} : {1}".format(str(i), str(class_length)))
output_folder_path = output_folder + str(i) # Output folder for each cluster
os.path.normpath(output_folder_path)
os.makedirs(output_folder_path)
# Save each face to the respective cluster folder
print("Saving faces to output folder...")
for k, index in enumerate(indices):
img, shape = images[index]
x = img
# file_path2=os.path.join("C:/Users/katku/Desktop/spyder/192.168.89.91_windows/final_crop_cluster_FaceRecognition/unique/",str(uuid.uuid4().hex[:15])+str(i))
file_path = os.path.join(output_folder_path, str(uuid.uuid4().hex[:15]) + str(k) + str(i))
# dlib.save_face_chip(img, shape, file_path2, size=150, padding=568.25)
dlib.save_face_chip(img, shape, file_path, size=150, padding=0.25)
print("--- %s seconds ---" % (time.time() - start))
sep_clust()
+39
View File
@@ -0,0 +1,39 @@
# What is the code to do : Extract faces from all image files in 'directory' and save them in 'out_src'.
import os
# - pip install ObjectExtractor ( OR pip3 install ObjectExtractor)
from main_application import *
from object_extractor import Extractor, FRONTALFACE_ALT2
import uuid
import main_application
import click
@click.command()
@click.argument('eventid', default='')
def crop(eventid):
# original_working_directory = os.getcwd()
# new_networked_directory = r'\\192.168.88.99\\Bizgaze\\port6003\\wwwroot\\_files\\'
# # change to the networked directory
# os.chdir(new_networked_directory)
CURRENT_PATH = os.path.dirname(__file__)
# extensions = ['jpeg', 'png']
inputImg = 'Z:\\1\\CopyGallery\\' + eventid + "\\"
out_src = '.\\sepration_crop\\' + eventid + "\\"
index = 1
for root, dirs, files in os.walk(inputImg, topdown=False):
for name in files:
f = os.path.join(root, name)
Extractor.extract(os.path.join(CURRENT_PATH, f), cascade_file=FRONTALFACE_ALT2,
output_directory=os.path.join(CURRENT_PATH, out_src),
output_prefix=str(uuid.uuid4().hex[:15]) + str(index),
start_count=1)
#os.remove(f)
index = index + 1
crop()
+10
View File
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Sample Code</title>
</head>
<body>
<h1>Opening a folder from HTML code</h1>
<a href='/home/bizgaze/PycharmProjects/img/output'>Click to open a folder</a>
</body>
</html>
+32
View File
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Gallery</h1>
</div>
<a class="button button5"href="/">HOME</a>
<hr>
{% for image_name in image_names %}
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<img class="img-responsive" src=" {{url_for('send_image', filename=image_name )}}"style="width:300px;height:300px;">
</div>
{% endfor %}
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
crossorigin="anonymous"></script>
</body>
</html>
+84
View File
@@ -0,0 +1,84 @@
<html>
<head>
<style>
.button {
background-color: #000000; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
margin: 4px 2px;
cursor: pointer;
}
.button1 {font-size: 10px;}
.button2 {font-size: 12px;}
.button3 {font-size: 16px;}
.button4 {font-size: 50px;}
.button5 {font-size: 24px;border-radius: 12px;}
</style>
</head>
<body>
<div style="background-image: url('https://lh3.googleusercontent.com/p/AF1QipONWF8G50u9Bu-dklcj3kzesofOn8Z0q0LdHeU1=w1080-h608-p-no-v0');
/* Full height */
height: 100%;
/ Center and scale the image nicely /
background-position: center;
background-repeat: no-repeat;
background-size: cover;">
<br> <br>
<form action="/upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<div class="custom-file">
<input type="file" class="button button5" name="image" id="image"> <br> <br><br>
<div class="bg"></div>
<input class="button button5" type="submit"> <br><br></div></div></form>
<form action="{{ url_for('predict') }}" method="GET">
<input type="submit" class="button button5" value="predict"></form>
<br>
<form action="{{ url_for('json') }}" method="GET">
<input type="submit" class="button button5" value="json"></form>
<br>
<style>
body {
width: 100%;
height:100%;
font-family: 'Helvetica';
background-color:#000000;
color: #fff;
font-size: 24px;
text-align:center;
letter-spacing:1.4px;
}
</style>
</body>
</html>
+39
View File
@@ -0,0 +1,39 @@
################################## Selection first file from all folder #######################
import os
import shutil
import click
@click.command()
@click.argument('eventid', default='')
def unq1(eventid):
p1 = r"C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\sepration_cluster\\" + eventid + "\\"
p2 = r"C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\unique_1\\" + eventid + "\\"
for path, folders, files in os.walk(p1):
if not files: continue
try:
src = os.path.join(path, files[0])
except IndexError:
pass
dst_path = path.replace(p1, '') + os.sep
# dst_folder = p2 + dst_path
# create the target dir if doesn't exist
# if not os.path.exists(dst_folder):
# os.makedirs(dst_folder)
# create dst file with only the first file
try:
dst = p2 + files[0]
except IndexError:
pass
# copy the file
shutil.copy2(src, dst)
unq1()
+352
View File
@@ -0,0 +1,352 @@
import pickle
import numpy as np
import face_recognition
import os
import cv2
import datetime
import click
@click.command()
@click.argument('eventid', default='')
def predict(eventid):
Gallery = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\unique_1\\' + eventid + "\\"
People = './ALL_UNQ/' + eventid + "/"
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 = "C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\output_unique_ALLUNQ\\" + name
path1 = "C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\output_unique_ALLUNQ\\" + 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="./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("./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)
try:
orig = image.copy()
image = cv2.resize(image, (0, 0), fx=0.9, fy=0.9, interpolation=cv2.INTER_LINEAR)
except AttributeError:
os.remove(s)
# Resize
# 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)
# m = q
# # print(m)
# # x.drop(x.columns[Unnam], axis=1, inplace=True)
# df = m.groupby([568], as_index=False).count()
# z = df[568].str.split('/', expand=True)
# z.rename({z.columns[-2]: 'Matched'}, axis=1, inplace=True)
# z.rename({z.columns[-1]: 'croped_guest_pic'}, axis=1, inplace=True)
# #z = z.iloc[:, 3:]
# z.to_csv('unique_people.csv')
# z=pd.read_csv('unique_people.csv')
# #z.drop(z.index[z['Matched'] == 568], inplace=True)
# z = z.iloc[:, 3:]
# z['Matched'] = z['Matched'].apply(str)
# z.to_csv('unique_people.csv',index=False)
# import os
# import shutil
# for root, dirs, files in os.walk('./output_unique_ALLUNQ/'+eventid+'/568/'):
# for file in files:
# path_file = os.path.join(root, file)
# shutil.move(path_file, './ALL_UNQ/'+eventid+"/")
print("Completed")
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()