from fastapi import FastAPI, UploadFile, File, HTTPException, Body, Form from PIL import Image from io import BytesIO import numpy as np import torch from skimage import transform import torch.nn as nn from torch.nn import functional as F import argparse import random import os from segment_anything import sam_model_registry from pydantic import BaseModel import cv2 from transformers import SamModel, SamProcessor join = os.path.join # os.environ['HF_HOME'] = '/.cache/huggingface/hub' # os.environ['TRANSFORMERS_CACHE'] = 'imgs' os.environ['HF_HOME'] = 'E:\\serverstuff\\cache\\huggingface\\hub' os.environ['TRANSFORMERS_CACHE'] = 'E:\\serverstuff\\cache\\images' os.environ["CUDA_VISIBLE_DEVICES"] = "" app = FastAPI() CHECKPOINT_PATH = 'ckpt/medsam_vit_b.pth' device = "cpu" def load_medsam_model(checkpoint_path=CHECKPOINT_PATH, device="cpu"): try: model_weights = torch.load(checkpoint_path, map_location=torch.device("cpu")) model = sam_model_registry["vit_b"]() for weight_name, weight_tensor in model_weights.items(): try: setattr(model, weight_name, weight_tensor) except AttributeError: print(f"Warning: Could not assign weight {weight_name}") model.eval() return model except FileNotFoundError: print("Error: Checkpoint file not found. Please verify the file path.") return None except Exception as e: print(f"Error loading model: {e}") return None # Example usage model = load_medsam_model() print("oke model") def medsam_inference(medsam_model, img_embed, box_1024, H, W): print("Chay do day kh a?") print("i, b, h, w", img_embed, box_1024, H, W) box_torch = torch.as_tensor(box_1024, dtype=torch.float, device=img_embed.device) if len(box_torch.shape) == 2: box_torch = box_torch[:, None, :] # (B, 1, 4) # Extract features for prompt and image regions sparse_embeddings, dense_embeddings = medsam_model.prompt_encoder( points=None, boxes=box_torch, masks=None, ) low_res_logits, _ = medsam_model.mask_decoder( image_embeddings=img_embed, # (B, 256, 64, 64) image_pe=medsam_model.prompt_encoder.get_dense_pe(), # (1, 256, 64, 64) sparse_prompt_embeddings=sparse_embeddings, # (B, 2, 256) dense_prompt_embeddings=dense_embeddings, # (B, 256, 64, 64) multimask_output=False, ) # Generate low-resolution segmentation prediction low_res_pred = torch.sigmoid(low_res_logits) # (1, 1, 256, 256) print("sigmoid - low_res_pred", low_res_pred) low_res_pred = F.interpolate( low_res_pred, size=(H, W), mode="bilinear", align_corners=False, ) # (1, 1, gt.shape) print("interpolate - low_res_pred", low_res_pred) low_res_pred = low_res_pred.squeeze() print("squeeze - ", low_res_pred) with torch.no_grad(): low_res_pred = low_res_pred.cpu().numpy() # Move to CPU and convert to NumPy print("numpy - ", low_res_pred) medsam_seg = (low_res_pred > 0.5).astype(np.uint8) # Adjust threshold as needed return medsam_seg async def process_image(file): contents = await file.read() img = Image.open(BytesIO(contents)) original_w, original_h = img.size print("original_h, original_w", (original_h, original_w)) img_np = np.array(img) print("img_np", img_np) print("img_np.shape", img_np.shape) if len(img_np.shape) == 2: img_3c = np.repeat(img_np[:, :, None], 3, axis=-1) elif img_np.shape[-1] == 4: # Convert RGBA image to RGB by discarding the alpha channel img_3c = img_np[..., :3] else: img_3c = img_np H, W, _ = img_3c.shape print("img_np.shape:", (H, W)) # Image preprocessing img_1024 = transform.resize( img_3c, (1024, 1024), order=3, preserve_range=True, anti_aliasing=True ).astype(np.uint8) print("img_1024 let me see", img_1024) #img_1024a = img_1024 / 255.0 # Normalize to [0, 1] img_1024 = img_1024 - img_1024.min() img_1024 = img_1024 / np.clip(img_1024.max() - img_1024.min(), a_min=1e-8, a_max=None) # Convert the shape to (3, H, W) img_1024_tensor = ( torch.tensor(img_1024).float().permute(2, 0, 1).unsqueeze(0).to("cpu") ) print("img_1024_tensor", img_1024_tensor) return img_1024_tensor, H, W def process_string(string): number_strings = string.split(",") numbers_list = map(int, number_strings) print(list(numbers_list)) def process_boundingbox(boundingbox_string, original_shape): H, W = original_shape number_strings = boundingbox_string.split(",") if len(number_strings) != 4: raise ValueError("Invalid bounding box format (expected 4 comma-separated values)") try: box_np = np.array([float(num) for num in number_strings]) print("box_np", box_np) except ValueError: raise ValueError("Invalid bounding box format (expected numerical values)") box_1024 = box_np / np.array([W, H, W, H]) * 1024 box_1024 = [[*box_1024]] return box_1024 def perform_segmentation(image_tensor, H, W, medsam_model, box, seg_path="assets/"): print("Performing segmentation") print("img, h, w", image_tensor, H, W) print("box", box) with torch.no_grad(): image_embedding = medsam_model.image_encoder(image_tensor) # (1, 256, 64, 64) print("img_emb", image_embedding) medsam_seg = medsam_inference(medsam_model, image_embedding, box, H, W) print("medsam_seg", medsam_seg) cv2.imwrite("brain_test1.png", medsam_seg * 255) medsam_list = medsam_seg.tolist() #medsam_list la de tra thang ve may # medsam_list # Convert NumPy array to list of lists # print("medsam_seg", medsam_seg) #medsam_seg la de dua qua mask return medsam_seg def medMask(mask, random_color=False, alpha = 0.95): print("do duoc day nha") if random_color: color = np.concatenate([np.random.random(3), np.array([alpha])], axis=0) else: color = np.array([251 / 255, 252 / 255, 30 / 255, alpha]) h, w = mask.shape[-2:] #mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1) mask_image = (mask.reshape(h, w, 1) * color.reshape(1, 1, -1)).astype(np.uint8) # Convert to uint8 print("xuong duoc toi day khong?") # Return the mask image as a NumPy array return mask_image @app.post("/test") async def segmentation(image: UploadFile = File(...), boundingbox: str = Form(...)): try: img_tensor, H, W = await process_image(image) #bounding_boxes = json.loads(boundingbox) print("number", boundingbox) # bounding_box = process_string(boundingbox) # print("bb", bounding_box) box = process_boundingbox(boundingbox, (H, W)) print("box", box) segmentation_result = perform_segmentation(image_tensor=img_tensor, H=H, W=W, medsam_model=model, box=box) segmentation_mask = medMask(segmentation_result) print("la duoc toi day?") print("segmask", segmentation_mask) print("segmasktype", segmentation_mask.shape) #### # Convert segmentation mask to grayscale image gray_mask = (segmentation_result * 255).astype(np.uint8) print("gray", gray_mask) gray_img = np.array(gray_mask, dtype=np.uint8) print("img", gray_img) # Save the grayscale and RGB masks as images cv2.imwrite('brain_test1.png', gray_img) return segmentation_result.tolist() except Exception as e: raise HTTPException(status_code=500, detail=str(e))