Esempio n. 1
0
def get_features_from_file(file_path):
    """Get features for faces in the image by path

    Accepts
    -------
    file_path: Str/Path-like object

    Returns
    -------
    256-dim vector or None

    Raises
    ------
    AssertionError: if file does not exist or is a directory
    AttributeError: if file is corrupted or is not a jpeg image
    """
    log.debug('checking if file "{}" exists'.format(file_path))
    assert (os.path.isfile(file_path))
    log.debug('reading file "{}"'.format(file_path))
    frame_binary = cv2.imread(file_path)
    if frame_binary is None:
        raise WrongFileTypeException(
            "Файл повреждён или не является изображением")
    faces = infer.infer_frame(frame_binary)
    if not faces:
        raise NoFacesException("Не найдено ни одного лица")
    if len(faces) > 1:
        raise TooManyFacesException("Более одного лица")
    return faces[0]["features"]
Esempio n. 2
0
 def delete(self):
     transaction_id = str(uuid())
     pk = int(request.args["id"])
     label_id = int(request.args["label_id"])
     filename = request.args["filename"]
     bus.push("file_deleted",
              transaction_id=transaction_id,
              id=pk,
              filename=filename,
              label_id=label_id)
     start = time()
     while True:
         if time() - start >= 20:
             log.debug(
                 "Transaction closed by timeout, photo id={} not deleted".
                 format(pk))
             return {"success": False, "reason": "Timeout"}, 400
         raw_msg = bus.client.lpop(transaction_id)
         if raw_msg:
             msg = bus._loads(raw_msg)[1]
             bus.client.delete(transaction_id)
             log.debug("new message: {}".format(msg))
             if msg["deleted"]:
                 return msg
             else:
                 return msg, 400
Esempio n. 3
0
 def receive_batch(self, ts, batch_data):
     start = time.time()
     log.debug("infer Received processing batch data")
     all_persons = 0
     infer_time = []
     for i in range(len(batch_data)):
         camera_id = i
         frame = batch_data[i]['frame']
         filename = batch_data[i]['file_name']
         frame_number = batch_data[i]['index_number']
         faces_rect = None
         pedestrian_rect = None
         faces_features = None
         nearest = None
         if batch_data[i]['ret']:
             start_infer = time.time()
             initial, faces_rect, faces_features = frame_handler.infer_frame(frame)
             initial_pedestrian, pedestrian_rect, *_ = pedestrian_frame_handler.infer_frame(frame) # NEW
             end_infer = time.time()
             infer_time.append(end_infer - start_infer)
         if faces_rect:
             init_height, init_width = initial
             now = datetime.now()
             all_persons += len(faces_rect)
             if (now - ts) < self.alert_actual:
                 #log.info('set: {} path: {}'.format(features_set.p.get_ids_list(), features_set.output_path))
                 nearest = features_set.find_nearest(faces_features)
             for j in range(len(faces_rect)):
                 l, t, r, b = faces_rect[j]
                 if nearest is not None:
                     label = nearest[0][j]
                     distance = nearest[1][j]
                     self.alert_send(now, ts, camera_id, label, distance, l, t, r, b,
                                     filename, frame_number, frame)
         # NEW
         if pedestrian_rect:
             init_ped_w, init_ped_h = initial_pedestrian
             for j in range(len(pedestrian_rect)):
                 lp, tp, rp, bp = pedestrian_rect[j]
                 self.storage_save(camera_id, ts, lp, tp, rp, bp, filename, frame_number,
                                   init_ped_h, init_ped_w)
         continue
     end = time.time()
     self.periods.append(end - start)
     if len(self.periods) > 1000:
         avg_period = sum(self.periods) / len(self.periods)
         self.periods = []
         log.info("infer Output persons : {}, ts: {}".format(all_persons, ts))
         log.info('infer cycle total FPS = {}, infer FPS = {}'.format(1/avg_period, 1/sum(infer_time)))
Esempio n. 4
0
 def __init__(self, inertia_time=30, actual_time=120, query_pack_size=100):
     log.debug("infer Prepare MySQL environment")
     executor.execute_query(query, 'create_mvideo_db')
     executor.execute_query(query, 'use_mvideo_db')
     executor.execute_query(query, 'create_frames_table', commit=True)
     log.debug("infer MySQL environment prepared")
     self.black_threshold = config.find('identify_threshold')
     self.periods = []
     self.query_pack = []
     self.prev_alerts = {}
     self.query_pack_size = query_pack_size
     self.alert_inertia = timedelta(seconds=inertia_time)
     self.alert_actual = timedelta(seconds=actual_time)
     bus.subscribe('/pack_batches', self.unpack_pack)
     ram_bus.subscribe('/new_frames', self.receive_batch)
     bus.listen()
Esempio n. 5
0
 def __init__(self, password=None):
     if password is None:
         self.password = config.find('telebot_password')
     self.subscriber_bot = config.find('telebot_subscriber')
     self.temp_states = {}
     executor.execute_query(queries,
                            'create_telegram_subscribers_table',
                            commit=True)
     try:
         executor.execute_query(queries,
                                'alter_bot_telegram_subscribers_table',
                                commit=True)
     except Exception as e:
         log.debug(e)
     messenger.change_token(self.subscriber_bot)
     messenger.add_handlers(self.start, commands=['start'])
     messenger.add_handlers(self.stop, commands=['stop'])
     messenger.add_handlers(self.input_password, content_types=['text'])
     messenger.add_handlers(self.confirm, callback=True)
     messenger.listen()
