代码拉取完成,页面将自动刷新
import itertools
import math
import os
import random
import sys
import numpy as np
import cv2
#get some random background image based on a opensource dataset
class WorldGenerator():
def __init__(self, img_dir, empty_dir, world_size):
self.output_shape = world_size
self.img_dir = img_dir
self.img_list = os.listdir(self.img_dir)
random.shuffle(self.img_list)
self.img_num = len(self.img_list)
#empty world
self.empty_world = cv2.imread(empty_dir + "blue.bmp")
self.empty_world = cv2.resize(self.empty_world, self.output_shape, interpolation = cv2.INTER_AREA)
#empth world means the background color is the same as the plate color
def generator_empty_world(self, color = 1):
return self.empty_world.copy()
def generate_one_world(self):
current_path = sys.path[0]
while True:
index = random.randint(0, self.img_num - 1)
fname = self.img_dir + self.img_list[index]
img = cv2.imread(fname, -1)
#we do not use one channnel image
if (len(img.shape) > 2 and img.shape[1] >= self.output_shape[0] and img.shape[0] >= self.output_shape[1]):
break
#get a roi with random position from the raw image
x = random.randint(0, img.shape[1] - self.output_shape[0])
y = random.randint(0, img.shape[0] - self.output_shape[1])
img = img[y:y + self.output_shape[1], x:x + self.output_shape[0]]
return img
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。