28 lines
904 B
Python
28 lines
904 B
Python
|
import os
|
||
|
from PIL import Image
|
||
|
import numpy as np
|
||
|
|
||
|
def check_image_labels_in_range(directory, valid_range=(0, 1)):
|
||
|
invalid_files = []
|
||
|
|
||
|
for filename in os.listdir(directory):
|
||
|
if filename.endswith(".png") or filename.endswith(".jpg"):
|
||
|
filepath = os.path.join(directory, filename)
|
||
|
image = Image.open(filepath)
|
||
|
labels = np.array(image)
|
||
|
|
||
|
if labels.max() > valid_range[1] or labels.min() < valid_range[0]:
|
||
|
invalid_files.append((filename, labels.min(), labels.max()))
|
||
|
|
||
|
return invalid_files
|
||
|
|
||
|
# 使用示例
|
||
|
directory_path = r'data/LoveDA/Val/pv/masks_png_convert'
|
||
|
invalid_files = check_image_labels_in_range(directory_path)
|
||
|
|
||
|
if invalid_files:
|
||
|
for file, min_val, max_val in invalid_files:
|
||
|
print(f"Invalid target {min_val} - {max_val} in {file}")
|
||
|
else:
|
||
|
print("All mask labels are within the valid range [0, 1].")
|