File size: 8,488 Bytes
b9449ed
 
b9e788f
b9449ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9e788f
 
 
 
112bc74
 
 
 
 
 
0cc6daa
112bc74
0cc6daa
ce0fc04
 
0cc6daa
ce0fc04
0cc6daa
 
 
 
 
 
 
ce0fc04
 
 
 
 
0cc6daa
 
 
ce0fc04
0cc6daa
 
ce0fc04
0cc6daa
ce0fc04
0cc6daa
 
ce0fc04
0cc6daa
ce0fc04
0cc6daa
 
ce0fc04
0cc6daa
 
 
ce0fc04
 
 
0cc6daa
 
 
 
ce0fc04
 
0cc6daa
 
ce0fc04
 
 
112bc74
 
 
ce0fc04
112bc74
ce0fc04
112bc74
 
 
 
0cc6daa
ce0fc04
 
0cc6daa
112bc74
 
 
 
 
 
 
 
 
ce0fc04
112bc74
 
 
 
 
ce0fc04
112bc74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51cfb16
 
 
 
 
 
 
 
ce0fc04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51cfb16
 
 
ce0fc04
51cfb16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce0fc04
51cfb16
 
 
 
 
 
 
ce0fc04
51cfb16
 
 
 
 
 
b9449ed
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from flask import Flask, request, jsonify
from detector.gesture_detector import detect_gesture
from flask import send_from_directory

app = Flask(__name__)

@app.route("/detect", methods=["POST"])
def detect():
    if "image" not in request.files:
        return jsonify({"error": "No image"}), 400

    file = request.files["image"]
    result = detect_gesture(file)

    return jsonify({
        # "image_base64": result["image_base64"],
        "saved_to": result["saved_to"],
        "detections": result["detections"],
        "total_detections": result["total_detections"]
    })

@app.route('/detected_images/<path:filename>')
def serve_detected_image(filename):
    return send_from_directory('/tmp/detected_images', filename)

@app.route("/")
def index():
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Gesture Detection</title>
        <style>
            body {
                font-family: 'Segoe UI', sans-serif;
                background: linear-gradient(to right, #eef2f3, #8e9eab);
                padding: 40px;
                max-width: 800px;
                margin: auto;
            }
            h1 {
                text-align: center;
                color: #333;
            }
            form, .nav-buttons {
                background-color: #fff;
                padding: 25px;
                border-radius: 15px;
                box-shadow: 0 4px 8px rgba(0,0,0,0.1);
                margin-bottom: 25px;
            }
            input[type="file"] {
                display: block;
                margin-bottom: 15px;
            }
            button {
                padding: 10px 25px;
                font-size: 16px;
                background-color: #28a745;
                color: white;
                border: none;
                border-radius: 6px;
                cursor: pointer;
                transition: background-color 0.3s;
            }
            button:hover {
                background-color: #218838;
            }
            .result img {
                max-width: 100%;
                border-radius: 10px;
                margin-top: 15px;
                box-shadow: 0 2px 6px rgba(0,0,0,0.2);
            }
            .nav-buttons {
                text-align: center;
            }
            .nav-buttons a button {
                background-color: #007BFF;
                margin: 0 10px;
            }
            .nav-buttons a button:hover {
                background-color: #0056b3;
            }
        </style>
    </head>
    <body>
        <h1>Image-Based Gesture Detection</h1>
        <form id="upload-form">
            <label for="image"><strong>Upload an image:</strong></label>
            <input type="file" id="image" name="image" accept="image/*" required />
            <button type="submit">Detect Gesture</button>
        </form>
        <div class="result" id="result"></div>
        <div class="nav-buttons">
            <p><strong>Want real-time detection?</strong></p>
            <a href="/webcam"><button>Open Webcam</button></a>
        </div>
        <script>
            const form = document.getElementById("upload-form");
            const resultDiv = document.getElementById("result");
            form.addEventListener("submit", async (e) => {
                e.preventDefault();
                const imageInput = document.getElementById("image");
                const file = imageInput.files[0];
                const formData = new FormData();
                formData.append("image", file);
                resultDiv.innerHTML = "<p>Processing...</p>";
                const response = await fetch("/detect", {
                    method: "POST",
                    body: formData
                });
                if (!response.ok) {
                    resultDiv.innerHTML = "<p style='color:red;'>Error detecting gesture.</p>";
                    return;
                }
                const data = await response.json();
                resultDiv.innerHTML = `
                    <p><strong>Total Detections:</strong> ${data.total_detections}</p>
                    <ul>
                        ${data.detections.map(d => `<li>${d.label} (${(d.confidence * 100).toFixed(1)}%)</li>`).join("")}
                    </ul>
                    <img src="${data.saved_to}" alt="Detected Image" />
                `;
            });
        </script>
    </body>
    </html>
    """

@app.route("/webcam")
def webcam_ui():
    return """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Live Gesture Detection</title>
        <style>
            body {
                font-family: 'Segoe UI', sans-serif;
                background: linear-gradient(to right, #e0eafc, #cfdef3);
                padding: 40px;
                max-width: 800px;
                margin: auto;
                text-align: center;
            }
            h2 {
                color: #333;
                margin-bottom: 20px;
            }
            video, canvas {
                width: 100%;
                max-width: 500px;
                border-radius: 10px;
                border: 2px solid #ccc;
                margin-bottom: 15px;
            }
            button {
                padding: 10px 20px;
                font-size: 16px;
                background-color: #007bff;
                color: white;
                border: none;
                border-radius: 6px;
                margin: 5px;
                cursor: pointer;
                transition: background-color 0.3s;
            }
            button:hover {
                background-color: #0056b3;
            }
            #result img {
                max-width: 100%;
                border-radius: 10px;
                box-shadow: 0 2px 6px rgba(0,0,0,0.2);
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <h2>Live Gesture Detection via Laptop Camera</h2>
        <video id="video" autoplay muted playsinline></video>
        <canvas id="canvas" style="display:none;"></canvas>
        <br>
        <button id="start">Start Detection</button>
        <button id="stop">Stop</button>
        <div id="result"></div>
        <script>
            const video = document.getElementById('video');
            const canvas = document.getElementById('canvas');
            const resultDiv = document.getElementById('result');
            let intervalId = null;

            navigator.mediaDevices.getUserMedia({ video: true })
                .then(stream => {
                    video.srcObject = stream;
                })
                .catch(err => {
                    alert("Failed to access webcam: " + err);
                });

            document.getElementById("start").onclick = () => {
                intervalId = setInterval(async () => {
                    canvas.width = video.videoWidth;
                    canvas.height = video.videoHeight;
                    canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
                    canvas.toBlob(async (blob) => {
                        const formData = new FormData();
                        formData.append("image", blob, "frame.jpg");
                        const res = await fetch("/detect", {
                            method: "POST",
                            body: formData
                        });
                        if (res.ok) {
                            const data = await res.json();
                            resultDiv.innerHTML = `
                                <p><strong>Total Detections:</strong> ${data.total_detections}</p>
                                <ul>
                                    ${data.detections.map(d => `<li>${d.label} (${(d.confidence * 100).toFixed(1)}%)</li>`).join("")}
                                </ul>
                                <img src="${data.saved_to}" />
                            `;
                        } else {
                            resultDiv.innerHTML = "<p style='color:red;'>Detection failed.</p>";
                        }
                    }, "image/jpeg");
                }, 1500);
            };

            document.getElementById("stop").onclick = () => {
                clearInterval(intervalId);
                resultDiv.innerHTML = "<p style='color:gray;'>Stopped.</p>";
            };
        </script>
    </body>
    </html>
    """

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)