def demo_image(imgs, model, opt, orig_image,min_x,min_y):
  #print(len(imgs))
  preds =[]
  for i in range(len(imgs)):
    image = imgs[i]
    s = max(image.shape[0], image.shape[1]) * 1.0
    c = np.array([image.shape[1] / 2., image.shape[0] / 2.], dtype=np.float32)
    trans_input = get_affine_transform(
        c, s, 0, [opt.input_w, opt.input_h])
    inp = cv2.warpAffine(image, trans_input, (opt.input_w, opt.input_h),
                          flags=cv2.INTER_LINEAR)
    inp = (inp / 255. - mean) / std
    inp = inp.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)
    inp = torch.from_numpy(inp).to(opt.device)
    out = model(inp)[-1]
    pred = get_preds(out['hm'].detach().cpu().numpy())[0]
    pred = transform_preds(pred, c, s, (opt.output_w, opt.output_h))
    preds.append(pred)
    #print(np.array(pred))

    #print(points_2d)
    pts_upper, pts_lower = mask_plot(image,np.array(pred),mpii_edges)
    pts_upper = np.array([pts_upper[:,0] + min_x[i] , pts_upper[:,1] + min_y[i]]).T
    pts_lower = np.array([pts_lower[:,0] + min_x[i], pts_lower[:,1] + min_y[i]]).T
    img = draw_fill(orig_image,pts_upper,pts_lower) 
  print('Predictions: '+str(len(preds)))
  #img = show_2d(preds,(0,0,255),mpii_edges,orig_image,min_x,min_y)
  
  
  return img
Ejemplo n.º 2
0
    def compute_features(self, register=False):

        img, src_point, self.roi = self.LandmarkDetector.detect_landmarks(
            0.4, return_img=True)
        self.max_score = 0
        self.id = -1

        if src_point:
            # align face to standard position
            a = img.pix_to_ai()

            T = image.get_affine_transform(src_point, self.dst_point)
            a = image.warp_affine_ai(img, self.img_face, T)
            a = self.img_face.ai_to_pix()

            if register:
                reg_img = image.Image('logo.jpg')
                a = reg_img.draw_image(
                    self.img_face,
                    (lcd.width() // 2 - 68, lcd.height() // 2 - 20))
                a = reg_img.draw_string(30,
                                        lcd.height() // 2 - 48,
                                        "Registring face",
                                        color=(0, 255, 0),
                                        scale=2,
                                        mono_space=False)
                a = lcd.display(reg_img)
                del (reg_img)
                time.sleep(2)

            a = self.img_face.pix_to_ai()
            # calculate face feature vector
            fmap = kpu.forward(self.fe_task, self.img_face)
            #print(fmap[:])
            vector = list(map(lambda x: x / 256, fmap[:]))
            self.feature = kpu.face_encode(vector)

            for id in self.db.keys():
                entry = _unhexlify(self.db[id]['vector'])
                score = kpu.face_compare(entry, self.feature)
                if score > self.max_score:
                    self.max_score = score
                    name = self.db[id]['name']
                    self.id = id

            if not self.max_score > self.threshold:
                name = 'Unknown'

            a = img.draw_rectangle(self.roi,
                                   color=(0x1c, 0xa2, 0xff),
                                   thickness=2)
            a = img.draw_string(self.roi[0],
                                self.roi[1] - 14,
                                ("%s %%: %.2f" % (name, self.max_score)),
                                color=(255, 255, 255),
                                scale=1.5,
                                mono_space=False)

            a = lcd.display(img)
            del (img)
Ejemplo n.º 3
0
    def pre_process(self, image):
        self.image_height, self.image_width = image.shape[0:2]
        trans_input = get_affine_transform(self.__meta_c, self.__meta_s, 0, [self.net_inp_width, self.net_inp_height])
        inp_image = cv2.warpAffine(image, trans_input, (self.net_inp_width, self.net_inp_height),
                                   flags=cv2.INTER_LINEAR)

        inp_image = ((inp_image / 255. - self.mean) / self.std).astype(np.float32)
        images = inp_image.transpose(2, 0, 1).reshape(1, 3, self.net_inp_height, self.net_inp_width)

        img_numpy = np.ascontiguousarray(images, dtype=np.float32)
        return img_numpy
    def __getitem__(self, index):
        if os.path.exists(self.imgs_path[index]):
            img = cv2.imread(self.imgs_path[index])
        else:
            print("%s not exists" % self.imgs_path[index])
        anns = np.array(self.words[index])
        bboxes = anns[:, :4]
        bboxes = np.array([self._coco_box_to_bbox(bb) for bb in bboxes])
        lms = np.zeros((anns.shape[0], 10), dtype=np.float32)
        if self.split == "train":
            for idx, ann in enumerate(anns):
                lm = np.zeros(10, dtype=np.float32) - 1
                if ann[4] >= 0:
                    for i in range(5):

                        lm[i * 2] = ann[4 + 3 * i]
                        lm[i * 2 + 1] = ann[4 + 3 * i + 1]
                lms[idx] = lm
        num_objs = min(len(anns), self.max_objs)
        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        s = max(img.shape[0], img.shape[1]) * 1.0
        input_h, input_w = self.default_resolution[0], self.default_resolution[
            1]

        flipped = False
        if self.split == 'train':
            s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
            w_border = self._get_border(128, img.shape[1])
            h_border = self._get_border(128, img.shape[0])
            c[0] = np.random.randint(low=w_border,
                                     high=img.shape[1] - w_border)
            c[1] = np.random.randint(low=h_border,
                                     high=img.shape[0] - h_border)
            if np.random.random() < 0.5:
                flipped = True
                img = img[:, ::-1, :]
                c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)

        inp1 = inp.copy()
        inp = (inp.astype(np.float32) / 255.)
        color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)
        inp = (inp - self.mean) / self.std
        inp = inp.transpose(2, 0, 1)

        output_h = input_h // self.down_ratio
        output_w = input_w // self.down_ratio

        num_classes = 1
        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])

        hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)

        landmarks = np.zeros((self.max_objs, 10), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        lm_reg = np.zeros((self.max_objs, 10), dtype=np.float32)
        lm_ind = np.zeros((self.max_objs), dtype=np.int64)
        lm_mask = np.zeros((self.max_objs), dtype=np.uint8)

        gt_det = []
        cls_id = 0
        for k in range(num_objs):
            flag_lm = False
            bbox = bboxes[k]
            lm = lms[k]
            bbox1 = bbox.copy()
            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1
                if lm[0] >= 0:
                    lm[0::2] = width - lm[0::2] - 1
                    l_tmp = lm.copy()
                    lm[0:2] = l_tmp[2:4]
                    lm[2:4] = l_tmp[0:2]
                    lm[6:8] = l_tmp[8:10]
                    lm[8:10] = l_tmp[6:8]

            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            if lm[0] >= 0:
                lm[:2] = affine_transform(lm[:2], trans_output)
                lm[2:4] = affine_transform(lm[2:4], trans_output)
                lm[4:6] = affine_transform(lm[4:6], trans_output)
                lm[6:8] = affine_transform(lm[6:8], trans_output)
                lm[8:10] = affine_transform(lm[8:10], trans_output)

            bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1)
            bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1)

            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]
            if h > 0 and w > 0:
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                ct = np.array([(bbox[0] + bbox[2]) / 2,
                               (bbox[1] + bbox[3]) / 2],
                              dtype=np.float32)
                ct_int = ct.astype(np.int32)

                draw_umich_gaussian(hm[cls_id], ct_int, radius)

                wh[k] = 1. * w, 1. * h

                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1
                if lm[0]>0 and lm[1]< output_h and lm[2] < output_w and lm[3] < output_h \
                    and lm[6] > 0 and lm[7] > 0 and lm[8] < output_w and lm[9] > 0:

                    lm_ind[k] = ct_int[1] * output_w + ct_int[0]
                    if h * w > 10:
                        lm_mask[k] = 1

                    lm_temp = lm.copy()
                    lm_int = lm_temp.astype(np.int32)
                    lm_reg[k] = lm_temp - lm_int
                    lm_temp[[0, 2, 4, 6,
                             8]] = lm_temp[[0, 2, 4, 6, 8]] - ct_int[0]
                    lm_temp[[1, 3, 5, 7,
                             9]] = lm_temp[[1, 3, 5, 7, 9]] - ct_int[1]
                    landmarks[k] = lm_temp
                gt_det.append([
                    4 * (ct[0] - w / 2), 4 * (ct[1] - h / 2),
                    4 * (ct[0] + w / 2), 4 * (ct[1] + h / 2)
                ])
        # if self.debug :# and ("COCO" in str(self.imgs_path[files_index])):
        #     print(len(lms), len(bboxes))
        #     import matplotlib
        #     matplotlib.use('Agg')
        #     import matplotlib.pyplot as plt
        #     for lm, bb in zip(lms, bboxes):
        #         plt.figure(figsize=(50, 50))

        #         if bb[3] - bb[1] > 0 and bb[2] - bb[0] and np.array(np.where(lm > 0)).shape[1] ==10:
        #             cv2.circle(inp1, (int(lm[0]), int(lm[1])), 2, (255, 0, 0), -1)
        #             cv2.circle(inp1, (int(lm[2]), int(lm[3])), 2, (255, 255, 0), -1)
        #             cv2.circle(inp1, (int(lm[4]), int(lm[5])), 2, (255, 155, 155), -1)
        #             cv2.circle(inp1, (int(lm[6]), int(lm[7])), 2, (255, 0, 255), -1)
        #             cv2.circle(inp1, (int(lm[8]), int(lm[9])), 2, (65, 86, 255), -1)
        #             plt.plot(bb[[0, 2, 2, 0, 0]].T, bb[[1, 1, 3, 3, 1]].T, '.-')
        #     plt.imshow(inp1)
        #     plt.axis('off')
        #     plt.savefig('debug/_after%s'%self.imgs_path[index].split("/")[-1])
        #     time.sleep(10)

        ret = {
            'input': inp,
            'hm': hm,
            'lm': landmarks,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh,
            'reg': reg,
            'lm_ind': lm_ind,
            'lm_mask': lm_mask
        }

        if not self.split == 'train':
            gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
                   np.zeros((1, 4), dtype=np.float32)
            meta = {'gt_det': gt_det}
            ret['meta'] = meta
        return ret
