File size: 15,023 Bytes
5acfbd2
336fa4a
eeac6a0
336fa4a
eeac6a0
336fa4a
1bd3004
336fa4a
65a47c9
eeac6a0
 
 
 
 
05c7c93
a13fdf1
05c7c93
 
 
 
336fa4a
 
eeac6a0
 
05c7c93
cdf9e57
05c7c93
 
 
 
 
 
b4e654e
05c7c93
 
 
 
336fa4a
4709c72
 
eeac6a0
4709c72
a13fdf1
eeac6a0
336fa4a
eeac6a0
05c7c93
 
 
7d5fc79
a13fdf1
 
 
7d5fc79
 
a13fdf1
 
 
 
 
 
 
 
 
 
 
eeac6a0
 
a13fdf1
65a47c9
 
 
eeac6a0
65a47c9
 
 
 
eeac6a0
65a47c9
 
 
 
 
 
eeac6a0
65a47c9
 
 
 
 
 
eeac6a0
05c7c93
eeac6a0
93a18ad
a13fdf1
eeac6a0
 
 
05c7c93
 
 
eeac6a0
e8ddde1
05c7c93
e8ddde1
 
eeac6a0
 
e8ddde1
eeac6a0
 
 
e8ddde1
 
 
 
05c7c93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eeac6a0
 
 
 
 
 
 
 
 
 
 
 
 
86dc4d5
5c8ce3b
 
 
 
 
 
eeac6a0
86dc4d5
e8ddde1
 
 
 
 
 
 
eeac6a0
05c7c93
eeac6a0
 
 
 
e8ddde1
 
eeac6a0
7d5fc79
05c7c93
7d5fc79
eeac6a0
 
 
3b61715
05c7c93
 
 
eeac6a0
3b61715
e8ddde1
3b61715
 
 
eeac6a0
3b61715
 
 
 
 
ade40fc
3b61715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eeac6a0
3b61715
6ed1467
3b61715
 
 
 
 
 
 
 
 
 
 
eeac6a0
05c7c93
8d8f9ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eeac6a0
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# test app.py
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi import FastAPI, File, UploadFile, HTTPException
from ndvi_predictor import load_model, normalize_rgb, predict_ndvi, create_visualization
from yolo_predictor import load_yolo_model, predict_yolo, predict_pipeline
from PIL import Image
from io import BytesIO
import numpy as np
import zipfile
import json
import rasterio
from rasterio.transform import from_bounds
import tempfile
import os
import logging
from resize_image import resize_image_optimized, resize_image_simple

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()

# Load models at startup
try:
    ndvi_model = load_model("ndvi_best_model")
    logger.info("NDVI model loaded successfully")
except Exception as e:
    logger.error(f"Failed to load NDVI model: {e}")
    ndvi_model = None

try:
    yolo_model = load_yolo_model("best_yolo_model.pt")
    logger.info("YOLO model loaded successfully")
except Exception as e:
    logger.error(f"Failed to load YOLO model: {e}")
    yolo_model = None

@app.get("/")
async def root():
    return {"message": "Welcome to the NDVI and YOLO prediction API!"}

# Example usage in your predict_ndvi endpoint:
@app.post("/predict_ndvi/")
async def predict_ndvi_api(file: UploadFile = File(...)):
    """Predict NDVI from RGB image"""
    if ndvi_model is None:
        return JSONResponse(status_code=500, content={"error": "NDVI model not loaded"})
    
    try:
        # Define target size (height, width)
        target_size = (640, 640)
        
        contents = await file.read()
        img = Image.open(BytesIO(contents)).convert("RGB")
        
        # Convert to numpy array
        rgb_array = np.array(img)
        
        # Resize image to target size
        rgb_resized = resize_image_optimized(rgb_array, target_size)
        
        # Normalize the resized image
        norm_img = normalize_rgb(rgb_resized)
        
        # Predict NDVI
        pred_ndvi = predict_ndvi(ndvi_model, norm_img)
        
        # Rest of the endpoint remains the same...
        # Visualization image as PNG
        vis_img_bytes = create_visualization(norm_img, pred_ndvi)
        vis_img_bytes.seek(0)
        
        # NDVI band as .npy
        ndvi_bytes = BytesIO()
        np.save(ndvi_bytes, pred_ndvi)
        ndvi_bytes.seek(0)
        
        # Create a ZIP containing both files
        zip_buf = BytesIO()
        with zipfile.ZipFile(zip_buf, "w") as zip_file:
            zip_file.writestr("ndvi_image.png", vis_img_bytes.read())
            ndvi_bytes.seek(0)
            zip_file.writestr("ndvi_band.npy", ndvi_bytes.read())
        
        zip_buf.seek(0)
        return StreamingResponse(
            zip_buf,
            media_type="application/x-zip-compressed",
            headers={"Content-Disposition": "attachment; filename=ndvi_output.zip"}
        )
    except Exception as e:
        logger.error(f"Error in predict_ndvi_api: {e}")
        return JSONResponse(status_code=500, content={"error": str(e)})


