depth_overlap_simple_shape / figure_generate.py
Yossh's picture
Upload 11 files
844398f
raw
history blame
5.01 kB
from PIL import Image, ImageDraw, ImageFilter
import numpy as np
import random
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 generate(self):
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)):
for j in range(i-1, -1, -1):
if not self.components[i].isdisjoint(self.components[j]):
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)
break
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(poly)
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(poly)
return poly
def rotate_component(comp, radian):
retv = set()
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
def process_polygon():
random_uuid = str(uuid.uuid4())
polygon = SimplePolygon(f"data/{random_uuid}.png", f"label/{random_uuid}.png", f"data/{random_uuid}.txt")
for _ in range(random.randint(4, 10)):
if random.randint(0,1)==0:
polygon.add_ellipse()
else:
polygon.add_rect()
polygon.generate()
polygon.save()
def main():
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists("label"):
os.makedirs("label")
num_processes = 12
total_runs = 100000
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):
pbar.update(1)
if __name__ == "__main__":
main()