def get_all_visitor_photos(): ''' Get photos of all visitors ''' logger.info("Trying to fetch all visitor images") cnx = get_connection() try: 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() resp = [] for row in res: resp.append(row) cursor.close() cnx.close() except Exception as ex: logger.exception(str(ex)) raise Exception("Some thing went wrong") return resp
def get_active_visitor_photos(): resp = sql_utils.get_all_visitor_photos() names = [r[0] for r in resp] logger.info("#of active visitors found : %d", len(names)) logger.info("active visitors found : %s", " | ".join(names)) # visitor_faces = np.vstack([image_utils.read_b64( # v[1]).reshape((1, 224, 224, 3)) for v in resp]) # logger.info("visitor faces shape %s", str(visitor_faces.shape)) # visitor_vectors = FM.vectorize(visitor_faces) # logger.info("Visitor Vectors.shape : %s", str(visitor_vectors.shape)) images = [] t0 = time() for _, img_b64 in resp: 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]
def get_connection(): logger.info("Trying to connect to MySQL database") cnx = None try: uid = os.environ.get("SQL_SERVER_USER_ID") pwd = os.environ.get("SQL_SERVER_USER_PWD") # print(uid, pwd) cnx = mysql.connector.connect(host=config.SQL_SERVER_IP, port=config.SQL_SERVER_PORT, user=uid, password=pwd, database=config.DB_NAME) except Exception: logger.exception("SQL Connection failed") exit(1) return cnx
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(["Visitor Management System"]) main_window = QtWidgets.QMainWindow() main_window.setWindowTitle("Visitor Management System") main_widget = MainWidget(haar_cascade_filepath) main_window.setCentralWidget(main_widget) main_window.show() sys.exit(app.exec_())
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
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))
def image_data_slot(self, image_data): if self.cnt == MAX: self.vm.update() faces = self.detect_faces(image_data) if len(faces) > 0: cropped_faces = [ image_data[y:y + h, x:x + w] for x, y, w, h in faces ] res = self.vm.find(cropped_faces) for (x, y, w, h), name in zip(faces, res): color = self._red if name == "" else self._green cv2.rectangle(image_data, (x, y), (x + w, y + h), color, self._width) # Write the person's name # Write some Text cv2.putText( img=image_data, text=name, org=(x, y - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, # fontFace=cv2.FONT_HERSHEY_PLAIN, fontScale=0.7, color=color, thickness=self._width) self.cnt = self.cnt - 1 if self.cnt > 0: self.image = self.get_qimage(image_data) if self.image.size() != self.size(): self.setFixedSize(self.image.size()) self.update() else: logger.info("Updating Vistor faces from database") empty_image = np.zeros_like(image_data).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) self.image = self.get_qimage(empty_image) if self.image.size() != self.size(): self.setFixedSize(self.image.size()) self.update() self.cnt = MAX
def update_faces(self, faces_list): logger.info("updating faces")
def update(self): names, face_vectors = visitor_utils.get_active_visitor_photos() self.names = names self.vectors = face_vectors logger.info("Visitor Model initialized")