Ejemplo n.º 5
0
 def run(self,
         on_detect,
         on_img,
         on_clear,
         on_people=None,
         always_show_img=False):
     img = sensor.snapshot()
     try:
         code = kpu.run_yolo2(self._m_fd, img)
     except Exception:
         return
     if code:
         for i in code:
             face_cut = img.cut(i.x(), i.y(), i.w(), i.h())
             face_cut_128 = face_cut.resize(128, 128)
             a = face_cut_128.pix_to_ai()
             #a = img.draw_image(face_cut_128, (0,0))
             # Landmark for face 5 points
             try:
                 fmap = kpu.forward(self._m_ld, face_cut_128)
             except Exception:
                 continue
             plist = fmap[:]
             le = (i.x() + int(plist[0] * i.w() - 10),
                   i.y() + int(plist[1] * i.h()))
             re = (i.x() + int(plist[2] * i.w()),
                   i.y() + int(plist[3] * i.h()))
             nose = (i.x() + int(plist[4] * i.w()),
                     i.y() + int(plist[5] * i.h()))
             lm = (i.x() + int(plist[6] * i.w()),
                   i.y() + int(plist[7] * i.h()))
             rm = (i.x() + int(plist[8] * i.w()),
                   i.y() + int(plist[9] * i.h()))
             a = img.draw_circle(le[0], le[1], 4)
             a = img.draw_circle(re[0], re[1], 4)
             a = img.draw_circle(nose[0], nose[1], 4)
             a = img.draw_circle(lm[0], lm[1], 4)
             a = img.draw_circle(rm[0], rm[1], 4)
             # align face to standard position
             src_point = [le, re, nose, lm, rm]
             T = image.get_affine_transform(src_point, self._dst_point)
             a = image.warp_affine_ai(img, self.img_face, T)
             a = self.img_face.ai_to_pix()
             #a = img.draw_image(img_face, (128,0))
             del (face_cut_128)
             # calculate face feature vector
             try:
                 fmap = kpu.forward(self._m_fe, self.img_face)
             except Exception:
                 continue
             feature = kpu.face_encode(fmap[:])
             scores = []
             for j in range(len(self.features)):
                 score = kpu.face_compare(self.features[j], feature)
                 scores.append(score)
             max_score = 0
             index = 0
             for k in range(len(scores)):
                 if max_score < scores[k]:
                     max_score = scores[k]
                     index = k
             if max_score > 85:
                 a = img.draw_rectangle(i.rect(), color=(0, 255, 0))
                 a = img.draw_string(
                     i.x(),
                     i.y(), ("%s :%2.1f" % (self.names[index], max_score)),
                     color=(0, 255, 0),
                     scale=2)
                 on_detect(self.names[index], feature, max_score, img)
             else:
                 a = img.draw_rectangle(i.rect(), color=(255, 0, 0))
                 # a = img.draw_string(i.x(),i.y(), ("X :%2.1f" % (max_score)), color=(255,0,0),scale=2)
                 on_img(img)
             if on_people:
                 on_people(feature, img)
             self._show_img_t = time.ticks_ms() / 1000.0
     else:
         if always_show_img:
             on_img(img)
         else:
             if time.ticks_ms(
             ) - self._show_img_t * 1000 < self.show_img_timeout * 1000:
                 on_img(img)
             else:
                 on_clear()