Esempio n. 6
0
 def __init__(self, inertia_time=30):
     self.prev_alerts = {}
     self.alert_inertia = timedelta(seconds=inertia_time)
     self.photos_dir = config.find('photos_dir')
     executor.execute_query(queries, 'create_alerts_table', commit=True)
     executor.execute_query(queries,
                            'create_telegram_subscribers_table',
                            commit=True)
     try:
         executor.execute_query(queries,
                                'alter_is_tester_alerts_table',
                                commit=True)
     except Exception as e:
         log.debug(e)
     try:
         executor.execute_query(queries,
                                'alter_is_tester_faces_table',
                                commit=True)
     except Exception as e:
         log.debug(e)
     try:
         executor.execute_query(queries,
                                'alter_bot_telegram_subscribers_table',
                                commit=True)
     except Exception as e:
         log.debug(e)
     blacklist_alert = """<b>Посетитель из черного списка: </b> 
     Идентификатор: {} 
     Похожесть: {}%, Камера: {}
     Время: {}"""
     whitelist_alert = """<b>Посетитель из белого списка: </b> 
     Идентификатор: {} 
     Похожесть: {}%, Камера: {}
     Время: {}"""
     messenger.add_mailing_template('blacklist', blacklist_alert)
     messenger.add_mailing_template('whitelist', whitelist_alert)
     bus.subscribe('/danger', self.receive_alert)
     bus.listen()
Esempio n. 7
0
    def post(self):
        """Add a new photo.

        Accepts
        -------
        request.files: Form data binaries

        Returns
        -------
        {"success": True}: JSON
        """
        transaction_id = str(uuid())
        filenames = []
        for key in request.files:
            file = request.files[key]
            old_filename = file.filename
            filename = str(uuid()) + ".jpg"
            filenames.append(old_filename)
            output_path = os.path.join("/tmp/", filename)
            with open(output_path, "wb"):
                file.save(output_path)
            bus.push("file_added",
                     filename=filename,
                     old_filename=old_filename,
                     transaction_id=transaction_id,
                     is_tester=self.is_tester)
        processed = []
        failed = {}
        start = time()
        while True:
            if time() - start >= 20:
                log.debug(
                    "Transaction closed by timeout with {} files left".format(
                        len(filenames)))
                break
            if not len(filenames):
                log.debug("all files processed in {} seconds".format(time() -
                                                                     start))
                break
            raw_msg = bus.client.lpop(transaction_id)

            if raw_msg:
                msg = bus._loads(raw_msg)[1]
                log.debug("new message: {}".format(msg))
                old_filename = msg.get("old_filename")
                if msg["processed"]:
                    processed.append(old_filename)
                    filenames.remove(old_filename)
                else:
                    failed[old_filename] = msg.get("error_message")
                    filenames.remove(old_filename)
        for filename in filenames:
            failed[filename] = "Timeout"
        res = {}
        status = 201
        res["success"] = processed
        if len(failed):
            res["error"] = failed
            status = 207
        log.debug("removing topic {}".format(transaction_id))
        bus.client.delete(transaction_id)
        return res, status
Esempio n. 8
0
def on_file_added(*args, **kwargs):
    assert (kwargs.get("filename"))
    assert (kwargs.get("old_filename"))
    assert (kwargs.get("transaction_id"))
    assert (kwargs.get("is_tester") is not None)
    log.debug("New file added: {} received".format(kwargs))
    filename = kwargs["filename"]
    old_filename = kwargs["old_filename"]
    destination_topic = kwargs["transaction_id"]
    is_tester = kwargs["is_tester"]
    file_path = os.path.join("/tmp/", filename)
    photos_dir = config.find('photos_dir')
    destination_path = os.path.join(photos_dir, filename)
    try:
        new_face_features = get_features_from_file(file_path)
    except AssertionError as e:
        log.warning('file "{}": {}'.format(file_path, e))
        bus.push(destination_topic,
                 filename=filename,
                 old_filename=old_filename,
                 processed=False,
                 error_message="Файл не найден")
        return
    except AttributeError as e:
        log.warning('failed to read from file "{}": {}'.format(file_path, e))
        bus.push(destination_topic,
                 filename=filename,
                 old_filename=old_filename,
                 processed=False,
                 error_message=
                 "Загруженный файл не является изображением в формате jpeg")
        return
    except FaceExtractError as e:
        bus.push(destination_topic,
                 filename=filename,
                 old_filename=old_filename,
                 processed=False,
                 error_message=str(e))
        return
    try:
        os.rename(file_path, destination_path)
    except OSError as e:
        bus.push(destination_topic,
                 filename=filename,
                 old_filename=old_filename,
                 processed=False,
                 error_message=str(e))
        return
    faces = executor.execute_query(query, 'select_faces', commit=True)
    labels_list = [row[2] for row in faces]
    new_face_label = max(labels_list) + 1
    labels_list.append(new_face_label)
    features_list = []
    for idx, row in enumerate(faces):
        file_path = os.path.join(photos_dir, row[1])
        try:
            features = get_features_from_file(file_path)
        except AssertionError as e:
            log.warning('file "{}": {}'.format(file_path, e))
            del labels_list[idx]
            continue
        except AttributeError as e:
            log.warning('failed to read from file "{}": {}'.format(
                file_path, e))
            del labels_list[idx]
            continue
        except FaceExtractError as e:
            log.warning('failed to extract features from file "{}": {}'.format(
                file_path, e))
            del labels_list[idx]
            continue
        features_list.append(features)
    features_list.append(new_face_features)
    features_set.rebuild(np.array(features_list), np.array(labels_list))
    executor.execute_query(query,
                           'insert_face',
                           filename=filename,
                           label_id=new_face_label,
                           is_tester=is_tester,
                           commit=True)
    bus.push(destination_topic,
             filename=filename,
             old_filename=old_filename,
             processed=True)