CLL_exp_annot / app.py
Skier8402's picture
Update app.py
07dd5b0 verified
raw
history blame
5.03 kB
'''
This is the originall CLL Explorer application that allows users to upload, process, and save images.
The application provides the following functionalities:
- Upload microscope images.
- Adjust image view with zoom and enhancement controls.
- Detect and measure cells automatically.
- Save analysis results and annotations.
The application is divided into the following sections:
1. **Upload Images**: Users can upload microscope images in JPG or PNG format.
2. **Select Image**: Users can select an image from the uploaded files.
3. **Processed Image**: Displays the processed image with zoom and enhancement controls.
4. **Image Controls**: Allows users to adjust the image view with sliders for X and Y coordinates, zoom, contrast, brightness, and sharpness.
5. **Save Options**: Provides options to save the processed image, image description, and image parameters.
To run the application:
1. Save the script in a Python file (e.g., app.py).
2. Run the script using the Streamlit command:
```bash
streamlit run app.py
'''
import streamlit as st
from PIL import Image, ImageEnhance
import pandas as pd
import numpy as np
import io
import os
def zoom_at(img, x, y, zoom):
'''
Zoom into an image at a specific location.
Parameters:
----------
img : PIL.Image
Input image.
x : int
X-coordinate of the zoom center.
y : int
Y-coordinate of the zoom center.
zoom : float
Zoom factor.
Returns:
-------
PIL.Image
Zoomed image.
Examples:
--------
>>> img = Image.open('image.jpg')
>>> zoomed_img = zoom_at(img, x=100, y=100, zoom=2.0)
'''
w, h = img.size
zoom2 = zoom * 2
img = img.crop((x - w / zoom2, y - h / zoom2,
x + w / zoom2, y + h / zoom2))
return img.resize((w, h), Image.LANCZOS)
st.set_page_config(page_title="CLL Explorer", layout="wide")
st.title("CLL Explorer - Malaria Cell Detection")
st.markdown("""
### About This Application
- **Upload** microscope images.
- **Adjust** image view with zoom and enhancement controls.
- **Detect** and measure cells automatically.
- **Save** analysis results and annotations.
""")
uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
if uploaded_files:
img_index = st.selectbox(
"Select Image",
range(len(uploaded_files)),
format_func=lambda x: uploaded_files[x].name
)
img_data = uploaded_files[img_index].read()
img = Image.open(io.BytesIO(img_data)).resize((500, 500))
# Display Processed Image first
st.subheader("Processed Image")
x = st.slider("X Coordinate", 0, 500, 250)
y = st.slider("Y Coordinate", 0, 500, 250)
zoom = st.slider("Zoom", 1, 10, 5)
with st.expander("Enhancement Settings", expanded=True):
contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
def apply_enhancements(image):
zoomed = zoom_at(image, x, y, zoom)
enhanced_contrast = ImageEnhance.Contrast(zoomed).enhance(contrast)
enhanced_bright = ImageEnhance.Brightness(enhanced_contrast).enhance(brightness)
enhanced_sharp = ImageEnhance.Sharpness(enhanced_bright).enhance(sharpness)
return enhanced_sharp
processed_img = apply_enhancements(img)
st.image(processed_img, caption="Processed Image")
# Show Original Image next
st.subheader("Original Image")
st.image(img, caption="Original Image")
# Save Options
save_image = st.checkbox("Save Processed Image")
if save_image:
processed_img.save("image-processed.jpg")
st.success("Image saved as `image-processed.jpg`")
with st.expander("Save Options"):
description = st.text_area("Describe the image", "")
if st.button("Save Description"):
with open("saved_image_description.txt", "w") as f:
f.write(description)
st.success("Description saved as `saved_image_description.txt`")
if st.button("Save Image Parameters"):
params = {
"coordinates_x": x,
"coordinates_y": y,
"zoom": zoom,
"contrast": contrast,
"brightness": brightness,
"sharpness": sharpness
}
with open("saved_image_parameters.json", "w") as f:
f.write(pd.DataFrame([params]).to_json(orient="records"))
st.success("Image parameters saved as `saved_image_parameters.json`")
if st.button("Rename Files"):
file_ext = str(np.random.randint(100))
os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
st.success("Files renamed successfully")