Spaces:
Running
<script>
Browse filesdocument.getElementById('identifyBtn').addEventListener('click', function() {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
alert('Please take a photo or upload an image first');
return;
}
const file = fileInput.files[0];
const formData = new FormData();
formData.append('image', file);
fetch('https://api.plant.id/v2/identify', {
method: 'POST',
headers: {
'Api-Key': 'YOUR_API_KEY'
},
body: formData
})
.then(response => response.json())
.then(data => {
if (data.suggestions && data.suggestions.length > 0) {
alert(`Plant identified: ${data.suggestions[0].plant_name}`);
// You can update your UI here with detailed plant info
} else {
alert('Plant could not be identified.');
}
})
.catch(error => {
console.error(error);
alert('Error identifying plant, try again');
});
});
</script>
is it can be in on the rest of code
that i can make the program accept photos?"
- index.html +22 -5
- results.html +18 -12
|
@@ -136,7 +136,15 @@
|
|
| 136 |
try {
|
| 137 |
const formData = new FormData();
|
| 138 |
formData.append('image', selectedFile);
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
const response = await fetch('https://api.plant.id/v2/identify', {
|
| 141 |
method: 'POST',
|
| 142 |
headers: {
|
|
@@ -145,20 +153,29 @@
|
|
| 145 |
},
|
| 146 |
body: formData
|
| 147 |
});
|
| 148 |
-
if (!response.ok) {
|
| 149 |
throw new Error('Identification failed');
|
| 150 |
}
|
| 151 |
const data = await response.json();
|
| 152 |
console.log('API Response:', data);
|
| 153 |
|
| 154 |
if (data.suggestions && data.suggestions.length > 0) {
|
| 155 |
-
//
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
window.location.href = 'results.html';
|
| 158 |
} else {
|
| 159 |
throw new Error('No plant identified');
|
| 160 |
}
|
| 161 |
-
|
| 162 |
console.error('Error:', error);
|
| 163 |
alert('Identification failed: ' + error.message);
|
| 164 |
} finally {
|
|
|
|
| 136 |
try {
|
| 137 |
const formData = new FormData();
|
| 138 |
formData.append('image', selectedFile);
|
| 139 |
+
|
| 140 |
+
// Store the file for results page
|
| 141 |
+
sessionStorage.setItem('uploadedImage', JSON.stringify({
|
| 142 |
+
name: selectedFile.name,
|
| 143 |
+
type: selectedFile.type,
|
| 144 |
+
lastModified: selectedFile.lastModified
|
| 145 |
+
}));
|
| 146 |
+
|
| 147 |
+
// Using Plant.id API (demo API key)
|
| 148 |
const response = await fetch('https://api.plant.id/v2/identify', {
|
| 149 |
method: 'POST',
|
| 150 |
headers: {
|
|
|
|
| 153 |
},
|
| 154 |
body: formData
|
| 155 |
});
|
| 156 |
+
if (!response.ok) {
|
| 157 |
throw new Error('Identification failed');
|
| 158 |
}
|
| 159 |
const data = await response.json();
|
| 160 |
console.log('API Response:', data);
|
| 161 |
|
| 162 |
if (data.suggestions && data.suggestions.length > 0) {
|
| 163 |
+
// Store plant data and uploaded image info
|
| 164 |
+
const result = {
|
| 165 |
+
plantData: data,
|
| 166 |
+
uploadedImage: {
|
| 167 |
+
name: selectedFile.name,
|
| 168 |
+
type: selectedFile.type,
|
| 169 |
+
size: selectedFile.size,
|
| 170 |
+
lastModified: selectedFile.lastModified
|
| 171 |
+
}
|
| 172 |
+
};
|
| 173 |
+
sessionStorage.setItem('plantResult', JSON.stringify(result));
|
| 174 |
window.location.href = 'results.html';
|
| 175 |
} else {
|
| 176 |
throw new Error('No plant identified');
|
| 177 |
}
|
| 178 |
+
} catch (error) {
|
| 179 |
console.error('Error:', error);
|
| 180 |
alert('Identification failed: ' + error.message);
|
| 181 |
} finally {
|
|
@@ -58,12 +58,11 @@
|
|
| 58 |
<script>
|
| 59 |
document.addEventListener('DOMContentLoaded', function() {
|
| 60 |
feather.replace();
|
| 61 |
-
|
| 62 |
-
const plantData = JSON.parse(sessionStorage.getItem('plantData'));
|
| 63 |
const resultsContainer = document.getElementById('plant-info');
|
| 64 |
|
| 65 |
-
if (!plantData || !plantData.suggestions || plantData.suggestions.length === 0) {
|
| 66 |
-
|
| 67 |
<div class="col-span-2 text-center py-8">
|
| 68 |
<i data-feather="alert-circle" class="mx-auto text-yellow-500 h-12 w-12 mb-4"></i>
|
| 69 |
<h3 class="text-xl font-bold text-gray-800 mb-2">No Plant Identified</h3>
|
|
@@ -73,11 +72,11 @@
|
|
| 73 |
feather.replace();
|
| 74 |
return;
|
| 75 |
}
|
| 76 |
-
|
|
|
|
| 77 |
const topSuggestion = plantData.suggestions[0];
|
| 78 |
const confidence = Math.round(topSuggestion.probability * 100);
|
| 79 |
-
|
| 80 |
-
resultsContainer.innerHTML = `
|
| 81 |
<div>
|
| 82 |
<div class="bg-gray-100 rounded-lg overflow-hidden mb-4">
|
| 83 |
<img id="plant-image" src="" alt="Identified plant" class="w-full h-64 object-cover">
|
|
@@ -121,19 +120,26 @@
|
|
| 121 |
</div>
|
| 122 |
</div>
|
| 123 |
`;
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
const reader = new FileReader();
|
| 128 |
reader.onload = function(e) {
|
| 129 |
document.getElementById('plant-image').src = e.target.result;
|
| 130 |
};
|
| 131 |
-
reader.readAsDataURL(
|
| 132 |
} else {
|
| 133 |
document.getElementById('plant-image').src = 'http://static.photos/nature/640x360/42';
|
| 134 |
}
|
| 135 |
-
|
| 136 |
-
feather.replace();
|
| 137 |
});
|
| 138 |
</script>
|
| 139 |
</body>
|
|
|
|
| 58 |
<script>
|
| 59 |
document.addEventListener('DOMContentLoaded', function() {
|
| 60 |
feather.replace();
|
| 61 |
+
const plantResult = JSON.parse(sessionStorage.getItem('plantResult'));
|
|
|
|
| 62 |
const resultsContainer = document.getElementById('plant-info');
|
| 63 |
|
| 64 |
+
if (!plantResult || !plantResult.plantData || !plantResult.plantData.suggestions || plantResult.plantData.suggestions.length === 0) {
|
| 65 |
+
resultsContainer.innerHTML = `
|
| 66 |
<div class="col-span-2 text-center py-8">
|
| 67 |
<i data-feather="alert-circle" class="mx-auto text-yellow-500 h-12 w-12 mb-4"></i>
|
| 68 |
<h3 class="text-xl font-bold text-gray-800 mb-2">No Plant Identified</h3>
|
|
|
|
| 72 |
feather.replace();
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
+
const plantData = plantResult.plantData;
|
| 76 |
+
const uploadedImage = plantResult.uploadedImage;
|
| 77 |
const topSuggestion = plantData.suggestions[0];
|
| 78 |
const confidence = Math.round(topSuggestion.probability * 100);
|
| 79 |
+
resultsContainer.innerHTML = `
|
|
|
|
| 80 |
<div>
|
| 81 |
<div class="bg-gray-100 rounded-lg overflow-hidden mb-4">
|
| 82 |
<img id="plant-image" src="" alt="Identified plant" class="w-full h-64 object-cover">
|
|
|
|
| 120 |
</div>
|
| 121 |
</div>
|
| 122 |
`;
|
| 123 |
+
// Show the uploaded image
|
| 124 |
+
const fileInputs = document.querySelectorAll('input[type="file"]');
|
| 125 |
+
let selectedFile = null;
|
| 126 |
|
| 127 |
+
fileInputs.forEach(input => {
|
| 128 |
+
if (input.files && input.files.length > 0) {
|
| 129 |
+
selectedFile = input.files[0];
|
| 130 |
+
}
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
if (selectedFile) {
|
| 134 |
const reader = new FileReader();
|
| 135 |
reader.onload = function(e) {
|
| 136 |
document.getElementById('plant-image').src = e.target.result;
|
| 137 |
};
|
| 138 |
+
reader.readAsDataURL(selectedFile);
|
| 139 |
} else {
|
| 140 |
document.getElementById('plant-image').src = 'http://static.photos/nature/640x360/42';
|
| 141 |
}
|
| 142 |
+
feather.replace();
|
|
|
|
| 143 |
});
|
| 144 |
</script>
|
| 145 |
</body>
|