Spaces:
Sleeping
Sleeping
File size: 3,026 Bytes
62f828b |
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 |
from fastapi import APIRouter, UploadFile, File
from io import BytesIO
from src.app.config import load_config
from src.pipeline import InferencePipeline
# Define the router
router = APIRouter()
# Load configuration
config = load_config()
inference_pipeline = InferencePipeline(config)
from fastapi import APIRouter, UploadFile, File, HTTPException
from io import BytesIO
import os
from fastapi import HTTPException
from fastapi.responses import FileResponse
@router.post("/predict")
async def process_image(
file: UploadFile = File(...),
apply_clahe_postprocess: bool = False,
apply_pre_contrast_adjustment: bool = True,
return_original_size: bool = True
):
"""
API endpoint to process and super-resolve an image.
Args:
file: Image file to process (PNG, JPEG, or DICOM).
apply_clahe_postprocess: Boolean indicating if CLAHE should be applied post-processing.
Returns:
FileResponse: Processed image file or error message.
"""
try:
# Validate apply_clahe_postprocess parameter
if not isinstance(apply_clahe_postprocess, bool):
raise HTTPException(
status_code=400,
detail="The 'apply_clahe_postprocess' parameter must be a boolean."
)
if not isinstance(apply_pre_contrast_adjustment, bool):
raise HTTPException(
status_code=400,
detail="The 'apply_pre_contrast_adjustment' parameter must be a boolean."
)
if not isinstance(return_original_size, bool):
raise HTTPException(
status_code=400,
detail="The 'return_original_size' parameter must be a boolean."
)
# Read the uploaded file into memory
file_bytes = await file.read()
# Perform inference with the pipeline
sr_image = inference_pipeline.run(BytesIO(file_bytes), apply_clahe_postprocess=apply_clahe_postprocess,
apply_pre_contrast_adjustment = apply_pre_contrast_adjustment,
return_original_size = return_original_size
)
# Save the processed image to a temporary file
output_file_path = "output_highres.png"
sr_image.save(output_file_path, format="PNG")
# Return the file as a response
return FileResponse(
path=output_file_path,
media_type="image/png",
filename="processed_image.png"
)
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An error occurred during processing: {str(e)}"
)
finally:
# Cleanup temporary file if it exists
if os.path.exists("output_highres.png"):
os.remove("output_highres.png")
|