Beispiel #1
0
def get_all_visitor_photos():
    '''
        Get photos of all visitors
    '''

    logger.info("Trying to fetch all visitor images")

    resp = []

    try:
        cnx = get_connection()
        if cnx:
            cursor = cnx.cursor()
            query = '''
                select v.name, i.image_data
                from visitor v,
                    images i
                where v.status = 1 
                and v.actual_photo is not null
                and  v.actual_photo = i.image_id;
            '''
            cursor.execute(query)
            res = cursor.fetchall()
            for row in res:
                resp.append(row)
            cursor.close()
            cnx.close()
    except Exception:
        logger.exception("Could not get visitor photos")

    return resp
Beispiel #2
0
def get_all_poi_photos():
    '''
        Get photos of all persons of interest
    '''

    logger.info("Trying to fetch all poi images")

    resp = []

    try:
        cnx = get_connection()
        if cnx:
            cursor = cnx.cursor()
            query = '''
                select p.type, p.name, i.image_data
                from person_of_interest p,
                    images i
                where p.image_id is not null
                and  p.image_id = i.image_id;
            '''
            cursor.execute(query)
            res = cursor.fetchall()

            for row in res:
                resp.append(row)
            cursor.close()
            cnx.close()
    except Exception:
        logger.exception("Could not get PoI photos")

    return resp
Beispiel #3
0
 def run(self):
     logger.info("Starting Camera thread")
     cnt = 0
     while (True):
         read, data = self.camera.read()
         updated_image = self.VM.process(data)
         signals.camera_signal.emit(updated_image)
         # if cnt % 7 == 0:
         #     signals.detect_faces.emit(data)
         cnt += 1
         logger.info("Frame : %d", cnt)
Beispiel #4
0
def get_connection():
    logger.info("Trying to connect to MySQL database")

    cnx = None

    try:
        cnx = mysql.connector.connect(host=config.SQL_SERVER_IP,
                                      port=config.SQL_SERVER_PORT,
                                      user=config.SQL_SERVER_USER_ID,
                                      password=config.SQL_SERVER_USER_PWD,
                                      database=config.DB_NAME,
                                      ssl_disabled=True
                                      )
    except Exception:
        logger.exception("SQL Connection failed")

    return cnx
Beispiel #5
0
 def schedule_update(self, frame):
     logger.info("Scheduling updatation of faces from central database")
     empty_image = np.zeros_like(frame).astype(np.uint8)
     # logger.info(str(empty_image.shape))
     cv2.putText(img=empty_image,
                 text="Wait ......",
                 org=(10, 100),
                 fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                 fontScale=1,
                 color=self._white,
                 thickness=self._width)
     cv2.putText(img=empty_image,
                 text="Refreshing images from Database",
                 org=(10, 150),
                 fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                 fontScale=1,
                 color=self._white,
                 thickness=self._width)
     return empty_image
Beispiel #6
0
    def get_face_from_image(self, img):
        '''
            Find , crop and resize the face in the given image.

            Raises exception if no or more than 1 face found in the image
        '''

        faces = []
        for model in self.face_cascade_models:
            faces.extend(list(model.detectMultiScale(img)))
        logger.info("# of faces found is %d", len(faces))

        face = image_utils.merge_faces(faces)
        x, y, w, h = face
        cropped_face = img[y:y + h, x:x + w]
        cropped_face = cv2.cvtColor(cropped_face, cv2.COLOR_BGR2RGB)
        cropped_face = cv2.resize(cropped_face, (224, 224))

        return cropped_face
Beispiel #7
0
def main():
    '''
        The main method
    '''

    logger.info("Starting desktop app !")

    haar_cascade_filepath = os.path.join(
        config.DATA_DIR, config.FACE_CASCADE_MODELS[1])

    # app = QtWidgets.QApplication(sys.argv)
    app = QtWidgets.QApplication([name])

    main_window = QtWidgets.QMainWindow()
    main_window.setWindowTitle(name)
    main_widget = MainWidget(haar_cascade_filepath)
    main_window.setCentralWidget(main_widget)
    main_window.show()
    sys.exit(app.exec_())
Beispiel #8
0
    def __init__(self):
        ''' Initialize '''
        logger.info("Initializing model")
        vggface = VGGFace(model='resnet50')
        out = vggface.get_layer("flatten_1").output
        self.model = Model(vggface.input, out)
        logger.info("vggface model initialized")
        # print(self.model.summary())

        self.face_cascade_models = []
        for fname in config.FACE_CASCADE_MODELS:
            cv2_model_file = os.path.join(config.DATA_DIR, fname)
            if not os.path.exists(cv2_model_file):
                raise Exception("CV2 model file %s does not exist",
                                cv2_model_file)
            self.face_cascade_models.append(
                cv2.CascadeClassifier(cv2_model_file))

        logger.info("OpenCV cascade models initialized. # of models : %d",
                    len(self.face_cascade_models))
Beispiel #9
0
 def update(self):
     names, face_vectors = download_utils.get_all_photos()
     self.names = names
     self.vectors = face_vectors
     logger.info("Photos refreshed")
Beispiel #10
0
def download_from_db():
    '''
    Download photos from central database

    Returns:
        [names, vectors] -- [Names and vectors of their faces]
    '''

    logger.info("Trying to fetch all photos from central database")
    resp1 = sql_utils.get_all_visitor_photos()
    resp2 = sql_utils.get_all_poi_photos()

    names = ["Unknown"] + ["Visitor-"+r[0]
                           for r in resp1] + [r[0]+"-"+r[1] for r in resp2]
    logger.info("#of  active visitors found : %d", len(names))
    logger.info("active visitors found : %s", " | ".join(names))

    images = [np.zeros((1, 224, 224, 3), dtype=np.uint8)]
    t0 = time()
    for _, img_b64 in resp1:
        img = image_utils.read_b64(img_b64)
        face = FM.get_face_from_image(img)
        images.append(face.reshape((1, 224, 224, 3)))

    for _, _, img_b64 in resp2:
        img = image_utils.read_b64(img_b64)
        face = FM.get_face_from_image(img)
        images.append(face.reshape((1, 224, 224, 3)))

    all_faces = np.vstack(images)
    logger.info("Shape of all faces : %s", str(all_faces.shape))
    visitor_vectors = FM.vectorize(all_faces)
    logger.info("Shape of vecs : %s", str(visitor_vectors.shape))
    logger.info(
        "Vectorization done. Time taken {:.3f} seconds".format(time()-t0))

    return [names, visitor_vectors]