Esempio n. 1
0
File: core.py Progetto: zbx91/Dwarf
    def __init__(self, app_window):
        self.app_window = app_window
        self.app = app_window.get_app_instance()

        self.java_available = False
        self.loading_library = False

        # frida device
        self.device = None

        # process
        self.pid = 0
        self.process = None
        self.script = None

        # kernel
        self.kernel = Kernel(self)

        # hooks
        self.hooks = {}
        self.on_loads = {}
        self.java_hooks = {}
        self.temporary_input = ''
        self.native_pending_args = None
        self.java_pending_args = None

        # context
        self.arch = ''
        self.pointer_size = 0
        self.contexts = {}
        self.context_tid = 0

        # tracers
        self.native_traced_tid = 0

        # core utils
        self.bus = EventBus()
        self.emulator = Emulator(self)
        self.git = Git()
        self.prefs = Prefs()
        self.script_manager = ScriptsManager(self)

        self.keystone_installed = False
        try:
            import keystone.keystone_const
            self.keystone_installed = True
        except:
            pass
Esempio n. 2
0
    def __init__(self, configs):
        # event bus and loop
        self.main_loop = asyncio.get_event_loop()
        self.bus = EventBus()

        # mongo db free4all
        connect('secret')
        self.mongo_db = Document._get_db()

        # discord
        self.discord_client = discord.Client()
        self.secret_server = discord.Server(id='326095959173890059')
        self.secret_channel = discord.Channel(id='404630967257530372',
                                              server=self.secret_server)
        self.welcome_channel = discord.Channel(id='404795628019777557',
                                               server=self.secret_server)

        # github
        self.git_client = Github(configs['github_username'], configs['github_password'])
        self.git_repo = self.git_client.get_repo('secRetDBot/secRet_dBot')
        self.web_repo = self.git_client.get_repo('secRetDBot/secRet-bot-frontend')

        # discord message handler
        self.message_handler = MessageHandler(self)

        # handlers
        self.handler_status = status.Status(self)
Esempio n. 3
0
    def setUp(self):
        self.bus = EventBus()
        self.window = teek.Window("Testing Window")
        self.window.geometry(800, 600)
        ui = POEForge(self.window, self.bus)

        self.mouse = Mouse(self.bus)
        self.keyboard = Keyboard(self.bus)
        self.preset = Preset("test", self.bus, xy_1=(0, 1), xy_2=(1, 2))
Esempio n. 4
0
 def setUp(self):
     self.bus = EventBus()
     self._queue = DataQueue()
     self.preset = Preset("test",
                          self.bus,
                          self._queue,
                          item="helmet",
                          xy_1=(0, 1),
                          xy_2=(1, 2))
Esempio n. 5
0
class Robo(object):
    def __init__(self):
        self.bus = EventBus()
        self.bus.add_event(self.extract_1, "login_finish")
        self.bus.add_event(self.extract_2, "login_finish")
        self.bus.add_event(self.extract_3, "login_finish")
        pass

    def login(self):
        print("Iniciando login")
        sleep(1)
        self.bus.emit("login_finish")
        print("Fim login")

    def extract_1(self):
        print("Iniciando extract_1")
        sleep(2)
        print("Fim extract_1")

    def extract_2(self):
        print("Iniciando extract_2")
        sleep(2)
        print("Fim extract_2")

    def extract_3(self):
        print("Iniciando extract_3")
        sleep(2)
        print("Fim extract_3")

    def finish(self):
        print("Fim !!")

    def exec(self):
        self.login()
Esempio n. 6
0
    def brelse(self, buffer):
        # Todo: wakeup all processes (event: waiting for any buffer to become free;
        self.eventBus.wake(EventBus.EVENT_WAIT_ANY_BUFFER)
        # Todo: wakeup all processes (event: waiting for this buffer to become free;
        self.eventBus.wake(EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))

        # raise processor execution level to block interrupts;
        if buffer.isDataValid() and not buffer.isOld():
            # enqueue buffer at end of free list;
            self.bufferCache.freeList.push(buffer)
        else:
            # enqueue buffer at beginning of free list;
            self.bufferCache.freeList.unshift(buffer)
        # lower processor execution level to allow interrupts;

        buffer.unlock()
        return buffer
Esempio n. 7
0
    def __init__(self):
        def get_input_handlers(controller, app_config):
            handlers = [PygameInputHandler(controller, app_config)]
            try:
                from pi_input_handler import PiInputHandler

                handlers.append(PiInputHandler(controller, app_config))
            except ImportError:
                print('Unable to import raspberrypi input handler')
            return handlers

        def get_zoneminder_client(app_config):
            zm_client = ZoneMinderClient(app_config.config[SERVER_HOST],
                                         app_config.config[SERVER_PORT],
                                         app_config.config[ZM_WEB_PATH],
                                         app_config.config[USER_NAME],
                                         app_config.config[PASSWORD],
                                         app_config.config[ZMS_WEB_PATH])
            return zm_client

        config = AppConfig()
        event_bus = EventBus()
        client = get_zoneminder_client(config)
        self.app_state = AppState()
        self.app_context = AppContext(config, client, event_bus)

        self.display = PygameDisplay(config)
        self.display.init()
        self.app_context.display_size = self.display.get_display_size()

        zm_stream_component = MonitorStreamComponent(self.app_context)
        group_selector_component = GroupSelectorComponent(self.app_context)
        monitor_selector_component = MonitorSelectorComponent(self.app_context)
        shutdown_prompt_component = ShutdownPromptSelector(self.app_context)
        menu_selector = MenuSelector(self.app_context)
        startup_component = StartUpComponent(self.app_context)
        self.component_manager = AppComponentManager(
            self.display, event_bus, startup_component, zm_stream_component, [
                group_selector_component, monitor_selector_component,
                shutdown_prompt_component, menu_selector
            ])
        self.input_manager = InputManager(get_input_handlers(
            event_bus, config))
        self.app_controller = AppController(self.app_context,
                                            self.input_manager, self.app_state)
        self.task_manager = TaskManager(event_bus)
Esempio n. 8
0
    def __init__(self):
        self._bus = EventBus()
        self._queue = DataQueue()
        self.preset = Preset("something here", self._bus, self._queue)
        self.controller_mouse = Mouse(self._bus, self._queue)
        self.controller_keyboard = Keyboard(self._bus, self._queue)

        # spin up threads for Teek
        teek.init_threads()

        click_listener = mouse.Listener(
            on_click=self.controller_mouse.on_click)

        window = teek.Window("Path of Exile Forge")
        window.geometry(1280, 720)
        POEForge(window, self._bus, self._queue)

        window.on_delete_window.connect(teek.quit)
        teek.run()
