-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from vladimirvivien/webcam-upgrade
Code and example updates
- Loading branch information
Showing
17 changed files
with
553 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# fileserv | ||
|
||
A simple file server that can be used to view generated images in a remote environment. | ||
|
||
## Run | ||
|
||
``` | ||
go run fileserv.go ":port" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
var ( | ||
port = ":5050" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) > 2 { | ||
port = os.Args[1] | ||
} | ||
|
||
// serve examples dir | ||
log.Printf("serving files on port %s", port) | ||
http.Handle("/", http.FileServer(http.Dir("../"))) | ||
if err := http.ListenAndServe(port, nil); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# camserv | ||
|
||
This is a simple example shows how easy it is to use go4vl to | ||
create a simple web application to stream camera images. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"mime/multipart" | ||
"net/http" | ||
"net/textproto" | ||
|
||
"github.com/vladimirvivien/go4vl/device" | ||
"github.com/vladimirvivien/go4vl/v4l2" | ||
) | ||
|
||
var ( | ||
frames <-chan []byte | ||
) | ||
|
||
func imageServ(w http.ResponseWriter, req *http.Request) { | ||
mimeWriter := multipart.NewWriter(w) | ||
w.Header().Set("Content-Type", fmt.Sprintf("multipart/x-mixed-replace; boundary=%s", mimeWriter.Boundary())) | ||
partHeader := make(textproto.MIMEHeader) | ||
partHeader.Add("Content-Type", "image/jpeg") | ||
|
||
var frame []byte | ||
for frame = range frames { | ||
partWriter, err := mimeWriter.CreatePart(partHeader) | ||
if err != nil { | ||
log.Printf("failed to create multi-part writer: %s", err) | ||
return | ||
} | ||
|
||
if _, err := partWriter.Write(frame); err != nil { | ||
log.Printf("failed to write image: %s", err) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
port := ":9090" | ||
devName := "/dev/video0" | ||
flag.StringVar(&devName, "d", devName, "device name (path)") | ||
flag.StringVar(&port, "p", port, "webcam service port") | ||
|
||
camera, err := device.Open( | ||
devName, | ||
device.WithPixFormat(v4l2.PixFormat{PixelFormat: v4l2.PixelFmtMJPEG, Width: 640, Height: 480}), | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to open device: %s", err) | ||
} | ||
defer camera.Close() | ||
|
||
if err := camera.Start(context.TODO()); err != nil { | ||
log.Fatalf("camera start: %s", err) | ||
} | ||
|
||
frames = camera.GetOutput() | ||
|
||
log.Printf("Serving images: [%s/stream]", port) | ||
http.HandleFunc("/stream", imageServ) | ||
log.Fatal(http.ListenAndServe(port, nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/vladimirvivien/go4vl/device" | ||
) | ||
|
||
func main() { | ||
dev, err := device.Open("/dev/video0", device.WithBufferSize(1)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer dev.Close() | ||
|
||
if err := dev.Start(context.TODO()); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
frame := <-dev.GetOutput() | ||
|
||
file, err := os.Create("pic.jpg") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.Write(frame); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#! /bin/bash | ||
# Run the following once to pull correct dependencies | ||
go get github.com/vladimirvivien/go4vl@latest | ||
go get github.com/esimov/pigo/core@latest | ||
go get github.com/fogleman/gg@8febc0f526adecda6f8ae80f3869b7cd77e52984 | ||
|
||
go mod tidy | ||
|
||
go build . |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/vladimirvivien/go4vl/exampels/webcam | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/esimov/pigo v1.4.5 | ||
github.com/fogleman/gg v1.3.1-0.20210928143535-8febc0f526ad | ||
) | ||
|
||
require ( | ||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect | ||
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect | ||
) |
Oops, something went wrong.