Spaces:
Sleeping
Sleeping
Create templates/index.html
Browse files- templates/index.html +120 -0
templates/index.html
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Camera Capture</title>
|
7 |
+
<style>
|
8 |
+
#videoElement {
|
9 |
+
width: 100%;
|
10 |
+
height: auto;
|
11 |
+
}
|
12 |
+
#camera-controls {
|
13 |
+
display: flex;
|
14 |
+
justify-content: space-between;
|
15 |
+
margin-top: 10px;
|
16 |
+
}
|
17 |
+
#capture-button {
|
18 |
+
margin-top: 10px;
|
19 |
+
}
|
20 |
+
</style>
|
21 |
+
</head>
|
22 |
+
<body>
|
23 |
+
|
24 |
+
<h1>Camera Capture</h1>
|
25 |
+
|
26 |
+
<!-- Camera Icon -->
|
27 |
+
<button id="open-camera-btn" onclick="startCamera()">📷</button>
|
28 |
+
|
29 |
+
<!-- Video Capture Element -->
|
30 |
+
<video id="videoElement" autoplay></video>
|
31 |
+
|
32 |
+
<!-- Camera Control Buttons -->
|
33 |
+
<div id="camera-controls">
|
34 |
+
<button id="switch-camera-btn" onclick="switchCamera()">Switch Camera</button>
|
35 |
+
<button id="capture-button" onclick="captureImage()">Capture</button>
|
36 |
+
</div>
|
37 |
+
|
38 |
+
<!-- Image Display and Action Buttons -->
|
39 |
+
<div id="captured-image-section" style="display:none;">
|
40 |
+
<img id="captured-image" src="" alt="Captured Image" width="100%">
|
41 |
+
<button onclick="retakeImage()">Retake</button>
|
42 |
+
<button onclick="submitImage()">Submit</button>
|
43 |
+
</div>
|
44 |
+
|
45 |
+
<script>
|
46 |
+
let videoStream;
|
47 |
+
let currentStream;
|
48 |
+
let videoElement = document.getElementById('videoElement');
|
49 |
+
let capturedImageSection = document.getElementById('captured-image-section');
|
50 |
+
let capturedImage = document.getElementById('captured-image');
|
51 |
+
|
52 |
+
function startCamera() {
|
53 |
+
navigator.mediaDevices.enumerateDevices()
|
54 |
+
.then(devices => {
|
55 |
+
devices.forEach(device => {
|
56 |
+
if (device.kind === 'videoinput') {
|
57 |
+
currentStream = device.deviceId;
|
58 |
+
startStream(currentStream);
|
59 |
+
}
|
60 |
+
});
|
61 |
+
})
|
62 |
+
.catch(err => console.error("Error accessing camera:", err));
|
63 |
+
}
|
64 |
+
|
65 |
+
function startStream(deviceId) {
|
66 |
+
navigator.mediaDevices.getUserMedia({
|
67 |
+
video: { deviceId: { exact: deviceId } }
|
68 |
+
})
|
69 |
+
.then(stream => {
|
70 |
+
videoElement.srcObject = stream;
|
71 |
+
videoStream = stream;
|
72 |
+
})
|
73 |
+
.catch(err => console.error("Error accessing stream:", err));
|
74 |
+
}
|
75 |
+
|
76 |
+
function switchCamera() {
|
77 |
+
if (videoStream) {
|
78 |
+
videoStream.getTracks().forEach(track => track.stop());
|
79 |
+
}
|
80 |
+
startCamera();
|
81 |
+
}
|
82 |
+
|
83 |
+
function captureImage() {
|
84 |
+
let canvas = document.createElement('canvas');
|
85 |
+
canvas.width = videoElement.videoWidth;
|
86 |
+
canvas.height = videoElement.videoHeight;
|
87 |
+
canvas.getContext('2d').drawImage(videoElement, 0, 0, canvas.width, canvas.height);
|
88 |
+
|
89 |
+
let imageData = canvas.toDataURL('image/jpeg');
|
90 |
+
capturedImage.src = imageData;
|
91 |
+
|
92 |
+
capturedImageSection.style.display = 'block';
|
93 |
+
|
94 |
+
// Sending the captured image to Flask server
|
95 |
+
fetch('/capture', {
|
96 |
+
method: 'POST',
|
97 |
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
98 |
+
body: 'image=' + encodeURIComponent(imageData)
|
99 |
+
}).then(response => response.json())
|
100 |
+
.then(data => console.log(data.message));
|
101 |
+
}
|
102 |
+
|
103 |
+
function retakeImage() {
|
104 |
+
capturedImageSection.style.display = 'none';
|
105 |
+
startCamera();
|
106 |
+
}
|
107 |
+
|
108 |
+
function submitImage() {
|
109 |
+
fetch('/post_to_instagram', {
|
110 |
+
method: 'POST'
|
111 |
+
})
|
112 |
+
.then(response => response.json())
|
113 |
+
.then(data => {
|
114 |
+
alert(data.message);
|
115 |
+
});
|
116 |
+
}
|
117 |
+
</script>
|
118 |
+
|
119 |
+
</body>
|
120 |
+
</html>
|