File size: 4,130 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Facial Expression Recognition</title>
    <style>

        body {

            font-family: Arial, sans-serif;

            background-color: #f4f4f4;

            text-align: center;

            margin: 0;

            padding: 0;

        }

        .container {

            width: 70%;

            margin: auto;

            padding: 20px;

            background: white;

            border-radius: 8px;

            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

            margin-top: 50px;

        }

        button, input[type="file"] {

            margin: 10px;

            padding: 10px 15px;

            border: none;

            border-radius: 5px;

            background: #007BFF;

            color: white;

            cursor: pointer;

        }

        button:hover {

            background: #0056b3;

        }

        input[type="file"] {

            cursor: pointer;

        }

        .output {

            margin-top: 20px;

            font-size: 18px;

            color: #333;

        }

    </style>
</head>
<body>
    <h1>Facial Expression Recognition</h1>
    <div class="container">
        <!-- Real-Time Detection Section -->
        <h2>Real-Time Detection</h2>
        <video id="video" width="480" height="360" autoplay></video>
        <button onclick="startRealTimePrediction()">Start Real-Time Detection</button>
        <div id="realTimeOutput" class="output"></div>

        <!-- Image Upload Section -->
        <h2>Image Upload</h2>
        <input type="file" id="imageInput" accept="image/*">
        <button onclick="uploadImage()">Upload and Predict</button>
        <div id="uploadOutput" class="output"></div>
    </div>

    <script>

        // Real-Time Prediction (using Base64)

        function startRealTimePrediction() {

            const video = document.getElementById('video');

            navigator.mediaDevices.getUserMedia({ video: true })

                .then(stream => {

                    video.srcObject = stream;

                })

                .catch(err => {

                    console.error("Error accessing camera: ", err);

                });



            setInterval(() => {

                const canvas = document.createElement('canvas');

                canvas.width = video.videoWidth;

                canvas.height = video.videoHeight;

                canvas.getContext('2d').drawImage(video, 0, 0);

                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 => {

                        document.getElementById('realTimeOutput').textContent = `Prediction: ${data.prediction}`;

                    })

                    .catch(error => console.error('Error:', error));

            }, 1000); // Perform predictions every second

        }



        // Upload Image Prediction

        function uploadImage() {

            const input = document.getElementById('imageInput');

            const file = input.files[0];

            if (!file) {

                alert('Please upload an image.');

                return;

            }



            const formData = new FormData();

            formData.append('image', file);



            fetch('/predict', {

                method: 'POST',

                body: formData

            })

                .then(response => response.json())

                .then(data => {

                    document.getElementById('uploadOutput').textContent = `Prediction: ${data.prediction}`;

                })

                .catch(error => console.error('Error:', error));

        }

    </script>
</body>
</html>