examples: add asciicam video to ascii in your terminal

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2024-09-11 14:20:13 +02:00
committed by Ron Evans
parent 7b3ce34eaa
commit 1dc4376427
4 changed files with 109 additions and 0 deletions

View File

@@ -2,6 +2,12 @@
GoCV comes with various useful command line utilities, that are also examples of how to use the package. GoCV comes with various useful command line utilities, that are also examples of how to use the package.
## ASCIIcam
Capture video from a connected webcam, and display it in the current terminal window in ASCII format.
https://github.com/hybridgroup/gocv/blob/release/cmd/asciicam/main.go
## Basic Drawing ## Basic Drawing
Demonstrates the basic drawing primitives available for drawing on images. Demonstrates the basic drawing primitives available for drawing on images.

76
cmd/asciicam/main.go Normal file
View File

@@ -0,0 +1,76 @@
// What it does:
//
// This example uses the VideoCapture class to capture video
// then displays the ASCII representation of the video frames.
//
// How to run:
//
// go run ./cmd/asciicam/main.go
//
package main
import (
"fmt"
"os"
"github.com/subeshb1/wasm-go-image-to-ascii/convert"
"gocv.io/x/gocv"
)
const (
// FixedWidth is the fixed width of the ASCII image
FixedWidth = 80
// FixedHeight is the fixed height of the ASCII image
FixedHeight = 40
)
var buf gocv.Mat
func main() {
if len(os.Args) < 2 {
fmt.Println("How to run:\n\tcaptest [camera ID]")
return
}
// parse args
deviceID := os.Args[1]
webcam, err := gocv.OpenVideoCapture(deviceID)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", deviceID)
return
}
defer webcam.Close()
// streaming, capture from webcam
buf = gocv.NewMat()
defer buf.Close()
asciiConverter := convert.NewImageConverter()
opts := &convert.Options{FixedWidth: FixedWidth, FixedHeight: FixedHeight, Colored: true}
fmt.Printf("Start reading device: %v\n", deviceID)
for {
if ok := webcam.Read(&buf); !ok {
fmt.Printf("Device error: %v\n", deviceID)
continue
}
if buf.Empty() {
continue
}
img, err := buf.ToImage()
if err != nil {
fmt.Printf("Error converting mat to image: %v\n", err)
continue
}
// clear screen
fmt.Print("\033[H\033[2J")
// print the ASCII representation of the frame
fmt.Println(asciiConverter.Image2ASCIIString(img, opts))
}
}

6
go.mod
View File

@@ -5,4 +5,10 @@ go 1.21
require ( require (
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e
github.com/pascaldekloe/goe v0.1.0 github.com/pascaldekloe/goe v0.1.0
github.com/subeshb1/wasm-go-image-to-ascii v0.0.0-20200725121413-d828986df340
)
require (
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
) )

21
go.sum
View File

@@ -1,4 +1,25 @@
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e h1:xCcwD5FOXul+j1dn8xD16nbrhJkkum/Cn+jTd/u1LhY= github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e h1:xCcwD5FOXul+j1dn8xD16nbrhJkkum/Cn+jTd/u1LhY=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs= github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subeshb1/wasm-go-image-to-ascii v0.0.0-20200725121413-d828986df340 h1:EN1NUPDAdhpWv9zNNdbovzqfJaRmnrwWaq0huRzQp9I=
github.com/subeshb1/wasm-go-image-to-ascii v0.0.0-20200725121413-d828986df340/go.mod h1:A2X7CsJFb8jEdYaWeCbs2HydXC69J4Iaw4DM+bly5iw=
github.com/wayneashleyberry/terminal-dimensions v1.0.0/go.mod h1:PW2XrtV6KmKOPhuf7wbtcmw1/IFnC39mryRET2XbxeE=
golang.org/x/sys v0.0.0-20181019160139-8e24a49d80f8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=