How can I decode the parameter Line (class <byte>) in Mambo DroneVision?

34 views Asked by At

I want to capture and post process of the image in the front camera of mambo FPV in real time. But when I try to run the demo code try to streaming the video using ffmpeg way I face difficult. When using function open_video() from mamboVision there is a parameter 'Line' can't be decode.

"""
Demo of the ffmpeg based mambo vision code (basically flies around and saves out photos as it flies)

Author: Amy McGovern
"""
from pyparrot.Minidrone import Mambo
from pyparrot.DroneVision import DroneVision
from pyparrot.Model import Model
import threading
import cv2
import time

# you will need to change this to the address of YOUR mambo
mamboAddr = "e0:14:d0:63:3d:d0"

# make my mambo object
# remember to set True/False for the wifi depending on if you are using the wifi or the BLE to connect
mambo = Mambo(mamboAddr, use_wifi=True)
print("trying to connect to mambo now")
success = mambo.connect(num_retries=3)
print("connected: %s" % success)

if (success):
    # get the state information
    print("sleeping")
    mambo.smart_sleep(1)
    mambo.ask_for_state_update()
    mambo.smart_sleep(1)

    print("Preparing to open vision")
    mamboVision = DroneVision(mambo, is_bebop=False, buffer_size=30)
    success = mamboVision.open_video()
    print("Success in opening vision is %s" % success)

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

        print("Sleeeping for 15 seconds - move the mambo around")
        mambo.smart_sleep(15)

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

        mambo.smart_sleep(5)

    print("disconnecting")
    mambo.disconnect()


When I run this code the error code said

 File "d:/pythonCode/pyparrot/examples/demoMamboVision copy.py", line 34, in <module>
success = mamboVision.open_video()
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pyparrot\DroneVision.py",line 148, in open_video
line_str = line.decode("utf-8")

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 9: invalid start byte

When I look into the DroneVision.py the function open_video()code

# we have bypassed the old opencv VideoCapture method because it was unreliable for rtsp
        # get the path for the config files
        fullPath = inspect.getfile(DroneVision)
        shortPathIndex = fullPath.rfind("/")
        if (shortPathIndex == -1):
            # handle Windows paths
            shortPathIndex = fullPath.rfind("\\")
        print(shortPathIndex)
        shortPath = fullPath[0:shortPathIndex]
        self.imagePath = join(shortPath, "images")
        self.utilPath = join(shortPath, "utils")
        print(self.imagePath)
        print(self.utilPath)

        if (self.cleanup_old_images):
            print("removing all the old images")
            shutil.rmtree(self.imagePath)
            os.mkdir(self.imagePath)

        # the first step is to open the rtsp stream through ffmpeg first
        # this step creates a directory full of images, one per frame
        print("Opening ffmpeg")
        if (self.is_bebop):
            cmdStr = "ffmpeg -protocol_whitelist \"file,rtp,udp\" -i %s/bebop.sdp -r 30 image_" % self.utilPath + "%03d.png"
            print(cmdStr)
            self.ffmpeg_process = \
                subprocess.Popen(cmdStr, shell=True, cwd=self.imagePath, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        else:
            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)

        # 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)


        # look for success in the stdout
        # If it starts correctly, it will have the following output in the stdout
        # Stream mapping:
        #   Stream #0:0 -> #0:0 (h264 (native) -> png (native))

        # if it fails, it has the following in stderr
        # Output file #0 does not contain any stream

        success = False
        while (not success):

            line = stderr_reader.readline()
            if (line is not None):
                print(type(line))
                print(line)
                line_str = line.decode("utf-8")
                # line_str = line.decode()
                print(line_str)
                if line_str.find("Stream #0:0 -> #0:0 (h264 (native) -> png (native))") > -1:
                    success = True
                    break
                if line_str.find("Output file #0 does not contain any stream") > -1:
                    print("Having trouble connecting to the camera 1.  A reboot of the mambo may help.")
                    break

            line = stdout_reader.readline()
            if (line is not None):
                line_str = line.decode("utf-8")
                print(line_str)
                if line_str.find("Output file #0 does not contain any stream") > -1:
                    print("Having trouble connecting to the camera 2.  A reboot of the mambo may help.")
                    break

                if line_str.find("Stream #0:0 -> #0:0 (h264 (native) -> png (native))") > -1:
                    success = True

The parameter can't be decode is Line, which I can't fully understand what is it doing. And the NonBlockingStreamReader is something do with Queue. So I just wonder how can I make this work.

At first I think it might be the way of decode is wrong. So I change the decode way I know. I have tried the "utf-16", "ascii", "iso-8859-1" and "cp1252". The "utf-16" and "iso-8559-1" can decode but the result is random code, and else just can't decode. And I print the type of Line and print Line it self. And what i got is

 <class 'bytes'>
 b"'ffmpeg'\xa4\xa3\xacO\xa4\xba\xb3\xa1\xa9\xce\xa5~\xb3\xa1\xa9R\xa5O\xa1B\xa5i\xb0\xf5\xa6\xe6\xaa\xba\xb5{\xa6\xa1\xa9\xce\xa7\xe5\xa6\xb8\xc0\xc9\xa1C\r\n"

I try to look up what can the encoding way it can be. I try several decode program online like "utf-8", "utf-16" but none of them can decode the element inside.

0

There are 0 answers