Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from PIL import Image
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
import json
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load model
|
| 8 |
model = pipeline("image-classification", model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification", top_k=1)
|
|
@@ -17,17 +19,15 @@ treatment_map = load_map("treatments.txt")
|
|
| 17 |
fertilizer_map = load_map("fertilizers.txt")
|
| 18 |
|
| 19 |
with open("critical_diseases.txt", "r") as f:
|
| 20 |
-
critical_diseases = set(line.strip() for line in f)
|
| 21 |
|
| 22 |
def predict_disease(base64_img):
|
| 23 |
-
# Decode base64
|
| 24 |
try:
|
| 25 |
img_bytes = base64.b64decode(base64_img)
|
| 26 |
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 27 |
except Exception as e:
|
| 28 |
return {"error": f"Invalid image: {str(e)}"}
|
| 29 |
|
| 30 |
-
# Predict
|
| 31 |
try:
|
| 32 |
result = model(image)[0]
|
| 33 |
label = result["label"]
|
|
@@ -43,7 +43,6 @@ def predict_disease(base64_img):
|
|
| 43 |
"fertilizer_recommendation": fertilizer
|
| 44 |
}
|
| 45 |
|
| 46 |
-
# Alerts (for integration)
|
| 47 |
if confidence < 0.6 or disease in critical_diseases:
|
| 48 |
output["alert"] = True
|
| 49 |
|
|
@@ -51,9 +50,26 @@ def predict_disease(base64_img):
|
|
| 51 |
except Exception as e:
|
| 52 |
return {"error": f"Prediction failed: {str(e)}"}
|
| 53 |
|
| 54 |
-
#
|
| 55 |
if __name__ == "__main__":
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
+
import os
|
| 7 |
+
import argparse
|
| 8 |
|
| 9 |
# Load model
|
| 10 |
model = pipeline("image-classification", model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification", top_k=1)
|
|
|
|
| 19 |
fertilizer_map = load_map("fertilizers.txt")
|
| 20 |
|
| 21 |
with open("critical_diseases.txt", "r") as f:
|
| 22 |
+
critical_diseases = set(line.strip() for line in f if line.strip())
|
| 23 |
|
| 24 |
def predict_disease(base64_img):
|
|
|
|
| 25 |
try:
|
| 26 |
img_bytes = base64.b64decode(base64_img)
|
| 27 |
image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 28 |
except Exception as e:
|
| 29 |
return {"error": f"Invalid image: {str(e)}"}
|
| 30 |
|
|
|
|
| 31 |
try:
|
| 32 |
result = model(image)[0]
|
| 33 |
label = result["label"]
|
|
|
|
| 43 |
"fertilizer_recommendation": fertilizer
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
if confidence < 0.6 or disease in critical_diseases:
|
| 47 |
output["alert"] = True
|
| 48 |
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
return {"error": f"Prediction failed: {str(e)}"}
|
| 52 |
|
| 53 |
+
# CLI Entry Point
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
parser = argparse.ArgumentParser(description="AI Tree Disease Predictor")
|
| 56 |
+
parser.add_argument("--image", type=str, help="Path to base64 file or image to encode")
|
| 57 |
+
parser.add_argument("--raw", action="store_true", help="Indicates the --image is a raw image, not base64 text")
|
| 58 |
+
args = parser.parse_args()
|
| 59 |
+
|
| 60 |
+
if not args.image:
|
| 61 |
+
print("β Please provide an image file using --image")
|
| 62 |
+
exit(1)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
if args.raw:
|
| 66 |
+
with open(args.image, "rb") as img_file:
|
| 67 |
+
base64_img = base64.b64encode(img_file.read()).decode('utf-8')
|
| 68 |
+
else:
|
| 69 |
+
with open(args.image, "r") as f:
|
| 70 |
+
base64_img = f.read().strip()
|
| 71 |
+
|
| 72 |
+
result = predict_disease(base64_img)
|
| 73 |
+
print(json.dumps(result, indent=2))
|
| 74 |
+
except FileNotFoundError:
|
| 75 |
+
print(f"β File not found: {args.image}")
|