Ejemplo n.º 6
0
import image
import lcd, sensor
import time

lcd.init()
# lcd.init(type=2, freq=20000000)

sensor.reset(freq=24000000)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)


matrix = image.get_affine_transform([(0,0), (240, 0), (240, 240)], [(60,60), (240, 0), (220, 200)])
print("matrix:")
print("[{:.02f}, {:.02f}, {:.02f}]".format(matrix[0], matrix[1], matrix[2]))
print("[{:.02f}, {:.02f}, {:.02f}]".format(matrix[3], matrix[4], matrix[5]))
print("[{:.02f}, {:.02f}, {:.02f}]".format(matrix[6], matrix[7], matrix[8]))


try:
    del img
    del img2
except Exception:
    pass

img2 = image.Image(size=(160, 120))
img2.pix_to_ai()
flag = False
while 1:
    img = sensor.snapshot()
    def __getitem__(self, index):
        if os.path.exists(self.imgs_path[index]):
            img = cv2.imread(self.imgs_path[index])
        else:
            print("%s not exists" % self.imgs_path[index])
        anns = self.words[index]
        num_objs = min(len(anns), self.max_objs)
        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        s = max(img.shape[0], img.shape[1]) * 1.0
        input_h, input_w = self.default_resolution[0], self.default_resolution[
            1]

        flipped = False
        if self.split == 'train':
            s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
            w_border = self._get_border(128, img.shape[1])
            h_border = self._get_border(128, img.shape[0])
            c[0] = np.random.randint(low=w_border,
                                     high=img.shape[1] - w_border)
            c[1] = np.random.randint(low=h_border,
                                     high=img.shape[0] - h_border)

            if np.random.random() < 0.5:
                flipped = True
                img = img[:, ::-1, :]
                c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)
        inp = (inp.astype(np.float32) / 255.)
        # if self.split == 'train':
        color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)
        inp = (inp - self.mean) / self.std
        inp = inp.transpose(2, 0, 1)

        output_h = input_h // self.down_ratio
        output_w = input_w // self.down_ratio
        num_classes = 1
        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])

        hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        landmarks = np.zeros((self.max_objs, 10), dtype=np.float32)
        dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)

        draw_gaussian = draw_msra_gaussian if self.mse_loss else \
                                        draw_umich_gaussian

        cls_id = 0
        for k in range(num_objs):
            ann = anns[k]
            bbox = np.array(ann[:4].copy())
            x_o, y_o, w_o, h_o = ann[0], ann[1], ann[2], ann[3]

            bbox = self._coco_box_to_bbox(bbox)
            lm = []
            for i in range(5):
                if self.split == 'train' and ann[4] > 0:
                    x = (ann[4 + 3 * i] - x_o) / (w_o + 1e-14)
                    y = (ann[4 + 3 * i + 1] - y_o) / (h_o + 1e-14)
                    _lm = [x, y]
                else:
                    _lm = [0, 0]
                lm.append(_lm)
            lm = np.array(lm).reshape(1, -1)[0]
            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1

            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1)
            bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1)

            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]
            if h > 0 and w > 0:

                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                ct = np.array([(bbox[0] + bbox[2]) / 2,
                               (bbox[1] + bbox[3]) / 2],
                              dtype=np.float32)
                ct_int = ct.astype(np.int32)

                draw_umich_gaussian(hm[cls_id], ct_int, radius)
                wh[k] = 1. * w, 1. * h
                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1

                landmarks[k] = lm

        ret = {
            'input': inp,
            'hm': hm,
            'lm': landmarks,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh,
            'reg': reg
        }

        if not self.split == 'train':
            gt_det = np.zeros((self.max_objs, 4), dtype=np.float32)
            for k in range(num_objs):
                ann = anns[k]
                bbox = np.array(ann[:4].copy())
                bbox = self._coco_box_to_bbox(bbox)
                gt_det[k:4] = bbox

            gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
                   np.zeros((1, 4), dtype=np.float32)
            meta = {'gt_det': gt_det, 'h': height, 'w': width}
            ret['meta'] = meta
        return ret
