Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,82 +4,87 @@ import pandas as pd
|
|
| 4 |
import numpy as np
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
-
from pathlib import Path
|
| 8 |
|
| 9 |
def zoom_at(img, x, y, zoom):
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# App title and description
|
| 13 |
st.set_page_config(page_title="Cell Explorer", layout="wide")
|
| 14 |
-
st.title("CLL Explorer
|
| 15 |
|
| 16 |
st.markdown("""
|
| 17 |
-
### About
|
| 18 |
-
This tool
|
| 19 |
-
- Upload microscope images
|
| 20 |
-
- Adjust image view with zoom and enhancement controls
|
| 21 |
-
- Detect and measure cells automatically
|
| 22 |
-
- Save analysis results and annotations
|
| 23 |
-
|
| 24 |
-
**Note**: Cell measurements are in micrometers (µm)
|
| 25 |
""")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
tab1, tab2, tab3 = st.tabs(["Image Analysis", "Detection Results", "Settings"])
|
| 29 |
-
|
| 30 |
-
with tab1:
|
| 31 |
-
col1, col2 = st.columns([2,1])
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
img_index = st.selectbox("Select Image", range(len(uploaded_files)))
|
| 38 |
-
img_data = uploaded_files[img_index].read()
|
| 39 |
-
img = Image.open(io.BytesIO(img_data)).resize((800, 800))
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
with col2:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
st.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if st.button("Save
|
| 72 |
-
|
| 73 |
-
save_dir = Path("analysis_results") / timestamp
|
| 74 |
-
save_dir.mkdir(parents=True, exist_ok=True)
|
| 75 |
-
|
| 76 |
-
img_processed.save(save_dir / "processed_image.jpg")
|
| 77 |
-
with open(save_dir / "analysis_notes.txt", "w") as f:
|
| 78 |
f.write(description)
|
| 79 |
-
st.success(
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import io
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
def zoom_at(img, x, y, zoom):
|
| 9 |
+
w, h = img.size
|
| 10 |
+
zoom2 = zoom * 2
|
| 11 |
+
img = img.crop((x - w / zoom2, y - h / zoom2,
|
| 12 |
+
x + w / zoom2, y + h / zoom2))
|
| 13 |
+
return img.resize((w, h), Image.LANCZOS)
|
| 14 |
|
|
|
|
| 15 |
st.set_page_config(page_title="Cell Explorer", layout="wide")
|
| 16 |
+
st.title("CLL Explorer - Annotation Tool")
|
| 17 |
|
| 18 |
st.markdown("""
|
| 19 |
+
### About This Application
|
| 20 |
+
This tool assists researchers in analyzing microscope images of blood cells for malaria detection:
|
| 21 |
+
- **Upload** microscope images.
|
| 22 |
+
- **Adjust** image view with zoom and enhancement controls.
|
| 23 |
+
- **Detect** and measure cells automatically.
|
| 24 |
+
- **Save** analysis results and annotations.
|
|
|
|
|
|
|
| 25 |
""")
|
| 26 |
|
| 27 |
+
uploaded_files = st.file_uploader("Upload Images", accept_multiple_files=True, type=["jpg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
if uploaded_files:
|
| 30 |
+
img_index = st.selectbox("Select Image", range(len(uploaded_files)), format_func=lambda x: uploaded_files[x].name)
|
| 31 |
+
img_data = uploaded_files[img_index].read()
|
| 32 |
+
img = Image.open(io.BytesIO(img_data)).resize((500, 500))
|
| 33 |
|
| 34 |
+
col1, col2 = st.columns([3, 2])
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
with col1:
|
| 37 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 38 |
|
| 39 |
with col2:
|
| 40 |
+
st.subheader("Image Controls")
|
| 41 |
+
x = st.slider("X Coordinate", 0, 500, 250)
|
| 42 |
+
y = st.slider("Y Coordinate", 0, 500, 250)
|
| 43 |
+
zoom = st.slider("Zoom", 1, 10, 5)
|
| 44 |
+
|
| 45 |
+
with st.expander("Enhancement Settings"):
|
| 46 |
+
contrast = st.slider("Contrast", 0.0, 5.0, 1.0)
|
| 47 |
+
brightness = st.slider("Brightness", 0.0, 5.0, 1.0)
|
| 48 |
+
sharpness = st.slider("Sharpness", 0.0, 2.0, 1.0)
|
| 49 |
+
|
| 50 |
+
processed_col, actions_col = st.columns([3, 2])
|
| 51 |
+
|
| 52 |
+
with processed_col:
|
| 53 |
+
img_zoomed = zoom_at(img, x, y, zoom)
|
| 54 |
+
img_contrast = ImageEnhance.Contrast(img_zoomed).enhance(contrast)
|
| 55 |
+
img_bright = ImageEnhance.Brightness(img_contrast).enhance(brightness)
|
| 56 |
+
img_sharp = ImageEnhance.Sharpness(img_bright).enhance(sharpness)
|
| 57 |
+
st.image(img_sharp, caption="Processed Image", use_column_width=True)
|
| 58 |
+
|
| 59 |
+
with actions_col:
|
| 60 |
+
save_image = st.checkbox("Save Image")
|
| 61 |
+
if save_image:
|
| 62 |
+
img_sharp.save("image-processed.jpg")
|
| 63 |
+
st.success("Image saved as `image-processed.jpg`")
|
| 64 |
+
|
| 65 |
+
with st.expander("Save Options"):
|
| 66 |
+
description = st.text_area("Describe the image", "")
|
| 67 |
+
if st.button("Save Description"):
|
| 68 |
+
with open("saved_image_description.txt", "w") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
f.write(description)
|
| 70 |
+
st.success("Description saved as `saved_image_description.txt`")
|
| 71 |
+
|
| 72 |
+
if st.button("Save Image Parameters"):
|
| 73 |
+
params = {
|
| 74 |
+
"coordinates_x": x,
|
| 75 |
+
"coordinates_y": y,
|
| 76 |
+
"zoom": zoom,
|
| 77 |
+
"contrast": contrast,
|
| 78 |
+
"brightness": brightness,
|
| 79 |
+
"sharpness": sharpness
|
| 80 |
+
}
|
| 81 |
+
with open("saved_image_parameters.json", "w") as f:
|
| 82 |
+
f.write(pd.DataFrame([params]).to_json(orient="records"))
|
| 83 |
+
st.success("Image parameters saved as `saved_image_parameters.json`")
|
| 84 |
+
|
| 85 |
+
if st.button("Rename Files"):
|
| 86 |
+
file_ext = str(np.random.randint(100))
|
| 87 |
+
os.rename("image-processed.jpg", f"img_processed{file_ext}.jpg")
|
| 88 |
+
os.rename("saved_image_parameters.json", f"saved_image_parameters{file_ext}.json")
|
| 89 |
+
os.rename("saved_image_description.txt", f"saved_image_description{file_ext}.txt")
|
| 90 |
+
st.success("Files renamed successfully")
|