DHEIVER commited on
Commit
ab20cff
·
verified ·
1 Parent(s): ddb0b8f

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +71 -0
app.js ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Access the video and canvas elements
2
+ const video = document.getElementById('video');
3
+ const canvas = document.getElementById('outputCanvas');
4
+ const ctx = canvas.getContext('2d');
5
+ const status = document.getElementById('status');
6
+
7
+ // Initialize the hand pose detection model
8
+ async function setupHandPoseDetection() {
9
+ try {
10
+ // Load the hand pose detection model
11
+ const model = handPoseDetection.SupportedModels.MediaPipeHands;
12
+ const detectorConfig = {
13
+ runtime: 'tfjs', // Use TensorFlow.js runtime
14
+ modelType: 'full' // Choose between 'lite' or 'full'
15
+ };
16
+ const detector = await handPoseDetection.createDetector(model, detectorConfig);
17
+
18
+ status.textContent = 'Model loaded!';
19
+
20
+ // Start video stream
21
+ await setupCamera();
22
+
23
+ // Detect hands in real-time
24
+ detectHands(detector);
25
+ } catch (error) {
26
+ console.error('Error loading the model:', error);
27
+ status.textContent = 'Failed to load model.';
28
+ }
29
+ }
30
+
31
+ // Set up the camera feed
32
+ async function setupCamera() {
33
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true });
34
+ video.srcObject = stream;
35
+ return new Promise((resolve) => {
36
+ video.onloadedmetadata = () => {
37
+ resolve(video);
38
+ };
39
+ });
40
+ }
41
+
42
+ // Detect hands and draw keypoints
43
+ async function detectHands(detector) {
44
+ if (video.readyState === video.HAVE_ENOUGH_DATA) {
45
+ // Clear the canvas
46
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
47
+
48
+ // Detect hands in the video frame
49
+ const hands = await detector.estimateHands(video);
50
+
51
+ // Draw keypoints on the canvas
52
+ if (hands.length > 0) {
53
+ hands.forEach((hand) => {
54
+ const keypoints = hand.keypoints;
55
+ keypoints.forEach((keypoint) => {
56
+ const { x, y } = keypoint;
57
+ ctx.beginPath();
58
+ ctx.arc(x, y, 5, 0, 2 * Math.PI); // Draw a circle at each keypoint
59
+ ctx.fillStyle = 'red';
60
+ ctx.fill();
61
+ });
62
+ });
63
+ }
64
+ }
65
+
66
+ // Repeat detection
67
+ requestAnimationFrame(() => detectHands(detector));
68
+ }
69
+
70
+ // Initialize the application
71
+ setupHandPoseDetection();