Ejemplo n.º 8
0
    def _get_label(self, c, s, rot, width, flipped, anns):
        def _coco_box_to_bbox(box):
            bbox = np.array([box[0], box[1], box[0] + box[2], box[1] + box[3]],
                            dtype=np.float32)
            return bbox

        output_res = self.opt.output_res
        num_joints = self.num_joints
        trans_output_rot = get_affine_transform(c, s, rot, [output_res, output_res]) # affine transform  to 128x128 with rotation
        trans_output = get_affine_transform(c, s, 0, [output_res, output_res]) # affine transform to 128x128 without rotation

        hm = np.zeros((self.num_classes, output_res, output_res), dtype=np.float32)
        hm_hp = np.zeros((num_joints, output_res, output_res), dtype=np.float32)
        dense_kps = np.zeros((num_joints, 2, output_res, output_res),
                             dtype=np.float32)
        dense_kps_mask = np.zeros((num_joints, output_res, output_res),
                                  dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        kps = np.zeros((self.max_objs, num_joints * 2), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        kps_mask = np.zeros((self.max_objs, self.num_joints * 2), dtype=np.uint8)
        hp_offset = np.zeros((self.max_objs * num_joints, 2), dtype=np.float32)
        hp_ind = np.zeros((self.max_objs * num_joints), dtype=np.int64)
        hp_mask = np.zeros((self.max_objs * num_joints), dtype=np.int64)

        draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \
            draw_umich_gaussian

        gt_det = []
        num_objs = min(len(anns), self.max_objs) # max number of objects, default 32
        for k in range(num_objs):
            ann = anns[k]
            bbox = _coco_box_to_bbox(ann['bbox'])
            cls_id = int(ann['category_id']) - 1
            pts = np.array(ann['keypoints'], np.float32).reshape(num_joints, 3)
            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1
                pts[:, 0] = width - pts[:, 0] - 1
                for e in self.flip_idx:
                    pts[e[0]], pts[e[1]] = pts[e[1]].copy(), pts[e[0]].copy()
            ## affine transform bbox to feature map 128x128
            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            bbox = np.clip(bbox, 0, output_res - 1)
            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]
            if (h > 0 and w > 0) or (rot != 0):
                ## 3.1 handle pure bbox
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = self.opt.hm_gauss if self.opt.mse_loss else max(0, int(radius))
                ct = np.array(
                    [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2], dtype=np.float32) # center of bbox
                ct_int = ct.astype(np.int32)
                wh[k] = 1. * w, 1. * h  # width and height of bbox
                ind[k] = ct_int[1] * output_res + ct_int[0] # center of bbox in feature map index 0-16384
                reg[k] = ct - ct_int # decimal of center of bbox
                reg_mask[k] = 1 # center mask ???
                num_kpts = pts[:, 2].sum()
                if num_kpts == 0: # if no key points, hm=0.9999, reg_mask[k]=0
                    hm[cls_id, ct_int[1], ct_int[0]] = 0.9999
                    reg_mask[k] = 0

                ## 3.2
                hp_radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                hp_radius = self.opt.hm_gauss \
                    if self.opt.mse_loss else max(0, int(hp_radius))
                for j in range(num_joints):
                    if pts[j, 2] > 0: # key points is visible
                        pts[j, :2] = affine_transform(pts[j, :2], trans_output_rot)
                        if pts[j, 0] >= 0 and pts[j, 0] < output_res and \
                                pts[j, 1] >= 0 and pts[j, 1] < output_res: # key points in output feature map
                            kps[k, j * 2: j * 2 + 2] = pts[j, :2] - ct_int # vector of key points to cneter of bbox
                            kps_mask[k, j * 2: j * 2 + 2] = 1
                            pt_int = pts[j, :2].astype(np.int32)
                            hp_offset[k * num_joints + j] = pts[j, :2] - pt_int
                            hp_ind[k * num_joints + j] = pt_int[1] * output_res + pt_int[0]
                            hp_mask[k * num_joints + j] = 1
                            if self.opt.dense_hp:
                                # must be before draw center hm gaussian
                                draw_dense_reg(dense_kps[j], hm[cls_id], ct_int,
                                               pts[j, :2] - ct_int, radius, is_offset=True)
                                draw_gaussian(dense_kps_mask[j], ct_int, radius)
                            draw_gaussian(hm_hp[j], pt_int, hp_radius)
                draw_gaussian(hm[cls_id], ct_int, radius)
                gt_det.append([bbox[0], bbox[1], bbox[2], bbox[3], 1] + # [0:4] bbox,[4] 1, [5:39] key points, [40] class id 0
                              pts[:, :2].reshape(num_joints * 2).tolist() + [cls_id])

        if rot != 0:
            hm = hm * 0 + 0.9999
            reg_mask *= 0
            kps_mask *= 0

        return gt_det, hm, reg, reg_mask, ind, wh, kps, kps_mask, hm_hp, \
               hp_offset, hp_ind, hp_mask, dense_kps, dense_kps_mask
Ejemplo n.º 9
0
def transform_preds(coords, center, scale, output_size):
    target_coords = np.zeros(coords.shape)
    trans = get_affine_transform(center, scale, 0, output_size, inv=1)
    for p in range(coords.shape[0]):
        target_coords[p, 0:2] = affine_transform(coords[p, 0:2], trans)
    return target_coords
Ejemplo n.º 10
0
    def work(img):
        img.pix_to_ai()

        code = kpu.run_yolo2(FaceReco.task_fd, img)
        if code:
            #for i in code:
            start = 0
            for pos in range(len(code)):
                i = code[pos]
                #print(i)
                # Cut face and resize to 128x128
                a = img.draw_rectangle(i.x(), i.y(), i.w(), i.h())
                face_cut = img.cut(i.x(), i.y(), i.w(), i.h())
                face_cut_128 = face_cut.resize(128, 128)
                a = face_cut_128.pix_to_ai()

                #a = ui.canvas.draw_image(face_cut, (320,0))
                #print(i)

                # Landmark for face 5 points
                fmap = kpu.forward(FaceReco.task_ld, face_cut_128)
                plist = fmap[:]
                le = (int(i.x() + plist[0]*i.w() - 5),
                      int(i.y() + plist[1]*i.h()))
                re = (int(i.x() + plist[2]*i.w()), int(i.y() + plist[3]*i.h()))
                nose = (int(i.x() + plist[4]*i.w()),
                        int(i.y() + plist[5]*i.h()))
                lm = (int(i.x() + plist[6]*i.w()), int(i.y() + plist[7]*i.h()))
                rm = (int(i.x() + plist[8]*i.w()), int(i.y() + plist[9]*i.h()))
                #print(le, re, nose, lm, rm)
                a = img.draw_circle(int(le[0]), int(le[1]), 2)
                a = img.draw_circle(int(re[0]), int(re[1]), 2)
                a = img.draw_circle(int(nose[0]), int(nose[1]), 2)
                a = img.draw_circle(int(lm[0]), int(lm[1]), 2)
                a = img.draw_circle(int(rm[0]), int(rm[1]), 2)
                # align face to standard position
                src_point = [le, re, nose, lm, rm]
                T = image.get_affine_transform(src_point, FaceReco.dst_point)
                a = image.warp_affine_ai(img, FaceReco.img_face, T)
                a = FaceReco.img_face.ai_to_pix()
                #a = ui.canvas.draw_image(FaceReco.img_face, (320,128))

                if ui.height > 240:
                    gc.collect()
                    tmp = face_cut_128.resize(80, 80)
                    #a = ui.canvas.draw_image(face_cut_128, (start, 240))
                    ui.canvas.draw_image(
                        tmp, 320 + int((pos % 2)*80), int((pos // 2)*80))
                    del(tmp)
                    #start = start + 80
                del(face_cut_128)

                # calculate face feature vector
                fmap = kpu.forward(FaceReco.task_fe, FaceReco.img_face)
                feature = kpu.face_encode(fmap[:])
                reg_flag = False
                scores = []
                for j in range(len(FaceReco.record_ftrs)):
                   score = kpu.face_compare(FaceReco.record_ftrs[j], feature)
                   scores.append(score)
                max_score = 0
                index = 0
                for k in range(len(scores)):
                   if max_score < scores[k]:
                       max_score = scores[k]
                       index = k
                if max_score > 85:
                    a = img.draw_string(i.x(), i.y(), ("%s:%2.1f" % (
                        FaceReco.names[index], max_score)), color=(0, 255, 0), scale=2)
                    if ui.height > 240:
                        a = ui.canvas.draw_string(100 * (pos % 3), 240 + 30 * (pos // 3), ("%s :%2.1f" % (
                            FaceReco.names[index], max_score)), color=(0, 255, 0), scale=2)
                else:
                    a = img.draw_string(i.x(), i.y(), ("X:%2.1f" % (
                        max_score)), color=(255, 0, 0), scale=2)
                    if ui.height > 240:
                        a = ui.canvas.draw_string(100 * (pos % 3), 240 + 30 * (pos // 3), ("X :%2.1f" % (
                            max_score)), color=(255, 0, 0), scale=2)

                if FaceReco.start_processing:
                    FaceReco.record_ftr = feature
                    if len(FaceReco.record_ftrs) == len(FaceReco.names):
                        FaceReco.record_ftrs = []
                    FaceReco.record_ftrs.append(FaceReco.record_ftr)
                    FaceReco.start_processing = False
Ejemplo n.º 11
0
    def __getitem__(self, index):
        assert index <= len(self), 'index range error'
        imgpath = self.lines[index].rstrip()
        img, label = load_data_detection(imgpath, self.shape)
        testlabel = label.copy()
        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)

        s = max(img.shape[0], img.shape[1]) * 1.0
        input_h, input_w = self.shape[0], self.shape[1]

        flipped = False
        if self.train:
            if not self.not_rand_crop:
                s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                w_border = self._get_border(128, img.shape[1])
                h_border = self._get_border(128, img.shape[0])
                c[0] = np.random.randint(low=w_border,
                                         high=img.shape[1] - w_border)
                c[1] = np.random.randint(low=h_border,
                                         high=img.shape[0] - h_border)

            if np.random.random() < self.flip:
                flipped = True
                img = img[:, ::-1, :]
                c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)

        inp = (inp.astype(np.float32) / 255.)
        if self.train and not self.no_color_aug:
            color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)

        output_h = input_h // 4
        output_w = input_w // 4

        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])
        img = np.array(inp)
        img = ((img - self.mean) / self.std).astype(np.float32)

        img = img.transpose(2, 0, 1)
        img = img.astype(np.float32)

        hm = np.zeros(
            (self.num_classes, int(self.shape[0] / 4), int(self.shape[1] / 4)),
            dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)

        label = np.array(label)
        for k in range(label.shape[0]):

            cls_id = int(label[k, 0])
            bbox = label[k, 1:5]

            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1
            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1)
            bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1)
            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]

            if h > 0 and w > 0:
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                ct = np.array([(bbox[0] + bbox[2]) / 2,
                               (bbox[1] + bbox[3]) / 2],
                              dtype=np.float32)
                ct_int = ct.astype(np.int32)
                draw_umich_gaussian(hm[cls_id], ct_int, radius)
                wh[k] = 1. * w, 1. * h
                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1

        ret = {
            'hm': hm,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh,
            "reg": reg
        }
        if Debug:
            testlabel = np.array(testlabel)
            cv2.rectangle(test_img,
                          (int(testlabel[0, 1]), int(testlabel[0, 2])),
                          (int(testlabel[0, 3]), int(testlabel[0, 4])),
                          (255, 0, 0), 2)
            cv2.imshow("heatmap", hm[int(label[0, 0])])
            cv2.imshow("tests", test_img)

            print(label)
            cv2.waitKey(0)
        return (img, ret)
