def getFaces(images):
    encs = []
    faces = []
    for n, img in enumerate(images):
        print('Working on image [{}]'.format(n))
        dets, scores, idx = detector.run(img, 1, -0.5)
        print('{} faces detected'.format(len(dets)))
        faces_n = []
        boxes = []
        encs_n = []
        for i, d in enumerate(dets):
            face = Face()
            x1 = d.top(); y2 = d.right(); x2 = d.bottom(); y1 = d.left();
            face.bound_box = (x1, y1, x2, y2)
            boxes.append((x1, y2, x2, y1))
            face.img_id = n
            face.myFace = img[x1:x2, y1:y2]
            faces_n.append(face)
            cv2.rectangle(img, (y1,x1), (y2,x2), (0, 0, 255), 5)
            print('{:.02f}'.format(scores[i]), idx[i], x1, y1, x2, y2)
        encs_n = fr.face_encodings(img, boxes)
        for i, e in enumerate(encs_n):
            faces_n[i].enc = e
            faces.append(faces_n[i])
            encs.append(e)
        plt.imshow(img)
        plt.axis('off')
        plt.show()
    return faces, np.array(encs)
Exemple #2
0
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
Exemple #4
0
 def detect_embedd(self, images):
     faces = []
     for ind, image in enumerate(images):
         print("Detecting faces in image {}/{}".format(
             ind + 1, len(images)))
         newFaces = []
         det_arr, emb_arr = get_faces(image)
         for fl, enc in zip(det_arr, emb_arr):
             face = Face()
             face.img_name = img.fileName
             face.enc = emb_arr
             x0, x1, x2, x3 = fl
             face.bound_box = (x1, x2, x3, x0)
             face.myFace = img.getImage()[x1:x3, x0:x2]
             newFaces.append(face)
         image.faces = np.array(newFaces)
         faces += newFaces
     return faces
 def detect(self, images):
     faces = []
     for ind, img in enumerate(images):
         print("Detecting faces in image {}/{}".format(
             ind + 1, len(images)))
         newfaces = []
         fls = fr.face_locations(img.getImage(), model=self.model_name)
         encs = fr.face_encodings(img.getImage(), fls)
         for (fl, enc) in zip(fls, encs):
             face = Face()
             face.bound_box = fl
             face.img_name = img.fileName
             face.enc = enc
             tlx, tly, brx, bry = fl
             x1 = tlx
             x2 = brx
             y1 = bry
             y2 = tly
             face.myFace = img.image[x1:x2, y1:y2]
             newfaces.append(face)
         img.faces = np.array(newfaces)
         faces += newfaces
     return faces