Skip to content

Commit

Permalink
tello: can stream drone video thru to opencv
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Apr 18, 2018
1 parent 296bd76 commit 79ffaab
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
86 changes: 86 additions & 0 deletions examples/tello_opencv.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 2 additions & 0 deletions platforms/dji/tello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 79ffaab

Please sign in to comment.