def __init__(self, app):
		QMainWindow.__init__(self)

		self.app = app
		authenticated = False

		self.terminator = Terminator.getInstance()

		if os.path.exists(userConfig):
			logging.debug('Authenticating on saved user details')
			authenticated, msg = self.authenticateUserDetails()

		if not authenticated:
			logging.debug('Not already logged in, presenting dialog.')
			loginDialog = LoginDialog()
			authenticated, msg, userID, key = loginDialog.getUserData()

			self.saveUserDetails(userID, key)

		if authenticated:
			logging.debug('User authenticated, initialising tabs.')
			tabs = MainWindowTabs(app)
			self.setCentralWidget(tabs)
		else:
			self.closeEvent(None)
    def __init__(self, feedID, clientKey):
        Thread.__init__(self)
        self.setName("FeedHandler")
        self.terminator = Terminator.getInstance()

        self.certHandler = CertificateHandler(feedID, 'server')
        self.certHandler.prep()

        self.publicKey, privateKey = self.certHandler.getKeyPair()
        clientsPath = self.certHandler.getEnrolledKeysPath()

        self.ctxHandler = ContextHandler(clientsPath)
        context = self.ctxHandler.getContext()

        self.saveClientKey(clientKey)

        self.socket = context.socket(zmq.REP)
        self.socket.setsockopt(zmq.RCVTIMEO, 20000)

        monitorSocket = self.socket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, feedID)
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.socket.curve_secretkey = privateKey
        self.socket.curve_publickey = self.publicKey
        self.socket.curve_server = True

        self.port = self.socket.bind_to_random_port('tcp://*',
                                                    min_port=49151,
                                                    max_port=65535)
        self.feedID = feedID
Exemplo n.º 3
0
    def __init__(self, addr):
        Thread.__init__(self)
        self.setName("FeedListener")
        self.terminator = Terminator.getInstance()

        certHandler = CertificateHandler("front", 'server')

        publicKey, privateKey = certHandler.getKeyPair()
        clientsPath = certHandler.getEnrolledKeysPath()

        self.ctxHandler = ContextHandler(clientsPath)
        context = self.ctxHandler.getContext()
        self.socket = context.socket(zmq.REP)

        monitorSocket = self.socket.get_monitor_socket()
        monitor = Monitor(monitorSocket, 'front')
        monitor.setDaemon(True)
        monitor.start()

        self.socket.curve_secretkey = privateKey
        self.socket.curve_publickey = publicKey
        self.socket.curve_server = True

        self.socket.bind(addr)
        self.socket.setsockopt(zmq.RCVTIMEO, 10000)
        logging.info('Socket setup, public key: %s', publicKey)
 def __init__(self, parent):
     Thread.__init__(self)
     self.parent = parent
     self.terminator = Terminator.getInstance()
     self.moveTopConnector = GenericConnector(self.parent.moveTop)
     self.progressBarSetup = GenericConnector(self.parent.setupProgressBar)
     self.progressBarUpdate = GenericConnector(
         self.parent.updateProgressBar)
    def __init__(self):
        Thread.__init__(self)
        self.terminator = Terminator.getInstance()
        self.setName("Authenticator")

        try:
            self.db = sql.connect(os.environ["DBHOST"], os.environ["DBUSER"],
                                  os.environ["DBPASS"], os.environ["DBNAME"])

            self.cursor = self.db.cursor()

            logging.debug('Connected to database')
        except:
            logging.critical('Exception occured connecting to database',
                             exc_info=True)
            self.terminator.autoTerminate()

        try:
            certHandler = CertificateHandler("front", 'server')
            publicKey, privateKey = certHandler.getKeyPair()
            clientKeysPath = certHandler.getEnrolledKeysPath()

            self.ctxHandler = ContextHandler(clientKeysPath)
            context = self.ctxHandler.getContext()
            self.socket = context.socket(zmq.REP)

            try:
                monitorSocket = self.socket.get_monitor_socket()
                self.monitor = Monitor(monitorSocket, 'Registration')
                self.monitor.setDaemon(True)
                self.monitor.start()

                logging.debug('Socket monitor thread started')
            except:
                logging.error(
                    'Exception occured starting socket monitor thread',
                    exc_info=True)

            self.socket.curve_secretkey = privateKey
            self.socket.curve_publickey = publicKey
            self.socket.curve_server = True
            self.socket.setsockopt(zmq.RCVTIMEO, 10000)
            self.socket.bind('tcp://0.0.0.0:5001')

            logging.debug('Socket setup and bound, listening')
        except:
            logging.critical('Exception occured setting up auth socket',
                             exc_info=True)
    def logout(self):
        terminator = Terminator.getInstance()

        dialog = QMessageBox()
        dialog.setWindowTitle("Logout")
        dialog.setText("Are you sure?")
        yesButton = QMessageBox.Yes
        dialog.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)

        ans = dialog.exec()

        if 'Yes' in dialog.clickedButton().text():
            os.remove('../userDetails.config')
            terminator.autoTerminate()
            self.app.exit()
            sys.exit
