mirror of
https://github.com/gen2brain/cam2ip.git
synced 2026-01-10 00:28:32 +00:00
Add video file reader
This commit is contained in:
50
video/video.go
Normal file
50
video/video.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Package video.
|
||||
package video
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"github.com/lazywei/go-opencv/opencv"
|
||||
)
|
||||
|
||||
// Video represents video.
|
||||
type Video struct {
|
||||
video *opencv.Capture
|
||||
}
|
||||
|
||||
// New returns new Video for given path.
|
||||
func New(filename string) (video *Video, err error) {
|
||||
video = &Video{}
|
||||
|
||||
video.video = opencv.NewFileCapture(filename)
|
||||
if video.video == nil {
|
||||
err = fmt.Errorf("video: can not open video %s", filename)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Read reads next frame from video and returns image.
|
||||
func (v *Video) Read() (img image.Image, err error) {
|
||||
if v.video.GrabFrame() {
|
||||
frame := v.video.RetrieveFrame(1)
|
||||
img = frame.ToImage()
|
||||
} else {
|
||||
err = fmt.Errorf("video: can not grab frame")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Close closes video.
|
||||
func (v *Video) Close() (err error) {
|
||||
if v.video == nil {
|
||||
err = fmt.Errorf("video: video is not opened")
|
||||
return
|
||||
}
|
||||
|
||||
v.video.Release()
|
||||
v.video = nil
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user