def main(): if not os.path.isfile(fname): print("Please train the data first") exit(0) videoGetter = Capturer(vSource).start() faceDetector = Detector(dname, cname, fname, videoGetter.frame, relayPin).start() #videoShower = Show(videoGetter.frame).start() # Setup GPIO for door lock GPIO.setmode(GPIO.BCM) GPIO.setup(relayPin, GPIO.OUT) GPIO.output(relayPin, 0) while True: # Stop the thread when its other worker stopped if videoGetter.stopped or faceDetector.stopped: #videoShower.stopped: if not videoGetter.stopped: videoGetter.stop() if not faceDetector.stopped: faceDetector.stop() #if not videoShower.stopped: videoShower.stop() break frame = videoGetter.frame # Capture a video frame faceDetector.frame = frame # Submit the video frame to detector for process #videoShower.frame = frame GPIO.cleanup() print("END")
def run(self): self.root.title("Screen Recorder") self.root.deiconify() self.root.resizable(0, 0) self.root.geometry("900x600") while True: Capturer.show_video(self.root, self.view.canvas) Capturer.record(self.recording, self.output) self.root.mainloop()
def __init__(self, trainedModelThrottle = "../training/case1/trained mlp/throttle.joblib", trainedModelSteering = "../training/case1/trained mlp/steering.joblib", runMode = "NeuralNet", windowTitle = "Cannonball", fps = -1): self.trainedModelThrottle = trainedModelThrottle self.trainedModelSteering = trainedModelSteering self.runMode = runMode self.capturer = Capturer(windowTitle) self.fps = fps if (self.runMode == "DecisionTree"): self.modelThrottle = DecisionTree() self.modelSteering = DecisionTree() else: self.modelThrottle = MultilayerPerception(False) self.modelSteering = MultilayerPerception(False) self.modelThrottle.loadModel(self.trainedModelThrottle) self.modelSteering.loadModel(self.trainedModelSteering)
def __init__(self): MainWindow.__init__(self) self.pktList = PktList() self.capturer = Capturer(self.pktList) self.cap_thread = None self.analyzer = Analyer(self.pktList, self.pktTableWidget, self) self.ana_thread = None MainWindow.set_devlist(self, self.capturer.devlist, WIN32) # self.testWidget() self.show()
class Display: #Constructor def __init__(self, trainedModelThrottle = "../training/case1/trained mlp/throttle.joblib", trainedModelSteering = "../training/case1/trained mlp/steering.joblib", runMode = "NeuralNet", windowTitle = "Cannonball", fps = -1): self.trainedModelThrottle = trainedModelThrottle self.trainedModelSteering = trainedModelSteering self.runMode = runMode self.capturer = Capturer(windowTitle) self.fps = fps if (self.runMode == "DecisionTree"): self.modelThrottle = DecisionTree() self.modelSteering = DecisionTree() else: self.modelThrottle = MultilayerPerception(False) self.modelSteering = MultilayerPerception(False) self.modelThrottle.loadModel(self.trainedModelThrottle) self.modelSteering.loadModel(self.trainedModelSteering) #Main program def run(self): #Start the capture loop while(True): startTime = time.time() rawFrame = self.capturer.getFrame() roadFrame = self.filterLines(rawFrame) roadCurvature, carPosition, outputFrame = self.calculateCurvature(roadFrame) speed = self.calculateSpeed(rawFrame) #Predict how to change throttle based on loaded model if self.modelThrottle.predict([[roadCurvature, int(speed), carPosition]])[0] == "Accelerate": Controller.accelerate() print("Accelerate ", end = '') elif self.modelThrottle.predict([[roadCurvature, int(speed), carPosition]])[0] == "Brake": Controller.brake() print("Brake ", end = '') elif self.modelThrottle.predict([[roadCurvature, int(speed), carPosition]])[0] == "Coast": Controller.coast() print("Coast ", end = '') else: print("Error ", self.modelThrottle.predict([[roadCurvature, int(speed), carPosition]])[0], " ", end = '') #Predict how to steer based on the loaded model if self.modelSteering.predict([[roadCurvature, int(speed), carPosition]])[0] == "Left": Controller.left() print("Left") elif self.modelSteering.predict([[roadCurvature, int(speed), carPosition]])[0] == "Right": Controller.right() print("Right") elif self.modelSteering.predict([[roadCurvature, int(speed), carPosition]])[0] == "Straight": Controller.straight() print("Straight") else: print("Error ", self.modelSteering.predict([[roadCurvature, int(speed), carPosition]])[0]) # cv.imshow("Road Frame", outputFrame) #Should ALWAYS be called last (to ensure accurate synchronization and avoid undetectable delays) self.syncClock(startTime, time.time(), False) #Check the current fps and synchronize it to what was specified def syncClock(self, startTime, endTime, verbose = False): maxFps = 1/(endTime - startTime) #Fastest possible framerate based on executed code #Ensure there is always at least 1ms of waiting time sleepTime = max(int((1/self.fps - 1/maxFps) * 1000), 1) if cv.waitKey(sleepTime) & 0xFF == ord("q"): cv.destroyAllWindows() sys.exit(0) displayFps = 1/(time.time() - startTime) if (verbose == True): print("FPS: ", displayFps, " Slept: ", sleepTime, "ms") return displayFps #Takes the raw image capture and isolates the road lines def filterLines(self, inputFrame): if (inputFrame.all() != None): outputFrame = cv.cvtColor(inputFrame, cv.COLOR_RGB2GRAY) #Convert to grayscale ret, outputFrame = cv.threshold(outputFrame, 245, 255, cv.THRESH_BINARY) #Threshold the image to find the lines return outputFrame else: print("No frame supplied!") return None #Calculates road curvature and car position def calculateCurvature(self, inputFrame): height, width = inputFrame.shape[:2] outputFrame, contours, hierarchy = cv.findContours(inputFrame, 0, 2) vxAvg = 0 vyAvg = 0 xAvg = 0 yAvg = 0 numberPoints = 0 for cnt in contours: #Check that the contour actually represents a line if (cv.contourArea(cnt) > 30): moment = cv.moments(cnt) #Calculate the moment of each blob [vx,vy,x,y] = cv.fitLine(cnt, cv.DIST_L2, 0, 0.01, 0.01) #Fit a straight line to each moment if (x > int(width/2)): vx *= -1; vy *= -1; if (y > height//3 and abs(vx) > 0.05 and abs(vx) < 0.99): cv.arrowedLine(outputFrame, (x, y), (x + (20 * vx), y + (20 * vy)), (255, 255, 255), 2, tipLength=0.5) #Display the direction of each moment #cv.line(outputFrame, (x, y), (x + (20 * vx), y + (20 * vy)), (255, 255, 255), 3) cv.putText(outputFrame, str(vx[0])[:5], (x, y), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) vxAvg += vx vyAvg += vy xAvg += x yAvg += y numberPoints += 1 if (numberPoints > 0): vxAvg /= -numberPoints vyAvg /= numberPoints xAvg /= numberPoints yAvg /= numberPoints return vxAvg[0], xAvg[0], outputFrame return -99, -99, outputFrame #Default return (no lines found) #Gets the speed displayed on the screen, using a modular method that works with different screen resolutions def calculateSpeed(self, inputFrame): if (inputFrame.all() != None): height, width = inputFrame.shape[:2] numberSize = (int(round(0.0969 * width)) - int(round(0.0781 * width))) * (int(round(0.9598 * height)) - int(round(0.9107 * height))) #Digit one digitOne = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.0781 * width)):int(round(0.0969 * width))][:,:,2] ret, digitOne = cv.threshold(digitOne, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines digitOneLeft = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.0781 * width)):int(round(0.0875 * width))][:,:,2] ret, digitOneLeft = cv.threshold(digitOneLeft, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines hundreds = cv.countNonZero(digitOne) + cv.countNonZero(digitOneLeft) hundreds = self.convertSpeed(hundreds, numberSize) #Digit two digitTwo = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.1031 * width)):int(round(0.1219 * width))][:,:,2] ret, digitTwo = cv.threshold(digitTwo, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines digitTwoLeft = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.1031 * width)):int(round(0.1125 * width))][:,:,2] ret, digitTwoLeft = cv.threshold(digitTwoLeft, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines tens = cv.countNonZero(digitTwo) + cv.countNonZero(digitTwoLeft) tens = self.convertSpeed(tens, numberSize) #Digit three digitThree = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.1281 * width)):int(round(0.1469 * width))][:,:,2] ret, digitThree = cv.threshold(digitThree, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines digitThreeLeft = inputFrame[int(round(0.9107 * height)):int(round(0.9598 * height)), int(round(0.1281 * width)):int(round(0.1375 * width))][:,:,2] ret, digitThreeLeft = cv.threshold(digitThreeLeft, 254, 255, cv.THRESH_BINARY) #Threshold the image to find the lines ones = cv.countNonZero(digitThree) + cv.countNonZero(digitThreeLeft) ones = self.convertSpeed(ones, numberSize) return str(hundreds) + str(tens) + str(ones) else: print("No frame supplied!") return -1 #Helper method to decode the digits into their real values based on the below table (640x448), numberSize = 264: # 0: 96 + 48 = 144 # 1: 40 + 0 = 40 # 2: 88 + 44 = 132 # 3: 92 + 36 = 128 # 4: 76 + 28 = 104 # 5: 80 + 40 = 120 # 6: 96 + 56 = 152 # 7: 68 + 24 = 92 # 8: 112 + 56 = 168 # 9: 96 + 40 = 136 def convertSpeed(self, num, numberSize): pixelRatio = num / numberSize if (pixelRatio == 0 or pixelRatio == 144 / 264): return 0 elif (pixelRatio == 40 / 264): return 1 elif (pixelRatio == 132 / 264): return 2 elif (pixelRatio == 128 / 264): return 3 elif (pixelRatio == 104 / 264): return 4 elif (pixelRatio == 120 / 264): return 5 elif (pixelRatio == 152 / 264): return 6 elif (pixelRatio == 92 / 264): return 7 elif (pixelRatio == 168 / 264): return 8 elif (pixelRatio == 136 / 264): return 9
class QSniffer(MainWindow): def __init__(self): MainWindow.__init__(self) self.pktList = PktList() self.capturer = Capturer(self.pktList) self.cap_thread = None self.analyzer = Analyer(self.pktList, self.pktTableWidget, self) self.ana_thread = None MainWindow.set_devlist(self, self.capturer.devlist, WIN32) # self.testWidget() self.show() @pyqtSignature("int") def on_promisCheckBox_stateChanged(self, p0): MainWindow.on_promisCheckBox_stateChanged(self, p0) # print "check int:%d"%p0 if(self.promisCheckBox.isChecked()): self.capturer.set_promisc(True) else: self.capturer.set_promisc(False) @pyqtSignature("") def on_startButton_clicked(self): MainWindow.on_startButton_clicked(self) if self.capturer.adhandle == None: curindex = self.devComboBox.currentIndex() if not self.capturer.open_dev(curindex): # TODO: handle open error return self.cap_thread = threading.Thread(target=self.capturer.start_capture) self.cap_thread.start() self.ana_thread = threading.Thread(target=self.analyzer.start_analize) self.ana_thread.start() @pyqtSignature("") def on_stopButton_clicked(self): MainWindow.on_stopButton_clicked(self) if self.cap_thread != None and self.cap_thread.is_alive(): self.capturer.stop_capture() self.cap_thread.join() if self.ana_thread != None and self.ana_thread.is_alive(): self.analyzer.stop_analize() self.ana_thread.join() @pyqtSignature("") def on_clearButton_clicked(self): MainWindow.on_clearButton_clicked(self) if self.cap_thread == None or self.ana_thread == None: pass elif self.cap_thread.is_alive() or self.ana_thread.is_alive(): if self.capturer.pktSrcType == 'dev': QtGui.QMessageBox.information(self, "Information", self.tr("You must stop capture first.")) return False elif self.capturer.pktSrcType == 'dump': self.on_stopButton_clicked() self.capturer.clear() self.analyzer.clear() self.pktList.clear() count = self.pktTableWidget.rowCount() for i in range(count): self.pktTableWidget.removeRow(0) self.pktTreeWidget.clear() self.pktAsciiBrowser.clear() self.pkt0xBrowser.clear() return True @pyqtSignature("") def on_exportButton_clicked(self): MainWindow.on_exportButton_clicked(self) if self.cap_thread == None or self.ana_thread == None: pass elif self.cap_thread.is_alive() or self.ana_thread.is_alive(): if self.capturer.pktSrcType == 'dev': QtGui.QMessageBox.information(self, "Information", self.tr("You must stop capture first.")) return False elif self.capturer.pktSrcType == 'dump': self.on_stopButton_clicked() fdialog = QtGui.QFileDialog() fdialog.setWindowTitle('Save pcap file') fname = fdialog.getSaveFileName(filter='pcap file(*.pcap)', directory='/') fname = unicode(fname) if str(fname) == '': return False if os.path.exists('~tmp'): shutil.move('~tmp', fname) return True @pyqtSignature("") def on_importButton_clicked(self): MainWindow.on_importButton_clicked(self) if not self.on_clearButton_clicked(): return False fdialog = QtGui.QFileDialog() fdialog.setWindowTitle('Select pcap file') fname = fdialog.getOpenFileName(directory='/') fname = unicode(fname) # print "file name:%r" % fname if fname == '': return if not self.capturer.open_dump(fname): return self.cap_thread = threading.Thread(target=self.capturer.start_capture) self.cap_thread.start() self.ana_thread = threading.Thread(target=self.analyzer.start_analize) self.ana_thread.start() @pyqtSignature("") def on_filterApplyButton_clicked(self): MainWindow.on_filterApplyButton_clicked(self) if self.capturer.adhandle == None: curindex = self.devComboBox.currentIndex() if not self.capturer.open_dev(curindex): return filterstr = str(self.filterLineEdit.text()) # print "%r" % filterstr if filterstr == "": return msg = self.filterMsgLable if self.capturer.compile_filter(filterstr): if self.capturer.set_filter(): msg.setText(QtCore.QString("<font style='color: green;'>Success</font>")) else: msg.setText(QtCore.QString("<font style='color: red;'>Error occur</font>")) else: msg.setText(QtCore.QString("<font style='color: red;'>Wrong syntax</font>")) @pyqtSignature("") def on_filterClearButton_clicked(self): MainWindow.on_filterClearButton_clicked(self) self.filterLineEdit.clear() @pyqtSignature("QString") def on_filterLineEdit_textChanged(self, p0): MainWindow.on_filterLineEdit_textChanged(self, p0) self.filterMsgLable.clear() @pyqtSignature("int") def on_devComboBox_currentIndexChanged(self, index): MainWindow.on_devComboBox_currentIndexChanged(self, index) self.analyzer.statistics.updateStatistics(self) # print "combobox index:%d" % index @pyqtSignature("int, int") def on_pktTableWidget_cellClicked(self, row, column): MainWindow.on_pktTableWidget_cellClicked(self, row, column) tree = self.pktTreeWidget tree.clear() pktItem = self.analyzer.itemlist[row] pktdata = pktItem.rawpkt[1] pTree = ProtocolTree(tree, pktdata) tree.insertTopLevelItems(0, pTree.parseProtocol()) p0xb = self.pkt0xBrowser p0xb.setText(QtCore.QString(PktContent.pkt0xContent(pktdata))) pasciib = self.pktAsciiBrowser pasciib.setText(QtCore.QString(PktContent.pktAsciiContent(pktdata))) def closeEvent(self, *args, **kwargs): if os.path.exists('./~tmp'): os.remove('./~tmp') self.on_stopButton_clicked() return MainWindow.closeEvent(self, *args, **kwargs)
def screenshot(self): timer = self.toInt() Capturer.take_screenshot(timer, self.root)