Draw face detection result
This commit is contained in:
parent
09d2d3b92d
commit
0df34c33d2
2 changed files with 43 additions and 7 deletions
3
deps.edn
3
deps.edn
|
|
@ -1,2 +1,3 @@
|
||||||
{:paths ["src"]
|
{:paths ["src"]
|
||||||
:deps {thheller/shadow-cljs {:mvn/version "2.8.109"}}}
|
:deps {thheller/shadow-cljs {:mvn/version "2.8.109"}
|
||||||
|
appliedscience/js-interop {:mvn/version "0.2.5"}}}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,48 @@
|
||||||
(ns heyarne.all-my-friends.core
|
(ns heyarne.all-my-friends.core
|
||||||
(:require ["@tensorflow-models/facemesh" :as facemesh]))
|
(:require ["@tensorflow/tfjs-core" :as tf]
|
||||||
|
["@tensorflow-models/facemesh" :as facemesh]
|
||||||
|
[applied-science.js-interop :as j]
|
||||||
|
[goog.dom :as dom]))
|
||||||
|
|
||||||
|
(def img (js/document.querySelector "img"))
|
||||||
|
|
||||||
|
(defn draw-results [img predictions]
|
||||||
|
(let [bounds (j/lookup (.getBoundingClientRect img))
|
||||||
|
parent (dom/getParentElement img)
|
||||||
|
canvas (doto (dom/createElement "canvas")
|
||||||
|
(.setAttribute "width" (:width bounds))
|
||||||
|
(.setAttribute "height" (:height bounds))
|
||||||
|
(.setAttribute "style" (str "display: block; "
|
||||||
|
"position: absolute; "
|
||||||
|
"top: " (:top bounds) "px; "
|
||||||
|
"left: " (:left bounds) "px")))
|
||||||
|
ctx (. canvas (getContext "2d"))]
|
||||||
|
;; remove previous results
|
||||||
|
(doseq [result-canvas (.querySelectorAll parent "canvas")]
|
||||||
|
(dom/removeNode result-canvas))
|
||||||
|
;; draw and append new results
|
||||||
|
(set! (.-strokeStyle ctx) "white")
|
||||||
|
(doseq [prediction predictions
|
||||||
|
[x y _] (j/get prediction :scaledMesh)]
|
||||||
|
(.beginPath ctx)
|
||||||
|
(.ellipse ctx x y 2 2 0 0 (* 2 Math/PI))
|
||||||
|
(.stroke ctx))
|
||||||
|
(dom/appendChild parent canvas)))
|
||||||
|
|
||||||
(defn detect-faces [model]
|
(defn detect-faces [model]
|
||||||
(println "Facemesh loaded")
|
(println "Facemesh loaded")
|
||||||
(.. model
|
(.. model
|
||||||
(estimateFaces (js/document.querySelector "img"))
|
(estimateFaces img)
|
||||||
(then (fn [predictions]
|
(then (fn [predictions]
|
||||||
(println "Predictions" predictions)))))
|
(set! (.-predictions js/window) predictions)
|
||||||
|
#_(println "Predictions" predictions)
|
||||||
|
(draw-results img predictions)))))
|
||||||
|
|
||||||
(.. facemesh
|
(defonce
|
||||||
(load)
|
init
|
||||||
(then detect-faces))
|
(do
|
||||||
|
(println "Initializing…")
|
||||||
|
(.. tf
|
||||||
|
(setBackend "webgl")
|
||||||
|
(then #(.load facemesh))
|
||||||
|
(then detect-faces))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue