import streamlit as st from PIL import Image, ImageEnhance import pandas as pd import numpy as np import io import os from pathlib import Path def zoom_at(img, x, y, zoom): # ...existing code... # App title and description st.set_page_config(page_title="Cell Explorer", layout="wide") st.title("CLL Explorer: Annotation Tool") st.markdown(""" ### About this Application This tool helps researchers analyze microscope images of blood cells for malaria detection: - Upload microscope images - Adjust image view with zoom and enhancement controls - Detect and measure cells automatically - Save analysis results and annotations **Note**: Cell measurements are in micrometers (µm) """) # Create tabs for different functions tab1, tab2, tab3 = st.tabs(["Image Analysis", "Detection Results", "Settings"]) with tab1: col1, col2 = st.columns([2,1]) with col1: 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))) img_data = uploaded_files[img_index].read() img = Image.open(io.BytesIO(img_data)).resize((800, 800)) st.image(img, caption="Original Image", use_column_width=True) with col2: if uploaded_files: st.subheader("Image Controls") x = st.slider("X Position", 0, 800, 400) y = st.slider("Y Position", 0, 800, 400) zoom = st.slider("Zoom Level", 1, 10, 5) with st.expander("Enhancement Settings"): 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) img_zoomed = zoom_at(img, x, y, zoom) img_processed = ImageEnhance.Contrast(img_zoomed).enhance(contrast) img_processed = ImageEnhance.Brightness(img_processed).enhance(brightness) img_processed = ImageEnhance.Sharpness(img_processed).enhance(sharpness) st.image(img_processed, caption="Processed View", use_column_width=True) with tab2: if uploaded_files: st.subheader("Cell Detection Results") # Add your existing cell detection code here col1, col2 = st.columns(2) with col1: description = st.text_area("Analysis Notes", "") with col2: if st.button("Save Analysis"): timestamp = pd.Timestamp.now().strftime("%Y%m%d_%H%M%S") save_dir = Path("analysis_results") / timestamp save_dir.mkdir(parents=True, exist_ok=True) img_processed.save(save_dir / "processed_image.jpg") with open(save_dir / "analysis_notes.txt", "w") as f: f.write(description) st.success(f"Analysis saved to {save_dir}") with tab3: st.subheader("Application Settings") st.checkbox("Enable Auto-Detection", value=True) st.selectbox("Measurement Unit", ["Micrometers", "Pixels"]) st.number_input("Detection Confidence Threshold", 0.0, 1.0, 0.5)