File size: 9,608 Bytes
5acfbd2
336fa4a
eeac6a0
336fa4a
eeac6a0
336fa4a
1bd3004
336fa4a
65a47c9
eeac6a0
 
 
 
 
05c7c93
 
 
 
 
336fa4a
 
eeac6a0
 
05c7c93
 
 
 
 
 
 
 
 
 
 
 
 
336fa4a
4709c72
 
eeac6a0
4709c72
eeac6a0
336fa4a
eeac6a0
05c7c93
 
 
7d5fc79
 
 
 
eeac6a0
 
65a47c9
 
 
eeac6a0
65a47c9
 
 
 
eeac6a0
65a47c9
 
 
 
 
 
eeac6a0
65a47c9
 
 
 
 
 
eeac6a0
05c7c93
eeac6a0
93a18ad
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
 
 
70135b4
05c7c93
 
 
eeac6a0
70135b4
e8ddde1
70135b4
 
 
eeac6a0
70135b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eeac6a0
70135b4
6ed1467
70135b4
 
 
 
 
 
 
 
 
 
 
eeac6a0
05c7c93
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
# 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

# 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.keras")
    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("4c_6c_regression.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!"}

@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:
        contents = await file.read()
        img = Image.open(BytesIO(contents)).convert("RGB")
        norm_img = normalize_rgb(np.array(img))
        pred_ndvi = predict_ndvi(ndvi_model, norm_img)
        
        # 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
        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)})