Ejemplo n.º 1
0
 def get(self, img):
     ret = self.detector.detect_face(img, det_type=0)
     if ret is None:
         return None
     bbox, points = ret
     if bbox.shape[0] == 0:
         return None
     bbox = bbox[0, 0:4]
     points = points[0, :].reshape((2, 5)).T
     M = img_helper.estimate_trans_bbox(bbox, self.image_size[0], s=2.0)
     rimg = cv2.warpAffine(img, M, self.image_size, borderValue=0.0)
     img = cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)
     img = np.transpose(img, (2, 0, 1))  #3*112*112, RGB
     input_blob = np.zeros((1, 3, self.image_size[1], self.image_size[0]),
                           dtype=np.uint8)
     input_blob[0] = img
     ta = datetime.datetime.now()
     data = mx.nd.array(input_blob)
     db = mx.io.DataBatch(data=(data, ))
     self.model.forward(db, is_train=False)
     alabel = self.model.get_outputs()[-1].asnumpy()[0]
     tb = datetime.datetime.now()
     print('time cost', (tb - ta).total_seconds())
     ret = np.zeros((alabel.shape[0], 2), dtype=np.float32)
     for i in xrange(alabel.shape[0]):
         a = cv2.resize(alabel[i], (self.image_size[1], self.image_size[0]))
         ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
         #ret[i] = (ind[0], ind[1]) #h, w
         ret[i] = (ind[1], ind[0])  #w, h
     return ret, M
Ejemplo n.º 2
0
    def get(self, img):
        # ret = self.detector.detect_face(img)
        ret = self.detector.detect(img, threshold=0.5)
        if ret is None:
            return None
        bbox = ret[:, 0:4]
        points = ret[:, 5:15].reshape(-1, 5, 2)
        # bbox, points = ret
        # if bbox.shape[0]==0:
        #   return None
        # bbox = bbox[:,0:4]
        # points = points[:,:].reshape((-1,2,5))
        # points = np.transpose(points, (0,2,1))
        # for b in bbox:
        #   cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 0), 2)
        # for p in points:
        #   for i in range(5):
        #     cv2.circle(img, (p[i][0], p[i][1]), 1, (0, 0, 255), 2)
        # cv2.imshow("detection result", img)
        # cv2.waitKey(0)

        M = np.zeros((bbox.shape[0], 2, 3), dtype=np.float32)
        ret = np.zeros((bbox.shape[0], 68, 2), dtype=np.float32)
        for i in range(bbox.shape[0]):
            M[i] = img_helper.estimate_trans_bbox(bbox[i, :],
                                                  self.image_size[0],
                                                  s=1.2)
            rimg = cv2.warpAffine(img, M[i], self.image_size, borderValue=0.0)
            img2 = cv2.cvtColor(rimg, cv2.COLOR_BGR2RGB)
            img2 = np.transpose(img2, (2, 0, 1))  #3*112*112, RGB
            # cv2.imshow("detection result", rimg)
            # cv2.waitKey(0)
            # filename = 'sample-images/%d.jpg'%(i+1)
            # cv2.imwrite(filename, rimg)
            input_blob = np.zeros(
                (1, 3, self.image_size[1], self.image_size[0]), dtype=np.uint8)
            input_blob[0] = img2
            ta = datetime.datetime.now()
            data = mx.nd.array(input_blob)
            db = mx.io.DataBatch(data=(data, ))
            self.model.forward(db, is_train=False)
            alabel = self.model.get_outputs()[-1].asnumpy()[0]
            tb = datetime.datetime.now()
            print('module time cost', (tb - ta).total_seconds())
            # ret = np.zeros( (alabel.shape[0], 2), dtype=np.float32)
            for j in xrange(alabel.shape[0]):
                a = cv2.resize(alabel[j],
                               (self.image_size[1], self.image_size[0]))
                ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
                #ret[i] = (ind[0], ind[1]) #h, w
                ret[i, j] = (ind[1], ind[0])  #w, h
        return ret, M