File size: 3,243 Bytes
f98d0eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Function to start the webcam
function startWebcam() {
    const video = document.getElementById('video');
    if (video) {
        navigator.mediaDevices.getUserMedia({ video: true })
            .then(stream => {
                video.srcObject = stream;
            })
            .catch(error => {
                console.error("Error accessing the webcam:", error);
            });
    } else {
        console.error("Video element not found.");
    }
}

// Call the startWebcam function once the DOM is fully loaded
document.addEventListener('DOMContentLoaded', (event) => {
    startWebcam();
});

// Function to start real-time detection
function startDetection() {
    const video = document.getElementById('video');
    const canvas = document.getElementById('overlay');
    const ctx = canvas.getContext('2d');
    const realTimeOutputDiv = document.getElementById('realTimeOutput');

    setInterval(() => {
        ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

        // Convert the captured frame to base64
        const imageData = canvas.toDataURL('image/png');

        fetch('/predict', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ image: imageData }),
        })
        .then(response => response.json())
        .then(data => {
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            if (data.prediction && data.bbox) {
                const { x, y, w, h } = data.bbox;

                // Draw bounding box
                ctx.strokeStyle = 'red';
                ctx.lineWidth = 2;
                ctx.strokeRect(x, y, w, h);

                // Display prediction label
                ctx.font = '16px Arial';
                ctx.fillStyle = 'red';
                ctx.fillText(data.prediction, x, y - 10);

                realTimeOutputDiv.innerHTML = `Real-Time Prediction: ${data.prediction}`;
            } else {
                realTimeOutputDiv.innerHTML = 'No face detected';
            }
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }, 1000); // Capture and send frame every second
}

// Function to upload an image
function uploadImage() {
    const fileInput = document.getElementById('imageUpload');
    const uploadOutputDiv = document.getElementById('uploadOutput');
    
    if (fileInput && fileInput.files.length > 0) {
        const file = fileInput.files[0];
        const formData = new FormData();
        formData.append('image', file);

        fetch('/predict', {
            method: 'POST',
            body: formData,
        })
        .then(response => response.json())
        .then(data => {
            if (data.prediction) {
                uploadOutputDiv.innerHTML = `Uploaded Image Prediction: ${data.prediction}`;
            } else {
                uploadOutputDiv.innerHTML = 'No face detected';
            }
        })
        .catch(error => {
            console.error('Error:', error);
        });
    } else {
        alert('Please select an image before uploading.');
    }
}