def addList(self, record_list):
        if type(record_list) is not list:
            self._log.error("record_list is not a list type, do nothing.")
            return

        try:
            db = sqlite3.connect(self._db_file)
            cur = db.cursor()
        except sqlite3.Error as e:
            self._log.error(str(e))
            raise exceptions.LibError(str(e))

        sql_add_list = []
        for record in record_list:
            rep_str = ",".join(str(x) for x in record.eigen)
            info = ('1', record.hash, record.name, rep_str, record.src_hash,
                    record.face_img, record.class_id)
            self._log.debug("add: " + str(info))
            sql_add_list.append(info)

        try:
            cur.executemany(_SQL_ADD_FACE, sql_add_list)
        except sqlite3.Error as e:
            self._log.error(str(e))

        db.commit()
        db.close()
Ejemplo n.º 2
0
    def eigenValue(self, image):
        if isinstance(image, basestring):
            pil_img = Image.open(image)
            np_img = np.asarray(pil_img)
            self._log.debug("PIL image: {}".format(str(pil_img)))
        elif isinstance(image, Image.Image):
            pil_img = image
            np_img = np.asarray(image)
        elif isinstance(image, np.ndarray):
            pil_img = Image.fromarray(image)
            np_img = image
        else:
            raise exceptions.LibError("Unknow know img type")

        rep = openfaceutils.neural_net.forward(np_img)
        # self._log.info("eigen: \n{}".format(rep))

        # rep_str = ",".join(
        #                 str(evalue)
        #                 for evalue in rep.tolist())

        phash = str(imagehash.phash(pil_img))

        # record = faceapi.FaceInfo(
        #                     phash, name, rep_str, "./test.png", class_id)
        #
        # self._face_db.addList([record])

        return (phash, rep.tolist())
Ejemplo n.º 3
0
    def predict(self, image):
        if self._svm is None:
            self._log.warn("self._svm is None")
            return None
        elif len(self._db_dict) == 0:
            self._log.warn("self._db_dict == 0")
            return None

        if isinstance(image, basestring):
            pil_img = Image.open(image)
            np_img = np.asarray(pil_img)
            #self._log.debug("PIL image: {}".format(str(pil_img)))
        elif isinstance(image, Image.Image):
            pil_img = image
            np_img = np.asarray(image)
        elif isinstance(image, np.ndarray):
            np_img = image
            pil_img = Image.fromarray(image)
        else:
            raise exceptions.LibError("Unknow image type")

        resultRecord = None
        phash = str(imagehash.phash(pil_img))
        if phash in self._db_dict:
            hit = self._db_dict[phash]
        else:
            rep = openfaceutils.neural_net.forward(np_img)
            pred = self._svm.predict(rep)
            #self._log.info("pred {}".format(pred))
            #self._log.info("df {}".format(self._svm.decision_function(rep)))
            class_id = pred[0]
            if _USE_PREDICTIVE:
                ##self._log.info("prob {}".format(self._svm.predict_proba(rep)))

                probability = max(self._svm.predict_proba(rep)[0])
                self._log.info("prob {} {}".format(
                    probability, np.argmax(self._svm.predict_proba(rep))))
                if probability <= _PROB_THRESH:
                    self._log.info("prob too low")
                    class_id = -1
            db_list = self._face_db.search('class_id', class_id, 1)
            #self._log.debug("result({}): {}".format(len(db_list), db_list))
            try:
                hit = db_list[0]
            except IndexError:
                hit = {
                    'name': 'Unknown',
                    'eigen': '',
                    'img_path': '',
                    'class_id': class_id
                }

        resultRecord = faceapi.FaceInfo(phash, hit['name'], hit['eigen'],
                                        hit['img_path'], hit['class_id'])

        return resultRecord
Ejemplo n.º 4
0
    def predict(self, image):
        if self._svm is None:
            self._log.warn("self._svm is None")
            return None
        elif len(self._db_dict) == 0:
            self._log.warn("self._db_dict == 0")
            return None

        if isinstance(image, basestring):
            pil_img = Image.open(image)
            np_img = np.asarray(pil_img)
            self._log.debug("PIL image: {}".format(str(pil_img)))
        elif isinstance(image, Image.Image):
            pil_img = image
            np_img = np.asarray(image)
        elif isinstance(image, np.ndarray):
            np_img = image
            pil_img = Image.fromarray(image)
        else:
            raise exceptions.LibError("Unknow image type")

        resultRecord = None
        phash = str(imagehash.phash(pil_img))
        if phash in self._db_dict:
            hit = self._db_dict[phash]
            hit_p = 0.95
            self._log.debug('match with hash: {}'.format(hit['name']))
        else:
            rep = openfaceutils.neural_net.forward(np_img)
            # predict_ret = self._svm.predict(rep)
            predict_p = self._svm.predict_proba(rep)[0]
            hit_p = max(predict_p)
            # if hit_p < (2.0/len(predict_p)):
            #     # probability of hit < average * 2
            #     return None
            # class_id = predict_p.index(hit_p)
            class_id = np.argmax(predict_p)
            self._log.info("svm({}, {}):\n{}".format(
                class_id, predict_p.max(), predict_p))
            db_list = self._face_db.search('class_id', class_id, 1)
            self._log.debug("result({}): {}".format(len(db_list), db_list))
            hit = db_list[0]

        resultRecord = faceapi.FaceInfo(
            phash,
            hit['name'],
            hit['eigen'],
            'src_img_no_need',
            hit['face_img'],
            hit['class_id'])
        resultRecord.scroe = hit_p
        self._face_db.Mark_Attendance(hit['name'])
        return resultRecord
Ejemplo n.º 5
0
    def detect(self, image, detect_multiple=_DETECT_ALL_FACES):
        if isinstance(image, basestring):
            img = Image.open(image)
            self._logger.debug("PIL image: {}".format(str(img)))
        elif isinstance(image, Image.Image):
            img = image

        else:
            raise exceptions.LibError("Unknown image type")

        img = _resize(img)
        buf = np.fliplr(np.asarray(img))
        im_width, im_height = img.size
        rgbFrame = np.zeros(
            # (_DEFAULT_IMG_H, _DEFAULT_IMG_W, 3),
            (im_height, im_width, 3),
            dtype=np.uint8)
        rgbFrame[:, :, 0] = buf[:, :, 0]
        rgbFrame[:, :, 1] = buf[:, :, 1]
        rgbFrame[:, :, 2] = buf[:, :, 2]

        if detect_multiple:
            face_boxes = openfaceutils.align.getAllFaceBoundingBoxes(rgbFrame)
            face_list = face_boxes
        else:
            face_box = openfaceutils.align.getLargestFaceBoundingBox(rgbFrame)
            face_list = [face_box] if face_box is not None else []

        detected_list = []
        for face_box in face_list:
            landmarks = openfaceutils.align.findLandmarks(rgbFrame, face_box)

            alignedFace = openfaceutils.align.align(
                openfaceutils.args.imgDim,
                rgbFrame,
                face_box,
                landmarks=landmarks,
                landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
            if alignedFace is None:
                continue

            face = FaceDetected()
            face.img = alignedFace
            face.area = face_box
            face.landmarks = landmarks

            detected_list.append(face)

        return detected_list