File size: 4,706 Bytes
efb95c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>์„ฑ๋ถ„๋ถ„์„ํ‘œ</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <style>

        body {

            font-family: 'Roboto', sans-serif;

            background-color: #f0f2f5;

            display: flex;

            justify-content: center;

            align-items: center;

            height: 100vh;

            margin: 0;

        }



        .container {

            background: #fff;

            padding: 20px;

            border-radius: 8px;

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

            text-align: center;

            max-width: 700px;

            width: 100%;

        }



        h1 {

            color: #333;

            margin-bottom: 20px;

        }



        video,

        canvas,

        img {

            border-radius: 8px;

            width: 100%;

            max-width: 640px;

            height: auto;

            margin-bottom: 20px;

        }



        button {

            background-color: #4CAF50;

            color: white;

            padding: 10px 20px;

            border: none;

            border-radius: 5px;

            cursor: pointer;

            font-size: 16px;

            transition: background-color 0.3s, transform 0.3s;

        }



        button:hover {

            background-color: #45a049;

            transform: scale(1.05);

        }



        .result {

            margin-top: 20px;

            font-size: 18px;

            font-weight: bold;

        }



        .good {

            color: green;

        }



        .normal {

            color: orange;

        }



        .dangerous {

            color: red;

        }

    </style>
</head>

<body>
    <div class="container">
        <h1>์„ฑ๋ถ„๋ถ„์„ํ‘œ</h1>
        <video id="video" autoplay></video>
        <button id="snap">์‚ฌ์ง„ ์ฐ๊ธฐ</button>
        <canvas id="canvas"></canvas>
        <p id="analysisResult" class="result"></p>
    </div>

    <script>

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

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

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

        const context = canvas.getContext('2d');

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



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

            .then(stream => {

                video.srcObject = stream;

            })

            .catch(err => {

                console.error("Error accessing webcam: " + err);

            });



        function processAnalysisResult(result) {

            const match = result.match(/\d+/);

            if (match) {

                const sugarContent = parseInt(match[0], 10);

                let message = '';

                let className = '';



                if (sugarContent >= 0 && sugarContent <= 20) {

                    message = 'good';

                    className = 'good';

                } else if (sugarContent >= 21 && sugarContent <= 50) {

                    message = 'normal';

                    className = 'normal';

                } else if (sugarContent >= 51) {

                    message = 'dangerous';

                    className = 'dangerous';

                }



                analysisResult.textContent = message;

                analysisResult.className = className;

            } else {

                analysisResult.textContent = 'Error: Could not analyze image.';

                analysisResult.className = '';

            }

        }



        snap.addEventListener('click', () => {

            context.drawImage(video, 0, 0, canvas.width, canvas.height);

            const dataURL = canvas.toDataURL('image/png');

            fetch('/save_image', {

                method: 'POST',

                headers: {

                    'Content-Type': 'application/json',

                },

                body: JSON.stringify({ image: dataURL })

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

                .then(data => {

                    if (data.analysis_result) {

                        processAnalysisResult(data.analysis_result);

                    } else {

                        analysisResult.textContent = 'Error: ์„ฑ๋ถ„๋ถ„์„ํ‘œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.';

                        analysisResult.className = '';

                    }

                    alert(data.message);

                });

        });

    </script>
</body>

</html>