added isolated function to merge into the dashboard

This commit is contained in:
MIRKO BANDELLO 2025-08-02 05:09:00 +02:00
parent d1dd1b4a8a
commit 87fd2aa818
4 changed files with 102 additions and 0 deletions

Binary file not shown.

BIN
basilico.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

View file

@ -0,0 +1,42 @@
from datetime import datetime
from script import PlantPredictor
def dashboard_plant_prediction(image_path, start_date, end_date, additional_notes=""):
"""
Simple function for dashboard integration
"""
try:
# Calculate days
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
days = (end_dt - start_dt).days
if days <= 0:
return {"success": False, "error": "Invalid date range"}
# Create predictor and run
predictor = PlantPredictor()
result = predictor.dashboard_plant_prediction(image_path, days, additional_notes)
if result:
return {"success": True, "result": result}
else:
return {"success": False, "error": "No result"}
except Exception as e:
return {"success": False, "error": str(e)}
# Test
if __name__ == "__main__":
result = dashboard_plant_prediction(
"./basilico.jpg",
"2024-08-01",
"2024-08-08",
"Test plant"
)
if result["success"]:
print(" SUCCESS!")
else:
print(f" ERROR: {result['error']}")

60
script.py Normal file
View file

@ -0,0 +1,60 @@
from datetime import datetime
import sys
import os
class PlantPredictor:
def dashboard_plant_prediction(
image_path: str,
start_date: str,
end_date: str,
additional_notes: str = ""
) -> dict:
try:
# Calcola giorni
start_dt = datetime.strptime(start_date, "%Y-%m-%d")
end_dt = datetime.strptime(end_date, "%Y-%m-%d")
days = (end_dt - start_dt).days
if days <= 0:
return {"success": False, "error": "End date must be after start date", "days": days}
# Log
print(f"Dashboard prediction request: {start_date} to {end_date} ({days} days) image={image_path}")
if additional_notes:
print(f"Notes: {additional_notes}")
# Inizializza il predictor e chiama il metodo
predictor = PlantPredictor()
result = predictor.predict_plant_growth(image_path, days, additional_notes)
# Unwrap risultato tuple
if isinstance(result, tuple) and len(result) == 5:
_img, conditions, weather_df, plant_type, plant_health = result
return {
"success": True,
"plant_analysis": {"plant_type": plant_type, "plant_health": plant_health},
"weather_conditions": conditions,
"weather_data_shape": weather_df.shape,
"parameters_used": {"start_date": start_date, "end_date": end_date, "days": days, "notes": additional_notes, "image": image_path},
"prediction_summary": {
"temperature_range": f"{conditions['avg_temp_min']}{conditions['avg_temp_max']}°C",
"total_rain": f"{conditions['total_rain']}mm",
"sunshine_hours": f"{conditions['total_sunshine_hours']}h"
}
}
else:
return {"success": False, "error": "Invalid result from PlantPredictor", "result": result}
except ValueError as e:
return {"success": False, "error": f"Date format error: {e}"}
except Exception as e:
return {"success": False, "error": f"Unexpected error: {e}"}
# Esempio di test
if __name__ == '__main__':
res = dashboard_plant_prediction(
image_path='./basilico.jpg',
start_date='2024-08-01',
end_date='2024-08-08',
additional_notes='Indoor day 3'
)
print(res)