Exemplo n.º 7
0
	def run(self):
		terminator = Terminator.getInstance()

		while not terminator.isTerminating():
			currentDisplayData = self.parent.drawSpace.painter.data[0]

			soundsToPlay = []
			msg = 'Locations Detected:'
			for level in currentDisplayData:
				lvlMsg = '\n\tLevel ' + str(level.levelID)
				camMsgs = ''
				for camera in level.cameras:
					if camera.alert is True:
						if camera.alerted is False:
							self.parent.drawSpace.controls.setMainFeedID(camera)
							camera.alerted = True
							soundsToPlay.append(camera.soundPath)

							alertMsg = (
								'Weapon detected at level ' +
								str(camera.levelID) + ' ' +
								camera.location)

							alerter = Thread(target=self.sendAlert, args=[alertMsg])
							alerter.start()

						camMsgs += '\n\t\t' + camera.location
					elif camera.alerted is True:
						camera.alerted = False
				if camMsgs != '':
					msg += lvlMsg + camMsgs + '\n'

			if len(soundsToPlay) > 0:
				self.alertDialog.emitSignal(msg)
				self.playUntilDone(self.playlist[self.index.get(self.detectedPath)])

			count = 0
			for path in soundsToPlay:
				self.playUntilDone(self.playlist[self.index.get(path)])

				if count == len(soundsToPlay) - 2:
					self.playUntilDone(self.playlist[self.index.get(self.andPath)])

				count += 1

			time.sleep(0.1)
	def run(self):
		terminator = Terminator.getInstance()
		while not terminator.isTerminating():
			try:
				msg = recv_monitor_message(self.socket)
			except:
				break
			event = msg.get("event")
			value = msg.get("value")
			endpoint = msg.get("endpoint")

			assigned = False
			for key, val in self.events.items():
				if event == val:
					assigned = True
					logging.debug(str(key) + ' ' + str(endpoint))

			if assigned is False:
				logging.error('Unknown monitor message: %s', msg)

		logging.debug('Monitor thread %s shutting down', self.feedID)
    def __init__(self, feedListener, authListener):
        Thread.__init__(self)
        self.setName("Enroller")

        self.feedListener = feedListener
        self.authListener = authListener

        self.terminator = Terminator.getInstance()

        self.unsecuredCtx = zmq.Context()
        self.unsecuredSocket = self.unsecuredCtx.socket(zmq.REP)

        monitorSocket = self.unsecuredSocket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, "Enroller")
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.unsecuredSocket.bind('tcp://0.0.0.0:5002')
        self.unsecuredSocket.setsockopt(zmq.RCVTIMEO, 10000)

        self.certHandler = CertificateHandler('front', 'server')
        self.publicKey, _ = self.certHandler.getKeyPair()

        self.publicKey = self.publicKey.decode('utf-8')
        def run(self):
            terminator = Terminator.getInstance()

            while not terminator.isTerminating():
                self.ready = True
                self.chartConn.emitSignal(None)
                self.timeConn.emitSignal(('0:00:00', '0:00:00', 0.0))
                self.displayConn.emitFrame(None)
                self.titleConn.emitSignal('')

                while self.path is None or self.resultPath is None:
                    time.sleep(0.01)

                self.titleConn.emitSignal(os.path.basename(self.path))

                self.ready = False
                self.reset = False
                self.feed = cv2.VideoCapture(self.path)

                videoTotal = self.feed.get(cv2.CAP_PROP_FRAME_COUNT)
                fps = self.feed.get(cv2.CAP_PROP_FPS)

                seconds = int(videoTotal / fps)
                vidLength = datetime.timedelta(seconds=seconds)

                resultsFile = open(self.resultPath, 'rb')
                resultsData = pickle.load(resultsFile)[::-1]
                resultsFile.close()

                self.chartConn.emitSignal((resultsData, videoTotal, fps))

                fpsTimer = time.time()
                self.frameCount = 0
                self.lastFrame = -1

                fph = FeedProcessHelper()

                while self.feed.isOpened() and self.reset is False:
                    # syncing results index with display frame
                    if self.frameCount != self.lastFrame + 1:
                        self.feed.set(cv2.CAP_PROP_POS_FRAMES, self.frameCount)
                        self.lastFrame = self.frameCount - 1

                    if self.feed.get(
                            cv2.CAP_PROP_POS_FRAMES) != self.frameCount:
                        self.feed.set(cv2.CAP_PROP_POS_FRAMES, self.frameCount)
                        logging.debug('Correcting frame position')

                    while time.time(
                    ) - fpsTimer < 1 / fps or self.pause is True:
                        time.sleep(0.01)

                    fpsTimer = time.time()
                    check, frame = self.feed.read()

                    if check:
                        results = resultsData[self.frameCount]
                        self.frameCount += 1
                        self.lastFrame += 1

                        classifications = results[0]
                        boundingBoxes = results[1]

                        regionResults = results[2][0]
                        drawCoords = results[2][1]

                        fph.drawResults(frame,
                                        regionResults,
                                        drawCoords, ["Weapon", "Weapon"],
                                        invert=True)

                        frame = cv2.resize(frame, (1280, 720))
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

                        if boundingBoxes != []:
                            height, width, _ = np.shape(frame)
                            for box in boundingBoxes:
                                xf, yf, wf, hf = box
                                x = int(self.scale(xf, 0, 1, 0, width))
                                y = int(self.scale(yf, 0, 1, 0, height))
                                w = int(self.scale(wf, 0, 1, 0, width))
                                h = int(self.scale(hf, 0, 1, 0, height))

                                cv2.rectangle(frame, (x, y), (x + w, y + h),
                                              (255, 255, 0), 2)

                        seconds = int(self.frameCount / fps)
                        curTime = datetime.timedelta(seconds=seconds)

                        self.timeConn.emitSignal(
                            (curTime, vidLength, classifications))
                        self.displayConn.emitFrame(frame)
                        self.pointConn.emitSignal(self.frameCount)
                    else:
                        self.feed.set(cv2.CAP_PROP_POS_FRAMES, 0)
                        self.frameCount = 0
                        self.lastFrame = -1

                self.feed.release()
