tomaito_web/index.html

557 lines
18 KiB
HTML
Raw Normal View History

2024-01-16 15:30:46 +01:00
<head>
<meta charset="UTF-8">
2024-01-19 12:16:55 +01:00
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- dodałem ten tag, aby ustawić viewport strony -->
2024-01-16 15:30:46 +01:00
<title>TomAIto</title>
2024-01-19 12:16:55 +01:00
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
2024-01-16 15:30:46 +01:00
<style>
2024-01-19 12:16:55 +01:00
/* Użyłem zmiennych CSS, aby łatwiej zmieniać kolory i czcionki */
:root {
--primary-color: #409c0f;
--secondary-color: #e2513b;
--dark-color: #333;
--light-color: #fff;
--font-family: 'Roboto', sans-serif;
}
2024-01-18 23:28:35 +01:00
body {
2024-01-19 12:16:55 +01:00
font-family: var(--font-family);
2024-01-18 23:28:35 +01:00
margin: 0;
2024-01-19 12:16:55 +01:00
padding: 20px;
background-color: var(--light-color);
color: var(--dark-color);
display: flex;
flex-direction: column;
align-items: center;
2024-01-18 23:28:35 +01:00
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode {
2024-01-19 12:16:55 +01:00
background-color: var(--dark-color);
color: var(--light-color) !important;
2024-01-18 23:28:35 +01:00
}
header {
2024-01-19 12:16:55 +01:00
background-color: var(--primary-color);
color: var(--light-color);
2024-01-18 23:28:35 +01:00
text-align: center;
2024-01-19 12:16:55 +01:00
padding: 20pt;
padding-right: 10%;
padding-left: 10%;
2024-01-18 23:28:35 +01:00
font-size: 24px;
2024-01-20 22:09:57 +01:00
width: 80%;
2024-01-18 23:28:35 +01:00
position: relative;
2024-01-16 15:30:46 +01:00
display: flex;
2024-01-18 23:28:35 +01:00
justify-content: center;
2024-01-16 15:30:46 +01:00
}
2024-01-18 23:28:35 +01:00
header span {
2024-01-19 12:16:55 +01:00
color: var(--secondary-color);
2024-01-18 23:28:35 +01:00
font-weight: bold;
}
#darkModeToggle {
width: 90px;
2024-01-19 12:16:55 +01:00
height: 45px;
font-size: 18px;
background-color: var(--secondary-color);
color: var(--light-color);
border: var(--light-color);
border-radius: 10px;
2024-01-16 15:30:46 +01:00
display: flex;
2024-01-18 23:28:35 +01:00
align-items: center;
justify-content: space-between;
padding: 5px;
cursor: pointer;
transition: background-color 0.3s;
position: absolute;
top: 50%;
right: 20px;
transform: translateY(-50%);
}
#darkModeToggle:hover {
2024-01-19 12:16:55 +01:00
background-color: var(--secondary-color);
2024-01-18 23:28:35 +01:00
}
2024-01-16 15:30:46 +01:00
2024-01-18 23:28:35 +01:00
canvas {
2024-01-19 12:16:55 +01:00
border: 1px solid var(--dark-color);
2024-01-20 22:09:57 +01:00
max-height: 600px;
2024-01-18 23:28:35 +01:00
display: block;
margin: auto;
margin-top: 20px;
2024-01-19 12:16:55 +01:00
background-color: var(--light-color);
2024-01-18 23:28:35 +01:00
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
#weatherInfo {
max-width: 100%;
2024-01-21 15:54:44 +01:00
position: relative;
2024-01-18 23:28:35 +01:00
margin: auto;
2024-01-21 15:54:44 +01:00
margin-top: 10px; /* Add some margin between legend and weatherInfo */
2024-01-18 23:28:35 +01:00
padding: 15px;
2024-01-19 12:16:55 +01:00
background-color: var(--light-color);
color: var(--dark-color);
2024-01-18 23:28:35 +01:00
}
2024-01-16 15:30:46 +01:00
form {
2024-01-18 23:28:35 +01:00
max-width: 600px;
width: 100%;
margin: auto;
2024-01-19 12:16:55 +01:00
margin-top: 20px;
2024-01-18 23:28:35 +01:00
padding: 20px;
2024-01-16 15:30:46 +01:00
display: flex;
flex-direction: column;
2024-01-18 23:28:35 +01:00
align-items: center;
2024-01-19 12:16:55 +01:00
background-color: var(--light-color);
2024-01-18 23:28:35 +01:00
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
2024-01-19 12:16:55 +01:00
color: var(--dark-color);
2024-01-18 23:28:35 +01:00
transition: background-color 0.3s, color 0.3s;
}
body.dark-mode form {
2024-01-19 12:16:55 +01:00
background-color: var(--dark-color);
color: var(--light-color);
2024-01-16 15:30:46 +01:00
}
label {
2024-01-19 12:16:55 +01:00
display: block;
margin-bottom: 5px;
font-size: 18px;
2024-01-18 23:28:35 +01:00
}
input,
select {
width: 100%;
box-sizing: border-box;
2024-01-19 12:16:55 +01:00
margin-bottom: 20px;
2024-01-18 23:28:35 +01:00
padding: 10px;
2024-01-19 12:16:55 +01:00
border: 1px solid var(--dark-color);
2024-01-18 23:28:35 +01:00
border-radius: 4px;
2024-01-19 12:16:55 +01:00
background-color: var(--light-color);
2024-01-18 23:28:35 +01:00
transition: border-color 0.3s;
font-weight: bold;
2024-01-19 12:16:55 +01:00
font-size: 16px;
2024-01-18 23:28:35 +01:00
}
input[type="file"]:focus {
outline: none;
2024-01-19 12:16:55 +01:00
border-color: var(--secondary-color); /* użyłem zmiennej CSS zamiast koloru szesnastkowego */
2024-01-16 15:30:46 +01:00
}
button {
2024-01-18 23:28:35 +01:00
width: 100%;
padding: 15px;
border: none;
border-radius: 4px;
2024-01-19 12:16:55 +01:00
background-color: var(--secondary-color); /* użyłem zmiennej CSS zamiast koloru szesnastkowego */
color: var(--light-color);
2024-01-18 23:28:35 +01:00
cursor: pointer;
2024-01-19 12:16:55 +01:00
transition: background-color 0.3s, transform 0.3s;
2024-01-18 23:28:35 +01:00
font-weight: bold;
2024-01-19 12:16:55 +01:00
font-size: 18px;
2024-01-18 23:28:35 +01:00
}
button:hover {
2024-01-19 12:16:55 +01:00
background-color: var(--primary-color); /* użyłem zmiennej CSS zamiast koloru szesnastkowego */
transform: scale(1.05);
}
2024-01-20 22:09:57 +01:00
#legend {
2024-01-21 15:54:44 +01:00
position: relative;
top: 0px;
align-items: center;
display: flex;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px;
font-size: 16px;
flex-wrap: wrap;
justify-content: center;
margin-top: 0;
}
2024-01-20 22:09:57 +01:00
2024-01-21 15:54:44 +01:00
#legend h3 {
margin-bottom: 10px;
font-size: 18px;
position: relative;
}
2024-01-20 22:09:57 +01:00
2024-01-21 15:54:44 +01:00
.legend-item {
display: flex;
align-items: center;
margin-right: 15px; /* Add some spacing between legend items */
}
2024-01-20 22:09:57 +01:00
2024-01-21 15:54:44 +01:00
.legend-color {
width: 20px;
height: 20px;
margin-right: 5px;
border-radius: 5px;
}
.legend-label {
font-weight: bold;
}
#weatherInfo.dark-mode {
background-color: var(--dark-color);
color: var(--light-color);
}
#legend.dark-mode {
background-color: rgba(0, 0, 0, 0.8);
color: var(--light-color);
}
2024-01-20 22:09:57 +01:00
2024-01-19 12:16:55 +01:00
/* dodałem zapytania medialne, aby dostosować styl strony do różnych szerokości okna przeglądarki */
@media (max-width: 768px) {
2024-01-21 15:54:44 +01:00
#legend {
position: relative;
align-items: left;
display: flex;
top: auto; /* Reset the top position */
bottom: 20px; /* Add some distance from the bottom of the page */
justify-content: center;
margin-top: 10px;
}
.legend-item {
margin-right: 0; /* Remove right margin on smaller screens */
margin-bottom: 10px; /* Add some spacing between legend items */
}
}
2024-01-19 12:16:55 +01:00
#darkModeToggle {
position: right;
transform: none;
margin-top: 10px;
}
@media (max-width: 600px) {
body {
padding: 10px;
}
header {
padding: 10pt;
font-size: 20px;
}
form {
padding: 10px;
}
label {
font-size: 16px;
}
input,
select,
button {
font-size: 14px;
}
2024-01-21 15:54:44 +01:00
#legend {
align-items: left;
top: auto; /* Reset the top position */
bottom: 20px; /* Add some distance from the bottom of the page */
margin-top: 10px;
flex-direction: column;
justify-content: left;
}
.legend-item {
margin-right: 0; /* Remove right margin on smaller screens */
margin-bottom: 10px; /* Add some spacing between legend items */
justify-content: left;
}
2024-01-16 15:30:46 +01:00
}
</style>
</head>
<body>
2024-01-18 23:28:35 +01:00
<header>
Tom<span style="color: #ff6600;">AI</span>to
<button id="darkModeToggle" onclick="toggleDarkMode()">Dark Mode</button>
</header>
2024-01-16 15:30:46 +01:00
<form id="uploadForm">
<label for="cropType">Crop Type:</label>
<select id="cropType" name="cropType">
<option value="external">External</option>
<option value="internal">Internal</option>
<option value="greenhouse">Greenhouse</option>
</select>
<label for="location">Location:</label>
<input type="text" id="location" name="location" required/>
<label for="uploadInput">Choose an image:</label>
<input id="uploadInput" type="file" required/>
2024-01-19 12:16:55 +01:00
<button type="submit" id="checkRipenessBtn">Check the ripeness</button>
2024-01-16 15:30:46 +01:00
</form>
2024-01-19 12:16:55 +01:00
<div id="loadingMessage" style="display: none;">Loading...</div>
<canvas style="display: none; max-width: 100%;"></canvas>
2024-01-21 15:54:44 +01:00
<div id="legend" style="display: none;"></div>
2024-01-19 12:16:55 +01:00
<div id="weatherInfo" style="display: none;"></div>
2024-01-16 15:30:46 +01:00
<script>
const form = document.getElementById("uploadForm");
2024-01-19 12:16:55 +01:00
const loadingMessage = document.getElementById("loadingMessage");
const weatherInfo = document.getElementById("weatherInfo");
const canvas = document.querySelector("canvas");
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
form.addEventListener("submit", async (event) => {
event.preventDefault();
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
// Show loading message and weather info
loadingMessage.style.display = "block";
weatherInfo.style.display = "block";
2024-01-19 12:16:55 +01:00
2024-01-21 15:54:44 +01:00
// Disable the button to prevent multiple submissions
document.getElementById("checkRipenessBtn").disabled = true;
2024-01-19 12:16:55 +01:00
2024-01-21 15:54:44 +01:00
const fileInput = document.getElementById("uploadInput");
const cropTypeInput = document.getElementById("cropType");
const locationInput = document.getElementById("location");
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
const file = fileInput.files[0];
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
const data = new FormData();
data.append("image_file", file);
data.append("cropType", cropTypeInput.value);
data.append("location", locationInput.value);
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
const response = await fetch("/detect", {
method: "post",
body: data
});
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
const boxes = await response.json();
draw_image_and_boxes(file, boxes);
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
// Hide loading message and enable the button after canvas is loaded
loadingMessage.style.display = "none";
document.getElementById("checkRipenessBtn").disabled = false;
2024-01-16 16:02:04 +01:00
2024-01-21 15:54:44 +01:00
const location = locationInput.value;
2024-01-16 15:30:46 +01:00
2024-01-21 15:54:44 +01:00
if (cropTypeInput.value === "external") {
updateWeatherInfo(location, cropTypeInput.value);
} else {
const randomTips = [
"water your tomatoes every day.",
"keep the temperature between 20-25 degrees Celsius.",
"place the tomatoes in sunlight."
];
2024-01-20 22:09:57 +01:00
2024-01-21 15:54:44 +01:00
const randomTip = randomTips[Math.floor(Math.random() * randomTips.length)];
weatherInfo.innerText = "TIP! To maximize your yields " + randomTip;
}
});
2024-01-20 22:09:57 +01:00
2024-01-21 15:54:44 +01:00
function draw_image_and_boxes(file, boxes) {
const img = new Image()
img.src = URL.createObjectURL(file);
img.onload = () => {
// Show canvas once the image is loaded
canvas.style.display = "block";
//weatherInfo.innerText = ""; // Clear previous weather info
const ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
ctx.lineWidth = 15;
2024-01-21 22:20:43 +01:00
ctx.font = "bold 70px Arial"; // Zwiększono rozmiar czcionki
2024-01-21 15:54:44 +01:00
// Draw bounding boxes
boxes.forEach(([x1, y1, x2, y2, object_type, prob]) => {
switch (object_type) {
case "b_fully_ripened":
object_type = "big fully ripened";
break;
case "b_half_ripened":
object_type = "big half ripened";
break;
case "b_green":
object_type = "big unripe";
break;
case "l_fully_ripened":
object_type = "small fully ripened";
break;
case "l_half_ripened":
object_type = "small half ripened";
break;
case "l_green":
object_type = "small unripe";
break;
default:
object_type = "#000000";
break;
}
// Przypisz różne kolory do różnych klas
let boxColor;
switch (object_type) {
case "big fully ripened":
boxColor = "#0077FF";
break;
case "big half ripened":
boxColor = "#00FFDE";
break;
case "big unripe":
boxColor = "#B2EBF2";
break;
case "small fully ripened":
boxColor = "#C62828";
break;
case "small half ripened":
boxColor = "#E57373";
break;
case "small unripe":
boxColor = "#FBD943";
break;
default:
boxColor = "#000000";
break;
}
2024-01-21 22:20:43 +01:00
const label = `${object_type}`;
2024-01-21 15:54:44 +01:00
ctx.strokeStyle = boxColor;
ctx.strokeRect(x1, y1, x2 - x1, y2 - y1);
ctx.fillStyle = boxColor;
const width = ctx.measureText(label).width;
2024-01-21 22:20:43 +01:00
ctx.fillRect(x1, y1 - 40, width + 10, 70);
2024-01-21 15:54:44 +01:00
ctx.fillStyle = "#000000";
2024-01-21 19:45:55 +01:00
ctx.fillText(label, x1, y1 + 10 ); // Przesunięcie etykiety
2024-01-21 15:54:44 +01:00
});
// Draw legend
drawLegend(ctx);
};
2024-01-20 22:09:57 +01:00
}
function drawLegend(ctx) {
2024-01-21 15:54:44 +01:00
// Remove existing legend container
const existingLegend = document.getElementById("legend");
if (existingLegend) {
existingLegend.parentNode.removeChild(existingLegend);
}
2024-01-20 22:09:57 +01:00
const legendContainer = document.createElement("div");
legendContainer.id = "legend";
const legendTitle = document.createElement("h3");
2024-01-21 15:54:44 +01:00
legendTitle.textContent = "";
2024-01-20 22:09:57 +01:00
legendContainer.appendChild(legendTitle);
const legendItems = [
2024-01-21 15:54:44 +01:00
{ label: "big fully ripened", color: "#0077FF" },
{ label: "big half ripened", color: "#00FFDE" },
{ label: "big unripe", color: "#B2EBF2" },
{ label: "small fully ripened", color: "#C62828" },
{ label: "small half ripened", color: "#E58633" },
{ label: "small unripe", color: "#FBD943" },
2024-01-20 22:09:57 +01:00
];
legendItems.forEach(item => {
const legendItem = document.createElement("div");
2024-01-21 15:54:44 +01:00
2024-01-20 22:09:57 +01:00
legendItem.classList.add("legend-item");
const legendColor = document.createElement("div");
legendColor.classList.add("legend-color");
legendColor.style.backgroundColor = item.color;
const legendLabel = document.createElement("div");
legendLabel.classList.add("legend-label");
legendLabel.textContent = item.label;
legendItem.appendChild(legendColor);
legendItem.appendChild(legendLabel);
legendContainer.appendChild(legendItem);
});
document.body.appendChild(legendContainer);
}
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
async function updateWeatherInfo(location, cropType) {
const apiKey = "1fc6a52c96331d035a828cc0c1606241";
const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${apiKey}`;
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
try {
const weatherResponse = await fetch(apiUrl);
const weatherData = await weatherResponse.json();
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
if (weatherData.main && weatherData.main.temp) {
const temperatureKelvin = weatherData.main.temp;
const temperatureCelsius = temperatureKelvin - 273.15;
const tempRound = Math.round(temperatureCelsius);
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
const weatherCondition = weatherData.weather[0].main;
const isRaining = weatherCondition === 'Rain';
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
let additionalInfo = `${isRaining ? 'Rain is expected in your town today, so you dont have to water your tomatoes' :
'No rainfall is expected in the city today, so you need to water your tomatoes!'} Temperature in ${location} is ${tempRound} °C.`;
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
if (tempRound < 15) {
additionalInfo += ' So you need to cover your tomatoes!';
}
weatherInfo.innerText = additionalInfo;
} else {
weatherInfo.innerText = "Unable to fetch weather data.";
}
2024-01-16 15:30:46 +01:00
2024-01-19 12:16:55 +01:00
} catch (error) {
console.error("Error fetching weather data:", error);
weatherInfo.innerText = "Error fetching weather data.";
}
2024-01-16 15:30:46 +01:00
}
</script>
2024-01-18 23:28:35 +01:00
<script>
// Dark mode toggle functionality
2024-01-21 15:54:44 +01:00
function toggleDarkMode() {
const body = document.body;
body.classList.toggle('dark-mode');
// Toggle dark mode for weatherInfo and legend
const weatherInfo = document.getElementById('weatherInfo');
const legend = document.getElementById('legend');
if (body.classList.contains('dark-mode')) {
weatherInfo.classList.add('dark-mode');
legend.classList.add('dark-mode');
} else {
weatherInfo.classList.remove('dark-mode');
legend.classList.remove('dark-mode');
}
// Change the text content of the toggle icon
const toggleIcon = document.getElementById('darkModeToggle');
toggleIcon.textContent = body.classList.contains('dark-mode') ? 'Light Mode' : 'Dark Mode';
}
2024-01-18 23:28:35 +01:00
</script>
2024-01-16 15:30:46 +01:00
</body>
2023-12-28 22:09:32 +01:00
</html>