def send_to_storage(self, **kwargs): for key in kwargs.keys(): if key in self.send_stages: executor.execute_query(queries, self.insert_key, stage=key, ts=kwargs[key], commit=True)
def __init__(self, stages=None, send_stages=None): """ :param stages: list/tuple of stages name :param send_stages: list/tuple of stages name for sending to storage """ self.create_key = 'create_ping_table' self.insert_key = 'insert_ping' if stages is None: stages = [] if send_stages is None: send_stages = [] self.stages = set(stages) self.send_stages = set(send_stages) super().__init__('ping', self.stages, self.send_to_storage) executor.execute_query(queries, self.create_key, commit=True)
def storage_save(self, camera_id, ts, l, t, r, b, filename, frame_number, #features, init_height, init_width): # Convert ndarray to json #features = json.dumps(features, cls=NumpyEncoder) #assert type(features) == str, "Features is not converted to JSON data type" # Query inserting Output Person data features = None if len(self.query_pack) >= self.query_pack_size: executor.execute_query(query, 'insert_many_frames', self.query_pack, many=True, commit=True) self.query_pack = [] else: self.query_pack.append((camera_id, ts, l, t, r, b, filename, frame_number, features, init_height, init_width, l / init_width, t / init_height, r / init_width, b / init_height))
def input_password(self, message): user_id = message.from_user.id input_text = message.text user_state = self.temp_states.pop(user_id, None) if user_state == 'input_password': if input_text == self.password: msg = 'Доступ разрешен, подписка оформлена.' executor.execute_query(queries, 'insert_telegram_subscriber', user_id=user_id, bot=self.subscriber_bot, commit=True) messenger.send_message(user_id, msg, parse_mode='html') else: msg = 'В доступе отказано.' messenger.send_message(user_id, msg, parse_mode='html')
def get_direction(track_id): try: select_track = 'select_tracks_reporter' track = executor.execute_query(queries, select_track, track_id=track_id, commit=True) if track: l0_f, t0_f, r0_f, b0_f, ts0, filename0, frame0, l1_f, t1_f, r1_f, b1_f, ts1, filename1, frame1, length, camera_id = track.pop( ) else: return 'TRACK WITH THIS ID={} DOES NOT EXIST'.format(track_id) image_file = '{path}/{track_id}.jpg'.format(path=IMAGE_PATH, track_id=track_id) f0 = '{path}/{filename0}'.format(path=VIDEOS_PATH, filename0=filename0) f1 = '{path}/{filename1}'.format(path=VIDEOS_PATH, filename1=filename1) if os.path.exists(f1): if not os.path.exists(image_file): cap = cv2.VideoCapture(f1) cap.set(1, int(frame1)) # set frame position in video width = int(cap.get(3)) height = int(cap.get(4)) ret, image = cap.read() if not ret: return 'CANNOT READ FILE' from_w = (l0_f, r0_f, l1_f, r1_f) from_h = (t0_f, b0_f, t1_f, b1_f) l0, r0, l1, r1 = tuple(int(x * width) for x in from_w) t0, b0, t1, b1 = tuple(int(x * height) for x in from_h) dist = math.sqrt((r1 - r0)**2 + (b1 - b0)**2) if dist: angle_x = math.acos((r1 - r0) / dist) * (180 / math.pi) angle_y = math.acos((b1 - b0) / dist) * (180 / math.pi) cv2.putText( image, 'Angle_x:{}, Angle_y:{}'.format(angle_x, angle_y), (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0)) cv2.rectangle(image, (l0, t0), (r0, b0), (0, 255, 0), 2) cv2.rectangle(image, (l1, t1), (r1, b1), (0, 255, 0), 2) cv2.arrowedLine(image, (r0 - ((r0 - l0) // 2), b0 - ((b0 - t0) // 2)), (r1 - ((r1 - l1) // 2), b1 - ((b1 - t1) // 2)), (255, 255, 0), 5) cv2.putText(image, 'First timestamp:{}'.format(ts0), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0)) cv2.putText(image, 'Last timestamp:{}'.format(ts1), (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0)) cv2.putText(image, 'Dist:{}, Track length:{}'.format(dist, length), (20, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0)) cv2.imwrite(image_file, image) else: return 'CANNOT FIND VIDEOFILE={}'.format(f1) return redirect('{uri}frames/{track_id}.jpg'.format(uri=URI, track_id=track_id)) except Exception as e: return str(e)
def get(self): try: res = executor.execute_query(query, "select_faces_with_argument", is_tester=self.is_tester, commit=True) except InterfaceError as e: return "Нет соединения с БД", 400 return res
def on_file_deleted(*args, **kwargs): pk = kwargs["id"] filename = kwargs["filename"] label_id = kwargs["label_id"] destination_topic = kwargs["transaction_id"] photos_dir = config.find('photos_dir') executor.execute_query(query, "delete_face_by_pk", pk=pk, commit=True) faces = executor.execute_query(query, 'select_faces', commit=True) labels_list = [row[2] for row in faces] 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) try: os.remove(os.path.join(config.find('photos_dir'), filename)) except (FileNotFoundError, IsADirectoryError, OSError) as e: log.error("api File {} cannot be deleted: {}".format(filename, e)) bus.push(destination_topic, deleted=False, filename=filename, label_id=label_id, reason=str(e)) return features_set.rebuild(np.array(features_list), np.array(labels_list)) bus.push(destination_topic, deleted=True, filename=filename, label_id=label_id)
def confirm(self, call): user_id = call.from_user.id user_state = self.temp_states.pop(user_id, None) if user_state == 'confirm_stop': if call.data == 'yes': msg = 'Вы отписались' executor.execute_query(queries, 'delete_telegram_subscriber', user_id=user_id, bot=self.subscriber_bot, commit=True) messenger.send_message(user_id, msg, parse_mode='html') messenger.answer_callback_query(callback_query_id=call.id, show_alert=False) else: msg = 'Отписка отменена.' messenger.send_message(user_id, msg, parse_mode='html') messenger.answer_callback_query(callback_query_id=call.id, show_alert=False)
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()
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()
def start(self, message): user_id = message.from_user.id subscribers = [ sub[0] for sub in executor.execute_query(queries, 'select_telegram_subscribers', bot=self.subscriber_bot) ] if user_id not in subscribers: msg = 'Введите пароль.' messenger.send_message(user_id, msg, parse_mode='html') self.temp_states[user_id] = 'input_password' else: msg = 'Вы уже подписаны.' messenger.send_message(user_id, msg, parse_mode='html')
def stop(self, message): user_id = message.from_user.id subscribers = [ sub[0] for sub in executor.execute_query(queries, 'select_telegram_subscribers', bot=self.subscriber_bot) ] if user_id in subscribers: keys = [('Да', 'yes'), ('Нет', 'no')] msg = 'Вы уверены, что хотите отписаться?' messenger.send_message( user_id, msg, parse_mode='html', reply_markup=messenger.inline_keyboard(keys)) self.temp_states[user_id] = 'confirm_stop' else: msg = 'Вы не подписаны.' messenger.send_message(user_id, msg, parse_mode='html')
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()
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)
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) try: os.remove(os.path.join(config.find('photos_dir'), filename)) except (FileNotFoundError, IsADirectoryError, OSError) as e: log.error("api File {} cannot be deleted: {}".format(filename, e)) bus.push(destination_topic, deleted=False, filename=filename, label_id=label_id, reason=str(e)) return features_set.rebuild(np.array(features_list), np.array(labels_list)) bus.push(destination_topic, deleted=True, filename=filename, label_id=label_id) executor.execute_query(query, 'create_faces_table', commit=True) bus.subscribe("file_added", on_file_added) bus.subscribe("file_deleted", on_file_deleted)
def receive_alert(self, ts, camera, label, distance, left, top, right, bottom, filename, frame_number, frame): now = datetime.now() prev_alert = self.prev_alerts.get(label) if prev_alert is not None: if (ts - prev_alert) >= self.alert_inertia: self.prev_alerts.pop(label, None) else: self.prev_alerts[label] = now prev_alert = self.prev_alerts.get(label) if prev_alert is None: photo_files, is_tester = executor.execute_query( queries, 'select_filename_by_id', label=label)[0] if is_tester: bot = 'whitelist' else: bot = 'blacklist' frame = self._image_prepared(frame, (left, top, right, bottom)) if photo_files: origin_filename = photo_files origin_path = os.path.join(self.photos_dir, origin_filename) origin = self._image_prepared(origin_path) else: origin_filename = 'None' origin = None log.info( 'alerter Sending alert message for label {}...'.format(label)) messenger.subscribers = [ sub[0] for sub in executor.execute_query( queries, 'select_telegram_subscribers_unmute', bot=bot) ] send_alert = messenger.send_mailing(bot, label, int(distance * 100), camera, ts, frame=frame, origin=origin) try: while True: res, user_id = next(send_alert) if res == 403: executor.execute_query(queries, 'telegram_subscriber_mute', user_id=user_id, commit=True) except StopIteration: log.info( 'alerter Sent alert message for label {} to messenger'. format(label)) executor.execute_query(queries, 'insert_alerts_table', camera_id=camera, ts=ts, l=left, t=top, r=right, b=bottom, label=label, distance=distance, frame_number=frame_number, filename=filename, origin=origin_filename, is_tester=is_tester, commit=True) log.info( 'alerter Sent alert for label {} to database'.format(label)) self.prev_alerts[label] = now