|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +# ------------------------------------------------------------- |
| 21 | +from typing import List, Optional, Union |
| 22 | + |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +from systemds.scuro.dataloader.base_loader import BaseLoader |
| 26 | +import cv2 |
| 27 | +from systemds.scuro.modality.type import ModalityType |
| 28 | + |
| 29 | + |
| 30 | +class ImageLoader(BaseLoader): |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + source_path: str, |
| 34 | + indices: List[str], |
| 35 | + data_type: Union[np.dtype, str] = np.float16, |
| 36 | + chunk_size: Optional[int] = None, |
| 37 | + load=True, |
| 38 | + ext=".jpg", |
| 39 | + ): |
| 40 | + super().__init__( |
| 41 | + source_path, indices, data_type, chunk_size, ModalityType.IMAGE, ext |
| 42 | + ) |
| 43 | + self.load_data_from_file = load |
| 44 | + |
| 45 | + def extract(self, file: str, index: Optional[Union[str, List[str]]] = None): |
| 46 | + self.file_sanity_check(file) |
| 47 | + |
| 48 | + image = cv2.imread(file, cv2.IMREAD_COLOR) |
| 49 | + image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| 50 | + |
| 51 | + if image.ndim == 2: |
| 52 | + height, width = image.shape |
| 53 | + channels = 1 |
| 54 | + else: |
| 55 | + height, width, channels = image.shape |
| 56 | + |
| 57 | + image = image.astype(np.float32) / 255.0 |
| 58 | + |
| 59 | + self.metadata[file] = self.modality_type.create_metadata( |
| 60 | + width, height, channels |
| 61 | + ) |
| 62 | + |
| 63 | + self.data.append(image) |
0 commit comments