Ejemplo n.º 12
0
        if np.random.random() < opt_aug_ddd:
            # augmentation되는 상황.
            aug = True
            sf = 0.4
            cf = 0.1

            s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)
            # center 이동시키고
            # print(np.clip(np.random.randn()*cf, -2*cf, 2*cf))

            c[0] += image.shape[1] * np.clip(np.random.randn() * cf, -2 * cf,
                                             2 * cf)
            c[1] += image.shape[0] * np.clip(np.random.randn() * cf, -2 * cf,
                                             2 * cf)

        trans_input = get_affine_transform(c, s, 0, [opt_input_w, opt_input_h])
        dst_in = cv2.resize(image,
                            dsize=(640, 480),
                            interpolation=cv2.INTER_AREA)

        inp = cv2.warpAffine(
            image,
            trans_input,
            (opt_input_w, opt_input_h),
            # (width, height),
            flags=cv2.INTER_LINEAR)

        opt_down_ratio = 4
        opt_output_w = opt_input_w // opt_down_ratio
        opt_output_h = opt_input_h // opt_down_ratio
        trans_output = get_affine_transform(c, s, 0,
Ejemplo n.º 13
0
  def __getitem__(self, index): 
    img_id = self.images[index] # id
    img_path = os.path.join(self.img_dir, self.coco.loadImgs(ids=[img_id])[0]['file_name']) 
    ann_ids = self.coco.getAnnIds(imgIds=[img_id]) 
    annotations = self.coco.loadAnns(ids=ann_ids) # label
    labels = np.array([self.cat_ids[anno['category_id']] for anno in annotations]) 
    bboxes = np.array([anno['bbox'] for anno in annotations], dtype=np.float32) 
    if len(bboxes) == 0: 。
      bboxes = np.array([[0., 0., 0., 0.]], dtype=np.float32)
      labels = np.array([[0]])
    bboxes[:, 2:] += bboxes[:, :2]  # x1 y1 w h to x1 y1 x2 y2

    img = cv2.imread(img_path)
    height, width = img.shape[0], img.shape[1]
    center = np.array([width / 2., height / 2.], dtype=np.float32)  # center of image 
    scale = max(height, width) * 1.0

    flipped = False 

    trans_img = get_affine_transform(center, scale, 0, [self.img_size['w'], self.img_size['h']]) 
    img = cv2.warpAffine(img, trans_img, (self.img_size['w'], self.img_size['h'])) 

    img = img.astype(np.float32) / 255. # [0,1]

    img = img.transpose(2, 0, 1)  # from [H, W, C] to [C, H, W]

    # Ground Truth heatmap 
    trans_fmap = get_affine_transform(center, scale, 0, [self.fmap_size['w'], self.fmap_size['h']]) 

    # vectors
    hmap = np.zeros((self.num_classes, self.fmap_size['h'], self.fmap_size['w']), dtype=np.float32)  # heatmap,size(3,96,96)
    w_h_ = np.zeros((self.max_objs, 2), dtype=np.float32)  # width and height
    pxpy = np.zeros((self.max_objs, 2), dtype=np.float32) # length and theta
    regs = np.zeros((self.max_objs, 2), dtype=np.float32)  # regression

    # index
    inds = np.zeros((self.max_objs,), dtype=np.int64)
    ind_masks = np.zeros((self.max_objs,), dtype=np.uint8)

    # detections = []
    for k, (bbox, label) in enumerate(zip(bboxes, labels)): 
      #if flipped:
      #  bbox[[0, 2]] = width - bbox[[2, 0]] - 1
      bbox[:2] = affine_transform(bbox[:2], trans_fmap) 
      bbox[2:] = affine_transform(bbox[2:], trans_fmap)
      bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, self.fmap_size['w'] - 1) 
      bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, self.fmap_size['h'] - 1)
      h, w = bbox[3] - bbox[1], bbox[2] - bbox[0] # h and w

      d = math.sqrt((bbox[3]-bbox[1])*(bbox[3]-bbox[1])+(bbox[2]-bbox[0])*(bbox[2]-bbox[0]))/2
      theta = math.pi-math.atan(h/w)

      if h > 0 and w > 0:
        obj_c = np.array([(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2], dtype=np.float32) 
        obj_c_int = obj_c.astype(np.int32) 

        radius = max(0, int(gaussian_radius((math.ceil(h), math.ceil(w)), self.gaussian_iou))) # gaussian_radius
        draw_umich_gaussian(hmap[label], obj_c_int, radius) 
        w_h_[k] = 1. * w, 1. * h
        pxpy[k] = 1. * d, 1. * theta 
        regs[k] = obj_c - obj_c_int  # discretization error 
        inds[k] = obj_c_int[1] * self.fmap_size['w'] + obj_c_int[0] # = fmap_w * cy + cx 
        ind_masks[k] = 1 

    return {'image': img,
            'hmap': hmap, 'w_h_':w_h_,'pxpy': pxpy, 'regs': regs, 'inds': inds, 'ind_masks': ind_masks,
            'c': center, 's': scale, 'img_id': img_id}
Ejemplo n.º 14
0
    def __getitem__(self, index):
        img_id = self.images[index]
        file_name = self.coco.loadImgs(ids=[img_id])[0]['file_name']
        img_path = os.path.join(self.img_dir, file_name)
        ann_ids = self.coco.getAnnIds(imgIds=[img_id])
        anns = self.coco.loadAnns(ids=ann_ids)
        num_objs = min(len(anns), self.max_objs)

        img = cv2.imread(img_path)

        height, width = img.shape[0], img.shape[1]
        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
        if self.opt.keep_res:
            input_h = (height | self.opt.pad) + 1
            input_w = (width | self.opt.pad) + 1
            s = np.array([input_w, input_h], dtype=np.float32)
        else:
            s = max(img.shape[0], img.shape[1]) * 1.0
            input_h, input_w = self.opt.input_h, self.opt.input_w

        flipped = False
        if self.split == 'train':
            if not self.opt.not_rand_crop:
                s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                w_border = self._get_border(128, img.shape[1])
                h_border = self._get_border(128, img.shape[0])
                c[0] = np.random.randint(low=w_border,
                                         high=img.shape[1] - w_border)
                c[1] = np.random.randint(low=h_border,
                                         high=img.shape[0] - h_border)
            else:
                sf = self.opt.scale
                cf = self.opt.shift
                c[0] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                c[1] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)

            if np.random.random() < self.opt.flip:
                flipped = True
                img = img[:, ::-1, :]
                c[0] = width - c[0] - 1

        trans_input = get_affine_transform(c, s, 0, [input_w, input_h])
        inp = cv2.warpAffine(img,
                             trans_input, (input_w, input_h),
                             flags=cv2.INTER_LINEAR)
        inp = (inp.astype(np.float32) / 255.)
        if self.split == 'train' and not self.opt.no_color_aug:
            color_aug(self._data_rng, inp, self._eig_val, self._eig_vec)
        inp = (inp - self.mean) / self.std
        inp = inp.transpose(2, 0, 1)

        output_h = input_h // self.opt.down_ratio
        output_w = input_w // self.opt.down_ratio
        num_classes = self.num_classes
        trans_output = get_affine_transform(c, s, 0, [output_w, output_h])

        hm = np.zeros((num_classes, output_h, output_w), dtype=np.float32)
        wh = np.zeros((self.max_objs, 2), dtype=np.float32)
        dense_wh = np.zeros((2, output_h, output_w), dtype=np.float32)
        reg = np.zeros((self.max_objs, 2), dtype=np.float32)
        ind = np.zeros((self.max_objs), dtype=np.int64)
        reg_mask = np.zeros((self.max_objs), dtype=np.uint8)
        cat_spec_wh = np.zeros((self.max_objs, num_classes * 2),
                               dtype=np.float32)
        cat_spec_mask = np.zeros((self.max_objs, num_classes * 2),
                                 dtype=np.uint8)

        draw_gaussian = draw_msra_gaussian if self.opt.mse_loss else \
                        draw_umich_gaussian

        gt_det = []
        for k in range(num_objs):
            ann = anns[k]
            bbox = self._coco_box_to_bbox(ann['bbox'])
            cls_id = int(self.cat_ids[ann['category_id']])
            if flipped:
                bbox[[0, 2]] = width - bbox[[2, 0]] - 1
            bbox[:2] = affine_transform(bbox[:2], trans_output)
            bbox[2:] = affine_transform(bbox[2:], trans_output)
            bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_w - 1)
            bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_h - 1)
            h, w = bbox[3] - bbox[1], bbox[2] - bbox[0]
            if h > 0 and w > 0:
                radius = gaussian_radius((math.ceil(h), math.ceil(w)))
                radius = max(0, int(radius))
                radius = self.opt.hm_gauss if self.opt.mse_loss else radius
                ct = np.array([(bbox[0] + bbox[2]) / 2,
                               (bbox[1] + bbox[3]) / 2],
                              dtype=np.float32)
                ct_int = ct.astype(np.int32)
                draw_gaussian(hm[cls_id], ct_int, radius)
                wh[k] = 1. * w, 1. * h
                ind[k] = ct_int[1] * output_w + ct_int[0]
                reg[k] = ct - ct_int
                reg_mask[k] = 1
                cat_spec_wh[k, cls_id * 2:cls_id * 2 + 2] = wh[k]
                cat_spec_mask[k, cls_id * 2:cls_id * 2 + 2] = 1
                if self.opt.dense_wh:
                    draw_dense_reg(dense_wh, hm.max(axis=0), ct_int, wh[k],
                                   radius)
                gt_det.append([
                    ct[0] - w / 2, ct[1] - h / 2, ct[0] + w / 2, ct[1] + h / 2,
                    1, cls_id
                ])

        ret = {
            'input': inp,
            'hm': hm,
            'reg_mask': reg_mask,
            'ind': ind,
            'wh': wh
        }
        if self.opt.dense_wh:
            hm_a = hm.max(axis=0, keepdims=True)
            dense_wh_mask = np.concatenate([hm_a, hm_a], axis=0)
            ret.update({'dense_wh': dense_wh, 'dense_wh_mask': dense_wh_mask})
            del ret['wh']
        elif self.opt.cat_spec_wh:
            ret.update({
                'cat_spec_wh': cat_spec_wh,
                'cat_spec_mask': cat_spec_mask
            })
            del ret['wh']
        if self.opt.reg_offset:
            ret.update({'reg': reg})
        if self.opt.debug > 0 or not self.split == 'train':
            gt_det = np.array(gt_det, dtype=np.float32) if len(gt_det) > 0 else \
                     np.zeros((1, 6), dtype=np.float32)
            meta = {'c': c, 's': s, 'gt_det': gt_det, 'img_id': img_id}
            ret['meta'] = meta
        return ret