Esempio n. 9
0
class Core:

    version = "0.1"

    def __init__(self, event_loop=None):
        """Hub core object"""

        self.event_loop = event_loop if event_loop else asyncio.get_event_loop(
        )

        self.location = os.path.dirname(__file__)
        self._state = CoreState.RUNNING

        self._lifecycle_hooks: Dict[CoreState, List[Callable]] = {
            key: []
            for key in CoreState
        }

        self.plugins: Dict = {}

        api_tokens = ["!;,[#S{9E>6L!WrSnHUEcMh}8TG)35"]
        if default_token := os.getenv("API_TOKEN"):
            api_tokens.append(default_token)

        self.api = API(self, api_tokens)
        self.storage = Storage(self)
        self.timer = Timer(self)
        self.io: IO = IO(self)
        self.bus: EventBus = EventBus(self)
        self.plugins = load_plugins(self)
        self.registry: EntityRegistry = EntityRegistry(self)
        self.engine = FlowEngine(self)

        self.core_state = CoreState.RUNNING

        api_port = os.getenv("API_PORT", 8081)

        self.add_job(self.api.start, int(api_port))
        # self.add_job(lambda: self.registry.activate_scene('test'))

        if os.getenv("CIF") == "1":
            self.add_job(self.cio)
class StateObserver:
    def __init__(self, stateregister):
        self.bus = EventBus()
        self.register = stateregister
        self.active_state = None

    def bind(self, event, callback):
        self.bus.register(event, callback)

    def get_register(self):
        return self.register

    def change_state(self, state):
        state.action(self, *self.active_state_args)

    def set_state(self, name, args=[]):
        state = self.register.get_state(name)
        self.bus.post('change', (self.active_state, state))
        if self.active_state is not None:
            self.bus.post('finish', self.active_state)
            print "[SM] State replaced"
        self.active_state = state
        self.active_state_args = args

    def state_finished(self, state):
        if state == self.active_state and not state.was_interrupted:
            self.bus.post('finish', state)
            self.active_state = None

    def get_active_state(self):
        return self.active_state

    def next_state(self):
        while self.active_state != None:
            yield self.active_state

    def get_states(self):
        return self.register.states[0]
class StateObserver:
    def __init__(self, stateregister):
        self.bus = EventBus()
        self.register = stateregister
        self.active_state = None

    def bind(self, event, callback):
        self.bus.register(event, callback)

    def get_register(self):
        return self.register

    def change_state(self, state):
        state.action(self, *self.active_state_args)

    def set_state(self, name, args=[]):
        state = self.register.get_state(name)
        self.bus.post('change', (self.active_state, state))
        if self.active_state is not None:
            self.bus.post('finish', self.active_state)
            print "[SM] State replaced"
        self.active_state = state
        self.active_state_args = args

    def state_finished(self, state):
        if state == self.active_state and not state.was_interrupted:
            self.bus.post('finish', state)
            self.active_state = None

    def get_active_state(self):
        return self.active_state

    def next_state(self):
        while self.active_state != None:
            yield self.active_state

    def get_states(self):
        return self.register.states[0]
Esempio n. 12
0
from event_bus import EventBus

bus = EventBus()

Esempio n. 13
0
    def getblk(self, blockNumber):
        """
        getblk algo from textbook
        """
        while (True):
            if self.bufferCache.hashQueue.has(blockNumber):
                buffer = self.bufferCache.hashQueue.get(blockNumber)
                if buffer.isLocked():
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 5"'
                    )
                    # logger.info("Scenario 5: Block was found on the hash queue, but its buffer is currently busy.")
                    # sleep until this buffer becomes free
                    self.eventBus.sleep(
                        EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))
                    self.eventBus.clear(
                        EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))
                    continue

                # Scenario 1
                buffer.lock()
                self.bufferCache.freeList.remove(buffer)
                logger.info(
                    f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 1"'
                )
                # logger.info("Scenario 1: Block is found on its hash queue and its buffer is free.")
                return buffer  #block found on hashQ

            else:
                if self.bufferCache.freeList.isEmpty():
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 4"'
                    )
                    # logger.info('Scenario 4: Block could not be found on the hash queue and the free list of buffers is empty.')
                    # Sleep until any buffer becomes free
                    self.eventBus.sleep(EventBus.EVENT_WAIT_ANY_BUFFER)
                    self.eventBus.clear(EventBus.EVENT_WAIT_ANY_BUFFER)
                    continue
                else:
                    buffer = self.bufferCache.freeList.pop()
                    # Scenario 3
                    if buffer.isDelayedWrite():
                        logger.info(
                            f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 3"'
                        )
                        # write buffer asynchronously to the disk
                        self.bwrite(buffer, synchronous=False)
                        continue

                    buffer.lock()
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 2"'
                    )
                    # logger.info("Scenario 2: Buffer not found on hash queue but a buffer from free list is assigned")
                    if buffer.blockNumber:
                        self.bufferCache.hashQueue.remove(buffer)
                    logger.debug(
                        f'REMOVE BUFFER{buffer} - UPDATED HASH QUEUE:{self.bufferCache.hashQueue}'
                    )
                    buffer.updateBlockNumber(blockNumber)
                    self.bufferCache.hashQueue.add(buffer)
                    return buffer  # block not in hashQ, taken from freelist
            time.sleep(.5)
Esempio n. 14
0
from event_bus import EventBus
from sample_event import SampleEvent

KEY_ESCAPE = 27

event_bus = EventBus()
assert event_bus is not None


def onEventKey(event: SampleEvent):
    assert event is not None
    assert event.key == KEY_ESCAPE


assert len(event_bus.getHandlers(SampleEvent)) == 0

event_bus.unregister(onEventKey)
assert len(event_bus.getHandlers(SampleEvent)) == 0

event_bus.register(onEventKey)
assert len(event_bus.getHandlers(SampleEvent)) == 1

event_bus.unregister(onEventKey)
assert len(event_bus.getHandlers(SampleEvent)) == 0

event_bus.register(onEventKey)
assert len(event_bus.getHandlers(SampleEvent)) == 1