Exemplo n.º 11
0
        elif mode == 'critical':
            loggerMode = logging.CRITICAL
    else:
        loggerMode = logging.INFO

    logging.basicConfig(
        format='%(levelname)s - %(asctime)s - %(threadName)s - %(message)s',
        level=loggerMode,
        handlers=[
            logging.FileHandler('../Logs/server_logs.txt'),
            logging.StreamHandler(sys.stdout)
        ])

    logging.info('\n\n\n\t\tBegin new set of logs:\n\n\n')

    terminator = Terminator.getInstance()

    certHandler = CertificateHandler('front', 'server')
    certHandler.prep()

    try:
        modelHandler = ModelHandler.getInstance()
    except:
        logging.critical('Exception loading model/graph', exc_info=True)
        terminator.autoTerminate()

    try:
        feedListener = FeedListener('tcp://0.0.0.0:5000')
        feedListener.setDaemon(True)
        feedListener.start()
        logging.debug('Feed listener thread started')
Exemplo n.º 12
0
    def run(self):
        if self.camera is not None:
            feedID = str(self.camera.camID)
        elif self.deferredMode or self.imageMode:
            feedID = self.filePath

        feedID = feedID.replace(' ', '')
        feedID = feedID.replace('/', '')
        feedID = feedID.replace('\\', '')
        feedID = feedID.replace(':', '')
        feedID = feedID.replace('.', '')
        feedID = feedID + str(uuid.uuid4().hex)

        socket, certHandler, ctxHandler = self.initialize(feedID)

        monitorSocket = socket.get_monitor_socket()
        monitor = Monitor(monitorSocket, feedID)
        monitor.setDaemon(True)
        monitor.start()

        terminator = Terminator.getInstance()
        deferredTimer = time.time()

        results = []

        notify = Notify()

        # fpsTimer = time.time()
        # showEvery = 1
        # count = 0
        # recordCount = 0

        # fps = []
        # times = []

        # cutoffTimer = time.time()

        fph = FeedProcessHelper()

        while not terminator.isTerminating() and self.end is False or (
                self.end is True and len(self.frames) > 0):
            if self.nextFrame is not None or len(self.frames) > 0:
                self.total += 1
                deferredTimer = time.time()

                if self.deferredMode or self.imageMode:
                    frame = self.frames.pop()
                else:
                    frame = self.nextFrame
                    self.nextFrame = None

                processFrame = frame[0]
                displayFrame = frame[1]
                socket.send(processFrame)

                serial = socket.recv()

                result = pickle.loads(serial)

                if result[
                        0] > 0.95 and not self.deferredMode and not self.imageMode:
                    regionResults = result[2][0]
                    drawCoords = result[2][1]
                    fph.drawResults(displayFrame, regionResults, drawCoords,
                                    ["Weapon", "Weapon"])

                    self.camera.alert = True
                    alertTimer = time.time()
                    self.updateConnector.emitSignal()

                    # if notify is not None and self.camera.alerted is False:
                    # 		notify.send(
                    # 			'Weapon detected at level ' +
                    # 			str(self.camera.levelID) + ' ' +
                    # 			self.camera.location)

                if not self.deferredMode and not self.imageMode:
                    if self.camera.alert:
                        if time.time() - alertTimer >= 30:
                            self.camera.alert = False
                            self.updateConnector.emitSignal()

                if not self.imageMode:
                    boundingBoxes = result[1]

                    if boundingBoxes != []:
                        height, width, _ = np.shape(displayFrame)
                        for box in boundingBoxes:
                            x, y, w, h = box
                            x = int(self.scale(x, 0, 1, 0, width))
                            y = int(self.scale(y, 0, 1, 0, height))
                            w = int(self.scale(w, 0, 1, 0, width))
                            h = int(self.scale(h, 0, 1, 0, height))

                            cv2.rectangle(displayFrame, (x, y), (x + w, y + h),
                                          (255, 255, 0), 2)

                # # if this display is the main, emit the frame signal to both displays

                if not self.deferredMode and not self.imageMode:
                    mainCam = self.layoutHandler.drawSpace.controls.getSelectedCamera(
                    )
                    if mainCam is not None:
                        if self.camera.camID == mainCam.camID:
                            self.mainDisplayConn.emitFrame(displayFrame)
                            # self.mainDisplay.camera = self.camera
                else:
                    results.append(result)

                # count += 1
                # if (time.time() - fpsTimer) > showEvery:
                # 	curFPS = count / (time.time() - fpsTimer)
                # 	fps.append(curFPS)
                # 	times.append(recordCount)
                # 	recordCount += 1
                # 	count = 0
                # 	fpsTimer = time.time()

                # 	if time.time() - cutoffTimer > 1200:
                # 		perfData = (fps, times)

                # 		with open('./performanceData' + self.camera.location, 'wb') as fp:
                # 			pickle.dump(perfData, fp, protocol=4)

                # sys.exit()

            elif (time.time() - deferredTimer) > 5 and self.deferredMode:
                socket.send_string("wait")
                timer = time.time()
                received = socket.recv_string()
                if received != 'ok':
                    logging.error('Unexcepted message %s', received)
            else:
                time.sleep(0.001)

        if self.deferredMode or self.imageMode:
            with open('../data/processed/' + self.filePath + '.result',
                      'wb') as fp:
                pickle.dump(results, fp, protocol=4)

        # socket.disable_monitor()
        try:
            socket.close()
            # ctxHandler.cleanup()
            certHandler.cleanup()
        except Exception as e:
            logging.debug("exception while ending")
