Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,62 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from io import BytesIO
|
| 4 |
-
from src.pipeline import InferencePipeline
|
| 5 |
-
from src.app.config import load_config
|
| 6 |
-
|
| 7 |
-
# Load configuration and initialize the inference pipeline
|
| 8 |
-
config = load_config()
|
| 9 |
-
inference_pipeline = InferencePipeline(config)
|
| 10 |
-
|
| 11 |
-
def process_image_from_bytes(file, apply_clahe_postprocess,apply_pre_contrast_adjustment,return_original_size):
|
| 12 |
-
"""
|
| 13 |
-
Process the image bytes using the inference pipeline.
|
| 14 |
-
|
| 15 |
-
Args:
|
| 16 |
-
file_bytes: The image file in bytes.
|
| 17 |
-
apply_clahe_postprocess: Boolean indicating if CLAHE postprocessing should be applied.
|
| 18 |
-
|
| 19 |
-
Returns:
|
| 20 |
-
The processed image.
|
| 21 |
-
"""
|
| 22 |
-
try:
|
| 23 |
-
# Perform super-resolution
|
| 24 |
-
sr_image = inference_pipeline.run(file, apply_pre_contrast_adjustment=apply_pre_contrast_adjustment, apply_clahe_postprocess=apply_clahe_postprocess,return_original_size=return_original_size)
|
| 25 |
-
return sr_image
|
| 26 |
-
except Exception as e:
|
| 27 |
-
return f"An exception occurred: {str(e)}"
|
| 28 |
-
|
| 29 |
-
# Define the Gradio interface
|
| 30 |
-
def gradio_interface():
|
| 31 |
-
with gr.Blocks() as demo:
|
| 32 |
-
gr.Markdown("""
|
| 33 |
-
# X-Ray Image Super-Resolution-Denoiser Demo
|
| 34 |
-
Provide image bytes to process and optionally apply CLAHE postprocessing.
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from src.pipeline import InferencePipeline
|
| 5 |
+
from src.app.config import load_config
|
| 6 |
+
|
| 7 |
+
# Load configuration and initialize the inference pipeline
|
| 8 |
+
config = load_config()
|
| 9 |
+
inference_pipeline = InferencePipeline(config)
|
| 10 |
+
|
| 11 |
+
def process_image_from_bytes(file, apply_clahe_postprocess,apply_pre_contrast_adjustment,return_original_size):
|
| 12 |
+
"""
|
| 13 |
+
Process the image bytes using the inference pipeline.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
file_bytes: The image file in bytes.
|
| 17 |
+
apply_clahe_postprocess: Boolean indicating if CLAHE postprocessing should be applied.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
The processed image.
|
| 21 |
+
"""
|
| 22 |
+
try:
|
| 23 |
+
# Perform super-resolution
|
| 24 |
+
sr_image = inference_pipeline.run(file, apply_pre_contrast_adjustment=apply_pre_contrast_adjustment, apply_clahe_postprocess=apply_clahe_postprocess,return_original_size=return_original_size)
|
| 25 |
+
return sr_image
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"An exception occurred: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Define the Gradio interface
|
| 30 |
+
def gradio_interface():
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("""
|
| 33 |
+
# X-Ray Image Super-Resolution-Denoiser Demo
|
| 34 |
+
Provide image bytes to process and optionally apply CLAHE postprocessing.
|
| 35 |
+
For github : Whole code with FastAPI and Docker - https://github.com/SerdarHelli/xray-superres-enhancer
|
| 36 |
+
""")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
file_input = gr.File(label="Upload Image (PNG, JPEG, or DICOM)")
|
| 40 |
+
apply_clahe_checkbox = gr.Checkbox(label="Apply CLAHE Postprocessing", value=False)
|
| 41 |
+
apply_pre_contrast_adjustment_checkbox = gr.Checkbox(label="Apply PreContrast Adjustment", value=False)
|
| 42 |
+
return_original_size_checkbox = gr.Checkbox(label="Return Original Size", value=True)
|
| 43 |
+
|
| 44 |
+
process_button = gr.Button("Process Image")
|
| 45 |
+
output_image = gr.Image(label="Processed Image")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
process_button.click(
|
| 50 |
+
process_image_from_bytes,
|
| 51 |
+
inputs=[file_input, apply_clahe_checkbox,apply_pre_contrast_adjustment_checkbox,return_original_size_checkbox],
|
| 52 |
+
outputs=output_image
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return demo
|
| 56 |
+
|
| 57 |
+
# Launch the Gradio interface
|
| 58 |
+
demo = gradio_interface()
|
| 59 |
+
|
| 60 |
+
demo.launch(
|
| 61 |
+
debug=True,
|
| 62 |
+
)
|