| commit | d456f6c5f57228857e6e2727df178c8350018386 | [log] [tgz] |
|---|---|---|
| author | Brian Stark <54910472+bpstark@users.noreply.github.com> | Mon Apr 01 17:06:31 2024 |
| committer | GitHub <noreply@github.com> | Mon Apr 01 17:06:31 2024 |
| tree | 188f448ccf1add8aa99c79536488d50802b9adcf | |
| parent | f8ddb313107678fd170755be03878eadadd56a24 [diff] |
Prevent file handles from leaking on failed open. (#67) * Prevent file handles from leaking on failed open. Since the webcam open function only returns the open fh on success, we need to close the FH ourselves in the event that the subsequent tests fail. --------- Co-authored-by: Brian Stark <bstark@carbonrobotics.com>
This is a go library for working with webcams and other video capturing devices. It depends entirely on V4L2 framework, thus will compile and work only on Linux machine.
$ go get github.com/blackjack/webcam
import "github.com/blackjack/webcam" // ... cam, err := webcam.Open("/dev/video0") // Open webcam if err != nil { panic(err.Error()) } defer cam.Close() // ... // Setup webcam image format and frame size here (see examples or documentation) // ... err = cam.StartStreaming() if err != nil { panic(err.Error()) } for { err = cam.WaitForFrame(timeout) switch err.(type) { case nil: case *webcam.Timeout: fmt.Fprint(os.Stderr, err.Error()) continue default: panic(err.Error()) } frame, err := cam.ReadFrame() if len(frame) != 0 { // Process frame } else if err != nil { panic(err.Error()) } }
For more detailed example see examples folder The number of frame buffers used may be set as:
// If already streaming, stop streaming. if streaming_on { cam.StopStreaming() } err = cam.SetBufferCount(64)
The library is still under development so API changes can happen. Currently library supports streaming using only MMAP method, which should be sufficient for most of devices available on the market. Other streaming methods can be added in future (please create issue if you need this).
Also currently image format is defined by 4-byte code received from V4L2, which is good in terms of compatibility with different versions of Linux kernel, but not very handy if you want to do some image manipulations. Plans are to aligh V4L2 image format codes with Image package from Go library.
See LICENSE file