Exemplo n.º 13
0
def run():
    modelFile = easygui.fileopenbox('Choose a model')
    model = tf.keras.models.load_model(modelFile)
    model.summary()

    fph = FeedProcessHelper()
    resultsHandler = ResultsHandler(16, 5)

    options = ['Video', 'Camera', 'Image']
    choice = options[easygui.indexbox(msg='What do you want to use?',
                                      choices=options)]

    if choice == 'Video':
        videoFile = easygui.fileopenbox('Choose a video')
        feed = cv2.VideoCapture(videoFile)
    elif choice == 'Camera':
        feed = cv2.VideoCapture(0)
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        uniqueID = str(uuid.uuid4().hex)
        outStream = cv2.VideoWriter('../Samples/sampleVideo' + uniqueID +
                                    '.mp4',
                                    fourcc,
                                    fps=24.0,
                                    frameSize=(640, 480))

    elif choice == 'Image':
        imageFile = easygui.fileopenbox('Choose an image')
        image = cv2.imread(imageFile)

        regions, drawCoords = fph.extractRegions(image, 4, (64, 64))
        results = np.around(model.predict(regions)[:, 10:], decimals=3)
        image = fph.drawResults(image, results, drawCoords,
                                ["Weapon", "Weapon"])

        cv2.imshow("image", image)
        cv2.waitKey(0)
        sys.exit()

    FPS = feed.get(cv2.CAP_PROP_FPS)
    terminator = Terminator.getInstance()
    fpsTimer = time.time()
    while feed.isOpened() and not terminator.isTerminating():
        while time.time() - fpsTimer < 1 / FPS:
            time.sleep(0.001)

        fpsTimer = time.time()

        check, frame = feed.read()

        if check:
            if choice == 'Camera':
                outStream.write(frame)
            regions, drawCoords = fph.extractRegions(frame, 4, (64, 64))

            results = np.around(model.predict(regions)[:, 10:], decimals=3)
            resultsHandler.append(results)
            frame = fph.drawResults(frame, resultsHandler.getAverages(),
                                    drawCoords, ["Weapon", "Weapon"])
            cv2.imshow("feed", frame)
            cv2.waitKey(1)
        else:
            feed.set(cv2.CAP_PROP_POS_FRAMES, 0)

    if choice == 'camera':
        outStream.release()