将视频捕捉传输到网络的方法
时间:2024-03-12
浏览:385
作者:佚名
大家好,我们又见面了啊~本文《将视频捕捉传输到网络的方法》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
问题内容
我有下面的代码可以读取摄像头并将其显示在 gui 窗口中,我想将相同的内容推送到我的服务器,网址为 localhost:8080/cam,我该怎么做?
package main
import (
"gocv.io/x/gocv"
)
func main() {
webcam, _ := gocv.VideoCaptureDevice(0)
defer webcam.Close() // Close the cam once execution completed
window := gocv.NewWindow("Hello")
defer window.Close() // Close the GUI once execution completed, though it is done automatically
img := gocv.NewMat()
defer img.Close() // Close the img once execution completed
for {
webcam.Read(&img)
window.IMShow(img)
// Want to do streaming here to localhost:8080/cam
if window.WaitKey(1) == 27 { // 27 => Esc
break
}
}
}
解决方案
package mjpeg
import (
"fmt"
"log"
"net/http"
"sync"
"time"
)
// stream represents a single video feed.
type stream struct {
m map[chan []byte]bool
frame []byte
lock sync.mutex
frameinterval time.duration
}
const boundaryword = "mjpegboundary"
const headerf = "\r\n" +
"--" + boundaryword + "\r\n" +
"content-type: image/jpeg\r\n" +
"content-length: %d\r\n" +
"x-timestamp: 0.000000\r\n" +
"\r\n"
// servehttp responds to http requests with the mjpeg stream, implementing the http.handler interface.
func (s *stream) servehttp(w http.responsewriter, r *http.request) {
log.println("stream:", r.remoteaddr, "connected")
w.header().add("content-type", "multipart/x-mixed-replace;boundary="+boundaryword)
c := make(chan []byte)
s.lock.lock()
s.m[c] = true
s.lock.unlock()
for {
time.sleep(s.frameinterval)
b := <-c
_, err := w.write(b)
if err != nil {
break
}
}
s.lock.lock()
delete(s.m, c)
s.lock.unlock()
log.println("stream:", r.remoteaddr, "disconnected")
}
// updatejpeg pushes a new jpeg frame onto the clients.
func (s *stream) updatejpeg(jpeg []byte) {
header := fmt.sprintf(headerf, len(jpeg))
if len(s.frame) < len(jpeg)+len(header) {
s.frame = make([]byte, (len(jpeg)+len(header))*2)
}
copy(s.frame, header)
copy(s.frame[len(header):], jpeg)
s.lock.lock()
for c := range s.m {
// select to skip streams which are sleeping to drop frames.
// this might need more thought.
select {
case c <- s.frame:
default:
}
}
s.lock.unlock()
}
// newstream initializes and returns a new stream.
func newstream() *stream {
return &stream{
m: make(map[chan []byte]bool),
frame: make([]byte, len(headerf)),
frameinterval: 50 * time.millisecond,
}
}
我正在运行的代码是:
package main
import (
"fmt"
"log"
"net/http"
"github.com/hybridgroup/mjpeg"
_ "github.com/hybridgroup/mjpeg"
"gocv.io/x/gocv"
)
func main() {
deviceID := 0
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
// create the mjpeg stream
stream := mjpeg.NewStream()
// start capturing
go func(webcam *gocv.VideoCapture, stream *mjpeg.Stream) {
defer webcam.Close()
window := gocv.NewWindow("Capture Window")
defer window.Close()
img := gocv.NewMat()
defer img.Close()
fmt.Printf("Start reading device: %v\n", deviceID)
for {
if ok := webcam.Read(&img); !ok {
fmt.Printf("Device closed: %v\n", deviceID)
return
}
if img.Empty() {
continue
}
buf, _ := gocv.IMEncode(".jpg", img)
stream.UpdateJPEG(buf)
window.IMShow(img)
if window.WaitKey(1) == 27 { // 27 => Esc
break
}
}
}(webcam, stream)
http.Handle("/", stream)
log.Fatal(http.ListenAndServe(":8080", nil))
}
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.26
11.26
11.26
热门专题
