Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- SoilNet.keras +3 -0
- app.py +115 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
SoilNet.keras filter=lfs diff=lfs merge=lfs -text
|
SoilNet.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8f65cd8fdae31632ff7a767ed5c23dc76b848dd57fcc9c0f4f54fb41eda790c3
|
| 3 |
+
size 134081172
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.models import load_model
|
| 2 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 3 |
+
import numpy as np
|
| 4 |
+
from flask import Flask, request, render_template, jsonify
|
| 5 |
+
from werkzeug.utils import secure_filename
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
model_path = os.path.join(os.path.dirname(__file__), "SoilNet.keras")
|
| 13 |
+
SoilNet = load_model(model_path)
|
| 14 |
+
|
| 15 |
+
# Classes dictionary
|
| 16 |
+
classes = {
|
| 17 |
+
0: "Alluvial Soil:-{ Rice, Wheat, Sugarcane, Maize, Cotton, Soyabean, Jute }",
|
| 18 |
+
1: "Black Soil:-{ Virginia, Wheat, Jowar, Millets, Linseed, Castor, Sunflower }",
|
| 19 |
+
2: "Clay Soil:-{ Rice, Lettuce, Chard, Broccoli, Cabbage, Snap Beans }",
|
| 20 |
+
3: "Red Soil:-{ Cotton, Wheat, Pulses, Millets, Oil Seeds, Potatoes }"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# API Key (set this securely in prod)
|
| 24 |
+
API_KEY = "your-secret-api-key-1234"
|
| 25 |
+
|
| 26 |
+
# Prediction function
|
| 27 |
+
def model_predict(image_path, model):
|
| 28 |
+
image = load_img(image_path, target_size=(224, 224))
|
| 29 |
+
image = img_to_array(image) / 255.0
|
| 30 |
+
image = np.expand_dims(image, axis=0)
|
| 31 |
+
|
| 32 |
+
result = np.argmax(model.predict(image), axis=-1)[0]
|
| 33 |
+
prediction = classes[result]
|
| 34 |
+
|
| 35 |
+
if result == 0:
|
| 36 |
+
return "Alluvial", "Alluvial.html"
|
| 37 |
+
elif result == 1:
|
| 38 |
+
return "Black", "Black.html"
|
| 39 |
+
elif result == 2:
|
| 40 |
+
return "Clay", "Clay.html"
|
| 41 |
+
elif result == 3:
|
| 42 |
+
return "Red", "Red.html"
|
| 43 |
+
|
| 44 |
+
# Route: Home (form)
|
| 45 |
+
@app.route('/', methods=['GET'])
|
| 46 |
+
def index():
|
| 47 |
+
return render_template('index.html')
|
| 48 |
+
|
| 49 |
+
# Route: Form-based upload + result display
|
| 50 |
+
@app.route('/predict', methods=['POST'])
|
| 51 |
+
def predict():
|
| 52 |
+
file = request.files.get('image')
|
| 53 |
+
if not file or file.filename == '':
|
| 54 |
+
return "No image uploaded", 400
|
| 55 |
+
|
| 56 |
+
# Validate extension
|
| 57 |
+
filename = secure_filename(file.filename)
|
| 58 |
+
if not filename.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')):
|
| 59 |
+
return "Unsupported file type", 400
|
| 60 |
+
|
| 61 |
+
# Save image
|
| 62 |
+
upload_folder = os.path.join(os.path.dirname(__file__), 'static', 'user_uploaded')
|
| 63 |
+
os.makedirs(upload_folder, exist_ok=True)
|
| 64 |
+
file_path = os.path.join(upload_folder, filename)
|
| 65 |
+
file.save(file_path)
|
| 66 |
+
|
| 67 |
+
# Check image is valid
|
| 68 |
+
try:
|
| 69 |
+
_ = load_img(file_path)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
os.remove(file_path)
|
| 72 |
+
return f"Invalid image file: {e}", 400
|
| 73 |
+
|
| 74 |
+
pred, output_page = model_predict(file_path, SoilNet)
|
| 75 |
+
user_image_path = os.path.join('static', 'user_uploaded', filename)
|
| 76 |
+
|
| 77 |
+
return render_template(output_page, pred_output=pred, user_image=user_image_path)
|
| 78 |
+
|
| 79 |
+
# Route: API endpoint with API key
|
| 80 |
+
@app.route('/api/predict', methods=['POST'])
|
| 81 |
+
def api_predict():
|
| 82 |
+
# Check for API key
|
| 83 |
+
key = request.headers.get('x-api-key')
|
| 84 |
+
if key != API_KEY:
|
| 85 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 86 |
+
|
| 87 |
+
# Validate image
|
| 88 |
+
file = request.files.get('image')
|
| 89 |
+
if not file or file.filename == '':
|
| 90 |
+
return jsonify({"error": "No image uploaded"}), 400
|
| 91 |
+
|
| 92 |
+
filename = secure_filename(file.filename)
|
| 93 |
+
if not filename.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')):
|
| 94 |
+
return jsonify({"error": "Unsupported file type"}), 400
|
| 95 |
+
|
| 96 |
+
# Save temp image
|
| 97 |
+
api_temp_folder = os.path.join(os.path.dirname(__file__), 'static', 'api_temp')
|
| 98 |
+
os.makedirs(api_temp_folder, exist_ok=True)
|
| 99 |
+
file_path = os.path.join(api_temp_folder, filename)
|
| 100 |
+
file.save(file_path)
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
_ = load_img(file_path)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
os.remove(file_path)
|
| 106 |
+
return jsonify({"error": f"Invalid image: {str(e)}"}), 400
|
| 107 |
+
|
| 108 |
+
pred, _ = model_predict(file_path, SoilNet)
|
| 109 |
+
os.remove(file_path) # Optional: delete after prediction
|
| 110 |
+
|
| 111 |
+
return jsonify({"soil_type": pred})
|
| 112 |
+
|
| 113 |
+
# Start the app
|
| 114 |
+
if __name__ == '__main__':
|
| 115 |
+
app.run(debug=True, threaded=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
werkzeug==1.0.0
|
| 2 |
+
numpy==1.19.1
|
| 3 |
+
gunicorn==19.9.0
|
| 4 |
+
Flask==1.1.1
|
| 5 |
+
h5py==2.9.0
|
| 6 |
+
tensorflow==2.3.0
|
| 7 |
+
pillow>=6.2.0
|