Skip to content

Instantly share code, notes, and snippets.

@rustyeddy
Created April 12, 2020 02:59
Show Gist options
  • Save rustyeddy/522ba1a1da015ac9cfc0523c4b25dd67 to your computer and use it in GitHub Desktop.
Save rustyeddy/522ba1a1da015ac9cfc0523c4b25dd67 to your computer and use it in GitHub Desktop.
// Start Video opens the camera (sensor) and data (vidoe) starts streaming in.
// We will be streaming MJPEG for our initial use case.
func (vid *VideoPlayer) StartVideo() {
var err error
var buf []byte
l.Info("StartVideo Entered ... ")
defer l.Info("StartVideo Finished")
// This is pretty simple, almost every system that openCV supports
// will use an integer or a string, for example I have testing this
// on the following devices with the respective strings
//
// ubuntu-amd64 /dev/video0 v4l
// macos 0 builtin
// raspberry-pi 0 CSI
// nano pipeline gstreamer-pipeline
//
// use -camstr to make sure it comes out correctly
var cstr interface{}
cstr = config.Camstr
if cstr == "0" {
cstr = 0
}
defer log.Info().Str("devid", config.Camstr).Msg("entered start vid")
if vid.Recording {
l.Error("camera already recording")
return
}
// Video pipeline are named. Setting them is as simple as passing
// in the name.
//vid.SetPipeline("face")
// Both API REST server and MQTT server have started up, we are
// now waiting for requests to come in and instruct us wat to do.
for img := range vid.StreamVideo(config.Camstr) {
// Here we run through the AI, or whatever filter chain we are going
// to use. For now it is hard coded with face detect, this will become
// more flexible by allowing serial and concurrent filters.
//if vid.doAI {
// faced.FindFace(img)
//}
if vid.VideoPipeline != nil {
vid.VideoPipeline.Send(img)
}
// TODO: replace following when GoCV is not available.
// Finalize the annotated image. XXX maybe we create a write channel?
buf, err = gocv.IMEncode(".jpg", *img)
if err != nil {
l.Fatal("Failed encoding jpg")
}
vid.Stream.UpdateJPEG(buf)
// Check to see if a nsapshot has been requested, if so then
// take a snapshot. TODO put this in the video pipeline
if vid.SnapRequest {
fname := "pub/img/snapshot.jpg"
// Create the store
var ok bool
if ok = gocv.IMWrite(fname, *img); !ok {
log.Error().Str("filename", fname).Msg("Snapshot failed to save ")
}
fmt.Printf("snap requested %s .. ok: %v ", fname, ok)
log.Info().Str("filename", fname).Msg("Snapshot saved")
vid.SnapRequest = false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment