6 Commits
1.2 ... 1.3

Author SHA1 Message Date
Milan Nikolic
cbc7d21b23 Shorten options 2018-01-27 03:50:50 +01:00
Milan Nikolic
45ca5bdedb New release 2018-01-27 03:28:09 +01:00
Milan Nikolic
173deebc88 Add video file reader 2018-01-27 03:22:33 +01:00
Milan Nikolic
7392d8d9b8 WebGL is now default 2018-01-27 01:59:46 +01:00
Milan Nikolic
1c9dfcb84c Move encoder 2018-01-27 01:47:04 +01:00
Milan Nikolic
f65a7cf1aa Fix golibjpegturbo missing repo 2018-01-10 11:39:04 +01:00
14 changed files with 224 additions and 74 deletions

View File

@@ -18,10 +18,10 @@ or
Binaries are compiled with static OpenCV library:
- [Android 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-android.tar.gz)
- [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-64bit.tar.gz)
- [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2-RPi.tar.gz)
- [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.2/cam2ip-1.2.zip)
- [Linux 64bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-64bit.tar.gz)
- [RPi 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-RPi.tar.gz)
- [RPi3 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3-RPi3.tar.gz)
- [Windows 32bit](https://github.com/gen2brain/cam2ip/releases/download/1.3/cam2ip-1.3.zip)
### Installation
@@ -38,16 +38,18 @@ Usage of ./cam2ip:
Bind address (default ":56000")
-delay int
Delay between frames, in milliseconds (default 10)
-frame-height float
-height float
Frame height (default 480)
-frame-width float
-width float
Frame width (default 640)
-htpasswd-file string
Path to htpasswd file, if empty auth is disabled
-index int
Camera index
-webgl
Use WebGL to draw images
-nowebgl
Disable WebGL drawing of images (html handler)
-video-file string
Use video file instead of camera
```
### Handlers

View File

@@ -7,11 +7,12 @@ import (
"github.com/gen2brain/cam2ip/camera"
"github.com/gen2brain/cam2ip/server"
"github.com/gen2brain/cam2ip/video"
)
const (
name = "cam2ip"
version = "1.0"
version = "1.3"
)
func main() {
@@ -19,11 +20,12 @@ func main() {
flag.IntVar(&srv.Index, "index", 0, "Camera index")
flag.IntVar(&srv.Delay, "delay", 10, "Delay between frames, in milliseconds")
flag.Float64Var(&srv.FrameWidth, "frame-width", 640, "Frame width")
flag.Float64Var(&srv.FrameHeight, "frame-height", 480, "Frame height")
flag.BoolVar(&srv.WebGL, "webgl", false, "Use WebGL to draw images")
flag.Float64Var(&srv.FrameWidth, "width", 640, "Frame width")
flag.Float64Var(&srv.FrameHeight, "height", 480, "Frame height")
flag.BoolVar(&srv.NoWebGL, "nowebgl", false, "Disable WebGL drawing of images (html handler)")
flag.StringVar(&srv.Bind, "bind-addr", ":56000", "Bind address")
flag.StringVar(&srv.Htpasswd, "htpasswd-file", "", "Path to htpasswd file, if empty auth is disabled")
flag.StringVar(&srv.FileName, "video-file", "", "Use video file instead of camera")
flag.Parse()
srv.Name = name
@@ -38,16 +40,35 @@ func main() {
}
}
srv.Camera, err = camera.NewCamera(srv.Index)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
if srv.FileName != "" {
if _, err = os.Stat(srv.FileName); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}
srv.Camera.SetProperty(camera.PropFrameWidth, srv.FrameWidth)
srv.Camera.SetProperty(camera.PropFrameHeight, srv.FrameHeight)
if srv.FileName != "" {
vid, err := video.New(srv.FileName)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
defer srv.Camera.Close()
srv.Reader = vid
} else {
cam, err := camera.New(srv.Index)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
cam.SetProperty(camera.PropFrameWidth, srv.FrameWidth)
cam.SetProperty(camera.PropFrameHeight, srv.FrameHeight)
srv.Reader = cam
}
defer srv.Reader.Close()
fmt.Fprintf(os.Stderr, "Listening on %s\n", srv.Bind)

View File

@@ -53,14 +53,12 @@ const (
// Camera represents camera.
type Camera struct {
Index int
camera *opencv.Capture
}
// NewCamera returns new Camera for given camera index.
func NewCamera(index int) (camera *Camera, err error) {
// New returns new Camera for given camera index.
func New(index int) (camera *Camera, err error) {
camera = &Camera{}
camera.Index = index
camera.camera = opencv.NewCameraCapture(index)
if camera.camera == nil {

View File

@@ -11,7 +11,7 @@ import (
)
func TestCamera(t *testing.T) {
camera, err := NewCamera(1)
camera, err := New(1)
if err != nil {
t.Fatal(err)
}

View File

@@ -1,15 +1,16 @@
package camera
// Package encoder.
package encoder
import (
"image"
//"image/jpeg"
"io"
jpeg "github.com/kjk/golibjpegturbo"
jpeg "github.com/antonini/golibjpegturbo"
)
// NewEncoder returns a new Encoder.
func NewEncoder(w io.Writer) *Encoder {
// New returns a new Encoder.
func New(w io.Writer) *Encoder {
return &Encoder{w}
}

View File

@@ -13,7 +13,7 @@ type HTML struct {
}
// NewHTML returns new HTML handler.
func NewHTML(bind string, width, height float64, gl bool) *HTML {
func NewHTML(bind string, width, height float64, nogl bool) *HTML {
h := &HTML{}
b := strings.Split(bind, ":")
@@ -21,9 +21,9 @@ func NewHTML(bind string, width, height float64, gl bool) *HTML {
bind = "127.0.0.1" + bind
}
tpl := html
if gl {
tpl = htmlWebGL
tpl := htmlWebGL
if nogl {
tpl = html
}
tpl = strings.Replace(tpl, "{BIND}", bind, -1)

View File

@@ -4,17 +4,18 @@ import (
"log"
"net/http"
"github.com/gen2brain/cam2ip/camera"
"github.com/gen2brain/cam2ip/encoder"
"github.com/gen2brain/cam2ip/reader"
)
// JPEG handler.
type JPEG struct {
camera *camera.Camera
reader reader.ImageReader
}
// NewJPEG returns new JPEG handler.
func NewJPEG(camera *camera.Camera) *JPEG {
return &JPEG{camera}
func NewJPEG(reader reader.ImageReader) *JPEG {
return &JPEG{reader}
}
// ServeHTTP handles requests on incoming connections.
@@ -28,15 +29,13 @@ func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-store, no-cache")
w.Header().Add("Content-Type", "image/jpeg")
img, err := j.camera.Read()
img, err := j.reader.Read()
if err != nil {
log.Printf("jpeg: read: %v", err)
return
}
enc := camera.NewEncoder(w)
err = enc.Encode(img)
err = encoder.New(w).Encode(img)
if err != nil {
log.Printf("jpeg: encode: %v", err)
return

View File

@@ -9,18 +9,19 @@ import (
"net/textproto"
"time"
"github.com/gen2brain/cam2ip/camera"
"github.com/gen2brain/cam2ip/encoder"
"github.com/gen2brain/cam2ip/reader"
)
// MJPEG handler.
type MJPEG struct {
camera *camera.Camera
reader reader.ImageReader
delay int
}
// NewMJPEG returns new MJPEG handler.
func NewMJPEG(camera *camera.Camera, delay int) *MJPEG {
return &MJPEG{camera, delay}
func NewMJPEG(reader reader.ImageReader, delay int) *MJPEG {
return &MJPEG{reader, delay}
}
// ServeHTTP handles requests on incoming connections.
@@ -55,15 +56,13 @@ loop:
continue
}
img, err := m.camera.Read()
img, err := m.reader.Read()
if err != nil {
log.Printf("mjpeg: read: %v", err)
continue
}
enc := camera.NewEncoder(partWriter)
err = enc.Encode(img)
err = encoder.New(partWriter).Encode(img)
if err != nil {
log.Printf("mjpeg: encode: %v", err)
continue

View File

@@ -8,34 +8,34 @@ import (
"golang.org/x/net/websocket"
"github.com/gen2brain/cam2ip/camera"
"github.com/gen2brain/cam2ip/encoder"
"github.com/gen2brain/cam2ip/reader"
)
// Socket handler.
type Socket struct {
camera *camera.Camera
reader reader.ImageReader
delay int
}
// NewSocket returns new socket handler.
func NewSocket(camera *camera.Camera, delay int) websocket.Handler {
s := &Socket{camera, delay}
func NewSocket(reader reader.ImageReader, delay int) websocket.Handler {
s := &Socket{reader, delay}
return websocket.Handler(s.write)
}
// write writes images to socket
func (s *Socket) write(ws *websocket.Conn) {
for {
img, err := s.camera.Read()
img, err := s.reader.Read()
if err != nil {
log.Printf("socket: read: %v", err)
continue
break
}
w := new(bytes.Buffer)
enc := camera.NewEncoder(w)
err = enc.Encode(img)
err = encoder.New(w).Encode(img)
if err != nil {
log.Printf("socket: encode: %v", err)
continue

View File

@@ -3,7 +3,7 @@
CHROOT="/usr/x86_64-pc-linux-gnu-static"
MINGW="/usr/i686-w64-mingw32"
RPI="/usr/armv6j-hardfloat-linux-gnueabi"
ANDROID="/opt/android-toolchain-arm7"
RPI3="/usr/armv7a-hardfloat-linux-gnueabi"
mkdir -p build
@@ -30,11 +30,10 @@ CGO_CFLAGS="-I$RPI/usr/include" \
CC="armv6j-hardfloat-linux-gnueabi-gcc" CXX="armv6j-hardfloat-linux-gnueabi-g++" \
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -v -x -o build/cam2ip.linux.arm -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip
PATH="$PATH:$ANDROID/bin" \
PKG_CONFIG="$ANDROID/bin/arm-linux-androideabi-pkg-config" \
PKG_CONFIG_PATH="$ANDROID/lib/pkgconfig" \
PKG_CONFIG_LIBDIR="$ANDROID/lib/pkgconfig" \
CGO_LDFLAGS="-L$ANDROID/lib" \
CGO_CFLAGS="-I$ANDROID/include" \
CC="arm-linux-androideabi-gcc" CXX="arm-linux-androideabi-g++" \
CGO_ENABLED=1 GOOS=android GOARCH=arm go build -v -x -o build/cam2ip.android.arm -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip
PKG_CONFIG="/usr/bin/armv7a-hardfloat-linux-gnueabi-pkg-config" \
PKG_CONFIG_PATH="$RPI3/usr/lib/pkgconfig" \
PKG_CONFIG_LIBDIR="$RPI3/usr/lib/pkgconfig" \
CGO_LDFLAGS="-L$RPI3/usr/lib" \
CGO_CFLAGS="-I$RPI3/usr/include" \
CC="armv7a-hardfloat-linux-gnueabi-gcc" CXX="armv7a-hardfloat-linux-gnueabi-g++" \
CGO_ENABLED=1 GOOS=linux GOARCH=arm go build -v -x -o build/cam2ip.linux.arm7 -ldflags "-linkmode external -s -w" github.com/gen2brain/cam2ip

15
reader/reader.go Normal file
View File

@@ -0,0 +1,15 @@
// Package reader.
package reader
import (
"image"
)
// ImageReader interface
type ImageReader interface {
// Read reads next frame from camera/video and returns image.
Read() (img image.Image, err error)
// Close closes camera/video.
Close() error
}

View File

@@ -8,8 +8,8 @@ import (
"github.com/abbot/go-http-auth"
"github.com/gen2brain/cam2ip/camera"
"github.com/gen2brain/cam2ip/handlers"
"github.com/gen2brain/cam2ip/reader"
)
// Server struct.
@@ -20,13 +20,17 @@ type Server struct {
Bind string
Htpasswd string
Index int
Delay int
Index int
Delay int
FrameWidth float64
FrameHeight float64
WebGL bool
Camera *camera.Camera
NoWebGL bool
FileName string
Reader reader.ImageReader
}
// NewServer returns new Server.
@@ -43,11 +47,11 @@ func (s *Server) ListenAndServe() error {
basic = auth.NewBasicAuthenticator(realm, auth.HtpasswdFileProvider(s.Htpasswd))
}
http.Handle("/html", newAuthHandler(handlers.NewHTML(s.Bind, s.FrameWidth, s.FrameHeight, s.WebGL), basic))
http.Handle("/jpeg", newAuthHandler(handlers.NewJPEG(s.Camera), basic))
http.Handle("/mjpeg", newAuthHandler(handlers.NewMJPEG(s.Camera, s.Delay), basic))
http.Handle("/html", newAuthHandler(handlers.NewHTML(s.Bind, s.FrameWidth, s.FrameHeight, s.NoWebGL), basic))
http.Handle("/jpeg", newAuthHandler(handlers.NewJPEG(s.Reader), basic))
http.Handle("/mjpeg", newAuthHandler(handlers.NewMJPEG(s.Reader, s.Delay), basic))
http.Handle("/socket", handlers.NewSocket(s.Camera, s.Delay))
http.Handle("/socket", handlers.NewSocket(s.Reader, s.Delay))
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)

50
video/video.go Normal file
View 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
}

62
video/video_test.go Normal file
View File

@@ -0,0 +1,62 @@
package video
import (
"fmt"
"image/jpeg"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestVideo(t *testing.T) {
video, err := New("test.mp4")
if err != nil {
t.Fatal(err)
}
defer video.Close()
tmpdir, err := ioutil.TempDir(os.TempDir(), "cam2ip")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tmpdir)
var i int
var n int = 10
timeout := time.After(time.Duration(n) * time.Second)
for {
select {
case <-timeout:
//fmt.Printf("Fps: %d\n", i/n)
return
default:
i += 1
img, err := video.Read()
if err != nil {
t.Error(err)
}
file, err := os.Create(filepath.Join(tmpdir, fmt.Sprintf("%03d.jpg", i)))
if err != nil {
t.Error(err)
}
err = jpeg.Encode(file, img, &jpeg.Options{Quality: 75})
if err != nil {
t.Error(err)
}
err = file.Close()
if err != nil {
t.Error(err)
}
}
}
}