PlantWeb / app.py
nafalrust's picture
fix: detect-weed
23cebd8
Raw History Blame Contribute Delete
9.72 kB
import os
os.makedirs("/tmp/mpl", exist_ok=True)
os.makedirs("/tmp/yolo", exist_ok=True)
os.environ["MPLCONFIGDIR"] = "/tmp/mpl"
os.environ["YOLO_CONFIG_DIR"] = "/tmp/yolo"
from flask import Flask, request, jsonify, send_file
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image, ImageDraw
import io
import matplotlib
import requests
from io import BytesIO
import pickle
from ultralytics import YOLO
import torch.serialization
import ultralytics.nn.tasks
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# === Path & Config ===
LABEL_ENCODER_PATH = "label_encoder.pkl"
CNN_MODEL_PATH = "model_trained.pt"
YOLO_MODEL_PATH = "best_model6.pt"
# === Load Label Encoder ===
label_encoder = None
if os.path.exists(LABEL_ENCODER_PATH):
with open(LABEL_ENCODER_PATH, 'rb') as f:
label_encoder = pickle.load(f)
# === Model Residual untuk Klasifikasi Penyakit ===
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels)
)
self.skip = nn.Conv2d(in_channels, out_channels, kernel_size=1) if in_channels != out_channels else nn.Identity()
def forward(self, x):
return torch.relu(self.conv(x) + self.skip(x))
class MiniResNet(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.layer1 = ResidualBlock(3, 32)
self.pool1 = nn.MaxPool2d(2)
self.layer2 = ResidualBlock(32, 64)
self.pool2 = nn.MaxPool2d(2)
self.layer3 = ResidualBlock(64, 128)
self.pool3 = nn.MaxPool2d(2)
self.layer4 = ResidualBlock(128, 128)
self.pool4 = nn.MaxPool2d(2)
self.layer5 = ResidualBlock(128, 128)
self.pool5 = nn.AdaptiveAvgPool2d((1, 1))
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(128, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, num_classes)
)
def forward(self, x):
x = self.pool1(self.layer1(x))
x = self.pool2(self.layer2(x))
x = self.pool3(self.layer3(x))
x = self.pool4(self.layer4(x))
x = self.pool5(self.layer5(x))
return self.classifier(x)
# === Load Models ===
def load_model(path, is_cnn=True):
if is_cnn:
loaded = torch.load(path, map_location=torch.device('cpu'))
model = MiniResNet(num_classes=len(label_encoder.classes_))
model.load_state_dict(loaded['state_dict'] if 'state_dict' in loaded else loaded)
model.eval()
return model
else:
def torch_safe_load_patch(file, map_location="cpu"):
ckpt = torch.load(file, map_location=map_location, weights_only=False)
return ckpt, file
ultralytics.nn.tasks.torch_safe_load = torch_safe_load_patch # override safe loader
return YOLO(path)
model_cnn = load_model(CNN_MODEL_PATH, is_cnn=True)
model_yolo = load_model(YOLO_MODEL_PATH, is_cnn=False)
# === Transforms ===
transform_common = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor()
])
transform_cnn = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# === CNN Prediction ===
def predict_cnn(image):
image = image.convert('RGB')
input_tensor = transform_cnn(image).unsqueeze(0)
with torch.no_grad():
output = model_cnn(input_tensor)
_, predicted = torch.max(output, 1)
class_idx = int(predicted.item())
label = label_encoder.inverse_transform([class_idx])[0] if label_encoder else str(class_idx)
return label
# === YOLO Detection ===
def predict_yolo(image):
# Simpan ukuran asli gambar untuk scaling bounding box
original_size = image.size
image_rgb = image.convert('RGB')
# YOLO membutuhkan input tensor, tapi kita perlu mempertahankan ukuran asli untuk bounding box
img_for_yolo = transform_common(image_rgb)
results = model_yolo(img_for_yolo.unsqueeze(0))[0]
# Gunakan gambar asli untuk drawing (tidak yang sudah di-resize)
draw_image = image_rgb.copy()
draw = ImageDraw.Draw(draw_image)
detection_count = 0
if results.boxes is not None and len(results.boxes) > 0:
# Scale factor untuk konversi dari ukuran model (256x256) ke ukuran asli
scale_x = original_size[0] / 256
scale_y = original_size[1] / 256
for box in results.boxes.data.tolist():
x1, y1, x2, y2, conf, cls = box
# Skip deteksi dengan confidence rendah
if conf < 0.3: # Threshold confidence
continue
# Scale coordinates ke ukuran asli
x1 = int(x1 * scale_x)
y1 = int(y1 * scale_y)
x2 = int(x2 * scale_x)
y2 = int(y2 * scale_y)
# Pastikan coordinates dalam bounds
x1 = max(0, min(x1, original_size[0]))
y1 = max(0, min(y1, original_size[1]))
x2 = max(0, min(x2, original_size[0]))
y2 = max(0, min(y2, original_size[1]))
# Gambar bounding box
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
# Tambahkan text label dengan background
label = f"Weed {conf:.2f}"
# Hitung ukuran text
try:
bbox = draw.textbbox((0, 0), label)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
except:
# Fallback untuk versi PIL lama
text_width, text_height = draw.textsize(label)
# Gambar background untuk text
draw.rectangle(
[x1, y1 - text_height - 4, x1 + text_width + 4, y1],
fill="red"
)
draw.text((x1 + 2, y1 - text_height - 2), label, fill="white")
detection_count += 1
# Jika tidak ada deteksi, tambahkan text informasi
if detection_count == 0:
draw.text((10, 10), "No weeds detected", fill="green", anchor="lt")
else:
draw.text((10, 10), f"Detected {detection_count} weed(s)", fill="red", anchor="lt")
img_io = BytesIO()
draw_image.save(img_io, 'PNG')
img_io.seek(0)
return img_io
# === UTILS ===
def load_image_from_request():
file = request.files.get('image')
if not file:
raise ValueError("No image uploaded")
return Image.open(io.BytesIO(file.read()))
def load_image_from_url():
try:
if request.is_json:
data = request.get_json()
else:
raise ValueError("Request content type must be application/json")
url = data.get('image_url')
if not url:
raise ValueError("No image_url provided")
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
return Image.open(BytesIO(response.content))
except Exception as e:
raise ValueError(f"Failed to load image from URL: {str(e)}")
# === CNN Endpoint ===
@app.route('/predict-disease/upload', methods=['POST'])
def predict_disease_upload():
try:
image = load_image_from_request()
result = predict_cnn(image)
return jsonify({'prediction': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/predict-disease/url', methods=['POST'])
def predict_disease_url():
try:
image = load_image_from_url()
result = predict_cnn(image)
return jsonify({'prediction': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
# === YOLO Detection Endpoint ===
# Tambahkan error handling yang lebih baik pada endpoint
@app.route('/detect-weed/url', methods=['POST'])
def detect_weed_url():
try:
image = load_image_from_url()
result_image = predict_yolo(image)
# Set proper headers untuk image response
response = send_file(
result_image,
mimetype='image/png',
as_attachment=False
)
# Tambahkan CORS headers untuk image response
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(f"Error in detect_weed_url: {str(e)}") # Log error
return jsonify({'error': str(e)}), 500
@app.route('/detect-weed/upload', methods=['POST'])
def detect_weed_upload():
try:
image = load_image_from_request()
result_image = predict_yolo(image)
# Set proper headers untuk image response
response = send_file(
result_image,
mimetype='image/png',
as_attachment=False
)
# Tambahkan CORS headers untuk image response
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(f"Error in detect_weed_upload: {str(e)}") # Log error
return jsonify({'error': str(e)}), 500
# === Run Flask App (Hugging Face Spaces expects port 7860) ===
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)