やったこと
__前回の記事__で作成したソースコードを拡張しました。
前回は、Telloをキーボード操作しながら、Telloの単眼カメラから送られてくる画像を、Macbookのウィンドウにそのまま出力しました。(ウィンドウ名: "drone")
今回は、オリジナルの画像に加えて、OpenCV2の組込みメソッドで加工した画像を、別のウィンドウに出力してみました。
( 追加したウィンドウ )
- 'Canny' ウィンドウ : エッジ検出処理をかけたフレーム画像
- 'bitwised_image' ウィンドウ : 色反転処理をかけたフレーム画像
実行方法
Terminal
% python3 keyboard-control-multi_window.py
動作結果
まず、- キーボード入力に反応して、意図した通りにTelloが動きました。
さらに、3つのウィンドウが立ち上がり、各ウィンドウに、オリジナル画像、エッジ処理後の画像、色反転処理後の画像が、次のように正常に出力されました。
-
Tello離陸前に、Telloを手で持って動かしたり、カメラの前にかざした手を動かすと、遅延なく、リアルタイムに画像がウィンドウに表示される。
-
Telloが移動中の単眼カメラのフレーム画像が、滑らかにウィンドウに出力される。
スクリプト・ファイル
keyboard-control-multi_window.py
from timeout_decorator import timeout, TimeoutError
from djitellopy import Tello
import cv2, math, time
TIMEOUT_SEC = 0.1
@timeout(TIMEOUT_SEC)
def input_with_timeout(msg=None):
return input(msg)
tello = Tello()
tello.connect()
tello.streamon()
frame_read = tello.get_frame_read()
# tello.takeoff()
while True:
# In reality you want to display frames in a seperate thread. Otherwise
# they will freeze while the drone moves.
img = frame_read.frame
cv2.imshow("drone", img)
cv2.imshow('Canny', cv2.Canny(img, 100, 200))
bitwised_img = cv2.bitwise_not(img)
cv2.imshow('Bitwised', bitwised_img)
#次の行(key = cv2.・・・)を削除すると、画像が受信できなくなる。
key = cv2.waitKey(1) & 0xff
try:
msg = input_with_timeout('\n{}秒以内に操作コマンドを入力して下さい :'.format(TIMEOUT_SEC))
print('\n操作コマンド: {} を受信しました。\n'.format(msg))
if msg == "i":
tello.takeoff()
elif msg == "w":
tello.move_forward(30)
elif msg == "s":
tello.move_back(30)
elif msg == "a":
tello.move_left(30)
elif msg == "d":
tello.move_right(30)
elif msg == "e":
tello.rotate_clockwise(30)
elif msg == "q":
tello.rotate_counter_clockwise(30)
elif msg == "r":
tello.move_up(30)
elif msg == "f":
tello.move_down(30)
elif msg == "g":
tello.land()
except TimeoutError:
print('\n操作コマンド入力時間切れ。次のフレーム画像を読み込みます。\n')
tello.land()