def __init__(self, parent): Thread.__init__(self) self.parent = parent self.updateLayout = GenericConnector(self.parent.updateLayout) self.alertDialog = GenericConnector(self.parent.showAlertDialog) self.index = {} self.playlist = [] soundsPath = '../data/Sounds' files = os.listdir(soundsPath) fullPaths = [] for file in files: path = soundsPath + '/' + file fullPaths.append(path) count = 0 for file in fullPaths: media = QUrl.fromLocalFile(file) self.playlist.append(media) self.index[file] = count count += 1 self.player = QMediaPlayer() self.player.stateChanged.connect(self.mediaStateChange) self.finished = True self.soundsPath = '../data/Sounds' self.detectedPath = soundsPath + '/weapondetected.mp3' self.andPath = soundsPath + '/and.mp3' self.notify = Notify()
def __init__(self, parent): QWidget.__init__(self) self.playing = True self.title = QLabel() font = QFont('Helvetica', 18) self.title.setFont(font) self.title.setAlignment(Qt.AlignCenter) self.surface = QLabel() self.placeholder = data_handler.getLabelledPixmap( 1280, 720, "Click on something in the ready list\nto view it here.", path='../data/placeholder.png') self.surface.setSizePolicy( QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)) self.setSizePolicy( QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)) self.surface.setPixmap(self.placeholder) self.timeDisplay = QLabel("0:00:00 / 0:00:00") font = QFont('Helvetica', 12) self.timeDisplay.setFont(font) self.controls = QHBoxLayout() self.controls.setAlignment(Qt.AlignCenter) self.togglePlay = QPushButton('Play/Pause') self.togglePlay.clicked.connect(self.toggle) self.togglePlay.setSizePolicy( QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)) self.resultDisplay = QLabel() self.resultDisplay.setFont(font) self.resultDisplay.setAlignment(Qt.AlignRight) self.resultDisplay.setMinimumWidth(50) self.resultDisplay.setMaximumWidth(50) self.controls.addWidget(self.timeDisplay) self.controls.addWidget(self.togglePlay) self.controls.addWidget(self.resultDisplay) self.analyser = self.Analyser(self) self.analyser.setDaemon(True) self.analyser.start() self.pause = GenericConnector(self.analyser.pausefeed) self.play = GenericConnector(self.analyser.playfeed) self.chart = self.Chart(self) outerLayout = QVBoxLayout() outerLayout.addWidget(self.title) outerLayout.addWidget(self.surface) outerLayout.addLayout(self.controls) outerLayout.addWidget(self.chart) self.setLayout(outerLayout)
def __init__(self, parent): QLabel.__init__(self) self.data = None self.padding = 10 self.leftPadding = 30 self.maxX = None self.currentPoints = None self.closestPoint = None self.currentPoint = 0 self.seek = GenericConnector(parent.analyser.seek) self.setMouseTracking(True) self.setSizePolicy( QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
def __init__(self, parent): QWidget.__init__(self) self.parent = parent self.setSizePolicy( QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)) files = os.listdir('../data/processed/') fullPaths = [] for file in files: fullPath = '../data/processed/' + file fullPaths.append(fullPath) files = self.parent.sortFiles(fullPaths) files = self.parent.addPreviews(files, ready=True) self.stopFeedConn = GenericConnector( self.parent.viewer.analyser.resetFeed) self.update()
def __init__(self, display): Thread.__init__(self) self.basePath = '../data/processed/' self.path = None self.resultPath = None self.frameCount = 0 self.displayConn = DisplayConnector(display) self.chartConn = GenericConnector(display.resetChart) self.pointConn = GenericConnector(display.updateChart) self.timeConn = GenericConnector(display.updateTimeDisplay) self.titleConn = GenericConnector(display.setTitle) self.reset = False self.ready = True self.pause = False
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, camera=None, display=None, mainDisplay=None, deferredMode=False, filePath=None, layoutHandler=None, imageMode=False): Thread.__init__(self) self.layoutHandler = layoutHandler if self.layoutHandler is not None: self.updateConnector = GenericConnector(layoutHandler.updateLayout) self.end = False self.frames = deque() self.nextFrame = None self.camera = camera self.display = display self.deferredMode = deferredMode self.imageMode = imageMode self.filePath = filePath if display is not None: self.displayConn = DisplayConnector(display) self.mainDisplayConn = DisplayConnector(mainDisplay) self.mainDisplay = mainDisplay self.serverAddr = 'tcp://35.204.135.105' self.localAddr = 'tcp://127.0.0.1' self.mainAddr = self.serverAddr self.initPort = ':5000' self.total = 0
class AlertWatcher(Thread): def __init__(self, parent): Thread.__init__(self) self.parent = parent self.updateLayout = GenericConnector(self.parent.updateLayout) self.alertDialog = GenericConnector(self.parent.showAlertDialog) self.index = {} self.playlist = [] soundsPath = '../data/Sounds' files = os.listdir(soundsPath) fullPaths = [] for file in files: path = soundsPath + '/' + file fullPaths.append(path) count = 0 for file in fullPaths: media = QUrl.fromLocalFile(file) self.playlist.append(media) self.index[file] = count count += 1 self.player = QMediaPlayer() self.player.stateChanged.connect(self.mediaStateChange) self.finished = True self.soundsPath = '../data/Sounds' self.detectedPath = soundsPath + '/weapondetected.mp3' self.andPath = soundsPath + '/and.mp3' self.notify = Notify() 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 sendAlert(self, msg): self.notify.send(msg) def playUntilDone(self, media): self.finished = False self.player.setMedia(media) self.player.play() while not self.finished: time.sleep(0.01) def mediaStateChange(self, state): if state == QMediaPlayer.StoppedState: self.finished = True
class Analyser(Thread): def __init__(self, display): Thread.__init__(self) self.basePath = '../data/processed/' self.path = None self.resultPath = None self.frameCount = 0 self.displayConn = DisplayConnector(display) self.chartConn = GenericConnector(display.resetChart) self.pointConn = GenericConnector(display.updateChart) self.timeConn = GenericConnector(display.updateTimeDisplay) self.titleConn = GenericConnector(display.setTitle) self.reset = False self.ready = True self.pause = False 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() def scale(self, val, inMin, inMax, outMin, outMax): return ((val - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin def set(self, contentType, name): self.resetFeed() self.path = self.basePath + name self.resultPath = self.basePath + name + '.result' def resetFeed(self): self.path = None self.resultPath = None self.reset = True self.pause = False while self.ready is False: time.sleep(0.001) def seek(self, pos): self.frameCount = pos def pausefeed(self): self.pause = True def playfeed(self): self.pause = False
class Chart(QLabel): def __init__(self, parent): QLabel.__init__(self) self.data = None self.padding = 10 self.leftPadding = 30 self.maxX = None self.currentPoints = None self.closestPoint = None self.currentPoint = 0 self.seek = GenericConnector(parent.analyser.seek) self.setMouseTracking(True) self.setSizePolicy( QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)) def paintEvent(self, event): if self.data is not None: painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) self.top = self.padding self.bottom = self.height() - self.padding self.left = self.leftPadding # repeat for readability later self.right = self.width() - self.padding # Y AXIS painter.drawLine(self.left, self.bottom, self.left, self.top) painter.setPen(QColor(0, 0, 255)) self.currentPoints = [] count = 0 for result in self.data: pred = result[0] yVal = int(self.scale(pred, 0, 1, self.bottom, self.top)) xVal = int( self.scale(count, 0, self.maxX, self.left, self.right)) self.currentPoints.append(QPoint(xVal, yVal)) count += 1 painter.drawPolyline(self.currentPoints) painter.drawText(self.left - self.leftPadding, self.top, "100%") painter.drawText(self.left - self.leftPadding, self.bottom, " 0%") painter.setPen(QColor(0, 255, 0)) xVal = int( self.scale(self.currentPoint, 0, self.maxX, self.left, self.right)) painter.drawLine(xVal, self.bottom, xVal, self.top) if self.closestPoint is not None: painter.setBrush(QColor(255, 0, 0)) pen = QPen(QColor(255, 0, 0)) pen.setWidth(4) painter.setPen(pen) painter.drawEllipse(self.closestPoint, 2, 2) val = self.scale(self.closestPoint.y(), self.bottom, self.top, 0, 1) val = str(round(val * 100, 2)) + '%' font = painter.font() font.setPixelSize(14) painter.setFont(font) textPoint = QPoint(self.closestPoint.x() + 5, self.closestPoint.y()) painter.drawText(textPoint, str(val)) def scale(self, val, inMin, inMax, outMin, outMax): return ((val - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin def mousePressEvent(self, event): if self.data is not None: newPos = int( self.scale(event.x(), self.left, self.right, 0, self.maxX)) self.seek.emitSignal(newPos) def mouseMoveEvent(self, event): if self.currentPoints is not None: dists = [] for point in self.currentPoints: x1 = event.x() y1 = event.y() x2 = point.x() y2 = point.y() dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) dists.append(dist) closest = min(dists) index = dists.index(closest) self.closestPoint = self.currentPoints[index] self.update()
class Viewer(QWidget): def __init__(self, parent): QWidget.__init__(self) self.playing = True self.title = QLabel() font = QFont('Helvetica', 18) self.title.setFont(font) self.title.setAlignment(Qt.AlignCenter) self.surface = QLabel() self.placeholder = data_handler.getLabelledPixmap( 1280, 720, "Click on something in the ready list\nto view it here.", path='../data/placeholder.png') self.surface.setSizePolicy( QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)) self.setSizePolicy( QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)) self.surface.setPixmap(self.placeholder) self.timeDisplay = QLabel("0:00:00 / 0:00:00") font = QFont('Helvetica', 12) self.timeDisplay.setFont(font) self.controls = QHBoxLayout() self.controls.setAlignment(Qt.AlignCenter) self.togglePlay = QPushButton('Play/Pause') self.togglePlay.clicked.connect(self.toggle) self.togglePlay.setSizePolicy( QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)) self.resultDisplay = QLabel() self.resultDisplay.setFont(font) self.resultDisplay.setAlignment(Qt.AlignRight) self.resultDisplay.setMinimumWidth(50) self.resultDisplay.setMaximumWidth(50) self.controls.addWidget(self.timeDisplay) self.controls.addWidget(self.togglePlay) self.controls.addWidget(self.resultDisplay) self.analyser = self.Analyser(self) self.analyser.setDaemon(True) self.analyser.start() self.pause = GenericConnector(self.analyser.pausefeed) self.play = GenericConnector(self.analyser.playfeed) self.chart = self.Chart(self) outerLayout = QVBoxLayout() outerLayout.addWidget(self.title) outerLayout.addWidget(self.surface) outerLayout.addLayout(self.controls) outerLayout.addWidget(self.chart) self.setLayout(outerLayout) def toggle(self): if self.playing is True: self.playing = False self.pause.emitSignal() else: self.playing = True self.play.emitSignal() def showAnalysis(self, contentType, name): # self.analyser.reset() self.title.setText(name) self.title.update() self.analyser.set(contentType, name) def updateChart(self, currentFrame): self.chart.currentPoint = currentFrame def resetChart(self, args): if args is not None: resultsData, totalFrames, fps = args self.chart.maxX = totalFrames self.chart.data = resultsData self.chart.fps = fps else: self.chart.maxX = None self.chart.data = None self.chart.fps = None self.chart.update() def updateTimeDisplay(self, args): cur, total, result = args self.timeDisplay.setText(str(cur) + " / " + str(total)) self.timeDisplay.update() percentage = '{:5.2f}'.format(result * 100) self.resultDisplay.setText(percentage) self.resultDisplay.update() def setTitle(self, title): self.title.setText(title) def updateDisplay(self, frame): if isinstance(frame, np.ndarray): h, w, c = np.shape(frame) pmap = QPixmap.fromImage( QImage(frame.data, w, h, 3 * w, QImage.Format_RGB888)) else: pmap = self.placeholder self.surface.setPixmap(pmap) self.surface.update() class Chart(QLabel): def __init__(self, parent): QLabel.__init__(self) self.data = None self.padding = 10 self.leftPadding = 30 self.maxX = None self.currentPoints = None self.closestPoint = None self.currentPoint = 0 self.seek = GenericConnector(parent.analyser.seek) self.setMouseTracking(True) self.setSizePolicy( QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)) def paintEvent(self, event): if self.data is not None: painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) self.top = self.padding self.bottom = self.height() - self.padding self.left = self.leftPadding # repeat for readability later self.right = self.width() - self.padding # Y AXIS painter.drawLine(self.left, self.bottom, self.left, self.top) painter.setPen(QColor(0, 0, 255)) self.currentPoints = [] count = 0 for result in self.data: pred = result[0] yVal = int(self.scale(pred, 0, 1, self.bottom, self.top)) xVal = int( self.scale(count, 0, self.maxX, self.left, self.right)) self.currentPoints.append(QPoint(xVal, yVal)) count += 1 painter.drawPolyline(self.currentPoints) painter.drawText(self.left - self.leftPadding, self.top, "100%") painter.drawText(self.left - self.leftPadding, self.bottom, " 0%") painter.setPen(QColor(0, 255, 0)) xVal = int( self.scale(self.currentPoint, 0, self.maxX, self.left, self.right)) painter.drawLine(xVal, self.bottom, xVal, self.top) if self.closestPoint is not None: painter.setBrush(QColor(255, 0, 0)) pen = QPen(QColor(255, 0, 0)) pen.setWidth(4) painter.setPen(pen) painter.drawEllipse(self.closestPoint, 2, 2) val = self.scale(self.closestPoint.y(), self.bottom, self.top, 0, 1) val = str(round(val * 100, 2)) + '%' font = painter.font() font.setPixelSize(14) painter.setFont(font) textPoint = QPoint(self.closestPoint.x() + 5, self.closestPoint.y()) painter.drawText(textPoint, str(val)) def scale(self, val, inMin, inMax, outMin, outMax): return ((val - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin def mousePressEvent(self, event): if self.data is not None: newPos = int( self.scale(event.x(), self.left, self.right, 0, self.maxX)) self.seek.emitSignal(newPos) def mouseMoveEvent(self, event): if self.currentPoints is not None: dists = [] for point in self.currentPoints: x1 = event.x() y1 = event.y() x2 = point.x() y2 = point.y() dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) dists.append(dist) closest = min(dists) index = dists.index(closest) self.closestPoint = self.currentPoints[index] self.update() class Analyser(Thread): def __init__(self, display): Thread.__init__(self) self.basePath = '../data/processed/' self.path = None self.resultPath = None self.frameCount = 0 self.displayConn = DisplayConnector(display) self.chartConn = GenericConnector(display.resetChart) self.pointConn = GenericConnector(display.updateChart) self.timeConn = GenericConnector(display.updateTimeDisplay) self.titleConn = GenericConnector(display.setTitle) self.reset = False self.ready = True self.pause = False 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() def scale(self, val, inMin, inMax, outMin, outMax): return ((val - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin def set(self, contentType, name): self.resetFeed() self.path = self.basePath + name self.resultPath = self.basePath + name + '.result' def resetFeed(self): self.path = None self.resultPath = None self.reset = True self.pause = False while self.ready is False: time.sleep(0.001) def seek(self, pos): self.frameCount = pos def pausefeed(self): self.pause = True def playfeed(self): self.pause = False
class ReadyList(QWidget): def __init__(self, parent): QWidget.__init__(self) self.parent = parent self.setSizePolicy( QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)) files = os.listdir('../data/processed/') fullPaths = [] for file in files: fullPath = '../data/processed/' + file fullPaths.append(fullPath) files = self.parent.sortFiles(fullPaths) files = self.parent.addPreviews(files, ready=True) self.stopFeedConn = GenericConnector( self.parent.viewer.analyser.resetFeed) self.update() def update(self): title = QLabel('Ready') font = QFont('Helvetica', 18) title.setFont(font) title.setAlignment(Qt.AlignCenter) buttonLayout = QHBoxLayout() deleteAll = QPushButton('Delete all processed media') deleteAll.clicked.connect(self.deleteAll) buttonLayout.addWidget(deleteAll) listLayout = QVBoxLayout() if len(self.parent.ready) > 0: for preview in self.parent.ready: if self.parent.viewer.analyser.path is not None: if os.path.basename(preview.itemPath) == os.path.basename( self.parent.viewer.analyser.path): preview.setStyleSheet("border: 3px solid blue") else: preview.setStyleSheet("border: 3px solid black") else: preview.setStyleSheet("border: 3px solid black") listLayout.addWidget(preview) else: pmap = data_handler.getLabelledPixmap( 256, 144, "Processed Content\n Will Appear Here", path='../data/placeholder.png') space = QLabel() space.setPixmap(pmap) space.setStyleSheet("border: 3px solid black") listLayout.addWidget(space) listWidget = QFrame() listWidget.setLayout(listLayout) listScroll = QScrollArea() listScroll.setWidget(listWidget) filler = QProgressBar() sizePolicy = QSizePolicy() sizePolicy.setRetainSizeWhenHidden(True) filler.setSizePolicy(sizePolicy) outerLayout = QVBoxLayout() outerLayout.addWidget(title) outerLayout.addLayout(buttonLayout) outerLayout.addWidget(filler) outerLayout.addWidget(listScroll) tempWidget = QWidget() tempWidget.setLayout(self.layout()) self.setLayout(outerLayout) filler.hide() def deleteAll(self): self.stopFeedConn.emitSignal() time.sleep(1) files = os.listdir('../data/processed/') for file in files: fullPath = '../data/processed/' + file os.remove(fullPath) self.parent.ready.clear() self.update()
class ProcessHandler(Thread): 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 run(self): while not self.terminator.isTerminating(): if len(self.parent.toProcess) > 0: path = self.parent.toProcess[0].itemPath itemType = self.parent.toProcess[0].itemType itemName = self.parent.toProcess[0].itemName if itemType == 'image': img = cv2.imread(path) encoded, pmap = self.preprocess(img) if encoded is not None: networker = Networker(filePath=itemName, imageMode=True) networker.setDaemon(True) networker.frames.append((encoded, pmap)) networker.end = True networker.start() while networker.isAlive(): time.sleep(0.01) elif itemType == 'video': networker = Networker(filePath=itemName, deferredMode=True) networker.setDaemon(True) networker.start() feedLoader = FeedLoader(feedID=path, networker=networker) feedLoader.setDaemon(True) feedLoader.start() fps = None total = None while fps is None or total is None: fps, total = feedLoader.getFeedDetails() self.progressBarSetup.emitSignal((0, total)) # wait for video to be finished while networker.is_alive(): self.progressBarUpdate.emitSignal((networker.total, )) time.sleep(1) basePath = '../data/Processed/' shutil.copy(path, basePath + itemName) self.moveTopConnector.emitSignal() time.sleep(2) def preprocess(self, frame): displaySize = (640, 360) processSize = (640, 480) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) displayFrame = cv2.resize(frame, displaySize) processFrame = cv2.resize(frame, processSize) pmap = QPixmap.fromImage( QImage(displayFrame.data, displaySize[0], displaySize[1], 3 * displaySize[0], QImage.Format_RGB888)) encodeCheck, jpegBuf = cv2.imencode('.jpg', processFrame) if encodeCheck: encoded = b64.b64encode(jpegBuf) else: encoded = None return encoded, pmap
class Networker(Thread): def __init__(self, camera=None, display=None, mainDisplay=None, deferredMode=False, filePath=None, layoutHandler=None, imageMode=False): Thread.__init__(self) self.layoutHandler = layoutHandler if self.layoutHandler is not None: self.updateConnector = GenericConnector(layoutHandler.updateLayout) self.end = False self.frames = deque() self.nextFrame = None self.camera = camera self.display = display self.deferredMode = deferredMode self.imageMode = imageMode self.filePath = filePath if display is not None: self.displayConn = DisplayConnector(display) self.mainDisplayConn = DisplayConnector(mainDisplay) self.mainDisplay = mainDisplay self.serverAddr = 'tcp://35.204.135.105' self.localAddr = 'tcp://127.0.0.1' self.mainAddr = self.serverAddr self.initPort = ':5000' self.total = 0 def setupSocket(self, certID, serverKey=True): certHandler = CertificateHandler(certID, 'client') ctxHandler = ContextHandler(certHandler.getEnrolledKeysPath()) context = ctxHandler.getContext() socket = context.socket(zmq.REQ) monitorSocket = socket.get_monitor_socket() monitor = Monitor(monitorSocket, certID) monitor.setDaemon(True) monitor.start() if certID != 'front': certHandler.prep() publicKey, privateKey = certHandler.getKeyPair() if serverKey: serverKey = certHandler.getEnrolledKeys() socket.curve_serverkey = serverKey socket.curve_secretkey = privateKey socket.curve_publickey = publicKey return socket, certHandler, ctxHandler def initialize(self, feedID): initSocket, frontCert, frontCtx = self.setupSocket('front', True) feedSocket, feedCert, feedCtx = self.setupSocket(feedID, False) publicKey, _ = feedCert.getKeyPair() initSocket.connect(self.mainAddr + self.initPort) initSocket.send_string(feedID + ' ' + publicKey.decode('utf-8')) initData = initSocket.recv_string() parts = initData.split(' ') port = ':' + parts[0] serverKey = parts[1] logging.debug('Assigned Port: %s using server key %s', port, serverKey) feedCert.savePublicKey(serverKey) serverKey = feedCert.getEnrolledKeys() feedSocket.curve_serverkey = serverKey feedSocket.connect(self.mainAddr + port) return feedSocket, feedCert, feedCtx 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") def scale(self, val, inMin, inMax, outMin, outMax): return ((val - inMin) / (inMax - inMin)) * (outMax - outMin) + outMin