Spaces:
Sleeping
Sleeping
Upload Colony_Analyzer_AI2_HF.py
Browse files- Colony_Analyzer_AI2_HF.py +13 -3
Colony_Analyzer_AI2_HF.py
CHANGED
|
@@ -71,10 +71,20 @@ from transformers import SegformerForSemanticSegmentation, SegformerImageProcess
|
|
| 71 |
image_processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b3-finetuned-cityscapes-1024-1024")
|
| 72 |
|
| 73 |
def preprocess_image(image):
|
| 74 |
-
#image
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
return image, inputs["pixel_values"]
|
| 77 |
-
|
| 78 |
def postprocess_mask(logits):
|
| 79 |
mask = torch.argmax(logits, dim=1) # Take argmax across the class dimension
|
| 80 |
return mask.squeeze().cpu().numpy() # Convert to NumPy array
|
|
|
|
| 71 |
image_processor = SegformerImageProcessor.from_pretrained("nvidia/segformer-b3-finetuned-cityscapes-1024-1024")
|
| 72 |
|
| 73 |
def preprocess_image(image):
|
| 74 |
+
# Gradio gives image in RGB as numpy array; explicitly ensure it's uint8
|
| 75 |
+
if image.dtype != np.uint8:
|
| 76 |
+
image = (255 * image).astype(np.uint8)
|
| 77 |
+
|
| 78 |
+
# explicitly confirm 3 channels RGB
|
| 79 |
+
if image.ndim == 2:
|
| 80 |
+
image = np.stack([image] * 3, axis=-1) # grayscale to RGB explicitly
|
| 81 |
+
elif image.shape[2] == 4:
|
| 82 |
+
image = image[:, :, :3] # RGBA to RGB explicitly
|
| 83 |
+
|
| 84 |
+
# Now explicitly preprocess using Hugging Face image_processor
|
| 85 |
+
inputs = image_processor(image, return_tensors="pt")
|
| 86 |
return image, inputs["pixel_values"]
|
| 87 |
+
|
| 88 |
def postprocess_mask(logits):
|
| 89 |
mask = torch.argmax(logits, dim=1) # Take argmax across the class dimension
|
| 90 |
return mask.squeeze().cpu().numpy() # Convert to NumPy array
|