TelloPyをMacbookで動かして、FPV1人称視点の腹画像と輪郭抽出モノクロ画像のリアルタイム受信に成功 


__[前回の記事](https://qiita.com/electronics_diy721/items/afc52610dc919a51a713)__に続けて、__TelloPy__をMacbookに入れて、サンプルコードを動かしてみました。

###Macbook側で受け取った画像

![スクリーンショット 2021-07-26 1.26.08.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1754134/c17a8ee0-5a4c-cd8c-a03f-a832a2c8c038.png)

![スクリーンショット 2021-07-26 1.25.34.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1754134/5461e30e-ea8a-6b2b-628c-94e9a73f1ee2.png)

###実行コード

```bash:Terminal
% python3 -m tellopy.examples.video_effect
```

####2つの画像をウィンドウに出力している処理の実装箇所

```Python3:video_effect.py
image = cv2.cvtColor(numpy.array(frame.to_image()), cv2.COLOR_RGB2BGR)
cv2.imshow('Original', image)
cv2.imshow('Canny', cv2.Canny(image, 100, 200))
```

- この２行目に、OpenCV2その他を用いて、別の画像処理を行うコードを噛ませれば、好きな処理を通過させた画像をウインドウに出力できる。

- 3行目のcv2.imshow()を追加すれば、3つ目のウィンドウが登場するはずだ。

__プログラムの本体__は、tello.pyにある。

- https://github.com/hanyazou/TelloPy/blob/develop-0.7.0/tellopy/_internal/tello.py

```Python3:tello.py

（ 省略 ）

from . import video_stream

（ 省略 ）

    def __init__(self, port=9000):
        self.tello_addr = ('192.168.10.1', 8889)

（ 省略 ）

        # File recieve state.
        self.file_recv = {}  # Map filenum -> protocol.DownloadedFile

        # Create a UDP socket
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.bind(('', self.port))
        self.sock.settimeout(2.0)

    def get_video_stream(self):
        """
        Get_video_stream is used to prepare buffer object which receive video data from the drone.
        """
        newly_created = False
        self.lock.acquire()
        log.info('get video stream')
        try:
            if self.video_stream is None:
                self.video_stream = video_stream.VideoStream(self)
                newly_created = True
            res = self.video_stream
        finally:
            self.lock.release()
        if newly_created:
            self.__send_exposure()
            self.__send_video_encoder_rate()
            self.start_video()

（ 省略 ）

```

__from . import video_stream__でimportしている、リアルタイムのストリーム画像の受信を担う実装コードは、__video_stream.py__と、__video_stream.py__からimportされている__protocol.py__にある。

- https://github.com/hanyazou/TelloPy/blob/develop-0.7.0/tellopy/_internal/video_stream.py

- https://github.com/hanyazou/TelloPy/blob/develop-0.7.0/tellopy/_internal/protocol.py

```Python3:
class VideoData(object):
    packets_per_frame = 0
    def __init__(self, data):
        self.h0 = byte(data[0])
        self.h1 = byte(data[1])
        if VideoData.packets_per_frame < (self.h1 & 0x7f):
            VideoData.packets_per_frame = (self.h1 & 0x7f)
```

###実行中のTerminal画面 （Macbook）

```bash:Terminal
electron@diynoMacBook-Pro ~ % cd TelloPy 
electron@diynoMacBook-Pro TelloPy % python3 -m tellopy.examples.video_effect
Tello: 00:56:55.347:  Info: start video thread
Tello: 00:56:55.347:  Info: send connection request (cmd="conn_req:9617")
Tello: 00:56:55.347:  Info: video receive buffer size = 524288
Tello: 00:56:55.347:  Info: state transit State::disconnected -> State::connecting
Tello: 00:56:55.393:  Info: connected. (port=9617)
Tello: 00:56:55.393:  Info: send_time (cmd=0x46 seq=0x01e4)
Tello: 00:56:55.393:  Info: state transit State::connecting -> State::connected
Tello: 00:56:55.393:  Info: get video stream
Tello: 00:56:55.394:  Info: LogData: UNHANDLED LOG DATA: id=   16, length=   4
Tello: 00:56:55.394:  Info: start video (cmd=0x25 seq=0x01e4)
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id= 1000, length=  44
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id= 1001, length=  16
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id=10100, length=  21
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id= 1002, length=  27
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id= 2064, length=  64
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id= 2208, length=  40
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id=10086, length=   4
Tello: 00:56:55.448:  Info: LogData: UNHANDLED LOG DATA: id=10085, length=  80
Tello: 00:56:55.549:  Info: LogData: UNHANDLED LOG DATA: id= 1710, length=   6
Tello: 00:56:55.652:  Info: LogData: UNHANDLED LOG DATA: id=   12, length=  55
non-existing PPS 0 referenced
non-existing PPS 0 referenced
decode_slice_header error
no frame!
non-existing PPS 0 referenced
non-existing PPS 0 referenced
decode_slice_header error
no frame!
Tello: 00:56:58.356:  Info: video data 1115500 bytes 543.6KB/sec
Tello: 00:57:00.356:  Info: video data 754065 bytes 368.2KB/sec
Tello: 00:57:02.357:  Info: video data 752525 bytes 367.4KB/sec
Tello: 00:57:04.356:  Info: video data 751162 bytes 366.8KB/sec
Tello: 00:57:06.357:  Info: video data 750970 bytes 366.6KB/sec
Tello: 00:57:08.357:  Info: video data 751264 bytes 366.8KB/sec
Tello: 00:57:10.357:  Info: video data 753874 bytes 368.1KB/sec
Tello: 00:57:12.358:  Info: video data 749773 bytes 366.0KB/sec
Tello: 00:57:14.359:  Info: video data 755464 bytes 368.8KB/sec
Tello: 00:57:16.359:  Info: video data 751139 bytes 366.7KB/sec
Tello: 00:57:18.360:  Info: video data 751957 bytes 367.1KB/sec
Tello: 00:57:20.360:  Info: video data 751067 bytes 366.7KB/sec
error while decoding MB 45 5, bytestream -6
Tello: 00:57:22.361:  Info: video data 744322 bytes 363.3KB/sec loss=1
error while decoding MB 0 35, bytestream -6
Tello: 00:57:24.361:  Info: video data 752618 bytes 367.5KB/sec loss=1
Tello: 00:57:26.361:  Info: video data 745160 bytes 363.8KB/sec
Tello: 00:57:28.387:  Info: video data 760753 bytes 366.7KB/sec
Tello: 00:57:30.388:  Info: video data 752702 bytes 367.4KB/sec
Tello: 00:57:32.390:  Info: video data 751086 bytes 366.4KB/sec

（ 省略 ）

Tello: 00:59:03.155: Error: video recv: timeout
Tello: 00:59:03.155:  Info: start video (cmd=0x25 seq=0x01e4)
Tello: 00:59:04.143: Error: recv: timeout
Tello: 00:59:04.143:  Info: send connection request (cmd="conn_req:9617")
Tello: 00:59:04.144:  Info: state transit State::connected -> State::connecting
Tello: 00:59:04.144:  Info: VideoStream.handle_event(DISCONNECTED)
Tello: 00:59:04.159: Error: video recv: timeout
Tello: 00:59:04.159:  Info: start video (cmd=0x25 seq=0x01e4)
Traceback (most recent call last):
  File "/Users/electron/TelloPy/tellopy/examples/video_effect.py", line 30, in main
    for frame in container.decode(video=0):
  File "av/container/input.pyx", line 183, in decode
  File "av/packet.pyx", line 103, in av.packet.Packet.decode
  File "av/stream.pyx", line 171, in av.stream.Stream.decode
  File "av/codec/context.pyx", line 506, in av.codec.context.CodecContext.decode
  File "av/codec/context.pyx", line 409, in av.codec.context.CodecContext._send_packet_and_recv
  File "av/error.pyx", line 336, in av.error.err_check
av.error.EOFError: [Errno 541478725] End of file
[Errno 541478725] End of file
Tello: 00:59:04.204:  Info: quit
Tello: 00:59:04.204:  Info: state transit State::connecting -> State::quit
Tello: 00:59:05.165: Error: video recv: timeout
Tello: 00:59:05.165:  Info: start video (cmd=0x25 seq=0x01e4)
Tello: 00:59:05.166:  Info: exit from the video thread.
Tello: 00:59:06.149:  Info: exit from the recv thread.
electron@diynoMacBook-Pro TelloPy % 
```

##参考にしたサイト

- [GitHub hanyazou/TelloPy DJI Tello drone controller python package](https://github.com/hanyazou/TelloPy)

__TelloPy__の__公式リポジトリ__を見て、以下のコードを動かしてみたところ、成功しました。


###環境構築

```bash:Terminal
electron@diynoMacBook-Pro TelloPy % pip3 install av

（ 省略 ）

Collecting av
  Downloading av-8.0.3-cp39-cp39-macosx_10_9_x86_64.whl (21.5 MB)
     |████████████████████████████████| 21.5 MB 21.8 MB/s 
Installing collected packages: av
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /usr/local/include/python3.9/av
  sysconfig: /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9/av
Successfully installed av-8.0.3
```

```bash:Terminal
electron@diynoMacBook-Pro TelloPy % pip3 install opencv-python

（ 省略 ）

Requirement already satisfied: opencv-python in /usr/local/lib/python3.9/site-packages (4.5.3.56)
Requirement already satisfied: numpy>=1.19.3 in /usr/local/lib/python3.9/site-packages (from opencv-python) (1.21.1)
```

```bash:Terminal
electron@diynoMacBook-Pro TelloPy % pip3 install image

（ 省略 ）

Successfully built image
Installing collected packages: sqlparse, asgiref, django, image

（ 省略 ）

Successfully installed asgiref-3.4.1 django-3.2.5 image-1.5.33 sqlparse-0.4.1
electron@diynoMacBook-Pro TelloPy %
```