mirror of
https://github.com/esimov/pigo-wasm-demos.git
synced 2025-09-26 20:31:20 +08:00
wasm: check for error condition in image method
This commit is contained in:
@@ -3,6 +3,7 @@ package facemask
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/draw"
|
||||
"math"
|
||||
"sync"
|
||||
"syscall/js"
|
||||
@@ -147,9 +148,10 @@ func (c *Canvas) Render() error {
|
||||
var data = make([]byte, width*height*4)
|
||||
c.done = make(chan struct{})
|
||||
|
||||
img := pixels.LoadImage("/images/surgical-mask.png")
|
||||
mask = js.Global().Call("eval", "new Image()")
|
||||
mask.Set("src", "data:image/png;base64,"+img)
|
||||
if img, err := pixels.LoadImage("/images/surgical-mask.png"); err != nil {
|
||||
mask = js.Global().Call("eval", "new Image()")
|
||||
mask.Set("src", "data:image/png;base64,"+img)
|
||||
}
|
||||
|
||||
maskWidth = js.ValueOf(mask.Get("naturalWidth")).Int()
|
||||
maskHeight = js.ValueOf(mask.Get("naturalHeight")).Int()
|
||||
@@ -174,8 +176,7 @@ func (c *Canvas) Render() error {
|
||||
uint8Arr := js.Global().Get("Uint8Array").New(rgba)
|
||||
js.CopyBytesToGo(data, uint8Arr)
|
||||
|
||||
dx, dy := c.windowSize.width, c.windowSize.height
|
||||
gray := pixels.RgbaToGrayscale(data, dx, dy)
|
||||
gray := pixels.RgbaToGrayscale(data, width, height)
|
||||
|
||||
// Reset the data slice to its default values to avoid unnecessary memory allocation.
|
||||
// Otherwise, the GC won't clean up the memory address allocated by this slice
|
||||
@@ -264,16 +265,20 @@ func (c *Canvas) StartWebcam() (*Canvas, error) {
|
||||
}
|
||||
|
||||
// triangulate triangulates the image passed as pixel data
|
||||
func (c *Canvas) triangulate(data []uint8, dets []int) ([]uint8, error) {
|
||||
func (c *Canvas) triangulate(data []uint8, size image.Rectangle) ([]uint8, error) {
|
||||
// Converts the buffer array to an image.
|
||||
img := pixels.PixToImage(data, int(float64(dets[2])))
|
||||
img := pixels.PixToImage(data, size)
|
||||
|
||||
// Call the face triangulation algorithm.
|
||||
res, _, _, err := c.triangle.Draw(img, *c.processor, func() {})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pixels.ImgToPix(res), nil
|
||||
|
||||
dst := image.NewNRGBA(res.Bounds())
|
||||
draw.Draw(dst, res.Bounds(), res, image.Point{}, draw.Over)
|
||||
|
||||
return pixels.ImgToPix(dst), nil
|
||||
}
|
||||
|
||||
// drawDetection draws the detected faces and eyes.
|
||||
@@ -338,7 +343,8 @@ func (c *Canvas) drawDetection(data []uint8, dets [][]int) error {
|
||||
|
||||
// Triangulate the facemask part.
|
||||
c.lock.Lock()
|
||||
triangle, err := c.triangulate(imgData, det)
|
||||
rect := image.Rect(0, 0, scale, scale)
|
||||
triangle, err := c.triangulate(imgData, rect)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -102,7 +102,11 @@ func (c *Canvas) Render() {
|
||||
c.done = make(chan struct{})
|
||||
|
||||
for i, file := range sunglasses {
|
||||
img := pixels.LoadImage(file)
|
||||
img, err := pixels.LoadImage(file)
|
||||
if err != nil {
|
||||
c.Alert(err)
|
||||
return
|
||||
}
|
||||
eyemasks[i] = js.Global().Call("eval", "new Image()")
|
||||
eyemasks[i].Set("src", "data:image/png;base64,"+img)
|
||||
}
|
||||
@@ -110,7 +114,11 @@ func (c *Canvas) Render() {
|
||||
eyeMaskHeight = js.ValueOf(eyemasks[0].Get("naturalHeight")).Int()
|
||||
|
||||
for i, file := range masks {
|
||||
img := pixels.LoadImage(file)
|
||||
img, err := pixels.LoadImage(file)
|
||||
if err != nil {
|
||||
c.Alert(err)
|
||||
return
|
||||
}
|
||||
mouthmasks[i] = js.Global().Call("eval", "new Image()")
|
||||
mouthmasks[i].Set("src", "data:image/png;base64,"+img)
|
||||
}
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -105,11 +104,11 @@ func RgbaToGrayscale(data []uint8, dx, dy int) []uint8 {
|
||||
}
|
||||
|
||||
// LoadImage load the source image and encodes it to base64 format.
|
||||
func LoadImage(path string) string {
|
||||
func LoadImage(path string) (string, error) {
|
||||
href := js.Global().Get("location").Get("href")
|
||||
u, err := url.Parse(href.String())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
u.Path = path
|
||||
@@ -117,14 +116,14 @@ func LoadImage(path string) string {
|
||||
|
||||
resp, err := http.Get(u.String())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
return base64.StdEncoding.EncodeToString(b), nil
|
||||
}
|
||||
|
@@ -378,6 +378,7 @@ func (c *Canvas) triangulate(data []uint8, size image.Rectangle) ([]uint8, error
|
||||
|
||||
dst := image.NewNRGBA(triangled.Bounds())
|
||||
draw.Draw(dst, triangled.Bounds(), triangled, image.Point{}, draw.Over)
|
||||
|
||||
return pixels.ImgToPix(dst), nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user