Ejemplo n.º 15
0
def generic_post_process(dets,
                         c,
                         s,
                         h,
                         w,
                         num_classes,
                         calibs=None,
                         height=-1,
                         width=-1):
    if not ('scores' in dets):
        return [{}], [{}]
    ret = []

    for i in range(len(dets['scores'])):
        preds = []
        trans = get_affine_transform(c[i], s[i], 0, (w, h),
                                     inv=1).astype(np.float32)
        for j in range(len(dets['scores'][i])):
            if dets['scores'][i][j] < 0.3:  # opt.out_thresh:
                break

            item = {}

            item['score'] = dets['scores'][i][j]
            item['class'] = int(dets['clses'][i][j]) + 1
            item['ct'] = transform_preds_with_trans(
                (dets['cts'][i][j]).reshape(1, 2), trans).reshape(2)

            if 'tracking' in dets:
                tracking = transform_preds_with_trans(
                    (dets['tracking'][i][j] + dets['cts'][i][j]).reshape(1, 2),
                    trans).reshape(2)
                item['tracking'] = tracking - item['ct']

            if 'bboxes' in dets:
                bbox = transform_preds_with_trans(
                    dets['bboxes'][i][j].reshape(2, 2), trans).reshape(4)
                item['bbox'] = bbox

            if 'hps' in dets:
                pts = transform_preds_with_trans(
                    dets['hps'][i][j].reshape(-1, 2), trans).reshape(-1)
                item['hps'] = pts

            if 'dep' in dets and len(dets['dep'][i]) > j:
                item['dep'] = dets['dep'][i][j]

            if 'dim' in dets and len(dets['dim'][i]) > j:
                item['dim'] = dets['dim'][i][j]

            if 'rot' in dets and len(dets['rot'][i]) > j:
                item['alpha'] = get_alpha(dets['rot'][i][j:j + 1])[0]

            if 'rot' in dets and 'dep' in dets and 'dim' in dets \
                    and len(dets['dep'][i]) > j:
                if 'amodel_offset' in dets and len(
                        dets['amodel_offset'][i]) > j:
                    ct_output = dets['bboxes'][i][j].reshape(2, 2).mean(axis=0)
                    amodel_ct_output = ct_output + dets['amodel_offset'][i][j]
                    ct = transform_preds_with_trans(
                        amodel_ct_output.reshape(1, 2),
                        trans).reshape(2).tolist()
                else:
                    bbox = item['bbox']
                    ct = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]
                item['ct'] = ct
                item['loc'], item['rot_y'] = ddd2locrot(
                    ct, item['alpha'], item['dim'], item['dep'], calibs[i])

            preds.append(item)

        ret.append(preds)

    return ret
