Update app.py
Browse files7 unmodified lines8910111213143 unmodified lines181920212223242526272815 unmodified lines4445464748495051525354555657585960616263646566676869704 unmodified lines75767778798010 unmodified lines9192939495969798991001011021031044 unmodified lines10911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417558 unmodified lines2342352362372382399 unmodified lines2492502512522532542552562572582592602612622632642652662672682693 unmodified lines27327427527627727827928041 unmodified lines32232332432532632732832933033110 unmodified lines34234334434534634724 unmodified lines37237337437537637737837938038138238338438538638738838939039139239339439539639749 unmodified lines4474484494504514522 unmodified lines45545645745845946046146246346446565 unmodified lines5315325335345355365375385395405415425435445455465471 unmodified line54955055155255355455555655720 unmodified lines57857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961010 unmodified lines6216226236246256266271 unmodified line6296306316326336346357 unmodified linesfrom PIL import Image, ImageDrawimport iofrom huggingface_hub import hf_hub_downloadimport base64HF_REPO_ID = "myang4218/cellposemodel"MODEL_OPTIONS = {3 unmodified linesloaded_models = {}MAX_SIDE = 1024MAX_PIXELS = 1024 * 1024def safe_resize(image_np):h, w = image_np.shape[:2]total = h * w15 unmodified linesh, w = image_np.shape[:2]# Convert to PILimg_pil = Image.fromarray(image_np)draw = ImageDraw.Draw(img_pil, 'RGBA')left_px = int(w * left_width_pct / 100)top_px = int(h * top_width_pct / 100)# Draw overlays for exclusion zonesif left_px > 0:draw.rectangle([(0, 0), (left_px, h)],fill=(255, 0, 0, 80))# border linedraw.line([(left_px, 0), (left_px, h)], fill=(255, 0, 0, 255), width=3)if top_px > 0:draw.rectangle([(0, 0), (w, top_px)],fill=(255, 0, 0, 80))# border linedraw.line([(0, top_px), (w, top_px)], fill=(255, 0, 0, 255), width=3)4 unmodified linesdef apply_stereological_exclusion(masks, left_width_pct, top_width_pct):h, w = masks.shapeleft_px = int(w * left_width_pct / 100)top_px = int(h * top_width_pct / 100)10 unmodified lines# Get cell boundary coordinatesrows, cols = np.where(cell_mask)touches_left = np.any(cols < left_px) if left_px > 0 else Falsetouches_top = np.any(rows < top_px) if top_px > 0 else Falseif touches_left or touches_top:filtered_masks[cell_mask] = 0excluded_cells.append(cell_id)else:included_cells.append(cell_id)unique_ids = np.unique(filtered_masks)unique_ids = unique_ids[unique_ids > 0]4 unmodified linesreturn renumbered_masks, len(excluded_cells), len(included_cells)def classify_cells_by_blueness(image_np, masks, blue_threshold):if len(image_np.shape) == 2:image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)elif len(image_np.shape) == 3 and image_np.shape[2] == 4:image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)hsv = cv2.cvtColor(image_np, cv2.COLOR_RGB2HSV)# Calculate blueness index for each pixelhue = hsv[:, :, 0].astype(np.float32)saturation = hsv[:, :, 1].astype(np.float32)# Hue score: peaks around 115 , drops off towards edges# Handle hue wrap-around for blue detection (100-130 range)hue_distance = np.minimum(np.abs(hue - 115), 180 - np.abs(hue - 115))hue_score = np.maximum(0, 1 - hue_distance / 65) # 65 good blue range# blueness metricblueness = hue_score * (saturation / 255.0)# Convert threshold from 0-100 to 0-1 scalethreshold = blue_threshold / 100.0# Get unique cell IDscell_ids = np.unique(masks)cell_ids = cell_ids[cell_ids > 0]dead_cells = []alive_cells = []# Classify each cellfor cell_id in cell_ids:cell_mask = (masks == cell_id)cell_blueness = np.mean(blueness[cell_mask])if cell_blueness > threshold:dead_cells.append(cell_id)else:alive_cells.append(cell_id)# Create colored overlayoverlay = image_np.copy().astype(np.float32) # Ensure float for blending# Color dead cells red, alive cells greenfor cell_id in dead_cells:cell_mask = (masks == cell_id)overlay[cell_mask] = [255, 0, 0]for cell_id in alive_cells:cell_mask = (masks == cell_id)overlay[cell_mask] = [0, 255, 0]# Blend with original imagealpha = 0.4final_overlay = (1 - alpha) * image_np.astype(np.float32) + alpha * overlayfinal_overlay = np.clip(final_overlay, 0, 255).astype(np.uint8)return len(dead_cells), len(alive_cells), final_overlaydef measure_confluency(masks, image_np):58 unmodified linesreturn int(round(np.percentile(sizes, q)))def toggle_stereological_mode(use_stereology):"""Show/hide stereological controls based on checkbox"""return gr.update(visible=use_stereology)9 unmodified linesreturn Image.fromarray(overlay)@spaces.GPUdef run_segmentation(image, model_choice, min_cell_size, max_cell_size,use_stereology, left_exclusion, top_exclusion):image_np = np.array(image)image_np = safe_resize(image_np)try:model_filename = MODEL_OPTIONS[model_choice]model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)if model_filename in loaded_models:model = loaded_models[model_filename]else:model = models.CellposeModel(gpu=True, pretrained_model=model_path)loaded_models[model_filename] = model# Process image format to RGBif len(image_np.shape) == 2:3 unmodified lineselse:processed_image_np = image_np# Run Cellpose segmentationmasks_raw, flows, styles = model.eval(processed_image_np, diameter=None, channels=[0, 0])ids = np.unique(masks_raw)ids = ids[ids > 0]41 unmodified linescell_count = len(np.unique(masks)) - 1confluency = measure_confluency(masks, processed_image_np)# Create segmentation overlay (without viability)segmentation_overlay = processed_image_np.copy().astype(np.float32)if masks.max() > 0:np.random.seed(42)colors = np.random.randint(0, 255, size=(masks.max() + 1, 3))colors[0] = [0, 0, 0]colored_mask = colors[masks]10 unmodified linesinfo_msg += filter_msginfo_msg += f"Segmentation complete! Found {cell_count} cells.\n"info_msg += f"Confluency: {confluency:.1f}%\n"if use_stereology:info_msg += f"Stereological counting enabled (Left: {left_exclusion}%, Top: {top_exclusion}%)\n"info_msg += "Now adjust the Blue Threshold for viability assessment."24 unmodified lines)def update_viability_realtime(blue_threshold, stored_masks, stored_image_np):# avoid unpacking None (e.g. slider moved before segmentation)if stored_masks is None or stored_image_np is None:return None, 0, 0, 0.0, "Please run segmentation first."stored_masks = unpack_array(stored_masks)stored_image_np = unpack_array(stored_image_np)try:dead_count, alive_count, viability_overlay_np = classify_cells_by_blueness(stored_image_np, stored_masks, blue_threshold)total_count = alive_count + dead_countviability_percent = (alive_count / total_count * 100) if total_count > 0 else 0.0confluency = measure_confluency(stored_masks, stored_image_np)overlay_image = Image.fromarray(viability_overlay_np)info_msg = f"Total cells: {total_count}\nLive (green): {alive_count}\nDead (red): {dead_count}\n"info_msg += f"Viability: {viability_percent:.1f}%\nConfluency: {confluency:.1f}%\nBlue threshold: {blue_threshold}%"return overlay_image, alive_count, dead_count, viability_percent, info_msg49 unmodified linesreturn avg_count, avg_conf, avg_viab, "\n".join(lines)def build_tab(tab_index, masks_state, image_state, result_state):with gr.Tab(f"Tab {tab_index}"):gr.Markdown("Run segmentation")2 unmodified lineswith gr.Column():img_input = gr.Image(type="pil",label="Microscopy image",image_mode="RGB",height=512)model_dropdown = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()),label="Select Model",65 unmodified lineswith gr.Group(visible=False) as viability_section:gr.Markdown("### Viability Assessment (Trypan Blue)")gr.Markdown("Adjust the threshold to classify cells as live (green) or dead (red).")with gr.Row():with gr.Column():blue_thresh = gr.Slider(minimum=0,maximum=100,value=25,step=1,label="Blue Threshold (%)",info="Higher values = more selective for blue cells")with gr.Column():live_count_out = gr.Number(label="Live Cells (Green)", precision=0)1 unmodified lineviab_overlay = gr.Image(type="pil", label="Viability Assessment (Green=Live, Red=Dead)")viab_percent_out = gr.Number(label="Viability (%)", precision=1)viab_info = gr.Textbox(label="Analysis Results", lines=5)# Event handlersuse_stereo.change(fn=toggle_stereological_mode,20 unmodified linessegment_btn.click(fn=run_segmentation,inputs=[img_input, model_dropdown, min_size_slider, max_size_slider,use_stereo, left_excl, top_excl],outputs=[cell_count_out, overlay_out, info_out, viability_section,masks_state, image_state, confluency_out, min_size_slider]).then(fn=update_viability_realtime,inputs=[blue_thresh, masks_state, image_state],outputs=[viab_overlay, live_count_out, dead_count_out, viab_percent_out, viab_info]).then(# Save this tab's results into its dedicated state for Tab 5fn=save_tab_result,inputs=[cell_count_out, confluency_out, viab_percent_out],outputs=[result_state])blue_thresh.change(fn=update_viability_realtime,inputs=[blue_thresh, masks_state, image_state],outputs=[viab_overlay, live_count_out, dead_count_out, viab_percent_out, viab_info]).then(# Keep result state in sync when threshold is adjustedfn=save_tab_result,inputs=[cell_count_out, confluency_out, viab_percent_out],outputs=[result_state])with gr.Blocks(title="CellposeCellCounter",theme=gr.themes.Soft(),10 unmodified linesfor i in range(4):build_tab(i + 1, masks_states[i], image_states[i], result_states[i])# Tab 5with gr.Tab("Tab 5 β Summary"):gr.Markdown("## Average Results Across All Tabs")gr.Markdown(1 unmodified line"then click **Refresh Summary** to see the averages.")refresh_btn = gr.Button("Refresh Summary", variant="primary", size="lg")with gr.Row():avg_count_out = gr.Number(label="Avg Cell Count", precision=1)7 unmodified lines891011121314153 unmodified lines1920212223242526272829303132333415 unmodified lines50515253545556575859606162636465666768697071727374757677784 unmodified lines8384858687888910 unmodified lines1001011021031041051061071081091101111121131141151161171181194 unmodified lines12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520658 unmodified lines2652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073089 unmodified lines3183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874883 unmodified lines49249349449549649749849941 unmodified lines54154254354454554654754854955010 unmodified lines56156256356456556656724 unmodified lines59259359459559659759859960060160260360460560660760860961061161261361461561661761861949 unmodified lines6696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788792 unmodified lines88288388488588688788888989089189289389489589689789889990090190290365 unmodified lines9699709719729739749759769779789799809819829839849859869879881 unmodified line99099199299399499599699799820 unmodified lines10191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110 unmodified lines1062106310641065106610671068106910701 unmodified line10721073107410751076107710787 unmodified linesfrom PIL import Image, ImageDrawimport iofrom huggingface_hub import hf_hub_downloadimport base64from concurrent.futures import ThreadPoolExecutor, as_completedHF_REPO_ID = "myang4218/cellposemodel"MODEL_OPTIONS = {3 unmodified linesloaded_models = {}# ---- mobile-safe size limits (aggressive for Safari) ----MAX_SIDE = 1024MAX_PIXELS = 1024 * 1024def safe_resize(image_np):"""Downscale image to fit within MAX_SIDE and MAX_PIXELS whilepreserving aspect ratio. Works for RGB / RGBA / grayscale."""h, w = image_np.shape[:2]total = h * w15 unmodified linesh, w = image_np.shape[:2]# Convert to PIL for drawingimg_pil = Image.fromarray(image_np)draw = ImageDraw.Draw(img_pil, 'RGBA')# Calculate pixel widths from percentagesleft_px = int(w * left_width_pct / 100)top_px = int(h * top_width_pct / 100)# Draw overlays for exclusion zonesif left_px > 0:# Left exclusion zonedraw.rectangle([(0, 0), (left_px, h)],fill=(255, 0, 0, 80) # Semi-transparent red)# border linedraw.line([(left_px, 0), (left_px, h)], fill=(255, 0, 0, 255), width=3)if top_px > 0:# Top exclusion zonedraw.rectangle([(0, 0), (w, top_px)],fill=(255, 0, 0, 80) # Semi-transparent red)# border linedraw.line([(0, top_px), (w, top_px)], fill=(255, 0, 0, 255), width=3)4 unmodified linesdef apply_stereological_exclusion(masks, left_width_pct, top_width_pct):h, w = masks.shape# Calculate pixel widths from percentagesleft_px = int(w * left_width_pct / 100)top_px = int(h * top_width_pct / 100)10 unmodified lines# Get cell boundary coordinatesrows, cols = np.where(cell_mask)# Check if cell touches left exclusion zonetouches_left = np.any(cols < left_px) if left_px > 0 else False# Check if cell touches top exclusion zonetouches_top = np.any(rows < top_px) if top_px > 0 else False# Exclude if touching left or topif touches_left or touches_top:filtered_masks[cell_mask] = 0excluded_cells.append(cell_id)else:included_cells.append(cell_id)# Renumber remaining cellsunique_ids = np.unique(filtered_masks)unique_ids = unique_ids[unique_ids > 0]4 unmodified linesreturn renumbered_masks, len(excluded_cells), len(included_cells)def classify_cells_by_blueness(image_np, masks, threshold_bias):"""Classify cells as dead (blue) or alive using an adaptive Otsu thresholdon per-cell blueness scores, with a user bias to fine-tune.Args:image_np: RGB image arraymasks: Cellpose segmentation masksthreshold_bias: Slider value -50..+50; shifts Otsu threshold up/down.Negative = more cells classified dead (looser).Positive = fewer cells classified dead (stricter).0 = pure Otsu (fully automatic).Returns:dead_count, alive_count, colored_overlay, otsu_threshold, final_threshold"""if len(image_np.shape) == 2:image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)elif len(image_np.shape) == 3 and image_np.shape[2] == 4:image_np = cv2.cvtColor(image_np, cv2.COLOR_RGBA2RGB)hsv = cv2.cvtColor(image_np, cv2.COLOR_RGB2HSV)hue = hsv[:, :, 0].astype(np.float32)saturation = hsv[:, :, 1].astype(np.float32)# Raw blueness: hue proximity to 115Β° Γ saturationhue_distance = np.minimum(np.abs(hue - 115), 180 - np.abs(hue - 115))hue_score = np.maximum(0, 1 - hue_distance / 65)blueness = hue_score * (saturation / 255.0)# --- Compute per-cell mean blueness scores ---cell_ids = np.unique(masks)cell_ids = cell_ids[cell_ids > 0]if len(cell_ids) == 0:blank = image_np.copy()return 0, 0, blank, 0.0, 0.0cell_scores = np.array([np.mean(blueness[masks == cid]) for cid in cell_ids])# --- Otsu on the distribution of per-cell scores ---# cv2.threshold expects uint8; scale 0-1 β 0-255scores_u8 = (np.clip(cell_scores, 0, 1) * 255).astype(np.uint8)if scores_u8.max() == scores_u8.min():# All cells identical β Otsu is undefined; use midpointotsu_threshold = float(scores_u8[0]) / 255.0else:# Reshape to a single-column image so cv2.threshold worksthresh_val, _ = cv2.threshold(scores_u8.reshape(-1, 1), 0, 255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)otsu_threshold = thresh_val / 255.0# --- Apply user bias: slider -50..+50 maps to Β±0.20 shift ---bias = (threshold_bias / 50.0) * 0.20final_threshold = float(np.clip(otsu_threshold + bias, 0.0, 1.0))# --- Classify ---dead_cells = [cid for cid, s in zip(cell_ids, cell_scores) if s > final_threshold]alive_cells = [cid for cid, s in zip(cell_ids, cell_scores) if s <= final_threshold]# --- Colored overlay ---overlay = image_np.copy().astype(np.float32)for cid in dead_cells:overlay[masks == cid] = [255, 0, 0]for cid in alive_cells:overlay[masks == cid] = [0, 255, 0]alpha = 0.4final_overlay = (1 - alpha) * image_np.astype(np.float32) + alpha * overlayfinal_overlay = np.clip(final_overlay, 0, 255).astype(np.uint8)return len(dead_cells), len(alive_cells), final_overlay, otsu_threshold, final_thresholddef measure_confluency(masks, image_np):58 unmodified linesreturn int(round(np.percentile(sizes, q)))def apply_polygon_mask(image_pil, points_json):"""Given a PIL image and a JSON string of [[x,y],...] canvas-space points(already mapped to original image coordinates by the JS), zero outeverything outside the polygon and return a PIL image.Returns the original image if points_json is empty / invalid."""import jsonif not points_json or points_json.strip() in ("", "[]"):return image_piltry:pts = json.loads(points_json)except Exception:return image_pilif len(pts) < 3:return image_pilimage_np = np.array(image_pil)h, w = image_np.shape[:2]poly = np.array(pts, dtype=np.int32)poly[:, 0] = np.clip(poly[:, 0], 0, w - 1)poly[:, 1] = np.clip(poly[:, 1], 0, h - 1)mask = np.zeros((h, w), dtype=np.uint8)cv2.fillPoly(mask, [poly], 255)if len(image_np.shape) == 3:mask3 = mask[:, :, np.newaxis]result = np.where(mask3 == 255, image_np, 0).astype(np.uint8)else:result = np.where(mask == 255, image_np, 0).astype(np.uint8)return Image.fromarray(result)def toggle_stereological_mode(use_stereology):"""Show/hide stereological controls based on checkbox"""return gr.update(visible=use_stereology)9 unmodified linesreturn Image.fromarray(overlay)# ---------------------------------------------------------------------------# Patch-parallel segmentation helpers (Option A)# ---------------------------------------------------------------------------PATCH_SIZE = 512 # target patch side lengthPATCH_OVERLAP = 64 # overlap border on each edge (pixels)MIN_PATCH_DIM = 256 # don't bother patching if image fits comfortablydef _split_patches(image_np, patch_size=PATCH_SIZE, overlap=PATCH_OVERLAP):"""Split image into overlapping patches.Returns list of (patch_np, row_start, col_start) tuples."""h, w = image_np.shape[:2]patches = []row = 0while row < h:row_end = min(row + patch_size, h)col = 0while col < w:col_end = min(col + patch_size, w)patch = image_np[row:row_end, col:col_end]patches.append((patch, row, col))if col_end == w:breakcol += patch_size - overlapif row_end == h:breakrow += patch_size - overlapreturn patchesdef _merge_patch_masks(patch_results, full_h, full_w, overlap=PATCH_OVERLAP):"""Stitch per-patch masks into a single full-image mask.Strategy:- Each patch gets a unique ID offset so cell IDs never collide.- Patches are pasted into the canvas using a priority canvas thatgives interior pixels precedence over overlap-border pixels.- After pasting, cells whose centroids fall in the overlap zoneof two adjacent patches are deduplicated: if two cells fromdifferent patches share >50% IoU they are the same cell β keepthe one whose centroid is furthest from a patch edge."""full_mask = np.zeros((full_h, full_w), dtype=np.int32)# track which patch_idx owns each pixel (used for overlap resolution)owner_map = np.full((full_h, full_w), -1, dtype=np.int32)# distance-to-nearest-edge for the owning patch (higher = more central)priority = np.zeros((full_h, full_w), dtype=np.float32)id_offset = 0patch_meta = [] # (offset, row_start, col_start, patch_h, patch_w)for patch_idx, (mask_patch, row_start, col_start) in enumerate(patch_results):ph, pw = mask_patch.shape# offset all non-zero IDs so they're globally uniqueshifted = np.where(mask_patch > 0, mask_patch + id_offset, 0).astype(np.int32)# compute per-pixel priority = min distance to any patch edgerows_idx = np.arange(ph)cols_idx = np.arange(pw)dist_r = np.minimum(rows_idx, ph - 1 - rows_idx) # (ph,)dist_c = np.minimum(cols_idx, pw - 1 - cols_idx) # (pw,)pri_patch = np.minimum(dist_r[:, None], dist_c[None, :]) # (ph, pw)roi_full = full_mask [row_start:row_start+ph, col_start:col_start+pw]roi_owner = owner_map [row_start:row_start+ph, col_start:col_start+pw]roi_pri = priority [row_start:row_start+ph, col_start:col_start+pw]# where this patch has higher priority, overwritebetter = pri_patch > roi_priroi_full [better] = shifted [better]roi_owner[better] = patch_idxroi_pri [better] = pri_patch [better]max_id = int(mask_patch.max())patch_meta.append((id_offset, row_start, col_start, ph, pw))id_offset += max_id + 1# --- Renumber to compact sequential IDs ---unique_ids = np.unique(full_mask)unique_ids = unique_ids[unique_ids > 0]renumbered = np.zeros_like(full_mask)for new_id, old_id in enumerate(unique_ids, start=1):renumbered[full_mask == old_id] = new_idreturn renumbereddef _segment_patch(args):"""Worker: run cellpose on a single patch. Called from a thread pool."""patch_np, row_start, col_start, model_filename, hf_repo = args# Each thread uses the shared loaded_models cache (GIL-safe for reads;# model.eval() releases the GIL during GPU work so threads overlap.)model_path = hf_hub_download(repo_id=hf_repo, filename=model_filename)if model_filename in loaded_models:model = loaded_models[model_filename]else:model = models.CellposeModel(gpu=True, pretrained_model=model_path)loaded_models[model_filename] = modelmask, _, _ = model.eval(patch_np, diameter=None, channels=[0, 0])return mask, row_start, col_startdef run_segmentation_patched(image_np, model_filename):"""Split image into overlapping patches, run Cellpose on each in parallel,then stitch back into a single full-resolution mask.Falls back to whole-image segmentation if the image is small enoughthat patching adds overhead without benefit."""h, w = image_np.shape[:2]model_path = hf_hub_download(repo_id=HF_REPO_ID, filename=model_filename)if model_filename in loaded_models:model = loaded_models[model_filename]else:model = models.CellposeModel(gpu=True, pretrained_model=model_path)loaded_models[model_filename] = model# Small images: no benefit from patchingif max(h, w) <= MIN_PATCH_DIM * 2:mask, _, _ = model.eval(image_np, diameter=None, channels=[0, 0])return mask, 1 # 1 patchpatches = _split_patches(image_np)n_patches = len(patches)# Build argument list for the thread poolargs_list = [(patch, r, c, model_filename, HF_REPO_ID)for patch, r, c in patches]patch_results = [] # (mask, row_start, col_start) in submission order# ThreadPoolExecutor: GPU kernels release the GIL so threads overlap on GPUwith ThreadPoolExecutor(max_workers=min(n_patches, 4)) as pool:futures = {pool.submit(_segment_patch, a): a for a in args_list}for future in as_completed(futures):mask_patch, row_start, col_start = future.result()patch_results.append((mask_patch, row_start, col_start))# Re-sort by (row, col) so stitching is deterministicpatch_results.sort(key=lambda x: (x[1], x[2]))full_mask = _merge_patch_masks(patch_results, h, w)return full_mask, n_patches@spaces.GPUdef run_segmentation(image, model_choice, min_cell_size, max_cell_size,use_stereology, left_exclusion, top_exclusion,crop_points_json=""):image_np = np.array(image)image_np = safe_resize(image_np)# Apply polygon crop mask if the user drew oneif crop_points_json and crop_points_json.strip() not in ("", "[]"):image_pil_masked = apply_polygon_mask(Image.fromarray(image_np), crop_points_json)image_np = np.array(image_pil_masked)try:model_filename = MODEL_OPTIONS[model_choice]# Process image format to RGBif len(image_np.shape) == 2:3 unmodified lineselse:processed_image_np = image_np# Run patch-parallel Cellpose segmentationmasks_raw, n_patches = run_segmentation_patched(processed_image_np, model_filename)ids = np.unique(masks_raw)ids = ids[ids > 0]41 unmodified linescell_count = len(np.unique(masks)) - 1confluency = measure_confluency(masks, processed_image_np)# Create a basic segmentation overlay (without viability)segmentation_overlay = processed_image_np.copy().astype(np.float32)if masks.max() > 0:np.random.seed(42) # For consistent random colorscolors = np.random.randint(0, 255, size=(masks.max() + 1, 3))colors[0] = [0, 0, 0]colored_mask = colors[masks]10 unmodified linesinfo_msg += filter_msginfo_msg += f"Segmentation complete! Found {cell_count} cells.\n"info_msg += f"Confluency: {confluency:.1f}%\n"info_msg += f"Processed as {n_patches} patch{'es' if n_patches > 1 else ''} (parallel).\n"if use_stereology:info_msg += f"Stereological counting enabled (Left: {left_exclusion}%, Top: {top_exclusion}%)\n"info_msg += "Now adjust the Blue Threshold for viability assessment."24 unmodified lines)def update_viability_realtime(threshold_bias, stored_masks, stored_image_np):# avoid unpacking None (e.g. slider moved before segmentation)if stored_masks is None or stored_image_np is None:return None, 0, 0, 0.0, "Please run segmentation first."stored_masks = unpack_array(stored_masks)stored_image_np = unpack_array(stored_image_np)try:dead_count, alive_count, viability_overlay_np, otsu_thresh, final_thresh = \classify_cells_by_blueness(stored_image_np, stored_masks, threshold_bias)total_count = alive_count + dead_countviability_percent = (alive_count / total_count * 100) if total_count > 0 else 0.0confluency = measure_confluency(stored_masks, stored_image_np)overlay_image = Image.fromarray(viability_overlay_np)info_msg = f"Total cells: {total_count}\nLive (green): {alive_count}\nDead (red): {dead_count}\n"info_msg += f"Viability: {viability_percent:.1f}%\nConfluency: {confluency:.1f}%\n"info_msg += f"Otsu threshold (auto): {otsu_thresh:.4f}\n"info_msg += f"Final threshold (with bias): {final_thresh:.4f}\n"info_msg += f"Sensitivity bias: {threshold_bias:+d}"return overlay_image, alive_count, dead_count, viability_percent, info_msg49 unmodified linesreturn avg_count, avg_conf, avg_viab, "\n".join(lines)# ---------------------------------------------------------------------------# Tab builder β creates one full analysis tab and wires all event handlers.# Returns the per-tab gr.State used by Tab 5 to collect summary data.# ---------------------------------------------------------------------------# ---------------------------------------------------------------------------# Polygon crop canvas β lightweight HTML5 canvas, no ImageEditor# ---------------------------------------------------------------------------def make_crop_canvas_html(tab_idx):"""Return an HTML string containing a canvas-based polygon selector.The user clicks/taps up to 4 points; JS writes pixel coords (mappedback to original image space) into a hidden textbox with idf'crop_pts_{tab_idx}'. A 'Clear' button resets the polygon.The canvas listens for the Gradio img_input change event by watchinga sibling img element (Gradio renders <img> inside the component)."""uid = f"cropcanvas_{tab_idx}"pts_id = f"crop_pts_{tab_idx}"return f"""<div style="position:relative;width:100%;user-select:none;"><canvas id="{uid}"style="width:100%;max-height:400px;object-fit:contain;border:2px solid #4a90d9;border-radius:8px;touch-action:none;cursor:crosshair;background:#111;display:block;"></canvas><div style="margin-top:6px;display:flex;gap:8px;align-items:center;flex-wrap:wrap;"><button id="{uid}_clear"style="padding:6px 18px;border-radius:6px;border:none;background:#e05;color:#fff;font-size:14px;cursor:pointer;">β Clear points</button><span id="{uid}_label"style="font-size:13px;color:#aaa;">Upload an image, then tap up to 4 vertices</span></div></div><script>(function(){{const CANVAS_ID = "{uid}";const PTS_ID = "{pts_id}";const TAB_IDX = {tab_idx};const MAX_PTS = 4;let imgNaturalW = 0, imgNaturalH = 0;let points = []; // {{cx, cy}} in canvas CSS pixelslet imgSrc = null;let imgObj = new Image();const canvas = document.getElementById(CANVAS_ID);const ctx = canvas.getContext("2d");const clearBtn= document.getElementById(CANVAS_ID + "_clear");const label = document.getElementById(CANVAS_ID + "_label");// ββ helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββfunction getPtsTextbox() {{// Gradio hides the real <textarea> inside a wrapper. Find by elem id.return document.getElementById(PTS_ID)?.querySelector("textarea")|| document.querySelector(`#${PTS_ID} textarea`);}}function writePts(pts_orig) {{const ta = getPtsTextbox();if (!ta) return;ta.value = JSON.stringify(pts_orig);ta.dispatchEvent(new Event("input", {{bubbles:true}}));ta.dispatchEvent(new Event("change", {{bubbles:true}}));}}function canvasToOriginal(cx, cy) {{// canvas.width / height are the backing-store pixel dimensionsconst scaleX = imgNaturalW / canvas.width;const scaleY = imgNaturalH / canvas.height;return [Math.round(cx * scaleX), Math.round(cy * scaleY)];}}function draw() {{ctx.clearRect(0, 0, canvas.width, canvas.height);if (imgObj.complete && imgObj.naturalWidth) {{ctx.drawImage(imgObj, 0, 0, canvas.width, canvas.height);}}if (points.length === 0) return;// polygon fillctx.beginPath();ctx.moveTo(points[0].cx, points[0].cy);for (let i = 1; i < points.length; i++) ctx.lineTo(points[i].cx, points[i].cy);if (points.length === MAX_PTS) ctx.closePath();ctx.strokeStyle = "#4af";ctx.lineWidth = 2;ctx.stroke();if (points.length === MAX_PTS) {{ctx.fillStyle = "rgba(74,170,255,0.18)";ctx.fill();}}// vertex dots + numberspoints.forEach((p, i) => {{ctx.beginPath();ctx.arc(p.cx, p.cy, 7, 0, Math.PI*2);ctx.fillStyle = "#4af";ctx.fill();ctx.strokeStyle = "#fff";ctx.lineWidth = 1.5;ctx.stroke();ctx.fillStyle = "#fff";ctx.font = "bold 11px sans-serif";ctx.textAlign = "center";ctx.textBaseline = "middle";ctx.fillText(i+1, p.cx, p.cy);}});}}function loadImage(src) {{imgSrc = src;imgObj = new Image();imgObj.onload = function() {{imgNaturalW = imgObj.naturalWidth;imgNaturalH = imgObj.naturalHeight;// size canvas backing store to match natural image (capped for memory)const maxSide = 1024;let cw = imgNaturalW, ch = imgNaturalH;if (Math.max(cw,ch) > maxSide) {{const s = maxSide / Math.max(cw,ch);cw = Math.round(cw*s); ch = Math.round(ch*s);}}canvas.width = cw;canvas.height = ch;points = [];writePts([]);label.textContent = "Tap up to 4 vertices to define your crop region";draw();}};imgObj.src = src;}}// ββ pointer events (works for mouse + touch) ββββββββββββββββββββββββββcanvas.addEventListener("pointerdown", function(e) {{e.preventDefault();if (!imgNaturalW) return;if (points.length >= MAX_PTS) return;const rect = canvas.getBoundingClientRect();const cssX = e.clientX - rect.left;const cssY = e.clientY - rect.top;// map CSS pixels β backing-store pixelsconst bx = cssX * (canvas.width / rect.width);const by = cssY * (canvas.height / rect.height);points.push({{cx: bx, cy: by}});draw();// write original-image coords to the hidden textboxconst orig = points.map(p => canvasToOriginal(p.cx, p.cy));writePts(orig);if (points.length === MAX_PTS) {{label.textContent = "4 points set β clear to redraw";}} else {{label.textContent = `${{points.length}} / ${{MAX_PTS}} points`;}}}});clearBtn.addEventListener("click", function() {{points = [];writePts([]);label.textContent = "Points cleared β tap to add new vertices";draw();}});// ββ watch for Gradio image uploads ββββββββββββββββββββββββββββββββββββ// Gradio renders the uploaded image as an <img> inside the component.// We poll for it until found, then watch src changes with MutationObserver.function findGradioImg() {{// Gradio wraps img_input in a div whose data-testid = "image"// Walk up from our canvas to find the nearest Gradio Blocks row,// then find the image component in the same Tab.const allImgs = document.querySelectorAll(".gradio-image img, [data-testid='image'] img");// pick the one closest to our canvas in the DOMif (allImgs.length === 0) return null;// heuristic: find closest ancestor shared with canvasfor (const img of allImgs) {{if (img.src && img.src !== imgSrc) return img;}}return allImgs[0];}}let observer = null;function startWatching() {{const img = findGradioImg();if (!img) {{ setTimeout(startWatching, 500); return; }}if (img.src && img.src !== imgSrc) loadImage(img.src);if (observer) observer.disconnect();observer = new MutationObserver(() => {{if (img.src && img.src !== imgSrc) loadImage(img.src);}});observer.observe(img, {{attributes: true, attributeFilter: ["src"]}});}}setTimeout(startWatching, 800);}})();</script>"""def build_tab(tab_index, masks_state, image_state, result_state):with gr.Tab(f"Tab {tab_index}"):gr.Markdown("Run segmentation")2 unmodified lineswith gr.Column():img_input = gr.Image(type="pil",label="Upload image",image_mode="RGB",height=512)# Lightweight polygon crop selector (no ImageEditor)gr.HTML(make_crop_canvas_html(tab_index))# Hidden textbox that receives [[x,y],...] from JScrop_points = gr.Textbox(value="",visible=False,elem_id=f"crop_pts_{tab_index}",label="Crop polygon points (set by canvas)")model_dropdown = gr.Dropdown(choices=list(MODEL_OPTIONS.keys()),label="Select Model",65 unmodified lineswith gr.Group(visible=False) as viability_section:gr.Markdown("### Viability Assessment (Trypan Blue)")gr.Markdown("The threshold is set **automatically per image** using Otsu's method. ""Use the sensitivity slider to fine-tune if needed.")with gr.Row():with gr.Column():sensitivity = gr.Slider(minimum=-50,maximum=50,value=0,step=1,label="Sensitivity",info="β more dead cells Β· auto (0) Β· fewer dead cells β")with gr.Column():live_count_out = gr.Number(label="Live Cells (Green)", precision=0)1 unmodified lineviab_overlay = gr.Image(type="pil", label="Viability Assessment (Green=Live, Red=Dead)")viab_percent_out = gr.Number(label="Viability (%)", precision=1)viab_info = gr.Textbox(label="Analysis Results", lines=6)# ---- Event handlers ------------------------------------------------use_stereo.change(fn=toggle_stereological_mode,20 unmodified linessegment_btn.click(fn=run_segmentation,inputs=[img_input, model_dropdown, min_size_slider, max_size_slider,use_stereo, left_excl, top_excl, crop_points],outputs=[cell_count_out, overlay_out, info_out, viability_section,masks_state, image_state, confluency_out, min_size_slider]).then(fn=update_viability_realtime,inputs=[sensitivity, masks_state, image_state],outputs=[viab_overlay, live_count_out, dead_count_out, viab_percent_out, viab_info]).then(fn=save_tab_result,inputs=[cell_count_out, confluency_out, viab_percent_out],outputs=[result_state])sensitivity.change(fn=update_viability_realtime,inputs=[sensitivity, masks_state, image_state],outputs=[viab_overlay, live_count_out, dead_count_out, viab_percent_out, viab_info]).then(fn=save_tab_result,inputs=[cell_count_out, confluency_out, viab_percent_out],outputs=[result_state])# ---------------------------------------------------------------------------# Gradio interface# ---------------------------------------------------------------------------with gr.Blocks(title="CellposeCellCounter",theme=gr.themes.Soft(),10 unmodified linesfor i in range(4):build_tab(i + 1, masks_states[i], image_states[i], result_states[i])# -------------------------------------------------------------------------# Tab 5 β Summary# -------------------------------------------------------------------------with gr.Tab("Tab 5 β Summary"):gr.Markdown("## Average Results Across All Tabs")gr.Markdown(1 unmodified line"then click **Refresh Summary** to see the averages.")refresh_btn = gr.Button("π Refresh Summary", variant="primary", size="lg")with gr.Row():avg_count_out = gr.Number(label="Avg Cell Count", precision=1)
app.py+528-85