Maulidaaa commited on
Commit
51cfb16
·
verified ·
1 Parent(s): 112bc74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py CHANGED
@@ -82,6 +82,82 @@ def index():
82
  </html>
83
  """
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  if __name__ == "__main__":
87
  app.run(host="0.0.0.0", port=5000)
 
82
  </html>
83
  """
84
 
85
+ @app.route("/webcam")
86
+ def webcam_ui():
87
+ return """
88
+ <!DOCTYPE html>
89
+ <html>
90
+ <head>
91
+ <title>Live Gesture Detection</title>
92
+ <style>
93
+ video, canvas { width: 480px; border: 1px solid #ccc; margin-bottom: 10px; }
94
+ #result img { max-width: 100%; margin-top: 10px; }
95
+ </style>
96
+ </head>
97
+ <body>
98
+ <h2>Live Gesture Detection from Webcam</h2>
99
+ <video id="video" autoplay muted playsinline></video>
100
+ <canvas id="canvas" style="display:none;"></canvas>
101
+ <br>
102
+ <button id="start">Start Detection</button>
103
+ <button id="stop">Stop</button>
104
+
105
+ <div id="result"></div>
106
+
107
+ <script>
108
+ const video = document.getElementById('video');
109
+ const canvas = document.getElementById('canvas');
110
+ const resultDiv = document.getElementById('result');
111
+ let intervalId = null;
112
+
113
+ navigator.mediaDevices.getUserMedia({ video: true })
114
+ .then(stream => {
115
+ video.srcObject = stream;
116
+ })
117
+ .catch(err => {
118
+ alert("Failed to access webcam: " + err);
119
+ });
120
+
121
+ document.getElementById("start").onclick = () => {
122
+ intervalId = setInterval(async () => {
123
+ canvas.width = video.videoWidth;
124
+ canvas.height = video.videoHeight;
125
+ canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
126
+
127
+ canvas.toBlob(async (blob) => {
128
+ const formData = new FormData();
129
+ formData.append("image", blob, "frame.jpg");
130
+
131
+ const res = await fetch("/detect", {
132
+ method: "POST",
133
+ body: formData
134
+ });
135
+
136
+ if (res.ok) {
137
+ const data = await res.json();
138
+ resultDiv.innerHTML = `
139
+ <p><strong>Total Detections:</strong> ${data.total_detections}</p>
140
+ <ul>
141
+ ${data.detections.map(d => `<li>${d.label} (${(d.confidence * 100).toFixed(1)}%)</li>`).join("")}
142
+ </ul>
143
+ <img src="${data.saved_to}" />
144
+ `;
145
+ } else {
146
+ resultDiv.innerHTML = "Detection failed.";
147
+ }
148
+ }, "image/jpeg");
149
+ }, 1500);
150
+ };
151
+
152
+ document.getElementById("stop").onclick = () => {
153
+ clearInterval(intervalId);
154
+ resultDiv.innerHTML = "Stopped.";
155
+ };
156
+ </script>
157
+ </body>
158
+ </html>
159
+ """
160
+
161
 
162
  if __name__ == "__main__":
163
  app.run(host="0.0.0.0", port=5000)