event_bus.emit(SampleEvent(KEY_ESCAPE))
Esempio n. 15
0
class Dwarf(object):
    bus = EventBus()

    def __init__(self, app_window):
        self.app_window = app_window
        self.app = app_window.get_app_instance()

        self.java_available = False
        self.loading_library = False

        # process
        self.pid = 0
        self.process = None
        self.script = None

        # hooks
        self.hooks = {}
        self.on_loads = {}
        self.java_hooks = {}
        self.temporary_input = ''
        self.native_pending_args = None
        self.java_pending_args = None

        self.keystone_installed = False
        try:
            import keystone.keystone_const
            self.keystone_installed = True
        except:
            pass

        self.prefs = Prefs()

    def attach(self, pid_or_package, script=None):
        if self.process is not None:
            self.detach()

        device = frida.get_usb_device()
        try:
            self.process = device.attach(pid_or_package)
        except Exception as e:
            utils.show_message_box(
                'Failed to attach to %s' % str(pid_or_package), str(e))
            return
        self.load_script(script)

    def detach(self):
        self.dwarf_api('_detach')
        if self.script is not None:
            self.script.unload()
        if self.process is not None:
            self.process.detach()

    def load_script(self, script=None):
        with open('lib/script.js', 'r') as f:
            s = f.read()
        self.script = self.process.create_script(s)
        self.script.on('message', self.on_message)
        self.script.on('destroyed', self.on_destroyed)
        self.script.load()

        if script is not None:
            self.dwarf_api('evaluateFunction', script)

        self.app_window.on_script_loaded()

    def spawn(self, package, script=None):
        if self.process is not None:
            self.detach()

        device = frida.get_usb_device()
        self.app_window.get_adb().kill_package(package)
        try:
            pid = device.spawn(package)
            self.process = device.attach(pid)
        except Exception as e:
            utils.show_message_box('Failed to spawn to %s' % package, str(e))
            return
        self.load_script(script)
        device.resume(pid)

    def on_message(self, message, data):
        if 'payload' not in message:
            print(message)
            return

        what = message['payload']
        parts = what.split(':::')
        if len(parts) < 2:
            print(what)
            return

        cmd = parts[0]
        if cmd == 'enumerate_java_classes_start':
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_start()
        elif cmd == 'enumerate_java_classes_match':
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_match(
                    parts[1])
        elif cmd == 'enumerate_java_classes_complete':
            self.app_window.get_menu().on_java_classes_enumeration_complete()
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_complete()
        elif cmd == 'enumerate_java_methods_complete':
            Dwarf.bus.emit(parts[1], json.loads(parts[2]))
        elif cmd == 'log':
            self.app.get_log_panel().log(parts[1])
        elif cmd == 'hook_java_callback':
            h = Hook(Hook.HOOK_JAVA)
            h.set_ptr(1)
            h.set_input(parts[1])
            if self.java_pending_args:
                h.set_condition(self.java_pending_args['condition'])
                h.set_logic(self.java_pending_args['logic'])
                self.java_pending_args = None
            self.java_hooks[h.get_input()] = h
            self.app.get_hooks_panel().hook_java_callback(h)
        elif cmd == 'hook_native_callback':
            h = Hook(Hook.HOOK_NATIVE)
            h.set_ptr(int(parts[1], 16))
            h.set_input(self.temporary_input)
            self.temporary_input = ''
            if self.native_pending_args:
                h.set_condition(self.native_pending_args['condition'])
                h.set_logic(self.native_pending_args['logic'])
                self.native_pending_args = None
            self.hooks[h.get_ptr()] = h
            self.app.get_hooks_panel().hook_native_callback(h)
        elif cmd == 'memory_scan_match':
            Dwarf.bus.emit(parts[1], parts[2], json.loads(parts[3]))
        elif cmd == 'memory_scan_complete':
            self.app_window.get_menu().on_bytes_search_complete()
            Dwarf.bus.emit(parts[1] + ' complete', 0, 0)
        elif cmd == 'onload_callback':
            self.loading_library = parts[1]
            self.app.get_log_panel().log('hook onload %s @thread := %s' %
                                         (parts[1], parts[3]))
            self.app.get_hooks_panel().hit_onload(parts[1], parts[2])
        elif cmd == 'set_context':
            data = json.loads(parts[1])
            self.app.get_contexts().append(data)

            if 'context' in data:
                sym = ''
                if 'pc' in data['context']:
                    name = data['ptr']
                    if 'moduleName' in data['symbol']:
                        sym = '(%s - %s)' % (data['symbol']['moduleName'],
                                             data['symbol']['name'])
                else:
                    name = data['ptr']
                self.app.get_contexts_panel().add_context(
                    data, library_onload=self.loading_library)
                if self.loading_library is None:
                    self.app.get_log_panel().log('hook %s %s @thread := %d' %
                                                 (name, sym, data['tid']))
                if len(self.app.get_contexts()
                       ) > 1 and self.app.get_registers_panel().have_context():
                    return
                self.app.get_session_ui().request_session_ui_focus()
            else:
                self.app.set_arch(data['arch'])
                if self.app.get_arch() == 'arm':
                    self.app.pointer_size = 4
                else:
                    self.app.pointer_size = 8
                self.pid = data['pid']
                self.java_available = data['java']
                self.app.get_log_panel().log('injected into := ' +
                                             str(self.pid))
                self.app_window.on_context_info()

            self.app.apply_context(data)
            if self.loading_library is not None:
                self.loading_library = None
        elif cmd == 'set_data':
            key = parts[1]
            if data:
                self.app.get_data_panel().append_data(
                    key, hexdump(data, result='return'))
            else:
                self.app.get_data_panel().append_data(key, str(parts[2]))
        elif cmd == 'update_modules':
            self.app.apply_context({
                'tid': parts[1],
                'modules': json.loads(parts[2])
            })
        elif cmd == 'update_ranges':
            self.app.apply_context({
                'tid': parts[1],
                'ranges': json.loads(parts[2])
            })
        else:
            print(what)

    def on_destroyed(self):
        self.app.get_log_panel().log('detached from %d. script destroyed' %
                                     self.pid)
        self.app_window.on_script_destroyed()

        self.pid = 0
        self.process = None
        self.script = None

    def dump_memory(self, file_path=None, ptr=0, length=0):
        if ptr == 0:
            ptr, inp = InputDialog.input_pointer(self.app)
        if ptr > 0:
            if length == 0:
                accept, length = InputDialog.input(self.app,
                                                   hint='insert length',
                                                   placeholder='1024')
                if not accept:
                    return
                try:
                    if length.startswith('0x'):
                        length = int(length, 16)
                    else:
                        length = int(length)
                except:
                    return
            if file_path is None:
                r = QFileDialog.getSaveFileName(
                    self.app, caption='Save binary dump to file')
                if len(r) == 0 or len(r[0]) == 0:
                    return
                file_path = r[0]
            data = self.read_memory(ptr, length)
            with open(file_path, 'wb') as f:
                f.write(data)

    def dwarf_api(self, api, args=None, tid=0):
        if tid == 0:
            tid = self.app.get_context_tid()
        if args is not None and not isinstance(args, list):
            args = [args]
        if self.script is None:
            return None
        try:
            return self.script.exports.api(tid, api, args)
        except Exception as e:
            self.app.get_log_panel().log(str(e))
            return None

    def hook_java(self, input=None, pending_args=None):
        if input is None or not isinstance(input, str):
            accept, input = InputDialog.input(
                self.app,
                hint='insert java class or methos',
                placeholder='com.package.class or com.package.class.method')
            if not accept:
                return
        self.java_pending_args = pending_args
        input = input.replace(' ', '')
        self.app.dwarf_api('hookJava', input)

    def hook_native(self, input=None, pending_args=None):
        if input is None or not isinstance(input, str):
            ptr, input = InputDialog.input_pointer(self.app)
        else:
            ptr = int(self.app.dwarf_api('evaluatePtr', input), 16)
        if ptr > 0:
            self.temporary_input = input
            self.native_pending_args = pending_args
            self.app.dwarf_api('hookNative', ptr)

    def hook_onload(self, input=None):
        if input is None or not isinstance(input, str):
            accept, input = InputDialog.input(self.app,
                                              hint='insert module name',
                                              placeholder='libtarget.so')
            if not accept:
                return
            if len(input) == 0:
                return

        if not input.endswith('.so'):
            input += '.so'

        if input in self.app.get_dwarf().on_loads:
            return

        self.dwarf_api('hookOnLoad', input)
        h = Hook(Hook.HOOK_ONLOAD)
        h.set_ptr(0)
        h.set_input(input)

        self.on_loads[input] = h
        if self.app.session_ui is not None and self.app.get_hooks_panel(
        ) is not None:
            self.app.get_hooks_panel().hook_onload_callback(h)

    def read_memory(self, ptr, len):
        if len > 1024 * 1024:
            position = 0
            next_size = 1024 * 1024
            data = bytearray()
            while True:
                try:
                    data += self.dwarf_api('readBytes',
                                           [ptr + position, next_size])
                except:
                    return None
                position += next_size
                diff = len - position
                if diff > 1024 * 1024:
                    next_size = 1024 * 1024
                elif diff > 0:
                    next_size = diff
                else:
                    break
            ret = bytes(data)
            del data
            return ret
        else:
            return self.dwarf_api('readBytes', [ptr, len])

    def get_loading_library(self):
        return self.loading_library

    def get_prefs(self):
        return self.prefs