Ejemplo n.º 16
0
 plist = fmap[:]
 le = (i.x() + int(plist[0] * i.w() - 10),
       i.y() + int(plist[1] * i.h()))
 re = (i.x() + int(plist[2] * i.w()), i.y() + int(plist[3] * i.h()))
 nose = (i.x() + int(plist[4] * i.w()),
         i.y() + int(plist[5] * i.h()))
 lm = (i.x() + int(plist[6] * i.w()), i.y() + int(plist[7] * i.h()))
 rm = (i.x() + int(plist[8] * i.w()), i.y() + int(plist[9] * i.h()))
 a = img.draw_circle(le[0], le[1], 4)
 a = img.draw_circle(re[0], re[1], 4)
 a = img.draw_circle(nose[0], nose[1], 4)
 a = img.draw_circle(lm[0], lm[1], 4)
 a = img.draw_circle(rm[0], rm[1], 4)
 # align face to standard position
 src_point = [le, re, nose, lm, rm]
 T = image.get_affine_transform(src_point, dst_point)
 a = image.warp_affine_ai(img, img_face, T)
 a = img_face.ai_to_pix()
 # a = img.draw_image(img_face, (128,0))
 del (face_cut_128)
 # calculate face feature vector
 fmap = kpu.forward(task_fe, img_face)
 feature = kpu.face_encode(fmap[:])
 reg_flag = False
 scores = []
 for j in range(len(record_ftrs)):
     score = kpu.face_compare(record_ftrs[j], feature)
     scores.append(score)
 max_score = 0
 index = 0
 for k in range(len(scores)):
