Spaces:
Sleeping

File size: 9,721 Bytes
82f8baa
 
 
 
 
 
 
 
a0f7af7
 
 
 
 
 
82f8baa
a0f7af7
 
 
 
5881c56
 
82f8baa
 
5881c56
a0f7af7
 
82f8baa
a0f7af7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5881c56
1dd29b1
 
5881c56
 
a0f7af7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23cebd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0f7af7
 
23cebd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0f7af7
 
23cebd8
a0f7af7
 
 
 
 
 
 
 
 
 
 
2497bc5
8c4ac04
 
 
 
 
2497bc5
 
 
 
8c4ac04
2497bc5
 
 
 
 
 
 
a0f7af7
8c4ac04
a0f7af7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23cebd8
 
 
a0f7af7
23cebd8
a0f7af7
23cebd8
 
 
 
 
 
 
 
 
 
 
a0f7af7
23cebd8
a0f7af7
 
23cebd8
 
a0f7af7
23cebd8
a0f7af7
23cebd8
 
 
 
 
 
 
 
 
 
 
a0f7af7
23cebd8
a0f7af7
23cebd8
a0f7af7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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)