File size: 6,779 Bytes
1433dbd |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
from PIL import Image, ImageDraw, ImageFilter
import numpy as np
import random
import math
class SimplePolygon:
pow2 = None
def __init__(self, file_name, depth_name, tags_name,resolution=(512, 512), init_color=(255, 255, 255)):
self.file_name = file_name
self.depth_name = depth_name
self.tags_name = tags_name
self.width = resolution[0]
self.height = resolution[1]
self.components= [set([(wi, hi) for wi in range(512) for hi in range(512)])]
self.img1 = np.full((self.width, self.height, 3), np.random.randint(0, 256, size=(3,), dtype=np.uint8), dtype=np.uint8)
self.img2 = np.zeros([self.width, self.height, 3], dtype=np.uint8)
self.count = 0
if self.pow2 is None:
self.pow2 = [x**2 for x in range(max(resolution[0],resolution[1])+1)]
def component_check(self, comp):
retv = set()
for wi, hi in comp:
if 0<=wi<self.width and 0<=hi<self.height:
retv.add((wi, hi))
return retv
def components_check(self, components):
retv = []
for i in range(len(components)):
is_contain = False
for j in range(i+1, len(components)):
if components[i] <= components[j]:
is_contain = True
break
if not is_contain:
retv.append(self.components[i])
return retv
def generate(self):
# 別の図形に含まれている図形は削除
self.components = self.components_check(self.components)
# 画像外の座標は除去
for i in range(len(self.components)):
self.components[i]=self.component_check(self.components[i])
layer = np.zeros_like(self.components, dtype=np.uint16)
noise = np.zeros(3, dtype=np.int16)
if random.randint(1, 5)==1:
noise = np.random.randint(-10, 10, size=(3,), dtype=np.int16)
for i in range(len(self.components)):
# ↓に重なってる図形の層+1 のmax
for j in range(i-1, -1, -1):
if not self.components[i].isdisjoint(self.components[j]):
layer[i]=max(layer[i], layer[j]+1)
# 色付ける
color = np.random.randint(0, 255, size=(3,), dtype=np.int16)
vec = np.random.randint(0, 255, size=(3,), dtype=np.int16)
ep = np.random.normal(0, 2, size=(2,))
for wi, hi in self.components[i]:
self.img1[wi][hi] = np.abs((color + vec * (ep[0] * wi / self.width + ep[1] * hi / self.height)+noise) % 512 - 256).astype(np.uint8)
# depth画像生成
self.layer_max = np.max(layer)
normalized_layer = ( (255 * layer) // self.layer_max).astype(np.uint8)
for wi in range(self.width):
for hi in range(self.height):
for i in range(len(self.components)-1, -1, -1):
if (wi,hi) in self.components[i]:
self.img2[wi][hi][0] = normalized_layer[i]
self.img2[wi][hi][1] = normalized_layer[i]
self.img2[wi][hi][2] = normalized_layer[i]
break
def add_rect(self):
left_top = [random.randint(0, self.width//10*9), random.randint(0, self.height//10*9)]
poly_width = random.randint(60, self.width//2)
poly_height = random.randint(60, self.height//2)
poly = set()
for wi in range(left_top[0], min(left_top[0]+poly_width, self.width)):
for hi in range(left_top[1], min(left_top[1]+poly_height, self.height)):
poly.add((wi, hi))
self.components.append(self.rotate_component(poly, random.uniform(-math.pi, math.pi)))
return poly
def add_ellipse(self):
center = [random.randint(self.width//10, self.width//10*9), random.randint(self.height//10, self.height//10*9)]
radius_width = random.randint(60, self.width//4)
radius_height = random.randint(60, self.height//4)
poly = set()
r = radius_width**2 * radius_height**2
h2 = radius_height**2
w2 = radius_width**2
for wi in range(self.width):
xx = h2 * self.pow2[abs(wi-center[0])]
if xx > r:
continue
for hi in range(self.height):
if xx + w2 * self.pow2[abs(hi-center[1])] <= r:
poly.add((wi, hi))
self.components.append(self.rotate_component(poly, random.uniform(-math.pi, math.pi)))
return poly
def rotate_component(self, comp, rad):
retv = set()
for wi, hi in comp:
new_wi = math.cos(rad)*(wi-self.width/2)-math.sin(rad)*(hi-self.height/2)+self.width/2
new_hi = math.sin(rad)*(wi-self.width/2)+math.cos(rad)*(hi-self.height/2)+self.height/2
new_wi = round(new_wi)
new_hi = round(new_hi)
for i in range(-1, 2): # -1,0,1
for j in range(-1, 2):
retv.add((new_wi+i, new_hi+j))
return retv
def save(self):
Image.fromarray(self.img1).filter(filter=ImageFilter.GaussianBlur(random.randint(0, 1))).save(self.file_name)
Image.fromarray(self.img2).save(self.depth_name)
with open(self.tags_name, "w") as file:
if self.layer_max is None:
file.write("")
else:
file.write(f"{self.layer_max+1}depth")
import uuid
import concurrent.futures
from tqdm import tqdm
import os
import threading
def process_polygon():
random_uuid = str(uuid.uuid4())
polygon = SimplePolygon(f"conditioning_images/{random_uuid}.png", f"images/{random_uuid}.png", f"conditioning_images/{random_uuid}.txt")
for _ in range(random.randint(4, 20)):
if random.randint(0,1)==0:
polygon.add_ellipse()
else:
polygon.add_rect()
polygon.generate()
polygon.save()
def main():
if not os.path.exists("conditioning_images"):
os.makedirs("conditioning_images")
if not os.path.exists("images"):
os.makedirs("images")
num_processes = 24
total_runs = 21037
with tqdm(total=total_runs, ncols=80) as pbar:
with concurrent.futures.ProcessPoolExecutor(max_workers=num_processes) as executor:
futures = [executor.submit(process_polygon) for _ in range(total_runs)]
for future in concurrent.futures.as_completed(futures):
try:
result = future.result()
pbar.update(1)
except Exception as e:
print(f"Error occurred: {e}")
pbar.update(1)
if __name__ == "__main__":
main()
|