-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Commit, predictions using MediaPipe
- Loading branch information
0 parents
commit 756fd2b
Showing
3 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" > | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Shadowcraft Project : WebcamDemo</title> | ||
<link rel="stylesheet" href="./style.css"> | ||
|
||
</head> | ||
<body> | ||
<!-- partial:index.partial.html --> | ||
<!-- Copyright 2023 The MediaPipe Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. --> | ||
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet"> | ||
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script> | ||
|
||
<body> | ||
<h1>Shadowcraft Demo</h1> | ||
<div id="liveView" class="videoView"> | ||
<button id="webcamButton" class="mdc-button mdc-button--raised"> | ||
<span class="mdc-button__ripple"></span> | ||
<span class="mdc-button__label">ENABLE WEBCAM</span> | ||
</button> | ||
<div style="position: relative;"> | ||
<video id="webcam" style="position: abso" autoplay playsinline></video> | ||
<canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px;"></canvas> | ||
</div> | ||
</div> | ||
</section> | ||
<!-- partial --> | ||
<script type="module" src="./script.js"></script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright 2023 The MediaPipe Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
import { HandLandmarker, FilesetResolver } from "https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]"; | ||
const demosSection = document.getElementById("demos"); | ||
let handLandmarker = undefined; | ||
let runningMode = "IMAGE"; | ||
let enableWebcamButton; | ||
let webcamRunning = false; | ||
// Before we can use HandLandmarker class we must wait for it to finish | ||
// loading. Machine Learning models can be large and take a moment to | ||
// get everything needed to run. | ||
const createHandLandmarker = async () => { | ||
const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/wasm"); | ||
handLandmarker = await HandLandmarker.createFromOptions(vision, { | ||
baseOptions: { | ||
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task`, | ||
delegate: "GPU" | ||
}, | ||
runningMode: runningMode, | ||
numHands: 2 | ||
}); | ||
demosSection.classList.remove("invisible"); | ||
}; | ||
createHandLandmarker(); | ||
/******************************************************************** | ||
// Demo 1: Grab a bunch of images from the page and detection them | ||
// upon click. | ||
********************************************************************/ | ||
// In this demo, we have put all our clickable images in divs with the | ||
// CSS class 'detectionOnClick'. Lets get all the elements that have | ||
// this class. | ||
const imageContainers = document.getElementsByClassName("detectOnClick"); | ||
// Now let's go through all of these and add a click event listener. | ||
for (let i = 0; i < imageContainers.length; i++) { | ||
// Add event listener to the child element whichis the img element. | ||
imageContainers[i].children[0].addEventListener("click", handleClick); | ||
} | ||
// When an image is clicked, let's detect it and display results! | ||
async function handleClick(event) { | ||
if (!handLandmarker) { | ||
console.log("Wait for handLandmarker to load before clicking!"); | ||
return; | ||
} | ||
if (runningMode === "VIDEO") { | ||
runningMode = "IMAGE"; | ||
await handLandmarker.setOptions({ runningMode: "IMAGE" }); | ||
} | ||
// Remove all landmarks drawed before | ||
const allCanvas = event.target.parentNode.getElementsByClassName("canvas"); | ||
for (var i = allCanvas.length - 1; i >= 0; i--) { | ||
const n = allCanvas[i]; | ||
n.parentNode.removeChild(n); | ||
} | ||
// We can call handLandmarker.detect as many times as we like with | ||
// different image data each time. This returns a promise | ||
// which we wait to complete and then call a function to | ||
// print out the results of the prediction. | ||
const handLandmarkerResult = handLandmarker.detect(event.target); | ||
console.log(handLandmarkerResult.handednesses[0][0]); | ||
const canvas = document.createElement("canvas"); | ||
canvas.setAttribute("class", "canvas"); | ||
canvas.setAttribute("width", event.target.naturalWidth + "px"); | ||
canvas.setAttribute("height", event.target.naturalHeight + "px"); | ||
canvas.style = | ||
"left: 0px;" + | ||
"top: 0px;" + | ||
"width: " + | ||
event.target.width + | ||
"px;" + | ||
"height: " + | ||
event.target.height + | ||
"px;"; | ||
event.target.parentNode.appendChild(canvas); | ||
const cxt = canvas.getContext("2d"); | ||
for (const landmarks of handLandmarkerResult.landmarks) { | ||
drawConnectors(cxt, landmarks, HAND_CONNECTIONS, { | ||
color: "#00FF00", | ||
lineWidth: 5 | ||
}); | ||
drawLandmarks(cxt, landmarks, { color: "#FF0000", lineWidth: 1 }); | ||
} | ||
} | ||
/******************************************************************** | ||
// Demo 2: Continuously grab image from webcam stream and detect it. | ||
********************************************************************/ | ||
const video = document.getElementById("webcam"); | ||
const canvasElement = document.getElementById("output_canvas"); | ||
const canvasCtx = canvasElement.getContext("2d"); | ||
// Check if webcam access is supported. | ||
const hasGetUserMedia = () => { var _a; return !!((_a = navigator.mediaDevices) === null || _a === void 0 ? void 0 : _a.getUserMedia); }; | ||
// If webcam supported, add event listener to button for when user | ||
// wants to activate it. | ||
if (hasGetUserMedia()) { | ||
enableWebcamButton = document.getElementById("webcamButton"); | ||
enableWebcamButton.addEventListener("click", enableCam); | ||
} | ||
else { | ||
console.warn("getUserMedia() is not supported by your browser"); | ||
} | ||
// Enable the live webcam view and start detection. | ||
function enableCam(event) { | ||
if (!handLandmarker) { | ||
console.log("Wait! objectDetector not loaded yet."); | ||
return; | ||
} | ||
if (webcamRunning === true) { | ||
webcamRunning = false; | ||
enableWebcamButton.innerText = "ENABLE PREDICTIONS"; | ||
} | ||
else { | ||
webcamRunning = true; | ||
enableWebcamButton.innerText = "DISABLE PREDICTIONS"; | ||
} | ||
// getUsermedia parameters. | ||
const constraints = { | ||
video: { | ||
width: { ideal: 1280 }, | ||
height: { ideal: 720 } | ||
} | ||
}; | ||
// Activate the webcam stream. | ||
navigator.mediaDevices.getUserMedia(constraints).then((stream) => { | ||
video.srcObject = stream; | ||
video.addEventListener("loadeddata", predictWebcam); | ||
}); | ||
} | ||
let lastVideoTime = -1; | ||
let results = undefined; | ||
console.log(video); | ||
async function predictWebcam() { | ||
canvasElement.style.width = video.videoWidth; | ||
; | ||
canvasElement.style.height = video.videoHeight; | ||
canvasElement.width = video.videoWidth; | ||
canvasElement.height = video.videoHeight; | ||
// Now let's start detecting the stream. | ||
if (runningMode === "IMAGE") { | ||
runningMode = "VIDEO"; | ||
await handLandmarker.setOptions({ runningMode: "VIDEO" }); | ||
} | ||
let startTimeMs = performance.now(); | ||
if (lastVideoTime !== video.currentTime) { | ||
lastVideoTime = video.currentTime; | ||
results = handLandmarker.detectForVideo(video, startTimeMs); | ||
} | ||
canvasCtx.save(); | ||
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height); | ||
if (results.landmarks) { | ||
for (const landmarks of results.landmarks) { | ||
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, { | ||
color: "#00FF00", | ||
lineWidth: 5 | ||
}); | ||
drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 }); | ||
} | ||
} | ||
canvasCtx.restore(); | ||
// Call this function again to keep predicting when the browser is ready. | ||
if (webcamRunning === true) { | ||
window.requestAnimationFrame(predictWebcam); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* Copyright 2023 The MediaPipe Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
@use "@material"; | ||
body { | ||
font-family: roboto; | ||
margin: 2em; | ||
color: #3d3d3d; | ||
--mdc-theme-primary: #007f8b; | ||
--mdc-theme-on-primary: #f1f3f4; | ||
} | ||
|
||
h1 { | ||
color: #007f8b; | ||
} | ||
|
||
h2 { | ||
clear: both; | ||
} | ||
|
||
em { | ||
font-weight: bold; | ||
} | ||
|
||
video { | ||
clear: both; | ||
display: block; | ||
transform: rotateY(180deg); | ||
-webkit-transform: rotateY(180deg); | ||
-moz-transform: rotateY(180deg); | ||
} | ||
|
||
section { | ||
opacity: 1; | ||
transition: opacity 500ms ease-in-out; | ||
} | ||
|
||
header, | ||
footer { | ||
clear: both; | ||
} | ||
|
||
.removed { | ||
display: none; | ||
} | ||
|
||
.invisible { | ||
opacity: 0.2; | ||
} | ||
|
||
.note { | ||
font-style: italic; | ||
font-size: 130%; | ||
} | ||
|
||
.videoView, | ||
.detectOnClick { | ||
position: relative; | ||
float: left; | ||
width: 48%; | ||
margin: 2% 1%; | ||
cursor: pointer; | ||
} | ||
|
||
.videoView p, | ||
.detectOnClick p { | ||
position: absolute; | ||
padding: 5px; | ||
background-color: #007f8b; | ||
color: #fff; | ||
border: 1px dashed rgba(255, 255, 255, 0.7); | ||
z-index: 2; | ||
font-size: 12px; | ||
margin: 0; | ||
} | ||
|
||
.highlighter { | ||
background: rgba(0, 255, 0, 0.25); | ||
border: 1px dashed #fff; | ||
z-index: 1; | ||
position: absolute; | ||
} | ||
|
||
.canvas { | ||
z-index: 1; | ||
position: absolute; | ||
pointer-events: none; | ||
} | ||
|
||
.output_canvas { | ||
transform: rotateY(180deg); | ||
-webkit-transform: rotateY(180deg); | ||
-moz-transform: rotateY(180deg); | ||
} | ||
|
||
.detectOnClick { | ||
z-index: 0; | ||
} | ||
|
||
.detectOnClick img { | ||
width: 100%; | ||
} |