Esempio n. 16
0
 def setUp(self):
     self.bus = EventBus()
     self.mouse = Mouse(self.bus)
     self.keyboard = Keyboard(self.bus)
Esempio n. 17
0
from event_bus import EventBus

g_eventBus = EventBus()
Esempio n. 18
0
 def setUp(self):
     self.bus = EventBus()
     self.window = teek.Window("Testing Window")
     self.window.geometry(800, 600)
     self.ui = POEForge(self.window, self.bus)
def upload_image(user_id, option):

    if request.method == "POST":
        if request.files:
            video = request.files["video"]

        if video.filename == "":
            print("No filename")
            return redirect(request.url)

        if allowed_video(video.filename):

            filename = secure_filename(video.filename)
            video.save(
                os.path.join(
                    "C:\\Users\\Алёша\\source\\repos\\VehiclesDataCollection\\ClientAngular\\src\\assets\\",
                    filename))
            #video.save(os.path.join("D:\\StealLog\\", filename))

            #option = request.form["option"]

            #if bool(option) == True:

            #drawing = DrawingMode()
            # need to transfer this points like limitation for smth
            #drawing.draw("C:\\Users\\Алёша\\source\\repos\\VehiclesDataCollection\\ClientAngular\\src\\assets\\" + filename)

            start_video_processing_event = StartVideoProcessingEvent(user_id)
            #body = json.dumps(start_video_processing_event.__dict__)

            #connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
            #channel = connection.channel()

            #print(type(self.event))
            #channel.queue_declare(queue='StartVideoProcessingEvent', durable=True)

            #channel.basic_publish(exchange='',
            #                  routing_key='StartVideoProcessingEvent',
            #                  body=body)

            #connection.close()
            event_bus = EventBus(start_video_processing_event)
            event_bus.publish()

            # start analyze stage
            args_parser = ArgsParser()
            args_parser.parse()

            # Create Object Detector
            detector_factory = DetectorFactory()
            object_detector = detector_factory.create_detector(
                'yolo', args_parser)

            # Create Object Tracker
            object_tracker = Tracker(100, 5, 5)

            analyzer = Analyzer(object_detector, object_tracker)
            # analyzer.analyze('videos/test_occlusion_short_1second.mp4', 'output/readyForRabbit.avi', user_id)
            analyzer.analyze(
                "C:\\Users\\Алёша\\source\\repos\\VehiclesDataCollection\\ClientAngular\\src\\assets\\"
                + filename,
                "C:\\Users\\Алёша\\source\\repos\\VehiclesDataCollection\\ClientAngular\\src\\assets\\output\\"
                + filename, user_id)

            print("Video saved")
        else:
            print("That file extension is not allowed")
            return redirect(request.url)

    return "OK"
Esempio n. 20
0
class Kernel:
    """
    Kernel class for emulating kernel
    """
    def __init__(self, bufferSize=20, diskSize=100):
        self.bufferCache = BufferCache(bufferSize)
        self.eventBus = EventBus()
        self.worker = Worker()
        self.disk = Disk(diskSize, self.eventBus)
        self.isRunning = False

    def addProcess(self, process):
        self.worker.add(process)

    def addProcesses(self, processList):
        [self.worker.add(process) for process in processList]

    def snapshot(self):
        """
        Function that implements the logger, gives us a view of the 
        variables and data structures in our program at every step
        """
        while self.isRunning:
            time.sleep(1.0)
            eventBus = "\n".join([
                f"[{event}]: {self.eventBus.isSet(event)}"
                for event in self.eventBus.events
            ])
            aliveProcesses = "\n".join(
                [str(p) for p in self.worker.started if p.isAlive()])
            exitedProcesses = "\n".join(
                [str(p) for p in self.worker.started if not p.isAlive()])
            logger.info("==========================")
            logger.info("BufferCache Status:")
            logger.info(f"{self.bufferCache}")
            logger.info("")
            logger.info("Disk:")
            logger.info(f"{self.disk}")
            logger.info("")
            logger.info("Event Bus:")
            logger.info(f"{eventBus}")
            logger.info("")
            logger.info("Alive Processes:")
            logger.info(f"{aliveProcesses}")
            logger.info("")
            logger.info("Exited Processes:")
            logger.info(f"{exitedProcesses}")
            logger.info("==========================")

    def boot(self):
        self.isRunning = True

        #Starts snapshotter thread
        snapshotter = Thread(None, self.snapshot, "Snapshotter")
        snapshotter.start()

        #starts processs thread
        self.worker.start()
        self.worker.join()

        logger.info(
            "Shutting down Kernel...\nSaving 'delay-write' buffers to disk...\n"
        )

        #After running all processes, writes all changes to the disk
        for buffer in self.bufferCache.__buffers__:
            logger.info(
                f'Kernel: Examining the buffer {buffer} for "delayed-write"')
            if buffer.isDelayedWrite():
                self.disk.writeBuffer(buffer)

        time.sleep(1)
        self.isRunning = False
        snapshotter.join()
        time.sleep(1)
        logger.info("\n\n==============Kernel Shutdown===============\n")

    def getblk(self, blockNumber):
        """
        getblk algo from textbook
        """
        while (True):
            if self.bufferCache.hashQueue.has(blockNumber):
                buffer = self.bufferCache.hashQueue.get(blockNumber)
                if buffer.isLocked():
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 5"'
                    )
                    # logger.info("Scenario 5: Block was found on the hash queue, but its buffer is currently busy.")
                    # sleep until this buffer becomes free
                    self.eventBus.sleep(
                        EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))
                    self.eventBus.clear(
                        EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))
                    continue

                # Scenario 1
                buffer.lock()
                self.bufferCache.freeList.remove(buffer)
                logger.info(
                    f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 1"'
                )
                # logger.info("Scenario 1: Block is found on its hash queue and its buffer is free.")
                return buffer  #block found on hashQ

            else:
                if self.bufferCache.freeList.isEmpty():
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 4"'
                    )
                    # logger.info('Scenario 4: Block could not be found on the hash queue and the free list of buffers is empty.')
                    # Sleep until any buffer becomes free
                    self.eventBus.sleep(EventBus.EVENT_WAIT_ANY_BUFFER)
                    self.eventBus.clear(EventBus.EVENT_WAIT_ANY_BUFFER)
                    continue
                else:
                    buffer = self.bufferCache.freeList.pop()
                    # Scenario 3
                    if buffer.isDelayedWrite():
                        logger.info(
                            f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 3"'
                        )
                        # write buffer asynchronously to the disk
                        self.bwrite(buffer, synchronous=False)
                        continue

                    buffer.lock()
                    logger.info(
                        f'getblk(): accessing |Block:{blockNumber}|, resulted in "Scenario 2"'
                    )
                    # logger.info("Scenario 2: Buffer not found on hash queue but a buffer from free list is assigned")
                    if buffer.blockNumber:
                        self.bufferCache.hashQueue.remove(buffer)
                    logger.debug(
                        f'REMOVE BUFFER{buffer} - UPDATED HASH QUEUE:{self.bufferCache.hashQueue}'
                    )
                    buffer.updateBlockNumber(blockNumber)
                    self.bufferCache.hashQueue.add(buffer)
                    return buffer  # block not in hashQ, taken from freelist
            time.sleep(.5)

    def brelse(self, buffer):
        # Todo: wakeup all processes (event: waiting for any buffer to become free;
        self.eventBus.wake(EventBus.EVENT_WAIT_ANY_BUFFER)
        # Todo: wakeup all processes (event: waiting for this buffer to become free;
        self.eventBus.wake(EventBus.EVENT_WAIT_SPECIFIC_BUFFER(buffer))

        # raise processor execution level to block interrupts;
        if buffer.isDataValid() and not buffer.isOld():
            # enqueue buffer at end of free list;
            self.bufferCache.freeList.push(buffer)
        else:
            # enqueue buffer at beginning of free list;
            self.bufferCache.freeList.unshift(buffer)
        # lower processor execution level to allow interrupts;

        buffer.unlock()
        return buffer

    def bread(self, blockNumber):
        logger.info(
            f"bread() - Reading [block number {blockNumber}] into buffer from disk"
        )

        buffer = self.getblk(blockNumber)
        if buffer.isDataValid():
            return buffer
        # initiate disk read
        buffer.setValidData(self.disk.read(buffer.blockNumber))

        return buffer

    def bwrite(self, buffer, synchronous=False):
        # initiate disk write;
        writer = Thread(None, self.disk.writeBuffer, "DISK_IO_WRITE", [buffer])
        writer.start()
        logger.info(
            f"bwrite() - Asynchronously writing buffer {buffer} to disk")

        if not synchronous:  # I/O synchronous
            # sleep (event: I/O complete);
            writer.join()
            self.brelse(buffer)
        elif buffer.isDelayedWrite():
            buffer.setOld(True)
Esempio n. 21
0
 def __init__(self, bufferSize=20, diskSize=100):
     self.bufferCache = BufferCache(bufferSize)
     self.eventBus = EventBus()
     self.worker = Worker()
     self.disk = Disk(diskSize, self.eventBus)
     self.isRunning = False
 def __init__(self, stateregister):
     self.bus = EventBus()
     self.register = stateregister
     self.active_state = None
Esempio n. 23
0
 def __init__(self):
     self.bus = EventBus()
     self.bus.add_event(self.extract_1, "login_finish")
     self.bus.add_event(self.extract_2, "login_finish")
     self.bus.add_event(self.extract_3, "login_finish")
     pass
 def __init__(self, stateregister):
     self.bus = EventBus()
     self.register = stateregister
     self.active_state = None
Esempio n. 25
0
""" Tests performance of executing multiple functions. """
# Modules
import json
import requests
from os import getcwd
from timeit import default_timer as timer
from threading import Thread
from multiprocessing import Process

# Libs
from event_bus import EventBus

bus = EventBus()
EVENT_ONE = 'test_1'
EVENT_TWO = 'test_2'
EVENT_THREE = 'test_3'

# -----------------------------------------------------------------------------

# ------------------------------------------
# CPU Heavy Tests
# ------------------------------------------


def create_event_one():
    for i in range(10):

        @bus.on(event=EVENT_ONE)
        def fib(n):
            if n == 0:
                return 0
