-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
178 lines (160 loc) · 6.95 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Egg Quality Detector</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<style>
body {
background-color: #f8f9fa;
}
#previewCanvas {
border: 1px solid #ced4da;
border-radius: 8px;
}
#videoElement {
border: 1px solid #ced4da;
border-radius: 8px;
width: 100%;
display: none;
}
</style>
</head>
<body>
<div class="container mt-5">
<h1 class="text-center text-primary">Egg Quality Detector</h1>
<p class="text-center text-muted">Upload an image or video to check egg quality</p>
<div class="row justify-content-center">
<div class="col-md-6">
<form id="uploadForm">
<div class="mb-3">
<label for="mediaInput" class="form-label">Upload Image/Video</label>
<input class="form-control" type="file" id="mediaInput" accept="image/*,video/*">
</div>
<div class="mb-3">
<label for="youtubeLink" class="form-label">Or Enter YouTube Link</label>
<input class="form-control" type="url" id="youtubeLink" placeholder="https://www.youtube.com/watch?v=example">
</div>
<div class="text-center">
<button type="button" id="startCamera" class="btn btn-secondary">Use Camera</button>
<button type="submit" class="btn btn-primary">Analyze</button>
</div>
</form>
<video id="videoElement" autoplay muted></video>
<canvas id="previewCanvas" class="mt-4 mx-auto d-block" width="400" height="300"></canvas>
<div id="results" class="mt-3 text-center">
<p class="text-muted">Results will appear here...</p>
</div>
</div>
</div>
</div>
<script>
const apiKey = "u247o3wH5luRvqN29OnN";
const apiUrl = "https://detect.roboflow.com/eggs14-ub7ik/1";
const mediaInput = document.getElementById('mediaInput');
const youtubeLink = document.getElementById('youtubeLink');
const previewCanvas = document.getElementById('previewCanvas');
const videoElement = document.getElementById('videoElement');
const startCameraButton = document.getElementById('startCamera');
const uploadForm = document.getElementById('uploadForm');
const ctx = previewCanvas.getContext('2d');
let cameraStream = null;
// Start camera
startCameraButton.addEventListener('click', async () => {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
try {
cameraStream = await navigator.mediaDevices.getUserMedia({ video: true });
videoElement.srcObject = cameraStream;
videoElement.style.display = 'block';
} catch (error) {
console.error('Camera access error:', error);
alert('Could not access the camera.');
}
} else {
alert('Camera not supported on this device.');
}
});
imageInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
ctx.clearRect(0, 0, previewCanvas.width, previewCanvas.height);
// Calculate the scale and position to center the image
const scale = Math.min(
previewCanvas.width / img.width,
previewCanvas.height / img.height
);
const scaledWidth = img.width * scale;
const scaledHeight = img.height * scale;
const x = (previewCanvas.width - scaledWidth) / 2;
const y = (previewCanvas.height - scaledHeight) / 2;
// Draw the image on the canvas
ctx.drawImage(img, x, y, scaledWidth, scaledHeight);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
async function processImage(file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch(`${apiUrl}?api_key=${apiKey}`, {
method: 'POST',
body: formData,
});
const result = await response.json();
drawPredictions(result);
} catch (error) {
console.error('Image processing error:', error);
alert('Error: Unable to process the image.');
}
}
async function processVideo(file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch(`${apiUrl}/batch-video?api_key=${apiKey}`, {
method: 'POST',
body: formData,
});
const result = await response.json();
console.log('Video results:', result);
alert('Video processed successfully.');
} catch (error) {
console.error('Video processing error:', error);
alert('Error: Unable to process the video.');
}
}
async function processYouTubeLink(link) {
alert(`YouTube video processing is not yet implemented: ${link}`);
}
function processCameraFrame() {
const frame = new Image();
frame.src = videoElement;
ctx.drawImage(frame, 0, 0, previewCanvas.width, previewCanvas.height);
alert('Camera frame captured. Processing not implemented yet.');
}
function drawPredictions(result) {
if (result && result.predictions && result.predictions.length > 0) {
result.predictions.forEach(prediction => {
const { x, y, width, height, class: label, confidence } = prediction;
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.strokeRect(x, y, width, height);
ctx.fillStyle = 'red';
ctx.font = '14px Arial';
ctx.fillText(`${label} (${Math.round(confidence * 100)}%)`, x, y - 5);
});
} else {
alert('No predictions found. Try a different file.');
}
}
</script>
</body>
</html>