Yossh commited on
Commit
1fcec89
1 Parent(s): 639af5d

Delete figure_generate.py

Browse files
Files changed (1) hide show
  1. figure_generate.py +0 -132
figure_generate.py DELETED
@@ -1,132 +0,0 @@
1
- from PIL import Image, ImageDraw, ImageFilter
2
- import numpy as np
3
- import random
4
- import math
5
-
6
- class SimplePolygon:
7
- pow2 = None
8
-
9
- def __init__(self, file_name, depth_name, tags_name,resolution=(512, 512), init_color=(255, 255, 255)):
10
- self.file_name = file_name
11
- self.depth_name = depth_name
12
- self.tags_name = tags_name
13
- self.width = resolution[0]
14
- self.height = resolution[1]
15
- self.components= [set([(wi, hi) for wi in range(512) for hi in range(512)])]
16
- self.img1 = np.full((self.width, self.height, 3), np.random.randint(0, 256, size=(3,), dtype=np.uint8), dtype=np.uint8)
17
- self.img2 = np.zeros([self.width, self.height, 3], dtype=np.uint8)
18
- self.count = 0
19
- if self.pow2 is None:
20
- self.pow2 = [x**2 for x in range(max(resolution[0],resolution[1])+1)]
21
-
22
- def generate(self):
23
- layer = np.zeros_like(self.components, dtype=np.uint16)
24
- noise = np.zeros(3, dtype=np.int16)
25
- if random.randint(1, 5)==1:
26
- noise = np.random.randint(-10, 10, size=(3,), dtype=np.int16)
27
- for i in range(len(self.components)):
28
- # ↓に重なってる図形の層+1 のmax
29
- for j in range(i-1, -1, -1):
30
- if not self.components[i].isdisjoint(self.components[j]):
31
- layer[i]=max(layer[i], layer[j]+1)
32
- # 色付ける
33
- color = np.random.randint(0, 255, size=(3,), dtype=np.int16)
34
- vec = np.random.randint(0, 255, size=(3,), dtype=np.int16)
35
- ep = np.random.normal(0, 2, size=(2,))
36
- for wi, hi in self.components[i]:
37
- self.img1[wi][hi] = np.abs((color + vec * (ep[0] * wi / self.width + ep[1] * hi / self.height)+noise) % 512 - 256).astype(np.uint8)
38
- # depth画像生成
39
- self.layer_max = np.max(layer)
40
- normalized_layer = ( (255 * layer) // self.layer_max).astype(np.uint8)
41
- for wi in range(self.width):
42
- for hi in range(self.height):
43
- for i in range(len(self.components)-1, -1, -1):
44
- if (wi,hi) in self.components[i]:
45
- self.img2[wi][hi][0] = normalized_layer[i]
46
- self.img2[wi][hi][1] = normalized_layer[i]
47
- self.img2[wi][hi][2] = normalized_layer[i]
48
- break
49
-
50
- def add_rect(self):
51
- left_top = [random.randint(0, self.width//10*9), random.randint(0, self.height//10*9)]
52
- poly_width = random.randint(60, self.width//2)
53
- poly_height = random.randint(60, self.height//2)
54
- poly = set()
55
- for wi in range(left_top[0], min(left_top[0]+poly_width, self.width)):
56
- for hi in range(left_top[1], min(left_top[1]+poly_height, self.height)):
57
- poly.add((wi, hi))
58
- self.components.append(self.rotate_component(poly, random.uniform(-math.pi, math.pi)))
59
- return poly
60
-
61
- def add_ellipse(self):
62
- center = [random.randint(self.width//10, self.width//10*9), random.randint(self.height//10, self.height//10*9)]
63
- radius_width = random.randint(60, self.width//4)
64
- radius_height = random.randint(60, self.height//4)
65
- poly = set()
66
- r = radius_width**2 * radius_height**2
67
- h2 = radius_height**2
68
- w2 = radius_width**2
69
- for wi in range(self.width):
70
- xx = h2 * self.pow2[abs(wi-center[0])]
71
- if xx > r:
72
- continue
73
- for hi in range(self.height):
74
- if xx + w2 * self.pow2[abs(hi-center[1])] <= r:
75
- poly.add((wi, hi))
76
- self.components.append(self.rotate_component(poly, random.uniform(-math.pi, math.pi)))
77
- return poly
78
- def rotate_component(self, comp, rad):
79
- retv = set()
80
- for wi, hi in comp:
81
- new_wi = math.cos(rad)*(wi-self.width/2)-math.sin(rad)*(hi-self.height/2)+self.width/2
82
- new_hi = math.sin(rad)*(wi-self.width/2)+math.cos(rad)*(hi-self.height/2)+self.height/2
83
- new_wi = round(new_wi)
84
- new_hi = round(new_hi)
85
- for i in range(-1, 2): # -1,0,1
86
- for j in range(-1, 2):
87
- retv.add((new_wi+i, new_hi+j))
88
- return retv
89
- def save(self):
90
- Image.fromarray(self.img1).filter(filter=ImageFilter.GaussianBlur(random.randint(0, 1))).save(self.file_name)
91
- Image.fromarray(self.img2).save(self.depth_name)
92
- with open(self.tags_name, "w") as file:
93
- if self.layer_max is None:
94
- file.write("")
95
- else:
96
- file.write(f"{self.layer_max+1}depth")
97
-
98
-
99
- import uuid
100
- import concurrent.futures
101
- from tqdm import tqdm
102
- import os
103
-
104
-
105
-
106
- def process_polygon():
107
- random_uuid = str(uuid.uuid4())
108
- polygon = SimplePolygon(f"conditioning_images/{random_uuid}.png", f"images/{random_uuid}.png", f"conditioning_images/{random_uuid}.txt")
109
- for _ in range(random.randint(4, 10)):
110
- if random.randint(0,1)==0:
111
- polygon.add_ellipse()
112
- else:
113
- polygon.add_rect()
114
- polygon.generate()
115
- polygon.save()
116
-
117
- def main():
118
- if not os.path.exists("conditioning_images"):
119
- os.makedirs("conditioning_images")
120
- if not os.path.exists("images"):
121
- os.makedirs("images")
122
- num_processes = 24
123
- total_runs = 100000
124
-
125
- with tqdm(total=total_runs, ncols=80) as pbar:
126
- with concurrent.futures.ProcessPoolExecutor(max_workers=num_processes) as executor:
127
- futures = [executor.submit(process_polygon) for _ in range(total_runs)]
128
- for future in concurrent.futures.as_completed(futures):
129
- pbar.update(1)
130
-
131
- if __name__ == "__main__":
132
- main()