Refactor drawing code into separate namespace
This commit is contained in:
parent
663ddfbce2
commit
670e211faf
3 changed files with 49 additions and 44 deletions
|
|
@ -1,7 +1,8 @@
|
|||
(ns heyarne.all-my-friends.views
|
||||
(:require [reagent.core :as r]
|
||||
[applied-science.js-interop :as j]
|
||||
[heyarne.all-my-friends.facemesh :refer [webcam-facemesh]]))
|
||||
[heyarne.all-my-friends.facemesh :refer [webcam-facemesh]]
|
||||
[heyarne.all-my-friends.visualize :as vis]))
|
||||
|
||||
;; minimalistic re-frame-like event handling
|
||||
|
||||
|
|
@ -39,12 +40,6 @@ Seit der globalen Covid19-Pandemie sind wir alle dazu gezwungen, auf physischen
|
|||
(.drawImage ctx video-elem 0 0 (.-width canvas) (.-height canvas))
|
||||
(.getImageData ctx 0 0 (.-width canvas) (.-height canvas))))
|
||||
|
||||
(defn draw-path [ctx path]
|
||||
(.moveTo ctx (aget path 0 0) (aget path 0 1))
|
||||
(doseq [[x y] (rest path)]
|
||||
(.lineTo ctx x y))
|
||||
(.stroke ctx))
|
||||
|
||||
(defn preview [{{:keys [video predictions]} :snapshot}]
|
||||
(let [canvas (js/document.createElement "canvas")
|
||||
ctx (.getContext canvas "2d")]
|
||||
|
|
@ -52,23 +47,7 @@ Seit der globalen Covid19-Pandemie sind wir alle dazu gezwungen, auf physischen
|
|||
(set! (.-predictions js/window) predictions)
|
||||
(set! (.-width canvas) (.-width video))
|
||||
(set! (.-height canvas) (.-height video))
|
||||
(set! (.-strokeStyle ctx) "pink")
|
||||
(set! (.-lineWidth ctx) 2)
|
||||
(set! (.-lineWidth ctx) "round")
|
||||
#_(.putImageData ctx video 0 0)
|
||||
(doseq [prediction predictions]
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :leftEyebrowLower]) 2))
|
||||
(draw-path ctx (j/get-in prediction [:annotations :leftEyeLower0]))
|
||||
(draw-path ctx (j/get-in prediction [:annotations :leftEyeUpper0]))
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :rightEyebrowLower]) 2))
|
||||
(draw-path ctx (j/get-in prediction [:annotations :rightEyeLower0]))
|
||||
(draw-path ctx (j/get-in prediction [:annotations :rightEyeUpper0]))
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :lipsUpperOuter]) 3 -3))
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :lipsLowerInner]) 3 -3))
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :lipsLowerOuter]) 1 -2))
|
||||
(draw-path ctx (.concat (j/get-in prediction [:annotations :noseTip])
|
||||
(j/get-in prediction [:annotations :midwayBetweenEyes])))
|
||||
(draw-path ctx (.slice (j/get-in prediction [:annotations :silhouette]) 5 -5)))
|
||||
(vis/draw-sketch ctx predictions)
|
||||
[:img {:src (.toDataURL canvas)}]))
|
||||
|
||||
(defn snapshot [{:keys [current-snapshot]}]
|
||||
|
|
@ -89,11 +68,11 @@ Seit der globalen Covid19-Pandemie sind wir alle dazu gezwungen, auf physischen
|
|||
(swap! result assoc
|
||||
:video video
|
||||
:predictions predictions)
|
||||
(on-faces-detected ctx video predictions))
|
||||
(on-faces-detected ctx predictions))
|
||||
:halt? halt?}]
|
||||
[:button {:on-click #(dispatch [:running/snapshot (update @result :video video-snapshot)])} "Cheese"]])))
|
||||
|
||||
(defn app [{:keys [on-faces-detected]}]
|
||||
(defn app []
|
||||
(let [state @state
|
||||
status (:status state)
|
||||
current-snapshot (get-in state [:snapshots :current])]
|
||||
|
|
@ -103,7 +82,7 @@ Seit der globalen Covid19-Pandemie sind wir alle dazu gezwungen, auf physischen
|
|||
[snapshot {:current-snapshot current-snapshot}])
|
||||
(case status
|
||||
:permission-rejected [:div "Sad :("]
|
||||
:running [running {:on-faces-detected on-faces-detected
|
||||
:running [running {:on-faces-detected vis/draw-wireframe
|
||||
:halt? (some? current-snapshot)}]
|
||||
|
||||
;; default
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue