|
19 | 19 | from PIL import Image, ExifTags |
20 | 20 | from torch.utils.data import Dataset |
21 | 21 | from tqdm import tqdm |
| 22 | +import mrcfile # support cryoEM micrograph format |
22 | 23 |
|
23 | 24 | import pickle |
24 | 25 | from copy import deepcopy |
|
29 | 30 | from partinet.DynamicDet.utils.general import check_requirements, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, \ |
30 | 31 | resample_segments, clean_str |
31 | 32 | from partinet.DynamicDet.utils.torch_utils import torch_distributed_zero_first |
| 33 | +from partinet.process_utils.guided_denoiser import transform |
32 | 34 |
|
33 | 35 | # Parameters |
34 | 36 | help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data' |
35 | | -img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo'] # acceptable image suffixes |
| 37 | +# acceptable image suffixes (extensions are compared case‑insensitively) |
| 38 | +# add 'mrc' so that LoadImages/LoadImagesAndLabels can see micrographs directly |
| 39 | +# micrographs are read with `mrcfile`; only uncompressed MRC files are supported |
| 40 | +img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo', 'mrc'] |
36 | 41 | vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes |
37 | 42 | logger = logging.getLogger(__name__) |
38 | 43 |
|
@@ -137,6 +142,7 @@ def __init__(self, path, img_size=640, stride=32): |
137 | 142 | else: |
138 | 143 | raise Exception(f'ERROR: {p} does not exist') |
139 | 144 |
|
| 145 | + # allow uncompressed .mrc files |
140 | 146 | images = [x for x in files if x.split('.')[-1].lower() in img_formats] |
141 | 147 | videos = [x for x in files if x.split('.')[-1].lower() in vid_formats] |
142 | 148 | ni, nv = len(images), len(videos) |
@@ -183,7 +189,15 @@ def __next__(self): |
183 | 189 | else: |
184 | 190 | # Read image |
185 | 191 | self.count += 1 |
186 | | - img0 = cv2.imread(path) # BGR |
| 192 | + # support cryo-EM micrographs saved as MRC |
| 193 | + if path.lower().endswith('.mrc'): |
| 194 | + img_mrc = mrcfile.read(path) |
| 195 | + img0 = transform(img_mrc).astype(np.uint8) |
| 196 | + # `transform` returns a single-channel image; networks expect BGR |
| 197 | + if img0.ndim == 2: |
| 198 | + img0 = cv2.cvtColor(img0, cv2.COLOR_GRAY2BGR) |
| 199 | + else: |
| 200 | + img0 = cv2.imread(path) # BGR |
187 | 201 | assert img0 is not None, 'Image Not Found ' + path |
188 | 202 | #print(f'image {self.count}/{self.nf} {path}: ', end='') |
189 | 203 |
|
@@ -668,7 +682,14 @@ def load_image(self, index): |
668 | 682 | img = self.imgs[index] |
669 | 683 | if img is None: # not cached |
670 | 684 | path = self.img_files[index] |
671 | | - img = cv2.imread(path) # BGR |
| 685 | + # support uncompressed mrc micrographs |
| 686 | + if path.lower().endswith('.mrc'): |
| 687 | + img_mrc = mrcfile.read(path) |
| 688 | + img = transform(img_mrc).astype(np.uint8) |
| 689 | + if img.ndim == 2: |
| 690 | + img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) |
| 691 | + else: |
| 692 | + img = cv2.imread(path) # BGR |
672 | 693 | assert img is not None, 'Image Not Found ' + path |
673 | 694 | h0, w0 = img.shape[:2] # orig hw |
674 | 695 | r = self.img_size / max(h0, w0) # resize image to img_size |
|
0 commit comments