Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess.py 1.6 KB

You have to be logged in to leave a comment. Sign In
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
46
47
48
49
50
51
52
53
54
55
56
57
  1. from joblib import Parallel, delayed
  2. from skimage.io import imread, imsave
  3. from skimage.transform import resize
  4. from skimage.restoration import denoise_tv_chambolle
  5. from pathlib import Path
  6. from tqdm import tqdm
  7. from dvc.api import params_show
  8. DATA_DIR = Path("data")
  9. train_dir = DATA_DIR / "raw" / "train"
  10. test_dir = DATA_DIR / "raw" / "test"
  11. def save_image(image_array, image_path):
  12. """
  13. Save an image given as NumPy array using its current path
  14. to create its target_path
  15. """
  16. # Create the path to the image in the prepared directory
  17. target_path = str(image_path).replace("raw", "prepared")
  18. Path(target_path).parent.mkdir(parents=True, exist_ok=True)
  19. imsave(target_path, image_array)
  20. def resize_image(image_path, target_size):
  21. """
  22. Resize image to target size.
  23. """
  24. image = imread(image_path) / 255.0
  25. image = resize(image, target_size, anti_aliasing=True)
  26. save_image(image, image_path)
  27. def denoise_image(image_path, weight):
  28. """
  29. Denoise image using total variation filter.
  30. """
  31. image = imread(image_path) / 255.0
  32. image = denoise_tv_chambolle(image, weight=weight, multichannel=True)
  33. save_image(image, image_path)
  34. if __name__ == "__main__":
  35. image_paths = []
  36. params = params_show()['preprocess']
  37. denoise_weight = params["denoise_weight"]
  38. for directory in list(train_dir.iterdir()) + \
  39. list(test_dir.iterdir()):
  40. image_paths.extend(list(directory.glob("*.png")))
  41. Parallel(n_jobs=10, backend="multiprocessing")(
  42. delayed(denoise_image)(path, denoise_weight) for path in tqdm(image_paths)
  43. )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...