Ignaciobfp commited on
Commit
bc465fb
·
1 Parent(s): 107c8d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -1
app.py CHANGED
@@ -5,8 +5,120 @@ import PIL
5
  import torchvision.transforms as transforms
6
 
7
  ##Extras por si pudiera reconstruir la imagen en HF también
 
8
  import os
9
- import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def subimages_from_directory(directorio):
12
  # Define el directorio a recorrer
 
5
  import torchvision.transforms as transforms
6
 
7
  ##Extras por si pudiera reconstruir la imagen en HF también
8
+ import numpy as np
9
  import os
10
+ import cv2
11
+
12
+ def extract_subimages(image : np.ndarray, wwidth, wheight, overlap_fraction):
13
+ """
14
+ Extracts subimages of the input image using a moving window of size (wwidth, wheight)
15
+ with the specified overlap fraction. Returns a tuple (subimages, coords) where subimages
16
+ is a list of subimages and coords is a list of tuples (x, y) indicating the top left corner
17
+ coordinates of each subimage in the input image.
18
+ """
19
+ subimages = []
20
+ coords = []
21
+ height, width, channels = image.shape
22
+ if channels > 3:
23
+ image = image[:,:,0:3]
24
+ channels = 3
25
+ overlap = int(max(0, min(overlap_fraction, 1)) * min(wwidth, wheight))
26
+ y = 0
27
+ while y + wheight <= height:
28
+ x = 0
29
+ while x + wwidth <= width:
30
+ subimage = image[y:y+wheight, x:x+wwidth, :]
31
+ subimages.append(subimage)
32
+ coords.append((x, y))
33
+ x += wwidth - overlap
34
+ y += wheight - overlap
35
+ if y < height:
36
+ y = height - wheight
37
+ x = 0
38
+ while x + wwidth <= width:
39
+ subimage = image[y:y+wheight, x:x+wwidth, :]
40
+ subimages.append(subimage)
41
+ coords.append((x, y))
42
+ x += wwidth - overlap
43
+ if x < width:
44
+ x = width - wwidth
45
+ subimage = image[y:y+wheight, x:x+wwidth, :]
46
+ subimages.append(subimage)
47
+ coords.append((x, y))
48
+ if x < width:
49
+ x = width - wwidth
50
+ y = 0
51
+ while y + wheight <= height:
52
+ subimage = image[y:y+wheight, x:x+wwidth, :]
53
+ subimages.append(subimage)
54
+ coords.append((x, y))
55
+ y += wheight - overlap
56
+ if y < height:
57
+ y = height - wheight
58
+ subimage = image[y:y+wheight, x:x+wwidth, :]
59
+ subimages.append(subimage)
60
+ coords.append((x, y))
61
+ return subimages, coords
62
+
63
+ # Si no hay archivos tif (labels) no se tratan, no hace falta considerarlo
64
+ def generate_and_save_subimages(path, output_dir_images, output_dir_labels = None):
65
+ if output_dir_labels:
66
+ if not os.path.exists(output_dir_labels):
67
+ os.makedirs(output_dir_labels)
68
+
69
+ if not os.path.exists(output_dir_images):
70
+ os.makedirs(output_dir_images)
71
+
72
+ for filename in os.listdir(path):
73
+ if filename.endswith(".png") or filename.endswith(".tif"):
74
+ filepath = os.path.join(path, filename)
75
+ image = cv2.imread(filepath)
76
+ subimages, coords = extract_subimages(image, 400, 400, 0.66)
77
+ for i, subimage in enumerate(subimages):
78
+ if filename.endswith(".png"):
79
+ output_filename = os.path.join(output_dir_images, f"{filename.rsplit('.', 1)[0]}_{coords[i][0]}_{coords[i][1]}.png")
80
+ cv2.imwrite(output_filename, subimage)
81
+ else:
82
+ if output_dir_labels:
83
+ output_filename = os.path.join(output_dir_labels, f"{filename.rsplit('.', 1)[0]}_{coords[i][0]}_{coords[i][1]}.tif")
84
+ cv2.imwrite(output_filename, subimage)
85
+
86
+ def generate_and_save_subimages_nolabel(path, output_dir_images, olverlap=0.0, imagesformat="png", split_in_dirs=True):
87
+ for entry in os.scandir(path):
88
+ if entry.is_file() and entry.name.lower().endswith(imagesformat):
89
+ filepath = entry.path
90
+ gss_single(filepath, output_dir_images, olverlap, imagesformat, split_in_dirs)
91
+
92
+ def gss_single(filepath, output_dir_images, olverlap=0.0, imagesformat="png", split_in_dirs=True):
93
+ image = cv2.imread(filepath)
94
+
95
+ if split_in_dirs:
96
+ dir_this_image = Path(output_dir_images)/filepath.rsplit('.', 1)[0]
97
+ os.makedirs(dir_this_image, exist_ok=True)
98
+ else:
99
+ os.makedirs(output_dir_images, exist_ok=True)
100
+
101
+ subimages, coords = extract_subimages(image, 400, 400, olverlap)
102
+ for i, subimage in enumerate(subimages):
103
+ if split_in_dirs:
104
+ output_filename = os.path.join(dir_this_image, f"{filepath.rsplit('.', 1)[0]}_{coords[i][0]}_{coords[i][1]}.png")
105
+ else:
106
+ output_filename = os.path.join(output_dir_images, f"{filepath.rsplit('.', 1)[0]}_{coords[i][0]}_{coords[i][1]}.png")
107
+ cv2.imwrite(output_filename, subimage)
108
+
109
+ def split_windows_in_folders(input_images_folder, output_images_folder):
110
+ for filename in os.listdir(input_images_folder):
111
+ dir_this_image = Path(output_images_folder)/filename.rsplit('.', 1)[0]
112
+ os.makedirs(dir_this_image, exist_ok=True)
113
+ if filename.endswith(".png"):
114
+ print(str(dir_this_image))
115
+ filepath = os.path.join(path, filename)
116
+ image = cv2.imread(filepath)
117
+ subimages, coords = extract_subimages(image, 400, 400, 0)
118
+ for i, subimage in enumerate(subimages):
119
+ output_filename = os.path.join(dir_this_image, f"{filename.rsplit('.', 1)[0]}_{coords[i][0]}_{coords[i][1]}.png")
120
+ cv2.imwrite(output_filename, subimage)
121
+
122
 
123
  def subimages_from_directory(directorio):
124
  # Define el directorio a recorrer