Update app.po
Browse files
app.po
CHANGED
|
@@ -460,6 +460,41 @@ from PIL import Image
|
|
| 460 |
# -------------------------------------------------------
|
| 461 |
# Soil Recognizer
|
| 462 |
# -------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
def soil_recognizer_ui():
|
| 464 |
st.header("🖼️ Soil Recognizer (Image / OCR)")
|
| 465 |
site = get_active_site()
|
|
@@ -467,29 +502,34 @@ def soil_recognizer_ui():
|
|
| 467 |
col1, col2 = st.columns(2)
|
| 468 |
|
| 469 |
with col1:
|
| 470 |
-
uploaded = st.file_uploader("Upload soil image", type=["jpg","jpeg","png"])
|
| 471 |
-
if uploaded:
|
| 472 |
-
img = Image.open(uploaded)
|
| 473 |
st.image(img, caption="Uploaded soil image", use_column_width=True)
|
| 474 |
-
# TODO: integrate your trained soil recognition model
|
| 475 |
-
st.success("Soil recognizer placeholder: model inference to be integrated.")
|
| 476 |
-
|
| 477 |
-
with col2:
|
| 478 |
-
st.subheader("📑 OCR Extraction")
|
| 479 |
-
if easyocr is None:
|
| 480 |
-
st.warning("easyocr not installed. Add `easyocr` to requirements.txt.")
|
| 481 |
-
else:
|
| 482 |
-
ocr_file = st.file_uploader("Upload photo of question/text (OCR)", type=["jpg","jpeg","png"], key="ocr_input")
|
| 483 |
-
if ocr_file:
|
| 484 |
-
reader = easyocr.Reader(['en'])
|
| 485 |
-
results = reader.readtext(np.array(Image.open(ocr_file)))
|
| 486 |
-
extracted_text = " ".join([r[1] for r in results])
|
| 487 |
-
st.text_area("Extracted text", extracted_text, height=150)
|
| 488 |
-
# TODO: parse extracted numbers for classification if possible
|
| 489 |
-
site["ocr_pending"] = True
|
| 490 |
-
save_active_site(site)
|
| 491 |
-
st.success("OCR text extracted. Parsed values will be linked to classifier soon.")
|
| 492 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
# -------------------------------------------------------
|
| 494 |
# Soil Classifier
|
| 495 |
# -------------------------------------------------------
|
|
|
|
| 460 |
# -------------------------------------------------------
|
| 461 |
# Soil Recognizer
|
| 462 |
# -------------------------------------------------------
|
| 463 |
+
import torch
|
| 464 |
+
import torch.nn as nn
|
| 465 |
+
import torchvision.transforms as transforms
|
| 466 |
+
from PIL import Image
|
| 467 |
+
import streamlit as st
|
| 468 |
+
|
| 469 |
+
# ----------------------------
|
| 470 |
+
# Load Soil Recognition Model
|
| 471 |
+
# ----------------------------
|
| 472 |
+
@st.cache_resource
|
| 473 |
+
def load_soil_model():
|
| 474 |
+
try:
|
| 475 |
+
model = torch.load("soil_best_model.pth", map_location=torch.device("cpu"))
|
| 476 |
+
model.eval()
|
| 477 |
+
return model
|
| 478 |
+
except Exception as e:
|
| 479 |
+
st.error(f"⚠️ Could not load soil model: {e}")
|
| 480 |
+
return None
|
| 481 |
+
|
| 482 |
+
soil_model = load_soil_model()
|
| 483 |
+
|
| 484 |
+
# Define soil classes (adjust if your model has different labels)
|
| 485 |
+
SOIL_CLASSES = ["Sand", "Silt", "Clay", "Gravel", "Peat"]
|
| 486 |
+
|
| 487 |
+
# Image preprocessing pipeline
|
| 488 |
+
transform = transforms.Compose([
|
| 489 |
+
transforms.Resize((224, 224)), # match training input size
|
| 490 |
+
transforms.ToTensor(),
|
| 491 |
+
transforms.Normalize([0.485, 0.456, 0.406], # ImageNet normalization
|
| 492 |
+
[0.229, 0.224, 0.225])
|
| 493 |
+
])
|
| 494 |
+
|
| 495 |
+
# ----------------------------
|
| 496 |
+
# Soil Recognizer UI
|
| 497 |
+
# ----------------------------
|
| 498 |
def soil_recognizer_ui():
|
| 499 |
st.header("🖼️ Soil Recognizer (Image / OCR)")
|
| 500 |
site = get_active_site()
|
|
|
|
| 502 |
col1, col2 = st.columns(2)
|
| 503 |
|
| 504 |
with col1:
|
| 505 |
+
uploaded = st.file_uploader("Upload soil image", type=["jpg", "jpeg", "png"])
|
| 506 |
+
if uploaded is not None:
|
| 507 |
+
img = Image.open(uploaded).convert("RGB")
|
| 508 |
st.image(img, caption="Uploaded soil image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
+
if soil_model:
|
| 511 |
+
try:
|
| 512 |
+
# Preprocess
|
| 513 |
+
img_t = transform(img).unsqueeze(0) # add batch dimension
|
| 514 |
+
with torch.no_grad():
|
| 515 |
+
outputs = soil_model(img_t)
|
| 516 |
+
probs = torch.softmax(outputs, dim=1)
|
| 517 |
+
conf, pred = torch.max(probs, 1)
|
| 518 |
+
predicted_class = SOIL_CLASSES[pred.item()]
|
| 519 |
+
confidence = conf.item()
|
| 520 |
+
|
| 521 |
+
# Save to site
|
| 522 |
+
site["Soil Profile"] = predicted_class
|
| 523 |
+
site["classifier_inputs"]["image_confidence"] = confidence
|
| 524 |
+
save_sites(SITES)
|
| 525 |
+
|
| 526 |
+
st.success(f"✅ Predicted: **{predicted_class}** ({confidence:.2%} confidence)")
|
| 527 |
+
|
| 528 |
+
except Exception as e:
|
| 529 |
+
st.error(f"❌ Inference error: {e}")
|
| 530 |
+
else:
|
| 531 |
+
st.warning("⚠️ Soil model not loaded. Please check `soil_best_model.pth`.")
|
| 532 |
+
|
| 533 |
# -------------------------------------------------------
|
| 534 |
# Soil Classifier
|
| 535 |
# -------------------------------------------------------
|