Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,31 +13,23 @@ model = load_model("ndvi_best_model.keras")
|
|
| 13 |
|
| 14 |
@app.post("/predict/")
|
| 15 |
async def predict_ndvi_api(file: UploadFile = File(...)):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
rgb_norm = normalize_rgb(rgb_np)
|
| 20 |
-
ndvi = predict_ndvi(model, rgb_norm)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
dst.write(ndvi, 1)
|
| 38 |
-
geotiff_buffer.seek(0)
|
| 39 |
|
| 40 |
-
return {
|
| 41 |
-
"ndvi_tiff": StreamingResponse(geotiff_buffer, media_type="image/tiff", headers={"Content-Disposition": "attachment; filename=predicted_ndvi.tif"}),
|
| 42 |
-
"visualization": StreamingResponse(vis_image, media_type="image/png", headers={"Content-Disposition": "attachment; filename=ndvi_visualization.png"})
|
| 43 |
-
}
|
|
|
|
| 13 |
|
| 14 |
@app.post("/predict/")
|
| 15 |
async def predict_ndvi_api(file: UploadFile = File(...)):
|
| 16 |
+
try:
|
| 17 |
+
contents = await file.read()
|
| 18 |
+
img = Image.open(BytesIO(contents)).convert("RGB")
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Preprocess & predict
|
| 21 |
+
norm_img = normalize_rgb(np.array(img))
|
| 22 |
+
pred_ndvi = predict_ndvi(model, norm_img)
|
| 23 |
|
| 24 |
+
# Create visualization
|
| 25 |
+
vis_img = create_visualization(norm_img, pred_ndvi)
|
| 26 |
+
|
| 27 |
+
# Convert to bytes
|
| 28 |
+
img_byte_arr = BytesIO()
|
| 29 |
+
vis_img.save(img_byte_arr, format='PNG')
|
| 30 |
+
img_byte_arr.seek(0)
|
| 31 |
+
|
| 32 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|