Spaces:
Sleeping
Sleeping
Fix pixel-to-micron conversion for slice preview - now uses correct physical dimensions
Browse files- EASY_SETUP.md +78 -0
- README.md +16 -1
- setup_and_run.bat +69 -0
- setup_and_run.sh +81 -0
- streamlit_app.py +23 -2
EASY_SETUP.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π¬ Cell Detection Tool - Easy Setup Guide
|
| 2 |
+
|
| 3 |
+
This guide will help you run the Cell Detection Tool on your computer in just a few clicks!
|
| 4 |
+
|
| 5 |
+
## π What You Need
|
| 6 |
+
|
| 7 |
+
1. **A computer** (Windows, Mac, or Linux)
|
| 8 |
+
2. **Internet connection** (for initial setup only)
|
| 9 |
+
3. **Your TIFF microscopy files**
|
| 10 |
+
|
| 11 |
+
## π Quick Start (3 Steps!)
|
| 12 |
+
|
| 13 |
+
### Step 1: Download the Code
|
| 14 |
+
1. Click the green "Code" button on this page
|
| 15 |
+
2. Select "Download ZIP"
|
| 16 |
+
3. Extract the ZIP file to your desktop or Documents folder
|
| 17 |
+
|
| 18 |
+
### Step 2: Run the Setup Script
|
| 19 |
+
|
| 20 |
+
#### On Windows:
|
| 21 |
+
- Double-click `setup_and_run.bat`
|
| 22 |
+
- If Windows asks about running the file, click "Yes" or "Run anyway"
|
| 23 |
+
|
| 24 |
+
#### On Mac:
|
| 25 |
+
- Open Terminal (press Cmd+Space, type "Terminal", press Enter)
|
| 26 |
+
- Type: `cd ` (with a space), then drag the downloaded folder into Terminal
|
| 27 |
+
- Press Enter, then type: `./setup_and_run.sh`
|
| 28 |
+
- Press Enter
|
| 29 |
+
|
| 30 |
+
#### On Linux:
|
| 31 |
+
- Open Terminal
|
| 32 |
+
- Navigate to the downloaded folder: `cd /path/to/cell-detection-tool`
|
| 33 |
+
- Run: `./setup_and_run.sh`
|
| 34 |
+
|
| 35 |
+
### Step 3: Use the App!
|
| 36 |
+
- Your web browser will automatically open to `http://localhost:8501`
|
| 37 |
+
- Upload your TIFF files and start detecting cells!
|
| 38 |
+
- **No file size limits** when running locally! π
|
| 39 |
+
|
| 40 |
+
## π§ Troubleshooting
|
| 41 |
+
|
| 42 |
+
### "Python not found" Error
|
| 43 |
+
**Windows:**
|
| 44 |
+
1. Download Python from https://python.org/downloads/
|
| 45 |
+
2. **IMPORTANT:** Check "Add Python to PATH" during installation
|
| 46 |
+
3. Restart your computer and try again
|
| 47 |
+
|
| 48 |
+
**Mac:**
|
| 49 |
+
1. Install Homebrew: `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`
|
| 50 |
+
2. Install Python: `brew install python3`
|
| 51 |
+
|
| 52 |
+
**Linux (Ubuntu/Debian):**
|
| 53 |
+
```bash
|
| 54 |
+
sudo apt update
|
| 55 |
+
sudo apt install python3 python3-pip python3-venv
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
### Still Having Issues?
|
| 59 |
+
1. Make sure you have an internet connection
|
| 60 |
+
2. Try running the setup script again
|
| 61 |
+
3. On Windows, try "Run as Administrator"
|
| 62 |
+
|
| 63 |
+
## π‘ Features
|
| 64 |
+
|
| 65 |
+
β
**No file size limits** (upload 1GB+ files!)
|
| 66 |
+
β
**Fast processing** (runs on your computer)
|
| 67 |
+
β
**Interactive parameter tuning**
|
| 68 |
+
β
**Slice preview** for quick testing
|
| 69 |
+
β
**Export results** (images + CSV data)
|
| 70 |
+
β
**Works offline** after initial setup
|
| 71 |
+
|
| 72 |
+
## π To Stop the App
|
| 73 |
+
|
| 74 |
+
Press `Ctrl+C` in the terminal/command window where the app is running.
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
**Need help?** The setup script will guide you through any issues and provide helpful error messages!
|
README.md
CHANGED
|
@@ -38,9 +38,24 @@ A Streamlit web application for automated detection and counting of circular cel
|
|
| 38 |
- **Filtering**: Remove false positives based on shape and contrast
|
| 39 |
- **Size constraints**: Set minimum cell diameter in microns
|
| 40 |
|
| 41 |
-
## Local
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
```bash
|
| 44 |
pip install -r requirements.txt
|
| 45 |
streamlit run streamlit_app.py
|
| 46 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
- **Filtering**: Remove false positives based on shape and contrast
|
| 39 |
- **Size constraints**: Set minimum cell diameter in microns
|
| 40 |
|
| 41 |
+
## π Easy Local Setup (Recommended)
|
| 42 |
+
|
| 43 |
+
**For non-technical users:** See [EASY_SETUP.md](EASY_SETUP.md) for one-click installation!
|
| 44 |
+
|
| 45 |
+
**Quick start:**
|
| 46 |
+
- **Windows:** Double-click `setup_and_run.bat`
|
| 47 |
+
- **Mac/Linux:** Run `./setup_and_run.sh` in terminal
|
| 48 |
+
|
| 49 |
+
## π§ Manual Development Setup
|
| 50 |
|
| 51 |
```bash
|
| 52 |
pip install -r requirements.txt
|
| 53 |
streamlit run streamlit_app.py
|
| 54 |
```
|
| 55 |
+
|
| 56 |
+
## β‘ Why Run Locally?
|
| 57 |
+
|
| 58 |
+
- **No file size limits** (upload 1GB+ files)
|
| 59 |
+
- **Faster processing** (uses your computer's resources)
|
| 60 |
+
- **More reliable** (no network timeouts)
|
| 61 |
+
- **Works offline** after initial setup
|
setup_and_run.bat
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
echo ====================================
|
| 3 |
+
echo Cell Detection Tool - Easy Setup
|
| 4 |
+
echo ====================================
|
| 5 |
+
echo.
|
| 6 |
+
|
| 7 |
+
:: Check if Python is installed
|
| 8 |
+
python --version >nul 2>&1
|
| 9 |
+
if %errorlevel% neq 0 (
|
| 10 |
+
echo ERROR: Python is not installed or not in PATH
|
| 11 |
+
echo Please install Python 3.8+ from https://python.org
|
| 12 |
+
echo Make sure to check "Add Python to PATH" during installation
|
| 13 |
+
pause
|
| 14 |
+
exit /b 1
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
echo β
Python found
|
| 18 |
+
echo.
|
| 19 |
+
|
| 20 |
+
:: Check if virtual environment exists
|
| 21 |
+
if not exist "venv" (
|
| 22 |
+
echo π§ Creating virtual environment...
|
| 23 |
+
python -m venv venv
|
| 24 |
+
if %errorlevel% neq 0 (
|
| 25 |
+
echo ERROR: Failed to create virtual environment
|
| 26 |
+
pause
|
| 27 |
+
exit /b 1
|
| 28 |
+
)
|
| 29 |
+
echo β
Virtual environment created
|
| 30 |
+
) else (
|
| 31 |
+
echo β
Virtual environment already exists
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
echo.
|
| 35 |
+
echo π§ Activating virtual environment and installing dependencies...
|
| 36 |
+
|
| 37 |
+
:: Activate virtual environment and install packages
|
| 38 |
+
call venv\Scripts\activate.bat
|
| 39 |
+
if %errorlevel% neq 0 (
|
| 40 |
+
echo ERROR: Failed to activate virtual environment
|
| 41 |
+
pause
|
| 42 |
+
exit /b 1
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
:: Upgrade pip first
|
| 46 |
+
python -m pip install --upgrade pip >nul 2>&1
|
| 47 |
+
|
| 48 |
+
:: Install requirements
|
| 49 |
+
echo Installing required packages... (this may take a few minutes)
|
| 50 |
+
pip install -r requirements.txt
|
| 51 |
+
if %errorlevel% neq 0 (
|
| 52 |
+
echo ERROR: Failed to install requirements
|
| 53 |
+
echo Make sure you have an internet connection
|
| 54 |
+
pause
|
| 55 |
+
exit /b 1
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
echo.
|
| 59 |
+
echo β
All dependencies installed successfully!
|
| 60 |
+
echo.
|
| 61 |
+
echo π Starting Cell Detection Tool...
|
| 62 |
+
echo The app will open in your web browser at http://localhost:8501
|
| 63 |
+
echo.
|
| 64 |
+
echo Press Ctrl+C to stop the application
|
| 65 |
+
echo.
|
| 66 |
+
|
| 67 |
+
:: Run the Streamlit app
|
| 68 |
+
streamlit run streamlit_app.py --server.port=8501
|
| 69 |
+
pause
|
setup_and_run.sh
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
echo "===================================="
|
| 4 |
+
echo "Cell Detection Tool - Easy Setup"
|
| 5 |
+
echo "===================================="
|
| 6 |
+
echo ""
|
| 7 |
+
|
| 8 |
+
# Colors for better output
|
| 9 |
+
RED='\033[0;31m'
|
| 10 |
+
GREEN='\033[0;32m'
|
| 11 |
+
BLUE='\033[0;34m'
|
| 12 |
+
YELLOW='\033[1;33m'
|
| 13 |
+
NC='\033[0m' # No Color
|
| 14 |
+
|
| 15 |
+
# Check if Python is installed
|
| 16 |
+
if ! command -v python3 &> /dev/null; then
|
| 17 |
+
if ! command -v python &> /dev/null; then
|
| 18 |
+
echo -e "${RED}ERROR: Python is not installed${NC}"
|
| 19 |
+
echo "Please install Python 3.8+ from:"
|
| 20 |
+
echo " - macOS: brew install python3 or download from python.org"
|
| 21 |
+
echo " - Ubuntu/Debian: sudo apt update && sudo apt install python3 python3-pip python3-venv"
|
| 22 |
+
echo " - Other Linux: Use your package manager"
|
| 23 |
+
exit 1
|
| 24 |
+
else
|
| 25 |
+
PYTHON_CMD="python"
|
| 26 |
+
fi
|
| 27 |
+
else
|
| 28 |
+
PYTHON_CMD="python3"
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
echo -e "${GREEN}β
Python found${NC}"
|
| 32 |
+
echo ""
|
| 33 |
+
|
| 34 |
+
# Check if virtual environment exists
|
| 35 |
+
if [ ! -d "venv" ]; then
|
| 36 |
+
echo -e "${BLUE}π§ Creating virtual environment...${NC}"
|
| 37 |
+
$PYTHON_CMD -m venv venv
|
| 38 |
+
if [ $? -ne 0 ]; then
|
| 39 |
+
echo -e "${RED}ERROR: Failed to create virtual environment${NC}"
|
| 40 |
+
echo "You may need to install python3-venv:"
|
| 41 |
+
echo " Ubuntu/Debian: sudo apt install python3-venv"
|
| 42 |
+
exit 1
|
| 43 |
+
fi
|
| 44 |
+
echo -e "${GREEN}β
Virtual environment created${NC}"
|
| 45 |
+
else
|
| 46 |
+
echo -e "${GREEN}β
Virtual environment already exists${NC}"
|
| 47 |
+
fi
|
| 48 |
+
|
| 49 |
+
echo ""
|
| 50 |
+
echo -e "${BLUE}π§ Activating virtual environment and installing dependencies...${NC}"
|
| 51 |
+
|
| 52 |
+
# Activate virtual environment
|
| 53 |
+
source venv/bin/activate
|
| 54 |
+
if [ $? -ne 0 ]; then
|
| 55 |
+
echo -e "${RED}ERROR: Failed to activate virtual environment${NC}"
|
| 56 |
+
exit 1
|
| 57 |
+
fi
|
| 58 |
+
|
| 59 |
+
# Upgrade pip first
|
| 60 |
+
pip install --upgrade pip > /dev/null 2>&1
|
| 61 |
+
|
| 62 |
+
# Install requirements
|
| 63 |
+
echo "Installing required packages... (this may take a few minutes)"
|
| 64 |
+
pip install -r requirements.txt
|
| 65 |
+
if [ $? -ne 0 ]; then
|
| 66 |
+
echo -e "${RED}ERROR: Failed to install requirements${NC}"
|
| 67 |
+
echo "Make sure you have an internet connection"
|
| 68 |
+
exit 1
|
| 69 |
+
fi
|
| 70 |
+
|
| 71 |
+
echo ""
|
| 72 |
+
echo -e "${GREEN}β
All dependencies installed successfully!${NC}"
|
| 73 |
+
echo ""
|
| 74 |
+
echo -e "${YELLOW}π Starting Cell Detection Tool...${NC}"
|
| 75 |
+
echo "The app will open in your web browser at http://localhost:8501"
|
| 76 |
+
echo ""
|
| 77 |
+
echo -e "${YELLOW}Press Ctrl+C to stop the application${NC}"
|
| 78 |
+
echo ""
|
| 79 |
+
|
| 80 |
+
# Run the Streamlit app
|
| 81 |
+
streamlit run streamlit_app.py --server.port=8501
|
streamlit_app.py
CHANGED
|
@@ -352,6 +352,9 @@ if uploaded is not None:
|
|
| 352 |
current_path = paths.get(sel or "", None)
|
| 353 |
if current_path:
|
| 354 |
pil_img = Image.open(current_path).convert("L")
|
|
|
|
|
|
|
|
|
|
| 355 |
slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
|
| 356 |
snp = np.array(slice_img)
|
| 357 |
h, w = snp.shape[:2]
|
|
@@ -360,14 +363,32 @@ if uploaded is not None:
|
|
| 360 |
"Selected slice is too small. Increase selection to at least 100Γ100."
|
| 361 |
)
|
| 362 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
if max(h, w) > 1024:
|
| 364 |
scale = max(h, w) / 1024.0
|
| 365 |
new_h, new_w = int(h / scale), int(w / scale)
|
| 366 |
snp = resize(snp, (new_h, new_w), preserve_range=True).astype(
|
| 367 |
np.uint8
|
| 368 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
roi_path = os.path.join(prev_dir, "slice.png")
|
| 370 |
iio.imwrite(roi_path, snp)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
if st.button("Preview on slice"):
|
| 372 |
# Get settings from session state if available, fallback to defaults
|
| 373 |
s = st.session_state.get("_settings", {})
|
|
@@ -397,8 +418,8 @@ if uploaded is not None:
|
|
| 397 |
threshold=0.03,
|
| 398 |
overlap=0.5,
|
| 399 |
downsample=downsample,
|
| 400 |
-
width_um=
|
| 401 |
-
height_um=
|
| 402 |
min_diam_um=min_diam_um,
|
| 403 |
threshold_mode=threshold_mode,
|
| 404 |
thresh_percent=float(thresh_percent),
|
|
|
|
| 352 |
current_path = paths.get(sel or "", None)
|
| 353 |
if current_path:
|
| 354 |
pil_img = Image.open(current_path).convert("L")
|
| 355 |
+
# Get original image dimensions for physical scaling calculation
|
| 356 |
+
orig_h, orig_w = pil_img.size[1], pil_img.size[0] # PIL uses (W, H) format
|
| 357 |
+
|
| 358 |
slice_img = st_cropper(pil_img, aspect_ratio=None, box_color="#00FF00")
|
| 359 |
snp = np.array(slice_img)
|
| 360 |
h, w = snp.shape[:2]
|
|
|
|
| 363 |
"Selected slice is too small. Increase selection to at least 100Γ100."
|
| 364 |
)
|
| 365 |
else:
|
| 366 |
+
# Calculate slice physical dimensions based on the crop ratio
|
| 367 |
+
# Get settings for original physical dimensions
|
| 368 |
+
s = st.session_state.get("_settings", {})
|
| 369 |
+
orig_width_um = s.get("width_um", 1705.6)
|
| 370 |
+
orig_height_um = s.get("height_um", 1706.81)
|
| 371 |
+
|
| 372 |
+
# Calculate slice physical dimensions proportionally
|
| 373 |
+
slice_width_um = orig_width_um * (w / orig_w)
|
| 374 |
+
slice_height_um = orig_height_um * (h / orig_h)
|
| 375 |
+
|
| 376 |
if max(h, w) > 1024:
|
| 377 |
scale = max(h, w) / 1024.0
|
| 378 |
new_h, new_w = int(h / scale), int(w / scale)
|
| 379 |
snp = resize(snp, (new_h, new_w), preserve_range=True).astype(
|
| 380 |
np.uint8
|
| 381 |
)
|
| 382 |
+
# Also scale the physical dimensions
|
| 383 |
+
slice_width_um = slice_width_um * (new_w / w)
|
| 384 |
+
slice_height_um = slice_height_um * (new_h / h)
|
| 385 |
+
|
| 386 |
roi_path = os.path.join(prev_dir, "slice.png")
|
| 387 |
iio.imwrite(roi_path, snp)
|
| 388 |
+
|
| 389 |
+
# Show slice info
|
| 390 |
+
st.caption(f"π Slice: {snp.shape[1]}Γ{snp.shape[0]} px β {slice_width_um:.1f}Γ{slice_height_um:.1f} Β΅m")
|
| 391 |
+
|
| 392 |
if st.button("Preview on slice"):
|
| 393 |
# Get settings from session state if available, fallback to defaults
|
| 394 |
s = st.session_state.get("_settings", {})
|
|
|
|
| 418 |
threshold=0.03,
|
| 419 |
overlap=0.5,
|
| 420 |
downsample=downsample,
|
| 421 |
+
width_um=slice_width_um, # Use calculated slice dimensions
|
| 422 |
+
height_um=slice_height_um, # Use calculated slice dimensions
|
| 423 |
min_diam_um=min_diam_um,
|
| 424 |
threshold_mode=threshold_mode,
|
| 425 |
thresh_percent=float(thresh_percent),
|