123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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()
|