Golang : Activate web camera and broadcast out base64 encoded images
For this tutorial, we will learn how to activate web camera with Golang + OpenCV and stream out base64 encoded images via HTTP server. This tutorial is derived from the lesson that I use to teach my son on the subject of computer vision and Golang. It is based on the Go-OpenCV tutorial on activating web camera (https://github.com/lazywei/go-opencv/blob/master/samples/webcam.go).
The program will activate the web camera when you point your web browser to localhost:8080
and begin streaming out base64 encoded images. You will have to scroll down the see the new images. LOL! Good enough to get a 3 years old kid excited about computer vision.
Before you start.
go get github.com/lazywei/go-opencv
Here you go!
package main
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/lazywei/go-opencv/opencv"
"image/png"
"net/http"
)
func broadcast(w http.ResponseWriter, r *http.Request) {
webCamera := opencv.NewCameraCapture(0)
if webCamera == nil {
panic("Unable to open camera")
}
defer webCamera.Release()
for {
if webCamera.GrabFrame() {
imgFrame := webCamera.RetrieveFrame(1)
if imgFrame != nil {
//fmt.Println(imgFrame.ImageSize())
//fmt.Println(imgFrame.ToImage())
// convert IplImage(Intel Image Processing Library)
// to image.Image
goImgFrame := imgFrame.ToImage()
// and then convert to []byte
// with the help of png.Encode() function
frameBuffer := new(bytes.Buffer)
//frameBuffer := make([]byte, imgFrame.ImageSize())
err := png.Encode(frameBuffer, goImgFrame)
if err != nil {
panic(err)
}
// convert the buffer bytes to base64 string - use buf.Bytes() for new image
imgBase64Str := base64.StdEncoding.EncodeToString(frameBuffer.Bytes())
// Embed into an html without PNG file
img2html := "<html><body><img src=\"data:image/png;base64," + imgBase64Str + "\" /></body></html>"
w.Write([]byte(fmt.Sprintf(img2html)))
// TODO :
// encode frames to stream via WebRTC
fmt.Println("Streaming....")
}
}
}
}
func main() {
fmt.Println("Broadcasting...")
mux := http.NewServeMux()
mux.HandleFunc("/", broadcast)
http.ListenAndServe(":8080", mux)
}
streaming out base64 encoded images to browser
NOTES:
In case you get these error messages :
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKGCONFIGPATH environment variable
Fix this problem by installing openCV.
On MacOSX :
>brew install homebrew/science/opencv
References:
https://github.com/lazywei/go-opencv/blob/master/opencv/goimage.go
https://www.socketloop.com/references/golang-net-http-servecontent-function-example
http://stackoverflow.com/questions/35667501/golang-seeking-through-a-video-serving-as-bytes
See also : Golang : Save webcamera frames to video file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.6k Golang : Query string with space symbol %20 in between
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+19.2k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+24k Golang : Fix type interface{} has no field or no methods and type assertions example
+17.7k Golang : Upload/Receive file progress indicator
+16.5k Golang : Test floating point numbers not-a-number and infinite example
+12.5k Golang : "https://" not allowed in import path
+12.4k Golang : Search and extract certain XML data example
+20.1k Golang : How to run your code only once with sync.Once object
+11.8k Golang : How to detect a server/machine network interface capabilities?
+15.5k Golang : Find location by IP address and display with Google Map
+5.8k Unix/Linux : How to test user agents blocked successfully ?