-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
45 lines (33 loc) · 1.31 KB
/
preprocessing.py
File metadata and controls
45 lines (33 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import torch
from torchvision import transforms
from PIL import Image
import json
from main import data_path_main
with open("config.json", "r") as config_file:
config = json.load(config_file)
class ImagePreprocessor:
def __init__(self, input_size=(config["input-size"], config["input-size"]), contrast_factor=1.5):
self.input_size = input_size
self.contrast_factor = contrast_factor
def apply_transforms(self, image):
transform = transforms.Compose([
transforms.Resize(self.input_size),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(contrast=self.contrast_factor),
transforms.ToTensor(),
])
return transform(image)
def load_and_preprocess_image(image_path, preprocessor):
image = Image.open(image_path).convert("RGB")
preprocessed_image = preprocessor.apply_transforms(image)
preprocessed_image = torch.unsqueeze(preprocessed_image, 0)
return preprocessed_image
def main():
data_path = data_path_main
preprocessor = ImagePreprocessor()
image_path = os.path.join(data_path, "path_to_an_image.jpg")
preprocessed_image = load_and_preprocess_image(image_path, preprocessor)
print("Preprocessed image shape:", preprocessed_image.shape)
if __name__ == "__main__":
main()