From 198574ce1e964ed070e287ec1d116e6308c87dec Mon Sep 17 00:00:00 2001 From: esimov Date: Sun, 22 May 2022 16:48:52 +0300 Subject: [PATCH] wasm: check for error condition in image method --- facemask/canvas.go | 24 +++++++++++++++--------- masquerade/canvas.go | 12 ++++++++++-- pixels/image.go | 11 +++++------ triangulate/canvas.go | 1 + 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/facemask/canvas.go b/facemask/canvas.go index da49d82..a5429d1 100644 --- a/facemask/canvas.go +++ b/facemask/canvas.go @@ -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 } diff --git a/masquerade/canvas.go b/masquerade/canvas.go index 7e57586..2ca1c54 100644 --- a/masquerade/canvas.go +++ b/masquerade/canvas.go @@ -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) } diff --git a/pixels/image.go b/pixels/image.go index 53b29f4..7fa2842 100644 --- a/pixels/image.go +++ b/pixels/image.go @@ -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 } diff --git a/triangulate/canvas.go b/triangulate/canvas.go index 1e99d98..e45c36d 100644 --- a/triangulate/canvas.go +++ b/triangulate/canvas.go @@ -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 }