Esempio n. 26
0
File: core.py Progetto: zbx91/Dwarf
class Dwarf(object):
    def __init__(self, app_window):
        self.app_window = app_window
        self.app = app_window.get_app_instance()

        self.java_available = False
        self.loading_library = False

        # frida device
        self.device = None

        # process
        self.pid = 0
        self.process = None
        self.script = None

        # kernel
        self.kernel = Kernel(self)

        # hooks
        self.hooks = {}
        self.on_loads = {}
        self.java_hooks = {}
        self.temporary_input = ''
        self.native_pending_args = None
        self.java_pending_args = None

        # context
        self.arch = ''
        self.pointer_size = 0
        self.contexts = {}
        self.context_tid = 0

        # tracers
        self.native_traced_tid = 0

        # core utils
        self.bus = EventBus()
        self.emulator = Emulator(self)
        self.git = Git()
        self.prefs = Prefs()
        self.script_manager = ScriptsManager(self)

        self.keystone_installed = False
        try:
            import keystone.keystone_const
            self.keystone_installed = True
        except:
            pass

    def _get_device(self):
        try:
            self.device = frida.get_usb_device()
        except frida.TimedOutError:
            self.device = None

        if self.device is None:
            # now check for a local device
            try:
                self.device = frida.get_local_device()
            except frida.TimedOutError:
                self.device = None

            if self.device is None:
                return 1
        return 0

    def _reinitialize(self):
        self.java_available = False
        self.loading_library = False

        # frida device
        self.device = None

        # process
        self._spawned = False
        self.pid = 0
        self.process = None
        self.script = None

        # hooks
        self.hooks = {}
        self.on_loads = {}
        self.java_hooks = {}
        self.temporary_input = ''
        self.native_pending_args = None
        self.java_pending_args = None

        # tracers
        self.native_traced_tid = 0

    def device_picked(self, device):
        self.device = device

    def attach(self, pid_or_package, script=None, print_debug_error=True):
        if self.device is None:
            # fallback to usb device
            # can come from -p in args
            err = self._get_device()
            if err > 0:
                return err

        if self.process is not None:
            self.detach()

        try:
            self.process = self.device.attach(pid_or_package)
            self.pid = self.process._impl.pid
            self._spawned = False
        except Exception as e:
            if print_debug_error:
                utils.show_message_box('Failed to attach to %s' % str(pid_or_package), str(e))
            return 2

        self.load_script(script)
        return 0

    def detach(self):
        if self.script is not None:
            self.dwarf_api('_detach')
            self.script.unload()
        if self.process is not None:
            self.process.detach()

    def load_script(self, script=None):
        with open('lib/script.js', 'r') as f:
            s = f.read()
        self.script = self.process.create_script(s)
        self.script.on('message', self.on_message)
        self.script.on('destroyed', self.on_destroyed)
        self.script.load()

        if script is not None:
            self.dwarf_api('evaluateFunction', script)

        self.app_window.on_script_loaded()

    def spawn(self, package, script=None):
        if self.device is None:
            # fallback to usb device
            # can come from -p in args
            err = self._get_device()
            if err > 0:
                return err

        if self.process is not None:
            self.detach()

        try:
            self.pid = self.device.spawn(package)
            self.process = self.device.attach(self.pid)
            self._spawned = True
        except Exception as e:
            utils.show_message_box('Failed to spawn to %s' % package, str(e))
            return 2
        self.load_script(script)
        return 0

    def on_message(self, message, data):
        if 'payload' not in message:
            print(message)
            return

        what = message['payload']
        parts = what.split(':::')
        if len(parts) < 2:
            print(what)
            return

        cmd = parts[0]
        if cmd == 'backtrace':
            if self.app.get_session_ui() is not None and self.app.get_backtrace_panel() is not None:
                try:
                    self.app.get_backtrace_panel().set_backtrace(json.loads(parts[1]))
                except:
                    pass
        elif cmd == 'emulator':
            # on a separate thread to allow js api recursion
            Thread(target=self.emulator.api, args=(parts[1:],)).start()
        elif cmd == 'enumerate_java_classes_start':
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_start()
            if self.app.get_java_trace_panel() is not None:
                self.app.get_java_trace_panel().on_enumeration_start()
        elif cmd == 'enumerate_java_classes_match':
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_match(parts[1])
            if self.app.get_java_trace_panel() is not None:
                self.app.get_java_trace_panel().on_enumeration_match(parts[1])
        elif cmd == 'enumerate_java_classes_complete':
            self.app_window.get_menu().on_java_classes_enumeration_complete()
            if self.app.get_java_classes_panel() is not None:
                self.app.get_java_classes_panel().on_enumeration_complete()
            if self.app.get_java_trace_panel() is not None:
                self.app.get_java_trace_panel().on_enumeration_complete()
        elif cmd == 'enumerate_java_methods_complete':
            self.bus.emit(parts[1], json.loads(parts[2]), parts[1])
        elif cmd == 'ftrace':
            if self.app.get_ftrace_panel() is not None:
                self.app.get_ftrace_panel().append_data(parts[1])
        elif cmd == 'enable_kernel':
            self.app_window.get_menu().enable_kernel_menu()
        elif cmd == 'hook_java_callback':
            h = Hook(Hook.HOOK_JAVA)
            h.set_ptr(1)
            h.set_input(parts[1])
            if self.java_pending_args:
                h.set_condition(self.java_pending_args['condition'])
                h.set_logic(self.java_pending_args['logic'])
                self.java_pending_args = None
            self.java_hooks[h.get_input()] = h
            self.app.get_hooks_panel().hook_java_callback(h)
        elif cmd == 'hook_native_callback':
            h = Hook(Hook.HOOK_NATIVE)
            h.set_ptr(int(parts[1], 16))
            h.set_input(self.temporary_input)
            h.set_bytes(binascii.unhexlify(parts[2]))
            self.temporary_input = ''
            if self.native_pending_args:
                h.set_condition(self.native_pending_args['condition'])
                h.set_logic(self.native_pending_args['logic'])
                self.native_pending_args = None
            self.hooks[h.get_ptr()] = h
            self.app.get_hooks_panel().hook_native_callback(h)
        elif cmd == 'hook_onload_callback':
            h = Hook(Hook.HOOK_ONLOAD)
            h.set_ptr(0)
            h.set_input(parts[1])

            self.on_loads[parts[1]] = h
            if self.app.session_ui is not None and self.app.get_hooks_panel() is not None:
                self.app.get_hooks_panel().hook_onload_callback(h)
        elif cmd == 'java_trace':
            panel = self.app.get_java_trace_panel()
            if panel is None:
                panel = self.app.get_session_ui().add_dwarf_tab(SessionUi.TAB_JAVA_TRACE)
            panel.on_event(parts[1], parts[2], parts[3])
        elif cmd == 'log':
            self.app.get_console_panel().get_js_console().log(parts[1])
        elif cmd == 'memory_scan_match':
            self.bus.emit(parts[1], parts[2], json.loads(parts[3]))
        elif cmd == 'memory_scan_complete':
            self.app_window.get_menu().on_bytes_search_complete()
            self.bus.emit(parts[1] + ' complete', 0, 0)
        elif cmd == 'onload_callback':
            self.loading_library = parts[1]
            self.app.get_console_panel().get_js_console().log('hook onload %s @thread := %s' % (
                parts[1], parts[3]))
            self.app.get_hooks_panel().hit_onload(parts[1], parts[2])
        elif cmd == 'release':
            if parts[1] in self.contexts:
                del self.contexts[parts[1]]
            self.app.on_tid_resumed(int(parts[1]))
        elif cmd == 'set_context':
            data = json.loads(parts[1])
            if 'context' in data:
                context = Context(data['context'])
                self.contexts[str(data['tid'])] = context

                sym = ''
                if 'pc' in context.__dict__:
                    name = data['ptr']
                    if context.pc.symbol_name is not None:
                        sym = '(%s - %s)' % (context.pc.symbol_module_name, context.pc.symbol_name)
                else:
                    name = data['ptr']
                self.app.get_contexts_lists_panel().add_context(data, library_onload=self.loading_library)
                # check if data['reason'] is 0 (REASON_HOOK)
                if self.loading_library is None and data['reason'] == 0:
                    self.log('hook %s %s @thread := %d' % (name, sym, data['tid']))
                if len(self.contexts.keys()) > 1 and self.app.get_context_panel().have_context():
                    return
                self.app.get_session_ui().request_session_ui_focus()
            else:
                self.arch = data['arch']
                self.pointer_size = data['pointerSize']
                self.java_available = data['java']
                self.app.get_console_panel().get_js_console().log('injected into := ' + str(self.pid))
                self.app_window.on_context_info()

            self.context_tid = data['tid']
            self.app.apply_context(data)
            if self.loading_library is not None:
                self.loading_library = None
        elif cmd == 'set_data':
            key = parts[1]
            if data:
                self.app.get_data_panel().append_data(key, hexdump(data, result='return'))
            else:
                self.app.get_data_panel().append_data(key, str(parts[2]))
        elif cmd == 'script_loaded':
            if self._spawned:
                self.device.resume(self.pid)
        elif cmd == 'tracer':
            panel = self.app.get_trace_panel()
            if panel is None:
                panel = self.app.get_session_ui().add_dwarf_tab(SessionUi.TAB_TRACE)
            if panel is not None:
                # safely checked later
                panel.start()

                trace_events_parts = parts[1].split(',')
                while len(trace_events_parts) > 0:
                    t = TraceEvent(trace_events_parts.pop(0),
                                   trace_events_parts.pop(0),
                                   trace_events_parts.pop(0),
                                   trace_events_parts.pop(0))
                    panel.event_queue.append(t)
        elif cmd == 'unhandled_exception':
            # todo
            pass
        elif cmd == 'update_modules':
            self.app.apply_context({'tid': parts[1], 'modules': json.loads(parts[2])})
        elif cmd == 'update_ranges':
            self.app.apply_context({'tid': parts[1], 'ranges': json.loads(parts[2])})
        elif cmd == 'watcher':
            exception = json.loads(parts[1])
            self.log('watcher hit op %s address %s @thread := %s' %
                     (exception['memory']['operation'], exception['memory']['address'], parts[2]))
        elif cmd == 'watcher_added':
            if self.app.get_watchers_panel() is not None:
                self.app.get_watchers_panel().add_watcher_callback(parts[1])
        elif cmd == 'watcher_removed':
            if self.app.get_watchers_panel() is not None:
                self.app.get_watchers_panel().remove_watcher_callback(parts[1])
        else:
            print(what)

    def on_destroyed(self):
        self._reinitialize()

        if self.app.get_console_panel() is not None:
            self.app.get_console_panel().get_js_console().log('detached from %d. script destroyed' % self.pid)
        self.app_window.on_script_destroyed()

    def add_watcher(self, ptr=None):
        if ptr is None:
            ptr, input = InputDialog.input_pointer(self.app)
            if ptr == 0:
                return
        return self.dwarf_api('addWatcher', ptr)

    def dump_memory(self, file_path=None, ptr=0, length=0):
        if ptr == 0:
            ptr, inp = InputDialog.input_pointer(self.app)
        if ptr > 0:
            if length == 0:
                accept, length = InputDialog.input(
                    self.app, hint='insert length', placeholder='1024')
                if not accept:
                    return
                try:
                    if length.startswith('0x'):
                        length = int(length, 16)
                    else:
                        length = int(length)
                except:
                    return
            if file_path is None:
                r = QFileDialog.getSaveFileName(self.app, caption='Save binary dump to file')
                if len(r) == 0 or len(r[0]) == 0:
                    return
                file_path = r[0]
            data = self.read_memory(ptr, length)
            with open(file_path, 'wb') as f:
                f.write(data)

    def dwarf_api(self, api, args=None, tid=0):
        if tid == 0:
            tid = self.context_tid
        if args is not None and not isinstance(args, list):
            args = [args]
        if self.script is None:
            return None
        try:
            return self.script.exports.api(tid, api, args)
        except Exception as e:
            self.app.get_console_panel().get_js_console().log(str(e))
            return None

    def hook_java(self, input=None, pending_args=None):
        if input is None or not isinstance(input, str):
            accept, input = InputDialog.input(
                self.app, hint='insert java class or methos',
                placeholder='com.package.class or com.package.class.method')
            if not accept:
                return
        self.java_pending_args = pending_args
        input = input.replace(' ', '')
        self.app.dwarf_api('hookJava', input)

    def hook_native(self, input=None, pending_args=None):
        if input is None or not isinstance(input, str):
            ptr, input = InputDialog.input_pointer(self.app)
        else:
            ptr = utils.parse_ptr(self.app.dwarf_api('evaluatePtr', input))
        if ptr > 0:
            self.temporary_input = input
            self.native_pending_args = pending_args
            self.app.dwarf_api('hookNative', ptr)

    def hook_onload(self, input=None):
        if input is None or not isinstance(input, str):
            accept, input = InputDialog.input(self.app, hint='insert module name', placeholder='libtarget.so')
            if not accept:
                return
            if len(input) == 0:
                return

        if not input.endswith('.so'):
            input += '.so'

        if input in self.app.get_dwarf().on_loads:
            return

        self.dwarf_api('hookOnLoad', input)

    def log(self, what):
        if self.app.get_console_panel() is not None:
            self.app.get_console_panel().get_js_console().log(what)

    def native_tracer_start(self, tid=0):
        if self.native_traced_tid > 0:
            return
        if tid == 0:
            accept, tid = InputDialog.input(self.app, hint='insert thread id to trace', placeholder=str(self.pid))
            if not accept:
                return
            try:
                if tid.startswith('0x'):
                    tid = int(tid, 16)
                else:
                    tid = int(tid)
            except:
                return
        self.native_traced_tid = tid
        self.app.dwarf_api('startNativeTracer', [tid, True])
        if self.app.get_trace_panel() is None:
            self.app.get_session_ui().add_dwarf_tab('trace', request_focus=True)
        self.app_window.get_menu().on_native_tracer_change(True)

    def native_tracer_stop(self):
        if self.native_traced_tid == 0:
            return
        self.dwarf_api('stopNativeTracer')
        if self.app.get_trace_panel() is not None:
            self.app.get_trace_panel().stop()
        self.native_traced_tid = 0
        self.app_window.get_menu().on_native_tracer_change(False)

    def read_memory(self, ptr, len):
        if len > 1024 * 1024:
            position = 0
            next_size = 1024 * 1024
            data = bytearray()
            while True:
                try:
                    data += self.dwarf_api('readBytes', [ptr + position, next_size])
                except:
                    return None
                position += next_size
                diff = len - position
                if diff > 1024 * 1024:
                    next_size = 1024 * 1024
                elif diff > 0:
                    next_size = diff
                else:
                    break
            ret = bytes(data)
            del data
            return ret
        else:
            return self.dwarf_api('readBytes', [ptr, len])

    def remove_watcher(self, ptr):
        return self.dwarf_api('removeWatcher', ptr)

    ###########
    #         #
    # getters #
    #         #
    ###########

    def get_bus(self):
        return self.bus

    def get_emulator(self):
        return self.emulator

    def get_git(self):
        return self.git

    def get_kernel(self):
        return self.kernel

    def get_loading_library(self):
        return self.loading_library

    def get_native_traced_tid(self):
        return self.native_traced_tid

    def get_prefs(self):
        return self.prefs

    def get_scripts_manager(self):
        return self.script_manager
