tello: can stream drone video thru to opencv

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2018-04-18 22:46:51 +02:00
parent 296bd767ae
commit 79ffaab5b4
2 changed files with 88 additions and 0 deletions

86
examples/tello_opencv.go Normal file
View File

@ -0,0 +1,86 @@
// +build example
//
// Do not build by default.
/*
You must have ffmpeg and ffplay installed in order to run this code. it will connect to the Tello
and then open a window using ffplay showing the streaming video.
How to run
go run examples/tello_opencv.go
*/
package main
import (
"fmt"
"io"
"os/exec"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/platforms/dji/tello"
"gobot.io/x/gobot/platforms/opencv"
"gocv.io/x/gocv"
)
const (
frameSize = 960 * 720 * 3
)
func main() {
drone := tello.NewDriver("8890")
window := opencv.NewWindowDriver()
work := func() {
ffmpeg := exec.Command("ffmpeg", "-i", "pipe:0", "-pix_fmt", "bgr24", "-vcodec", "rawvideo",
"-an", "-sn", "-f", "rawvideo", "pipe:1")
ffmpegIn, _ := ffmpeg.StdinPipe()
ffmpegOut, _ := ffmpeg.StdoutPipe()
if err := ffmpeg.Start(); err != nil {
fmt.Println(err)
return
}
go func() {
for {
buf := make([]byte, frameSize)
if _, err := io.ReadFull(ffmpegOut, buf); err != nil {
fmt.Println(err)
continue
}
img := gocv.NewMatFromBytes(720, 960, gocv.MatTypeCV8UC3, buf)
if img.Empty() {
continue
}
window.ShowImage(img)
window.WaitKey(1)
}
}()
drone.On(tello.ConnectedEvent, func(data interface{}) {
fmt.Println("Connected")
drone.StartVideo()
gobot.Every(1*time.Second, func() {
drone.StartVideo()
})
})
drone.On(tello.VideoFrameEvent, func(data interface{}) {
pkt := data.([]byte)
if _, err := ffmpegIn.Write(pkt); err != nil {
fmt.Println(err)
}
})
}
robot := gobot.NewRobot("tello",
[]gobot.Connection{},
[]gobot.Device{drone, window},
work,
)
robot.Start()
}

View File

@ -59,3 +59,5 @@ https://tellopilots.com/forums/tello-development.8/
Special thanks to [@Kragrathea](https://github.com/Kragrathea) who figured out a LOT of the packets and code as implemented in C#: [https://github.com/Kragrathea/TelloPC](https://github.com/Kragrathea/TelloPC)
Also thanks to [@microlinux](https://github.com/microlinux) with the Python library which served as the first example for the Tello community: [https://github.com/microlinux/tello](https://github.com/microlinux/tello)
Thank you to bluejune for the [https://bitbucket.org/PingguSoft/pytello](https://bitbucket.org/PingguSoft/pytello) repo, especially the Wireshark Lua dissector which has proven indispensable.