Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sepration_cluster.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import sys
  2. import os
  3. import dlib
  4. import glob
  5. import time
  6. import uuid
  7. from main_application import *
  8. import click
  9. @click.command()
  10. @click.argument('eventid', default='')
  11. def sep_clust(eventid):
  12. Gallery = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\front_face\\' + eventid + "\\"
  13. start = time.time()
  14. # if len(sys.argv) != 3:
  15. # print("Please specify valid arguments. Call the program like this \npython face_clustering.py -specify input folder- -specify output path-")
  16. # exit()
  17. predictor_path = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\model\\shape_predictor_68_face_landmarks.dat'
  18. face_rec_model_path = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\model\\dlib_face_recognition_resnet_model_v1.dat'
  19. # faces_folder_path = sys.argv[1]
  20. output_folder = 'C:\\Users\\Administrator\\Documents\\AI\\runtimecropimages\\sepration_cluster\\' + eventid + "\\"
  21. import os
  22. import shutil
  23. files = output_folder
  24. for root, dirs, files in os.walk(files):
  25. for f in files:
  26. os.unlink(os.path.join(root, f))
  27. for d in dirs:
  28. shutil.rmtree(os.path.join(root, d))
  29. detector = dlib.get_frontal_face_detector() # a detector to find the faces
  30. sp = dlib.shape_predictor(predictor_path) # shape predictor to find face landmarks
  31. facerec = dlib.face_recognition_model_v1(face_rec_model_path) # face recognition model
  32. descriptors = []
  33. images = []
  34. for root, dirs, files in os.walk(Gallery, topdown=False):
  35. for name in files:
  36. f = os.path.join(root, name)
  37. # Load the images from input folder
  38. # for f in glob.glob(os.path.join(faces_folder_path, "*")):
  39. print("Processing file: {}".format(f))
  40. img = dlib.load_rgb_image(f)
  41. # 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.
  42. dets = detector(img, 1)
  43. print("Number of faces detected: {}".format(len(dets)))
  44. # Now process each face we found.
  45. for k, d in enumerate(dets):
  46. # Get the landmarks/parts for the face in box d.
  47. shape = sp(img, d)
  48. # Compute the 128D vector that describes the face in img identified by shape.
  49. face_descriptor = facerec.compute_face_descriptor(img, shape)
  50. descriptors.append(face_descriptor)
  51. images.append((img, shape))
  52. # Cluster the faces.
  53. labels = dlib.chinese_whispers_clustering(descriptors, 0.40)
  54. num_classes = len(set(labels)) # Total number of clusters
  55. print("Number of clusters: {}".format(num_classes))
  56. for i in range(0, num_classes):
  57. indices = []
  58. class_length = len([label for label in labels if label == i])
  59. for j, label in enumerate(labels):
  60. if label == i:
  61. indices.append(j)
  62. print("Indices of images in the cluster {0} : {1}".format(str(i), str(indices)))
  63. print("Size of cluster {0} : {1}".format(str(i), str(class_length)))
  64. output_folder_path = output_folder + str(i) # Output folder for each cluster
  65. os.path.normpath(output_folder_path)
  66. os.makedirs(output_folder_path)
  67. # Save each face to the respective cluster folder
  68. print("Saving faces to output folder...")
  69. for k, index in enumerate(indices):
  70. img, shape = images[index]
  71. x = img
  72. # 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))
  73. file_path = os.path.join(output_folder_path, str(uuid.uuid4().hex[:15]) + str(k) + str(i))
  74. # dlib.save_face_chip(img, shape, file_path2, size=150, padding=568.25)
  75. dlib.save_face_chip(img, shape, file_path, size=150, padding=0.25)
  76. print("--- %s seconds ---" % (time.time() - start))
  77. sep_clust()