Ejemplo n.º 17
0
 re = (i.x() + int(plist[2] * i.w()), i.y() + int(plist[3] * i.h())
       )  # 计算右眼位置
 nose = (i.x() + int(plist[4] * i.w()),
         i.y() + int(plist[5] * i.h()))  #计算鼻子位置
 lm = (i.x() + int(plist[6] * i.w()), i.y() + int(plist[7] * i.h())
       )  #计算左嘴角位置
 rm = (i.x() + int(plist[8] * i.w()), i.y() + int(plist[9] * i.h())
       )  #右嘴角位置
 a = img.draw_circle(le[0], le[1], 4)
 a = img.draw_circle(re[0], re[1], 4)
 a = img.draw_circle(nose[0], nose[1], 4)
 a = img.draw_circle(lm[0], lm[1], 4)
 a = img.draw_circle(rm[0], rm[1], 4)  # 在相应位置处画小圆圈
 # align face to standard position
 src_point = [le, re, nose, lm, rm]  # 图片中 5 坐标的位置
 T = image.get_affine_transform(
     src_point, dst_point)  # 根据获得的5点坐标与标准正脸坐标获取仿射变换矩阵
 a = image.warp_affine_ai(img, img_face,
                          T)  #对原始图片人脸图片进行仿射变换,变换为正脸图像
 a = img_face.ai_to_pix()  # 将正脸图像转为kpu格式
 #a = img.draw_image(img_face, (128,0))
 del (face_cut_128)  # 释放裁剪人脸部分图片
 # calculate face feature vector
 fmap = kpu.forward(task_fe, img_face)  # 计算正脸图片的196维特征值
 feature = kpu.face_encode(fmap[:])  #获取计算结果
 reg_flag = False
 scores = []  # 存储特征比对分数
 for j in range(len(record_ftrs)):  #迭代已存特征值
     score = kpu.face_compare(record_ftrs[j],
                              feature)  #计算当前人脸特征值与已存特征值的分数
     scores.append(score)  #添加分数总表
 max_score = 0
Ejemplo n.º 18
0
    def _get_input(self, img):
        def _get_border(border, size):
            """
            :param border: 0 <= border <= 128, size > 2*border, then we can get a rander center in central area
            :param size: image height and width
            :return: min border
            """
            i = 1
            while size - border // i <= border // i:
                i *= 2
            return border // i

        opt = self.opt
        split = self.split
        _data_rng = self._data_rng
        _eig_val = self._eig_val
        _eig_vec = self._eig_vec
        mean = self.mean
        std = self.std

        c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)  # image center
        s = max(img.shape[1], img.shape[0]) * 1.0  # max img length
        rot = 0
        flipped = False
        if split == 'train':
            if not opt.not_rand_crop:  # random crop, get center and scale
                s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                # to make sure that we can get a random cneter point, namely img.shape > 2*border
                w_border = _get_border(128, img.shape[1])
                h_border = _get_border(128, img.shape[0])
                c[0] = np.random.randint(low=w_border, high=img.shape[1] - w_border)
                c[1] = np.random.randint(low=h_border, high=img.shape[0] - h_border)
            else:
                sf = opt.scale
                cf = opt.shift
                c[0] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                c[1] += s * np.clip(np.random.randn() * cf, -2 * cf, 2 * cf)
                s = s * np.clip(np.random.randn() * sf + 1, 1 - sf, 1 + sf)
            if np.random.random() < opt.aug_rot:  # whether or not to rotate
                rf = opt.rotate
                rot = np.clip(np.random.randn() * rf, -rf * 2, rf * 2)

            if np.random.random() < opt.flip:  # flip
                flipped = True
                img = img[:, ::-1, :]
                c[0] = img.shape[1] - c[0] - 1

            # use affine transform to scale, rotate and crop image
        trans_input = get_affine_transform(
            c, s, rot, [opt.input_res, opt.input_res])
        inp = cv2.warpAffine(img, trans_input,
                             (opt.input_res, self.opt.input_res),
                             flags=cv2.INTER_LINEAR)

        # normalize, color augment and standardize image
        inp = (inp.astype(np.float32) / 255.)  # normalize
        if not opt.no_color_aug:  # color augment
            color_aug(_data_rng, inp, _eig_val, _eig_vec)
        inp = (inp - mean) / std  # standardize
        inp = inp.transpose(2, 0, 1)  # change channel (3, 512, 512)

        return inp, c, s, rot, flipped