Esempio n. 1
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)
Esempio n. 2
0
 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
 index = 0
 for k in range(len(scores)):  #迭代所有比对分数,找到最大分数和索引值
Esempio n. 3
0
 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)):
     if max_score < scores[k]:
Esempio n. 4
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()
Esempio n. 5
0
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()

    image.warp_affine_ai(img, img2, matrix)

    img2.ai_to_pix()
    if flag:
        lcd.display(img2)
    else:
        lcd.display(img)
    flag = not flag
    time.sleep_ms(500)

Esempio n. 6
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