diff --git a/__pycache__/script.cpython-311.pyc b/__pycache__/script.cpython-311.pyc new file mode 100644 index 0000000..36213a5 Binary files /dev/null and b/__pycache__/script.cpython-311.pyc differ diff --git a/basilico.jpg b/basilico.jpg new file mode 100644 index 0000000..d84b39d Binary files /dev/null and b/basilico.jpg differ diff --git a/forDashboardIntegration.py b/forDashboardIntegration.py new file mode 100644 index 0000000..0e07774 --- /dev/null +++ b/forDashboardIntegration.py @@ -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']}") \ No newline at end of file diff --git a/script.py b/script.py new file mode 100644 index 0000000..3c28508 --- /dev/null +++ b/script.py @@ -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)