Spaces:
Sleeping
Sleeping
Removed redundant validation
Browse files- yolo_predictor.py +0 -66
yolo_predictor.py
CHANGED
|
@@ -13,69 +13,6 @@ def load_yolo_model(model_path):
|
|
| 13 |
"""Load YOLO model from .pt file"""
|
| 14 |
return YOLO(model_path)
|
| 15 |
|
| 16 |
-
def validate_4channel_tiff(image_path):
|
| 17 |
-
"""
|
| 18 |
-
Validate that the input TIFF file has 4 channels and is readable
|
| 19 |
-
|
| 20 |
-
Args:
|
| 21 |
-
image_path: Path to input TIFF image
|
| 22 |
-
|
| 23 |
-
Returns:
|
| 24 |
-
bool: True if valid 4-channel TIFF
|
| 25 |
-
|
| 26 |
-
Raises:
|
| 27 |
-
ValueError: If validation fails
|
| 28 |
-
"""
|
| 29 |
-
if not os.path.exists(image_path):
|
| 30 |
-
logger.error(f"Image file does not exist: {image_path}")
|
| 31 |
-
raise ValueError(f"Image file does not exist: {image_path}")
|
| 32 |
-
|
| 33 |
-
logger.info(f"Validating TIFF file: {image_path}")
|
| 34 |
-
|
| 35 |
-
try:
|
| 36 |
-
# Primary validation with tifffile
|
| 37 |
-
img_array = tifffile.imread(image_path)
|
| 38 |
-
|
| 39 |
-
# Check array shape and channels
|
| 40 |
-
if len(img_array.shape) == 3:
|
| 41 |
-
if img_array.shape[0] == 4:
|
| 42 |
-
# Shape is (4, H, W)
|
| 43 |
-
channels = 4
|
| 44 |
-
height, width = img_array.shape[1], img_array.shape[2]
|
| 45 |
-
elif img_array.shape[2] == 4:
|
| 46 |
-
# Shape is (H, W, 4)
|
| 47 |
-
channels = 4
|
| 48 |
-
height, width = img_array.shape[0], img_array.shape[1]
|
| 49 |
-
else:
|
| 50 |
-
channels = min(img_array.shape[0], img_array.shape[2])
|
| 51 |
-
height, width = img_array.shape[0], img_array.shape[1]
|
| 52 |
-
else:
|
| 53 |
-
logger.error(f"Invalid image shape: {img_array.shape}. Expected 3D array with 4 channels.")
|
| 54 |
-
raise ValueError(f"Invalid image shape: {img_array.shape}. Expected 3D array with 4 channels.")
|
| 55 |
-
|
| 56 |
-
if channels != 4:
|
| 57 |
-
logger.error(f"YOLO model expects 4-channel images, but got {channels} channels")
|
| 58 |
-
raise ValueError(f"YOLO model expects 4-channel images, but got {channels} channels")
|
| 59 |
-
|
| 60 |
-
logger.info(f"Validation successful: {channels} channels, {height}x{width}, dtype: {img_array.dtype}")
|
| 61 |
-
return True
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
logger.warning(f"Tifffile validation failed: {str(e)}, trying rasterio fallback")
|
| 65 |
-
# Fallback validation with rasterio
|
| 66 |
-
try:
|
| 67 |
-
with rasterio.open(image_path) as src:
|
| 68 |
-
if src.count != 4:
|
| 69 |
-
logger.error(f"YOLO model expects 4-channel images, but got {src.count} channels")
|
| 70 |
-
raise ValueError(f"YOLO model expects 4-channel images, but got {src.count} channels")
|
| 71 |
-
|
| 72 |
-
logger.info(f"Validation successful (rasterio): {src.count} channels, {src.width}x{src.height}, dtype: {src.dtypes[0]}")
|
| 73 |
-
return True
|
| 74 |
-
|
| 75 |
-
except Exception as e2:
|
| 76 |
-
logger.error(f"Could not validate TIFF file. Tifffile error: {str(e)}, Rasterio error: {str(e2)}")
|
| 77 |
-
raise ValueError(f"Could not validate TIFF file. Errors: tifffile={str(e)}, rasterio={str(e2)}")
|
| 78 |
-
|
| 79 |
def predict_yolo(yolo_model, image_path, conf=0.01):
|
| 80 |
"""
|
| 81 |
Predict using YOLO model on 4-channel TIFF image
|
|
@@ -90,9 +27,6 @@ def predict_yolo(yolo_model, image_path, conf=0.01):
|
|
| 90 |
"""
|
| 91 |
logger.info(f"Starting YOLO prediction on: {image_path} with confidence: {conf}")
|
| 92 |
|
| 93 |
-
# Validate input file
|
| 94 |
-
validate_4channel_tiff(image_path)
|
| 95 |
-
|
| 96 |
logger.info("Running YOLO model inference...")
|
| 97 |
# Run YOLO prediction directly on the input file
|
| 98 |
results = yolo_model([image_path], conf=conf)
|
|
|
|
| 13 |
"""Load YOLO model from .pt file"""
|
| 14 |
return YOLO(model_path)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def predict_yolo(yolo_model, image_path, conf=0.01):
|
| 17 |
"""
|
| 18 |
Predict using YOLO model on 4-channel TIFF image
|
|
|
|
| 27 |
"""
|
| 28 |
logger.info(f"Starting YOLO prediction on: {image_path} with confidence: {conf}")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
logger.info("Running YOLO model inference...")
|
| 31 |
# Run YOLO prediction directly on the input file
|
| 32 |
results = yolo_model([image_path], conf=conf)
|