@app.post("/predict_yolo/")
async def predict_yolo_api(file: UploadFile = File(...)):
    """Predict YOLO results from 4-channel TIFF image"""
    if yolo_model is None:
        return JSONResponse(status_code=500, content={"error": "YOLO model not loaded"})
    
    try:
        # Save uploaded file temporarily with proper extension
        file_extension = '.tiff' if file.filename and file.filename.lower().endswith(('.tif', '.tiff')) else '.tiff'
        
        with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as tmp_file:
            contents = await file.read()
            tmp_file.write(contents)
            tmp_file.flush()  # Ensure data is written
            tmp_file_path = tmp_file.name
        
        try:
            # Verify the file was written correctly
            if not os.path.exists(tmp_file_path) or os.path.getsize(tmp_file_path) == 0:
                raise ValueError("Failed to create temporary file")
            
            logger.info(f"Processing YOLO prediction for file: {file.filename}, temp path: {tmp_file_path}")
            
            # Additional validation: check if file has 4 channels
            try:
                import tifffile
                test_array = tifffile.imread(tmp_file_path)
                if len(test_array.shape) == 3:
                    if test_array.shape[0] == 4 or test_array.shape[2] == 4:
                        channels = 4
                    else:
                        channels = test_array.shape[0] if test_array.shape[0] <= 4 else test_array.shape[2]
                else:
                    channels = 1
                
                if channels != 4:
                    raise ValueError(f"YOLO model expects 4-channel images, but uploaded file has {channels} channels")
                    
            except Exception as validation_error:
                logger.warning(f"Could not validate channels: {validation_error}")
            
            # Predict using YOLO model
            results = predict_yolo(yolo_model, tmp_file_path)
            
            # Convert results to JSON-serializable format
            results_dict = {
                "boxes": {
                    "xyxyn": results.boxes.xyxyn.tolist() if results.boxes is not None else None,
                    "conf": results.boxes.conf.tolist() if results.boxes is not None else None,
                    "cls": results.boxes.cls.tolist() if results.boxes is not None else None
                },
                "classes": results.boxes.cls.tolist() if results.boxes is not None else None,
                "names": results.names,
                "orig_shape": results.orig_shape,
                "speed": results.speed,
                "masks": {
                    "data": results.masks.data.tolist() if results.masks is not None else None,
                    "orig_shape": results.masks.orig_shape if results.masks is not None else None,
                    "xy": [seg.tolist() for seg in results.masks.xy] if results.masks is not None else None,
                    "xyn": [seg.tolist() for seg in results.masks.xyn] if results.masks is not None else None
                }
            }

            # Handle growth stages if present in the results
            if hasattr(results, 'boxes') and results.boxes is not None:
                if hasattr(results.boxes, 'data') and len(results.boxes.data) > 0:
                    # Check if there are additional columns for growth stages
                    if results.boxes.data.shape[1] > 6:
                        growth_stages = results.boxes.data[:, 6:].tolist()
                        results_dict["growth_stages"] = growth_stages
            
            logger.info(f"YOLO prediction completed successfully")
            return JSONResponse(content=results_dict)
            
        finally:
            # Clean up temporary file
            if os.path.exists(tmp_file_path):
                os.unlink(tmp_file_path)
            
    except Exception as e:
        logger.error(f"Error in predict_yolo_api: {e}")
        return JSONResponse(status_code=500, content={"error": str(e)})

@app.post("/predict_pipeline/")
async def predict_pipeline_api(file: UploadFile = File(...)):
    """Full pipeline: RGB -> NDVI -> 32-bit 4-channel TIFF (RGB+NDVI) -> YOLO prediction"""
    if ndvi_model is None or yolo_model is None:
        return JSONResponse(status_code=500, content={"error": "Models not loaded properly"})
    
    try:
        logger.info(f"Starting full pipeline for file: {file.filename}")
        
        # Read uploaded RGB image
        contents = await file.read()
        logger.info(f"Read {len(contents)} bytes from uploaded file")
        
        # Convert to PIL Image and then to numpy array
        img = Image.open(BytesIO(contents)).convert("RGB")
        rgb_array = np.array(img)
        logger.info(f"Converted to RGB array with shape: {rgb_array.shape}")
        
        # Run the full pipeline (now includes resizing internally)
        results = predict_pipeline(ndvi_model, yolo_model, rgb_array)
        logger.info("Pipeline processing completed successfully")
        
        # Convert results to JSON-serializable format
        results_dict = {
            "boxes": {
                "xyxyn": results.boxes.xyxyn.tolist() if results.boxes is not None else None,
                "conf": results.boxes.conf.tolist() if results.boxes is not None else None,
                "cls": results.boxes.cls.tolist() if results.boxes is not None else None
            },
            "classes": results.boxes.cls.tolist() if results.boxes is not None else None,
            "names": results.names,
            "orig_shape": results.orig_shape,
            "speed": results.speed,
            "masks": {
                "data": results.masks.data.tolist() if results.masks is not None else None,
                "orig_shape": results.masks.orig_shape if results.masks is not None else None,
                "xy": [seg.tolist() for seg in results.masks.xy] if results.masks is not None else None,
                "xyn": [seg.tolist() for seg in results.masks.xyn] if results.masks is not None else None
            }
        }

        # Handle growth stages if present in the results
        if hasattr(results, 'boxes') and results.boxes is not None:
            if hasattr(results.boxes, 'data') and len(results.boxes.data) > 0:
                # Check if there are additional columns for growth stages
                if results.boxes.data.shape[1] > 6:
                    growth_stages = results.boxes.data[:, 6:].tolist()
                    results_dict["growth_stages"] = growth_stages
        
        logger.info(f"Pipeline prediction completed successfully with {len(results_dict['boxes']['xyxyn']) if results_dict['boxes']['xyxyn'] else 0} detections")
        return JSONResponse(content=results_dict)
        
    except Exception as e:
        logger.error(f"Error in predict_pipeline_api: {e}")
        return JSONResponse(status_code=500, content={"error": str(e)})
    
