@@ -4,64 +4,52 @@ let isCountingEnabled = false;
44let currentStream ;
55let facingMode = "user" ;
66
7- // New variables for push-up detection
87let previousBrightness = 0 ;
9- let brightnessDiffThreshold = 10 ; // Adjust based on testing
8+ let brightnessDiffThreshold = 10 ;
109let isGoingDown = true ;
1110let lastPushUpTime = 0 ;
12- let pushUpCooldown = 1000 ; // 1 second cooldown between push-ups
11+ let pushUpCooldown = 1000 ;
1312
1413async function setupCamera ( ) {
15- console . log ( "Setting up camera..." ) ;
1614 video = document . getElementById ( 'video' ) ;
1715 const constraints = {
1816 video : { facingMode : facingMode }
1917 } ;
2018 try {
2119 currentStream = await navigator . mediaDevices . getUserMedia ( constraints ) ;
2220 video . srcObject = currentStream ;
23- console . log ( "Camera setup successful" ) ;
2421 } catch ( error ) {
2522 console . error ( "Camera setup failed:" , error ) ;
2623 }
2724 return new Promise ( ( resolve ) => {
2825 video . onloadedmetadata = ( ) => {
29- console . log ( "Video metadata loaded" ) ;
3026 resolve ( video ) ;
3127 } ;
3228 } ) ;
3329}
3430
3531function startDetection ( ) {
36- console . log ( "Starting detection..." ) ;
37- if ( ! video ) {
38- console . warn ( "Video not ready. Aborting detection start." ) ;
39- return ;
40- }
32+ if ( ! video ) return ;
4133 detect ( ) ;
4234}
4335
4436async function detect ( ) {
4537 if ( ! isCountingEnabled ) return ;
4638 try {
4739 const imageTensor = tf . browser . fromPixels ( video ) ;
48- const resizedImage = tf . image . resizeBilinear ( imageTensor , [ 64 , 64 ] ) ; // Smaller size for faster processing
40+ const resizedImage = tf . image . resizeBilinear ( imageTensor , [ 64 , 64 ] ) ;
4941 const grayImage = tf . image . rgbToGrayscale ( resizedImage ) ;
5042 const brightness = tf . mean ( grayImage ) . dataSync ( ) [ 0 ] ;
5143
52- console . log ( "Current brightness:" , brightness . toFixed ( 2 ) ) ;
53-
5444 const brightnessDiff = brightness - previousBrightness ;
5545
5646 if ( Math . abs ( brightnessDiff ) > brightnessDiffThreshold ) {
5747 let currentTime = Date . now ( ) ;
5848 if ( currentTime - lastPushUpTime > pushUpCooldown ) {
5949 if ( isGoingDown && brightnessDiff > 0 ) {
60- console . log ( "Push-up going up" ) ;
6150 isGoingDown = false ;
6251 } else if ( ! isGoingDown && brightnessDiff < 0 ) {
6352 pushUpCount ++ ;
64- console . log ( "Push-up completed. Count:" , pushUpCount ) ;
6553 updateStats ( ) ;
6654 isGoingDown = true ;
6755 lastPushUpTime = currentTime ;
@@ -71,7 +59,6 @@ async function detect() {
7159
7260 previousBrightness = brightness ;
7361
74- // Clean up tensors
7562 imageTensor . dispose ( ) ;
7663 resizedImage . dispose ( ) ;
7764 grayImage . dispose ( ) ;
@@ -84,16 +71,13 @@ async function detect() {
8471function updateStats ( ) {
8572 document . getElementById ( 'pushUpCount' ) . textContent = pushUpCount ;
8673 const pushUpsPerToken = parseInt ( document . getElementById ( 'pushUpsPerToken' ) . value ) ;
87- console . log ( "Updating stats. Push-ups:" , pushUpCount , "Push-ups per token:" , pushUpsPerToken ) ;
8874 if ( pushUpCount % pushUpsPerToken === 0 ) {
8975 tokenCount ++ ;
90- console . log ( "Token earned. New token count:" , tokenCount ) ;
9176 document . getElementById ( 'tokenCount' ) . textContent = tokenCount ;
9277 }
9378}
9479
9580document . getElementById ( 'switchCamera' ) . addEventListener ( 'click' , async ( ) => {
96- console . log ( "Switching camera..." ) ;
9781 facingMode = facingMode === "user" ? "environment" : "user" ;
9882 if ( currentStream ) {
9983 currentStream . getTracks ( ) . forEach ( track => track . stop ( ) ) ;
@@ -103,50 +87,45 @@ document.getElementById('switchCamera').addEventListener('click', async () => {
10387
10488document . getElementById ( 'startStop' ) . addEventListener ( 'click' , ( ) => {
10589 isCountingEnabled = ! isCountingEnabled ;
106- console . log ( "Counting enabled:" , isCountingEnabled ) ;
10790 document . getElementById ( 'startStop' ) . textContent = isCountingEnabled ? 'Stop' : 'Start' ;
91+ document . getElementById ( 'startStop' ) . classList . toggle ( 'bg-green-500' ) ;
92+ document . getElementById ( 'startStop' ) . classList . toggle ( 'bg-red-500' ) ;
10893 if ( isCountingEnabled ) {
10994 startDetection ( ) ;
11095 }
11196} ) ;
11297
113- // Reward system
11498const rewards = [
11599 { name : 'Extra TV time' , cost : 1 } ,
116100 { name : 'Favorite snack' , cost : 2 } ,
117101 { name : 'Movie night' , cost : 3 }
118102] ;
119103
120104function displayRewards ( ) {
121- console . log ( "Displaying rewards" ) ;
122105 const rewardsContainer = document . getElementById ( 'rewards' ) ;
123- rewardsContainer . innerHTML = '<h3>Rewards:</h3>' ;
106+ rewardsContainer . innerHTML = '<h3 class="text-xl font-semibold mb-4" >Rewards:</h3>' ;
124107 rewards . forEach ( reward => {
125108 const button = document . createElement ( 'button' ) ;
126109 button . textContent = `${ reward . name } (${ reward . cost } tokens)` ;
110+ button . className = 'w-full bg-indigo-500 text-white px-4 py-2 rounded hover:bg-indigo-600 transition' ;
127111 button . addEventListener ( 'click' , ( ) => redeemReward ( reward ) ) ;
128112 rewardsContainer . appendChild ( button ) ;
129113 } ) ;
130114}
131115
132116function redeemReward ( reward ) {
133- console . log ( "Attempting to redeem reward:" , reward . name ) ;
134117 if ( tokenCount >= reward . cost ) {
135118 tokenCount -= reward . cost ;
136119 document . getElementById ( 'tokenCount' ) . textContent = tokenCount ;
137- console . log ( "Reward redeemed. New token count:" , tokenCount ) ;
138120 alert ( `You've redeemed: ${ reward . name } ` ) ;
139121 } else {
140- console . log ( "Not enough tokens to redeem reward" ) ;
141122 alert ( 'Not enough tokens!' ) ;
142123 }
143124}
144125
145126async function init ( ) {
146- console . log ( "Initializing app..." ) ;
147127 await setupCamera ( ) ;
148128 displayRewards ( ) ;
149- console . log ( "App initialization complete" ) ;
150129}
151130
152131init ( ) ;
0 commit comments