Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,37 @@ from PIL import Image
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
-
import os
|
| 7 |
import argparse
|
|
|
|
| 8 |
|
| 9 |
# Load model
|
| 10 |
-
model = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load mapping data
|
| 13 |
def load_map(file_path):
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
disease_map = load_map("diseases.txt")
|
| 18 |
treatment_map = load_map("treatments.txt")
|
| 19 |
fertilizer_map = load_map("fertilizers.txt")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
| 24 |
def predict_disease(base64_img):
|
| 25 |
try:
|
| 26 |
img_bytes = base64.b64decode(base64_img)
|
|
@@ -50,15 +63,15 @@ def predict_disease(base64_img):
|
|
| 50 |
except Exception as e:
|
| 51 |
return {"error": f"Prediction failed: {str(e)}"}
|
| 52 |
|
| 53 |
-
# CLI
|
| 54 |
if __name__ == "__main__":
|
| 55 |
-
parser = argparse.ArgumentParser(description="AI Tree Disease
|
| 56 |
-
parser.add_argument("--image", type=str, help="Path to
|
| 57 |
-
parser.add_argument("--raw", action="store_true", help="Indicates the
|
| 58 |
args = parser.parse_args()
|
| 59 |
|
| 60 |
-
if not args.image:
|
| 61 |
-
print("
|
| 62 |
exit(1)
|
| 63 |
|
| 64 |
try:
|
|
@@ -66,10 +79,10 @@ if __name__ == "__main__":
|
|
| 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
|
| 75 |
-
print(f"
|
|
|
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
import json
|
|
|
|
| 6 |
import argparse
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# Load model
|
| 10 |
+
model = pipeline(
|
| 11 |
+
"image-classification",
|
| 12 |
+
model="linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification",
|
| 13 |
+
top_k=1
|
| 14 |
+
)
|
| 15 |
|
| 16 |
# Load mapping data
|
| 17 |
def load_map(file_path):
|
| 18 |
+
try:
|
| 19 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 20 |
+
return dict(line.strip().split(":", 1) for line in f if ":" in line)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"β οΈ Failed to load {file_path}: {e}")
|
| 23 |
+
return {}
|
| 24 |
|
| 25 |
disease_map = load_map("diseases.txt")
|
| 26 |
treatment_map = load_map("treatments.txt")
|
| 27 |
fertilizer_map = load_map("fertilizers.txt")
|
| 28 |
|
| 29 |
+
# Load critical diseases
|
| 30 |
+
try:
|
| 31 |
+
with open("critical_diseases.txt", "r", encoding='utf-8') as f:
|
| 32 |
+
critical_diseases = set(line.strip() for line in f if line.strip())
|
| 33 |
+
except:
|
| 34 |
+
critical_diseases = set()
|
| 35 |
|
| 36 |
+
# Main prediction function
|
| 37 |
def predict_disease(base64_img):
|
| 38 |
try:
|
| 39 |
img_bytes = base64.b64decode(base64_img)
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
return {"error": f"Prediction failed: {str(e)}"}
|
| 65 |
|
| 66 |
+
# CLI Interface
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
parser = argparse.ArgumentParser(description="πΏ AI-Powered Tree Disease Detector")
|
| 69 |
+
parser.add_argument("--image", type=str, help="Path to image or base64 file", required=True)
|
| 70 |
+
parser.add_argument("--raw", action="store_true", help="Indicates that the file is a raw image (e.g., JPG, PNG)")
|
| 71 |
args = parser.parse_args()
|
| 72 |
|
| 73 |
+
if not os.path.isfile(args.image):
|
| 74 |
+
print(f"β File not found: {args.image}")
|
| 75 |
exit(1)
|
| 76 |
|
| 77 |
try:
|
|
|
|
| 79 |
with open(args.image, "rb") as img_file:
|
| 80 |
base64_img = base64.b64encode(img_file.read()).decode('utf-8')
|
| 81 |
else:
|
| 82 |
+
with open(args.image, "r", encoding="utf-8") as f:
|
| 83 |
base64_img = f.read().strip()
|
| 84 |
|
| 85 |
result = predict_disease(base64_img)
|
| 86 |
print(json.dumps(result, indent=2))
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(json.dumps({"error": f"Unexpected error: {str(e)}"}, indent=2))
|