Skip to content

Commit

Permalink
Added example for VideoStream class
Browse files Browse the repository at this point in the history
  • Loading branch information
apockill committed Sep 27, 2016
1 parent 3b86caa commit 47a8acd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
4 changes: 0 additions & 4 deletions CalibrationsGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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_()
Expand Down
21 changes: 21 additions & 0 deletions Examples/VideoStreamExample.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion Logic/Global.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def ready(self):
return False



printRedirectFunc = lambda classString, string: None
# Initiate Global Variables
def init():
global keysPressed
Expand Down
4 changes: 2 additions & 2 deletions Logic/Robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions Logic/Video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
24 changes: 7 additions & 17 deletions MainGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 47a8acd

Please sign in to comment.