def getFaces(images): ''' images :: tuple(name, numpy array) n :: ground_truth number of faces in an image ''' faces = [] for img in images: print("Working on new image") fls = fr.face_locations(img[1], model='hog') encs = fr.face_encodings(img[1], fls) if len(fls) == 0: print("GOT NO FACES IN IMAGE") print(len(encs), len(fls)) b = 1 for (fl, enc) in zip(fls, encs): face = Face() face.bound_box = fl face.img_name = img[0] face.box_no = b b += 1 face.enc = enc faces.append(face) x1, y2, x2, y2 = fl #print(x1,y1,x2,y2) #if len(fls)>10: # cv2.rectangle(img[1], (y1,x1), (y2,x2), (0, 0, 255), 5) #if len(fls)>10: # plt.imshow(img[1]) # plt.show() return faces
def getFaces(images, n=-1): ''' images :: tuple(name, numpy array) n :: ground_truth number of faces in an image ''' faces = [] possible_people = [] for img in images: print("Working on new image") fls = fr.face_locations(img[1], model='hog') encs = fr.face_encodings(img[1], fls) if len(fls) == 0: print("GOT NO FACES IN IMAGE") if (len(fls) == n): start = len(faces) end = start + n possible_people.append(list(range(start, end))) print(len(encs), len(fls)) b = 1 fig, ax = plt.subplots(1) ax.imshow(img[1]) for (fl, enc) in zip(fls, encs): face = Face() face.bound_box = fl face.img_name = img[0] face.box_no = b b += 1 face.enc = enc faces.append(face) tlx, tly, brx, bry = fl #x1, y2, x2, y1 x1 = tlx x2 = brx y1 = bry y2 = tly face.myFace = img[1][x1:x2, y1:y2] #print(x1,y1,x2,y2) if len(fls) > -1: cv2.rectangle(img[1], (y1, x1), (y2, x2), (0, 0, 255), 5) rect = patches.Rectangle((x1, y1), (x2 - x1), (y2 - y1), linewidth=2, edgecolor='b') #ax.add_patch(rect) if len(fls) > -1: y = cv2.resize(np.array(img[1]), (0, 0), fx=0.4, fy=0.4) cv2.imshow('disp', y) cv2.waitKey(1) #plt.show() #cv2.imshow(img[0], img[1]) #cv2.waitKey() #cv2.destroyAllWindows() if (possible_people): return faces, possible_people[0] else: print("NO IMAGE WITH ALL PHOTOS") #exit(0) return faces, possible_people