def __init__(self): self.tracker = tracker self.executor = executor self.download_interval = datetime.timedelta( seconds=config.find('counter/download_interval')) self.track_max_lag = datetime.timedelta( minutes=config.find('counter/track_max_lag')) self.select_limit = config.find('counter/select_limit') self._select_from_frames = 'select_frames_tracker' self._select_from_tracks = 'select_tracks_tracker' self._update_frames_status = 'update_frames_status' self._update_tracks = 'update_tracks_tracker' self._insert_into_tracks = 'insert_tracks_tracker' self._create_frames_table = 'create_frames_table' self._create_tracks_table = 'create_tracks_table' # creating tables is not exists self.executor.execute_query(queries, self._create_frames_table) self.executor.execute_query(queries, self._create_tracks_table) self._prev_time = datetime.datetime.now() self.frames = self.executor.execute_query(queries, self._select_from_frames, limit=self.select_limit, commit=True) self.preload_tracks(self._prev_time) self.preload_ids = self.tracker.get_all_track_ids()
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 __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, streams=None, timeout=1, fps=25, file_time=60): self.videos_dir = config.find('videos_path') self.standby = cv2.imread( os.path.join(sys.path[0], *config.find('standby').split('/'))) self.timeout = timedelta(milliseconds=timeout) self.file_time = config.find('video_duration') or file_time self.fps = config.find('framerate') or fps self.ext = config.find('ext') or 'avi' self.ts_start = None self.caps_list = [] self.list_to_push = [] self.lock = Lock() for stream in config.find('streams') or streams: log.info('reader Processing stream {}'.format(stream)) cap = {} cap['stream'] = stream cap['cap'] = cv2.VideoCapture(stream, cv2.CAP_GSTREAMER) cap['cap'].set(3, 300) cap['cap'].set(4, 300) cap['width'] = 300 cap['height'] = 300 cap['standby'] = cv2.resize(self.standby, (cap['width'], cap['height'])) cap['time_off'] = None cap['writer'] = None self.caps_list.append(cap)
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 __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 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)
from flask import Flask, redirect from scope import config, log, executor, queries import math import cv2 import os log.info('reporter Initializing Flask...') app = Flask(__name__) log.info('reporter Flask initialized') URI = config.find('reporter/uri', '/') VIDEOS_PATH = config.find('reader/videos_path') IMAGE_PATH = config.find('reporter/image_path') @app.route("/reporter/get_direction/<track_id>") 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)