Skip to content

Commit

Permalink
Improving vision processing. This is an interface change where the us…
Browse files Browse the repository at this point in the history
…er no longer calls start_vision_buffering but now just open_video and close_video
  • Loading branch information
amymcgovern committed Feb 22, 2018
1 parent f18660c commit c8b6f8b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
24 changes: 16 additions & 8 deletions DroneVision.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def __init__(self, drone_object, is_bebop, buffer_size=200):
self.user_vision_thread = None
self.vision_running = True

# the vision thread starts opencv on these files. That will happen inside the other thread
# so here we just sent the image index to 1 ( to start)
self.image_index = 1


def set_user_callback_function(self, user_callback_function=None, user_callback_args=None):
"""
Set the (optional) user callback function for handling the new vision frames. This is
Expand Down Expand Up @@ -99,9 +104,12 @@ def open_video(self):
self.ffmpeg_process = \
subprocess.Popen("ffmpeg -i rtsp://192.168.99.1/media/stream2 -r 30 image_%03d.png &",
shell=True, cwd=self.imagePath, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print("Opening non-blocking readers")

# immediately start the vision buffering (before we even know if it succeeded since waiting puts us behind)
self._start_video_buffering()

# open non-blocking readers to look for errors or success
print("Opening non-blocking readers")
stderr_reader = NonBlockingStreamReader(self.ffmpeg_process.stderr)
stdout_reader = NonBlockingStreamReader(self.ffmpeg_process.stdout)

Expand Down Expand Up @@ -143,14 +151,10 @@ def open_video(self):
stdout_reader.finish_reader()
stderr_reader.finish_reader()

# the second thread starts opencv on these files. That will happen inside the other thread
# so here we just sent the image index to 1 ( to start)
self.image_index = 1

# return whether or not it worked
return success

def start_video_buffering(self):
def _start_video_buffering(self):
"""
If the video capture was successfully opened, then start the thread to buffer the stream
Expand Down Expand Up @@ -232,11 +236,15 @@ def get_latest_valid_picture(self):
"""
return self.buffer[self.buffer_index]

def stop_vision_buffering(self):
def close_video(self):
"""
Should stop the vision thread
Stop the vision processing and all its helper threads
"""

# the helper threads look for this variable to be true
self.vision_running = False

# kill the ffmpeg subprocess
self.ffmpeg_process.kill()

# send the command to kill the vision stream (bebop only)
Expand Down
5 changes: 3 additions & 2 deletions examples/demoBebopVision.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def save_pictures(self, args):

if (success):
print("Vision successfully started!")
bebopVision.start_video_buffering()
#removed the user call to this function (it now happens in open_video())
#bebopVision.start_video_buffering()

# skipping actually flying for safety purposes indoors - if you want
# different pictures, move the bebop around by hand
Expand All @@ -50,7 +51,7 @@ def save_pictures(self, args):
bebop.pan_tilt_camera_velocity(pan_velocity=0, tilt_velocity=-2, duration=4)
bebop.smart_sleep(25)
print("Finishing demo and stopping vision")
bebopVision.stop_vision_buffering()
bebopVision.close_video()

# disconnect nicely so we don't need a reboot
bebop.disconnect()
Expand Down
7 changes: 4 additions & 3 deletions examples/demoMamboVision.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def save_pictures(self, args):
filename = "test_image_%06d.png" % self.index
cv2.imwrite(filename, img)
self.index +=1
#print(self.index)
print(self.index)



Expand Down Expand Up @@ -52,7 +52,8 @@ def save_pictures(self, args):

if (success):
print("Vision successfully started!")
mamboVision.start_video_buffering()
#removed the user call to this function (it now happens in open_video())
#mamboVision.start_video_buffering()

if (testFlying):
print("taking off!")
Expand All @@ -78,7 +79,7 @@ def save_pictures(self, args):

# done doing vision demo
print("Ending the sleep and vision")
mamboVision.stop_vision_buffering()
mamboVision.close_video()

mambo.smart_sleep(5)

Expand Down

0 comments on commit c8b6f8b

Please sign in to comment.