# New endpoints to add to your FastAPI app
from yolo_predictor import predict_yolo_with_image, predict_pipeline_with_image, pil_image_to_bytes

@app.post("/predict_yolo_image/")
async def predict_yolo_image_api(file: UploadFile = File(...)):
    """Predict YOLO results from 4-channel TIFF image and return annotated image"""
    if yolo_model is None:
        return JSONResponse(status_code=500, content={"error": "YOLO model not loaded"})
    
    try:
        # Save uploaded file temporarily with proper extension
        file_extension = '.tiff' if file.filename and file.filename.lower().endswith(('.tif', '.tiff')) else '.tiff'
        
        with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as tmp_file:
            contents = await file.read()
            tmp_file.write(contents)
            tmp_file.flush()  # Ensure data is written
            tmp_file_path = tmp_file.name
        
        try:
            # Verify the file was written correctly
            if not os.path.exists(tmp_file_path) or os.path.getsize(tmp_file_path) == 0:
                raise ValueError("Failed to create temporary file")
            
            logger.info(f"Processing YOLO prediction with image output for file: {file.filename}, temp path: {tmp_file_path}")
            
            # Additional validation: check if file has 4 channels
            try:
                import tifffile
                test_array = tifffile.imread(tmp_file_path)
                if len(test_array.shape) == 3:
                    if test_array.shape[0] == 4 or test_array.shape[2] == 4:
                        channels = 4
                    else:
                        channels = test_array.shape[0] if test_array.shape[0] <= 4 else test_array.shape[2]
                else:
                    channels = 1
                
                if channels != 4:
                    raise ValueError(f"YOLO model expects 4-channel images, but uploaded file has {channels} channels")
                    
            except Exception as validation_error:
                logger.warning(f"Could not validate channels: {validation_error}")
            
            # Predict using YOLO model and get annotated image
            annotated_image = predict_yolo_with_image(yolo_model, tmp_file_path)
            
            # Convert PIL Image to bytes for response
            img_bytes = pil_image_to_bytes(annotated_image, format='PNG')
            
            logger.info(f"YOLO prediction with image output completed successfully")
            
            return StreamingResponse(
                img_bytes,
                media_type="image/png",
                headers={"Content-Disposition": f"attachment; filename=yolo_annotated_{file.filename}.png"}
            )
            
        finally:
            # Clean up temporary file
            if os.path.exists(tmp_file_path):
                os.unlink(tmp_file_path)
            
    except Exception as e:
        logger.error(f"Error in predict_yolo_image_api: {e}")
        return JSONResponse(status_code=500, content={"error": str(e)})

@app.post("/predict_pipeline_image/")
async def predict_pipeline_image_api(file: UploadFile = File(...)):
    """Full pipeline with image output: RGB -> NDVI -> 32-bit 4-channel TIFF (RGB+NDVI) -> YOLO prediction -> Annotated Image"""
    if ndvi_model is None or yolo_model is None:
        return JSONResponse(status_code=500, content={"error": "Models not loaded properly"})
    
    try:
        logger.info(f"Starting full pipeline with image output for file: {file.filename}")
        
        # Read uploaded RGB image
        contents = await file.read()
        logger.info(f"Read {len(contents)} bytes from uploaded file")
        
        # Convert to PIL Image and then to numpy array
        img = Image.open(BytesIO(contents)).convert("RGB")
        rgb_array = np.array(img)
        logger.info(f"Converted to RGB array with shape: {rgb_array.shape}")
        
        # Run the full pipeline with image output (includes resizing internally)
        annotated_image = predict_pipeline_with_image(ndvi_model, yolo_model, rgb_array)
        logger.info("Pipeline processing with image output completed successfully")
        
        # Convert PIL Image to bytes for response
        img_bytes = pil_image_to_bytes(annotated_image, format='PNG')
        
        logger.info(f"Pipeline prediction with image output completed successfully")
        
        return StreamingResponse(
            img_bytes,
            media_type="image/png",
            headers={"Content-Disposition": f"attachment; filename=pipeline_annotated_{file.filename}.png"}
        )
        
    except Exception as e:
        logger.error(f"Error in predict_pipeline_image_api: {e}")
        return JSONResponse(status_code=500, content={"error": str(e)})