From 47a8acd039897ac4eec353996975d312b8100305 Mon Sep 17 00:00:00 2001 From: apockill Date: Tue, 27 Sep 2016 11:13:04 -0700 Subject: [PATCH] Added example for VideoStream class --- CalibrationsGUI.py | 4 ---- Examples/VideoStreamExample.py | 21 +++++++++++++++++++++ Logic/Global.py | 2 +- Logic/Robot.py | 4 ++-- Logic/Video.py | 13 ------------- MainGUI.py | 24 +++++++----------------- 6 files changed, 31 insertions(+), 37 deletions(-) create mode 100644 Examples/VideoStreamExample.py diff --git a/CalibrationsGUI.py b/CalibrationsGUI.py index 1cd7827..d50a6d3 100644 --- a/CalibrationsGUI.py +++ b/CalibrationsGUI.py @@ -193,8 +193,6 @@ def calibrateMotion(self): self.robotError() return - # Make sure VideoStream is collecting new frames - vStream.setPaused(False) showStep(1, "Do not make any movement in the cameras view until the next message appears.") @@ -282,8 +280,6 @@ def calibrateCoordinates(self): if reply == 0: startFromScratch = True if reply == 1: startFromScratch = False - # Make sure everything is ready - vStream.setPaused(False) coordWizard = CoordWizard(self.env, startFromScratch, parent=self) coordWizard.exec_() diff --git a/Examples/VideoStreamExample.py b/Examples/VideoStreamExample.py new file mode 100644 index 0000000..33ac9a3 --- /dev/null +++ b/Examples/VideoStreamExample.py @@ -0,0 +1,21 @@ +from Logic import Video +import cv2 + + +# Create a VideoStream and start a video-retrieval thread +vStream = Video.VideoStream() +vStream.startThread() +vStream.setNewCamera(0) + +# Play video until the user presses "q" +key = None +while not key == ord("q"): + frame = vStream.getFilteredFrame() + + # If the camera has started up, then show the frame + if frame is not None: cv2.imshow("frame", frame) + + key = cv2.waitKey(10) + +# Close the VideoStream Thread +vStream.endThread() diff --git a/Logic/Global.py b/Logic/Global.py index 79a096f..19a32e1 100644 --- a/Logic/Global.py +++ b/Logic/Global.py @@ -126,7 +126,7 @@ def ready(self): return False - +printRedirectFunc = lambda classString, string: None # Initiate Global Variables def init(): global keysPressed diff --git a/Logic/Robot.py b/Logic/Robot.py index 5a649f8..3b59777 100644 --- a/Logic/Robot.py +++ b/Logic/Robot.py @@ -84,7 +84,7 @@ def __init__(self): self.zMin, self.zMax = -5, 25 # Set up some constants for other functions to use - self.home = {'x': 0.0, 'y': 15.0, 'z': 20.0} + self.home = {'x': 0.0, 'y': 15.0, 'z': 15} self.__exiting = False # When true, any time-taking functions will exit ASAP. Used for quickly ending threads @@ -417,7 +417,7 @@ def __setupThread(self, com): # Check if the uArm was able to connect successfully if self.__uArm.connected(): printf("Robot| SUCCESS: uArm successfully connected") - self.__uArm.setXYZ(self.home['x'], self.home['y'], self.home['z'], self.speed) + # self.__uArm.setXYZ(self.home['x'], self.home['y'], self.home['z'], self.speed) self.__threadRunning = False self.setPos(**self.home) self.setActiveServos(all=False) diff --git a/Logic/Video.py b/Logic/Video.py index c952b12..1cbfee7 100644 --- a/Logic/Video.py +++ b/Logic/Video.py @@ -66,13 +66,11 @@ def __init__(self, fps=24): self.running = False self.setCamera = None # When this is a number and videoThread is on, it will attempt to setNewCamera(new) - self.paused = True self.cameraID = None self.fps = fps self.cap = None # An OpenCV capture object - self.frame = None self.frameList = [] # Used in computer vision tasks, this is a list of the last 5 frames (unfiltered) self.frameCount = 0 # Used in waitForNewFrame() @@ -98,15 +96,6 @@ def setNewCamera(self, cameraID): else: printf("Video| ERROR: Tried to set camera while camera was already being set! cameraID ", self.setCamera) - def setPaused(self, value): - # Tells the main frunction to grab more frames - if value is False: - # If you want to play video, make sure the main thread is running - if self.mainThread is None: - self.startThread() - - self.paused = value - def connected(self): # Returns True or False if there is a camera successfully connected if self.cap is None: return False @@ -148,7 +137,6 @@ def __videoThread(self): fpsTimer.wait() if not fpsTimer.ready(): continue if self.setCamera is not None: self.__setNewCamera(self.setCamera) - if self.paused: continue if self.cap is None: continue @@ -258,7 +246,6 @@ def __setNewCamera(self, cameraID): def getFrame(self): # Returns the latest frame grabbed from the camera if self.frame is not None: - # return self.frame.copy() else: return None diff --git a/MainGUI.py b/MainGUI.py index d77d9a8..168400e 100644 --- a/MainGUI.py +++ b/MainGUI.py @@ -73,7 +73,6 @@ def __init__(self): self.loadData = [] #Set when file is loaded. Used to check if the user has changed anything when closing self.programTitle = 'uArm Creator Studio' self.scriptToggleBtn = QtWidgets.QAction(QtGui.QIcon(Paths.run_script), 'Run', self) - self.videoToggleBtn = QtWidgets.QAction(QtGui.QIcon(Paths.play_video), 'Video', self) self.devicesBtn = QtWidgets.QAction(QtGui.QIcon(Paths.devices_neither), 'Devices', self) self.centralWidget = QtWidgets.QStackedWidget() self.controlPanel = ControlPanelGUI.ControlPanel(self.env, parent=self) @@ -82,8 +81,9 @@ def __init__(self): # Create Menu items and set up the GUI + self.cameraWidget.play() self.initUI() - self.setVideo("play") + # After initUI: Restore the window geometry to the state it was when the user last closed the window @@ -191,7 +191,6 @@ def initUI(self): objMngrBtn = QtWidgets.QAction(QtGui.QIcon(Paths.objectManager), 'Resources', self) self.scriptToggleBtn.setToolTip('Run/Pause the command script (Ctrl+R)') - self.videoToggleBtn.setToolTip('Play/Pause the video stream and Vision tracking') self.devicesBtn.setToolTip('Open Camera and Robot settings',) calibrateBtn.setToolTip('Open Robot and Camera Calibration Center') objMngrBtn.setToolTip('Open Resource Manager') @@ -200,13 +199,11 @@ def initUI(self): self.scriptToggleBtn.triggered.connect(self.toggleScript) - self.videoToggleBtn.triggered.connect(lambda: self.setVideo("toggle")) self.devicesBtn.triggered.connect(self.openDevices) calibrateBtn.triggered.connect(self.openCalibrations) objMngrBtn.triggered.connect(self.openObjectManager) toolbar.addAction(self.scriptToggleBtn) - toolbar.addAction(self.videoToggleBtn) toolbar.addAction(self.devicesBtn) toolbar.addAction(calibrateBtn) toolbar.addAction(objMngrBtn) @@ -278,22 +275,15 @@ def setVideo(self, state): vStream = self.env.getVStream() - if state == "play": # Make sure the videoStream object has a camera, or if the cameras changed, change it # if not vStream.connected() or not vStream.cameraID == cameraID: # vStream.setNewCamera(cameraID) self.cameraWidget.play() - vStream.setPaused(False) - self.videoToggleBtn.setIcon(QtGui.QIcon(Paths.pause_video)) - self.videoToggleBtn.setText("Pause") if state == "pause": self.cameraWidget.pause() - vStream.setPaused(True) - self.videoToggleBtn.setIcon(QtGui.QIcon(Paths.play_video)) - self.videoToggleBtn.setText("Play") def toggleScript(self): @@ -439,12 +429,12 @@ def openDevices(self): if self.interpreter.threadRunning(): self.endScript() - self.setVideo("pause") # If you don't pause video, scanning for cameras may crash the program + self.cameraWidget.pause() deviceWindow = DeviceWindow(parent=self) accepted = deviceWindow.exec_() - self.setVideo("play") + self.cameraWidget.play() if not accepted: printf("GUI| Cancel clicked, no settings applied.") return @@ -467,14 +457,14 @@ def openDevices(self): - self.setVideo("play") + self.cameraWidget.play() def openCalibrations(self): # This handles the opening and closing of the Calibrations window printf("GUI| Opening Calibrations Window") if self.interpreter.threadRunning(): self.endScript() - self.setVideo("pause") + self.cameraWidget.pause() coordSettings = self.env.getSetting("coordCalibrations") motionSettings = self.env.getSetting("motionCalibrations") @@ -492,7 +482,7 @@ def openCalibrations(self): else: printf("GUI| Cancel clicked, no calibrations applied.") - self.setVideo("play") + self.cameraWidget.play() def openObjectManager(self, openResourceWindow=None): # This handles the opening and closing of the ObjectManager window