import os,shutil import random from decimal import Decimal from PIL import Image, ImageDraw, ImageFont def t1(img_name): with open('./labels/train2017/' + img_name + '.txt', 'a+') as f: f.write(' '.join([str(36), str(0.090557), str(0.074689), str(0.094427), str(0.130705) + '\n'])) f.write(' '.join([str(36), str(0.335139), str(0.073133), str(0.092879), str(0.129668) + '\n'])) f.write(' '.join([str(36), str(0.578173), str(0.074170), str(0.091331), str(0.129668) + '\n'])) f.write(' '.join([str(36), str(0.822755), str(0.073651), str(0.091331), str(0.132780) + '\n'])) f.write(' '.join([str(39), str(0.830108), str(0.963174), str(0.084365), str(0.054979) + '\n'])) f.write(' '.join([str(39), str(0.585526), str(0.963174), str(0.081269), str(0.052905) + '\n'])) f.write(' '.join([str(39), str(0.340944), str(0.963174), str(0.082817), str(0.052905) + '\n'])) f.write(' '.join([str(39), str(0.095975), str(0.962656), str(0.083591), str(0.051867) + '\n'])) def t(label_index, x_center, y_center, w, h, img_name): with open('./labels/train2017/' + img_name + '.txt', 'a+') as f: f.write(' '.join([str(label_index), str(x_center), str(y_center), str(w), str(h) + '\n'])) def draw_label(x, y, jianju, text, img_name, label_index, width, height, zifu_W, zifu_H): font = ImageFont.truetype('arial.ttf', 25) for i in range(0, len(text)): if i < 4: draw.text((x + jianju * i, y), text[i], font=font, fill=(118, 91, 100)) # 128 101 110 label = label_index[i] # draw.rectangle([x + kk * i, y, x + kk * i + k, y + y_], outline='blue', width=1) else: label = label_index[i] draw.text((x + jianju * i, y), text[i], font=font, fill=(82, 67, 62)) xmin = int(x + jianju * i - 5) ymin = int(y + zifu_H / 3 - 5) xmax = int(x + jianju * i + zifu_W + 5) ymax = int(y + zifu_H * 5 / 3) x_center = Decimal(str(round(float((xmin + xmax) / (2 * width)), 6))).quantize(Decimal('0.000000')) y_center = Decimal(str(round(float((ymin + ymax) / (2 * height)), 6))).quantize(Decimal('0.000000')) w = Decimal(str(round(float((xmax - xmin) / width), 6))).quantize(Decimal('0.000000')) h = Decimal(str(round(float((ymax - ymin) / height), 6))).quantize(Decimal('0.000000')) t(label, x_center, y_center, w, h, img_name) draw.rectangle([xmin, ymin, xmax, ymax], outline='blue', width=1) img.save('./images/train2017/' + img_name + ".jpg") if __name__ == '__main__': # 打开图片 txt_path = "./labels/train2017" if not os.path.exists(txt_path): os.makedirs(txt_path) else: shutil.rmtree(txt_path) os.makedirs(txt_path) save_path = "./images/train2017" if not os.path.exists(save_path): os.makedirs(save_path) else: shutil.rmtree(save_path) os.makedirs(save_path) letter_lists = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] num_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] # 生成照片张数 num_save = 1000 # 一组字符的长度 test_dist = 250 for name in range(num_save): img_name = str(name) t1(img_name) img = Image.open('a.jpg') width, height = img.size # 创建一个可以在给定图像上绘画的对象 draw = ImageDraw.Draw(img) zifu_W = 15 zifu_H = 15 jianju = 25 jianju_H = 80 y = 150 x = 50 for z in range(10): for i in range(4): text = [] label_index = [] for j in range(10): if j == 0 or j == 3: letter_random_number = random.randint(0, len(letter_lists) - 1) # print(letter_lists[letter_random_number]) text.append(letter_lists[letter_random_number]) label_index.append(letter_random_number + 10) else: num_random_number = random.randint(0, len(num_list) - 1) # print(num_list[num_random_number]) text.append(num_list[num_random_number]) label_index.append(num_random_number) # 添加文本到图像上 print("text=", text) draw_label(x + i * test_dist + 80, y + z * jianju_H, jianju, text, img_name, label_index, width, height, zifu_W, zifu_H) # 保存修改后的图像 # img.save('res.jpg') # HSV转RGB的公式是: # # R = H360 + 6S/10 - 360V/100 # G = H360 - 2*(6S/10 - 360V/100) # B = H360 + 4(6S/10 - 360V/100) # # 其中H的范围是[0, 360],S和V的范围是[0, 100]。 # # 所以对于你的HSV值 H=9, S=85, V=177,我们可以得出: # # R = 9360 + 685/10 - 360177/100 = 217 # G = 9360 - 2*(685/10 - 360177/100) = 254 # B = 9360 + 4(685/10 - 360177/100) = 85
Python 自动生成字符,数组的coco数据集
最新推荐文章于 2024-07-08 03:28:55 发布