Esempio n. 27
0
    def analyze(self, video_path, output_video_path, user_id):

        # Create opencv video capture object
        cap = cv2.VideoCapture(video_path)
        writer = None

        # Variables initialization
        skip_frame_count = 0
        track_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0),
                        (0, 255, 255), (255, 0, 255), (255, 127, 255),
                        (127, 0, 255), (127, 0, 127)]

        tracks_dictionary = {}

        frame_count = 0
        # Infinite loop to process video frames
        while (True):
            # Capture frame-by-frame
            ret, frame = cap.read()

            if frame is None:
                break

            # necessary to create dictionary
            frame_count += 1

            # Make copy of original frame
            orig_frame = copy.copy(frame)

            # ???????????
            # Skip initial frames that display logo
            if (skip_frame_count < 15):
                skip_frame_count += 1
                continue

            # Detect and return centeroids of the objects in the frame
            centers = self.object_detector.detect(frame)

            # If centroids are detected then track them
            if (len(centers) > 0):

                # Track object using Kalman Filter
                self.object_tracker.update(centers)

                # For identified object tracks draw tracking line
                # Use various colors to indicate different track_id
                for i in range(len(self.object_tracker.tracks)):

                    # check if track is in my dictionary or need to add it
                    if self.object_tracker.tracks[i].track_id not in tracks_dictionary:
                        tracks_dictionary[self.object_tracker.tracks[i].track_id] = {}

                    if (len(self.object_tracker.tracks[i].trace) > 1):
                        # Add frame and coordinates this track on this frame
                        # For Unscented Kalman Filter
                        tracks_dictionary[self.object_tracker.tracks[i].track_id][frame_count] = [
                            self.object_tracker.tracks[i].trace[len(self.object_tracker.tracks[i].trace) - 1][0],
                            self.object_tracker.tracks[i].trace[len(self.object_tracker.tracks[i].trace) - 1][1]]

                        for j in range(len(self.object_tracker.tracks[i].trace) - 1, 1, -1):
                            # For Unscented Kalman Filter
                            x1 = self.object_tracker.tracks[i].trace[j][0]
                            y1 = self.object_tracker.tracks[i].trace[j][1]
                            x2 = self.object_tracker.tracks[i].trace[j - 1][0]
                            y2 = self.object_tracker.tracks[i].trace[j - 1][1]
                            clr = self.object_tracker.tracks[i].track_id % 9
                            cv2.line(frame, (int(x1), int(y1)), (int(x2), int(y2)), track_colors[clr], 2)

                            text = "{}".format(self.object_tracker.tracks[i].track_id)
                            cv2.putText(frame, text, (int(self.object_tracker.tracks[i].trace[len(self.object_tracker.tracks[i].trace) - 1][0]),
                                                      int(self.object_tracker.tracks[i].trace[len(self.object_tracker.tracks[i].trace) - 1][
                                                              1] + 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

                frame = cv2.resize(frame, (1250, 750))
                cv2.imshow('Tracking', frame)
                # check if the video writer is None
                if writer is None:
                    # initialize our video writer
                    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
                    writer = cv2.VideoWriter(output_video_path, fourcc, 30, (frame.shape[1], frame.shape[0]),
                                             True)

                # write the output frame to disk
                writer.write(frame)

            #cv2.waitKey(50)

            # Check for key strokes
            #k = cv2.waitKey(50) & 0xff
            #if k == 27:  # 'esc' key has been pressed, exit program.
            #    break
            #if k == 112:  # 'p' has been pressed. this will pause/resume the code.
            #    pause = not pause
            #    if (pause is True):
            #        print("Code is paused. Press 'p' to resume..")
            #        while (pause is True):
            #            # stay in this loop until
            #            key = cv2.waitKey(30) & 0xff
            #            if key == 112:
            #                pause = False
            #                print("Resume code..!!")
            #                break

        graphic_builder = GraphicBuilder(tracks_dictionary)
        graphic_builder.build()

        finish_video_processing_event = FinishVideoProcessingEvent(output_video_path, graphic_builder.tracks_dictionary, graphic_builder.imagePaths, user_id)

        event_bus = EventBus(finish_video_processing_event)
        event_bus.publish()

        cap.release()
        writer.release()
        cv2.destroyAllWindows()
Esempio n. 28
0
 def __init__(self, processes=[]):
     super(Worker, self).__init__(daemon=True)
     self.queued = processes
     self.started = []
     self.eventBus = EventBus()