pre_process.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. import copy
  2. import json
  3. import os
  4. import random
  5. import re
  6. import sys
  7. import time
  8. import traceback
  9. from glob import glob
  10. from itertools import combinations, product
  11. import chardet
  12. import cv2
  13. import jieba
  14. import numpy as np
  15. from PIL import ImageFont, ImageDraw, Image
  16. from captcha.image import ImageCaptcha
  17. from keras_preprocessing.sequence import pad_sequences
  18. from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
  19. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  20. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  21. from click_captcha.utils import np2pil, pil2np, pil_resize, pil_rotate, pil2np_a, np2pil_a, pil_resize_a, pil_rotate_a
  22. def gen_siamese(paths, batch_size=32, shape=(40, 40), cls_num=1):
  23. num = len(paths)
  24. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/click/"
  25. i = 0
  26. while True:
  27. if i >= num:
  28. i = 0
  29. random.shuffle(paths)
  30. height, width = shape[:2]
  31. X1 = np.zeros((batch_size, height, width, 1))
  32. X2 = np.zeros((batch_size, height, width, 1))
  33. Y = np.zeros((batch_size, 2))
  34. for j in range(batch_size):
  35. # 生成标注数据
  36. img1, img2, label = paths[i][:-1].split("\t")
  37. # print(img1, img2, label)
  38. img1 = cv2.imread(data_path + img1)
  39. img1 = pil_resize(img1, shape[0], shape[1])
  40. img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  41. img1 = np.expand_dims(img1, axis=-1)
  42. img2 = cv2.imread(data_path + img2)
  43. img2 = pil_resize(img2, shape[0], shape[1])
  44. img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  45. img2 = np.expand_dims(img2, axis=-1)
  46. if label == "1":
  47. label = np.array([0, 1])
  48. else:
  49. label = np.array([1, 0])
  50. X1[j] = img1
  51. X2[j] = img2
  52. Y[j] = label
  53. yield {"input_1": X1, "input_2": X2}, {"output": Y}
  54. def gen_mobile(paths, batch_size=32, shape=(40, 40), cls_num=5710, data_path="click"):
  55. num = len(paths)
  56. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/" + data_path + "/"
  57. i = 0
  58. random.shuffle(paths)
  59. while True:
  60. if i >= num:
  61. i = 0
  62. random.shuffle(paths)
  63. height, width = shape[:2]
  64. if len(shape) > 2:
  65. channel = 3
  66. else:
  67. channel = 1
  68. X = np.zeros((batch_size, height, width, channel))
  69. Y = np.zeros((batch_size, cls_num))
  70. j = 0
  71. error_num = 0
  72. while j < batch_size:
  73. # for j in range(batch_size):
  74. if i >= num:
  75. random.shuffle(paths)
  76. i = 0
  77. path = paths[i].split(os.sep)[-1]
  78. char_index = int(path.split("_")[0])
  79. label = np.zeros(cls_num)
  80. # print("char_index", char_index)
  81. label[char_index] = 1
  82. # print("label", np.argmax(label), char_index)
  83. img1 = cv2.imread(data_path + path)
  84. img1 = pil_resize(img1, shape[0], shape[1])
  85. img1 = img1 / 255.
  86. # img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  87. # img1 = np.expand_dims(img1, axis=-1)
  88. X[j] = img1
  89. Y[j] = label
  90. i += 1
  91. j += 1
  92. # print("error_num", error_num)
  93. yield X, Y
  94. def gen_yolo_char(paths, batch_size, input_shape, anchors, num_classes, box_num=6):
  95. """data generator for fit_generator"""
  96. n = len(paths)
  97. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/detect/"
  98. i = 0
  99. while True:
  100. image_data = []
  101. box_data = []
  102. batch_cnt = 0
  103. while batch_cnt < batch_size:
  104. try:
  105. if i == 0:
  106. np.random.shuffle(paths)
  107. ss = paths[i][:-1].split(" ")
  108. image_path = ss[0]
  109. image = cv2.imread(data_path+image_path)
  110. image = pil_resize(image, input_shape[0], input_shape[1])
  111. image_show = copy.deepcopy(image)
  112. image = image / 255.
  113. box = np.array([np.array(list(map(int, box.split(',')))) for box in ss[1:]])
  114. # box数不同,复制
  115. if box.shape[0] < box_num:
  116. box = np.concatenate([box, box[:2, :]], axis=0)
  117. # show
  118. # box_show = box.tolist()
  119. # for b in box_show:
  120. # print("box", b)
  121. # cv2.rectangle(image_show, (b[0], b[1]), (b[2], b[3]), (255, 0, 0), 2)
  122. # cv2.imshow("image_show", image_show)
  123. # cv2.waitKey(0)
  124. image_data.append(image)
  125. box_data.append(box)
  126. i = (i+1) % n
  127. batch_cnt += 1
  128. except:
  129. i = (i+1) % n
  130. continue
  131. # print
  132. # print(image.shape)
  133. # image_show = (image*255).astype(np.uint8)
  134. # print("annotation_lines[i]", annotation_lines[i])
  135. # for _b in box:
  136. # print(_b)
  137. # cv2.rectangle(image_show, (int(_b[0]), int(_b[1])), (int(_b[2]), int(_b[3])), (0, 255, 0), 1)
  138. # cv2.imshow("image", image_show)
  139. # cv2.waitKey(0)
  140. image_data = np.array(image_data)
  141. box_data = np.array(box_data)
  142. # print(image_data.shape, box_data.shape)
  143. y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
  144. yield [image_data, *y_true], np.zeros(batch_size)
  145. def gen_yolo_puzzle(paths, batch_size, input_shape, anchors, num_classes, box_num=1):
  146. """data generator for fit_generator"""
  147. n = len(paths)
  148. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/detect2/"
  149. i = 0
  150. while True:
  151. image_data = []
  152. box_data = []
  153. batch_cnt = 0
  154. while batch_cnt < batch_size:
  155. try:
  156. if i == 0:
  157. np.random.shuffle(paths)
  158. ss = paths[i][:-1].split(" ")
  159. image_path = ss[0]
  160. image = cv2.imread(data_path+image_path)
  161. image = pil_resize(image, input_shape[0], input_shape[1])
  162. image_show = copy.deepcopy(image)
  163. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  164. image = 255. - image
  165. image = np.uint8(image)
  166. # cv2.imshow("image", image)
  167. # cv2.waitKey(0)
  168. image = np.expand_dims(image, -1)
  169. image = image / 255.
  170. box = np.array([np.array(list(map(int, box.split(',')))) for box in ss[1:]])
  171. # box数不同,复制
  172. if box.shape[0] < box_num:
  173. box = np.concatenate([box, box[:2, :]], axis=0)
  174. # show
  175. # box_show = box.tolist()
  176. # for b in box_show:
  177. # print("box", b)
  178. # cv2.rectangle(image_show, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2)
  179. # cv2.imshow("image_show", image_show)
  180. # cv2.waitKey(0)
  181. image_data.append(image)
  182. box_data.append(box)
  183. i = (i+1) % n
  184. batch_cnt += 1
  185. except:
  186. i = (i+1) % n
  187. continue
  188. # print
  189. # print(image.shape)
  190. # image_show = (image*255).astype(np.uint8)
  191. # for _b in box:
  192. # print(_b)
  193. # cv2.rectangle(image_show, (int(_b[0]), int(_b[1])), (int(_b[2]), int(_b[3])), (0, 255, 0), 1)
  194. # cv2.imshow("image", image_show)
  195. # cv2.waitKey(0)
  196. image_data = np.array(image_data)
  197. box_data = np.array(box_data)
  198. # print(image_data.shape, box_data.shape)
  199. y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
  200. yield [image_data, *y_true], np.zeros(batch_size)
  201. def gen_drag(paths, batch_size=32, shape=(128, 256), cls_num=2):
  202. num = len(paths)
  203. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/drag/"
  204. map_path = data_path+"map.txt"
  205. with open(map_path, "r") as f:
  206. _list = f.readlines()
  207. map_dict = {}
  208. for s in _list:
  209. ss = s[:-1].split(" ")
  210. map_dict[ss[0]] = ss[1]
  211. i = 0
  212. random.shuffle(paths)
  213. while True:
  214. if i >= num:
  215. i = 0
  216. random.shuffle(paths)
  217. height, width = shape[:2]
  218. if len(shape) > 2:
  219. channel = 3
  220. else:
  221. channel = 1
  222. X = np.zeros((batch_size, height, width, channel))
  223. Y = np.zeros((batch_size, height, width, 1))
  224. for j in range(batch_size):
  225. if i >= num:
  226. random.shuffle(paths)
  227. i = 0
  228. path = paths[i].split(os.sep)[-1]
  229. w_index = int(map_dict.get(path))
  230. # label = np.zeros(cls_num)
  231. # print("char_index", char_index)
  232. # label[w_index] = 1
  233. # print("label", np.argmax(label), char_index)
  234. img1 = cv2.imread(data_path + path)
  235. img1 = pil_resize(img1, shape[0], shape[1])
  236. # cv2.imshow("image", img1)
  237. label = np.full((shape[0], shape[1], 1), 0, dtype='uint8')
  238. label[:, w_index, 0] = 1
  239. # label[:, w_index, 1] = 1
  240. # cv2.imshow("label", np.expand_dims(label[..., 0], -1))
  241. # cv2.waitKey(0)
  242. img1 = img1 / 255.
  243. # img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  244. # img1 = np.expand_dims(img1, axis=-1)
  245. i += 1
  246. X[j] = img1
  247. Y[j] = label
  248. yield X, Y
  249. def gen_phrase(map_list, batch_size=32, shape=(5707, 3)):
  250. voc_dim, timesteps = shape[:2]
  251. data_list = []
  252. for line in map_list:
  253. data_list.append([eval(line[:-3]), int(line[-2:-1])])
  254. num = len(data_list)
  255. i = 0
  256. random.shuffle(data_list)
  257. while True:
  258. X = np.zeros((batch_size, timesteps))
  259. Y = np.zeros((batch_size, 1))
  260. for j in range(batch_size):
  261. if i >= num:
  262. random.shuffle(data_list)
  263. i = 0
  264. data = data_list[i]
  265. d_list = [x for x in data[0]]
  266. d_list = d_list + [voc_dim]*(timesteps-len(d_list))
  267. X[j] = np.array(d_list)
  268. Y[j] = data[1]
  269. i += 1
  270. yield X, Y
  271. def generate_data_siamese(char_num=6, char_shape=(40, 40)):
  272. bg_paths = glob("../data/base/*")
  273. char_path = "../data/chinese_2500.txt"
  274. with open(char_path, "r") as f:
  275. char_str = f.read()
  276. data_dir = "../data/click/"
  277. for i in range(1000):
  278. bg = cv2.imread(random.sample(bg_paths, 1)[0])
  279. char_list = [char_str[x] for x in random.sample(range(len(char_str)), char_num)]
  280. image_np, position_list, tips_image_list = char_on_image(bg, char_list, char_shape)
  281. # for j in range(len(char_list)):
  282. # char = char_list[j]
  283. # p = position_list[j]
  284. # print(char)
  285. # cv2.rectangle(image_np, [p[1], p[0]], [p[1]+char_shape[1], p[0]+char_shape[0]], (255, 0, 0), 2)
  286. # cv2.imshow("generate_data", image_np)
  287. # cv2.waitKey(0)
  288. # 保存tips图片
  289. tips_path_list = []
  290. for k in range(len(tips_image_list)):
  291. tips_image = tips_image_list[k]
  292. tips_path = str(i) + "_" + str(k) + ".jpg"
  293. tips_path_list.append(tips_path)
  294. cv2.imwrite(data_dir+tips_path, tips_image)
  295. # 保存文字区域图片
  296. char_path_list = []
  297. for j in range(len(char_list)):
  298. p = position_list[j]
  299. char_path = str(i) + "_" + str(len(tips_image_list)+j) + ".jpg"
  300. char_path_list.append(char_path)
  301. cv2.imwrite(data_dir+char_path, image_np[p[0]:p[0]+char_shape[0], p[1]:p[1]+char_shape[1], :])
  302. # 生成映射数据
  303. with open("../data/click/map.txt", "a") as f:
  304. for j in range(len(tips_path_list)):
  305. tips_path = tips_path_list[j]
  306. for k in range(len(char_path_list)):
  307. char_path = char_path_list[k]
  308. if j == k:
  309. f.write(tips_path + "\t" + char_path + "\t" + str(1) + "\n")
  310. else:
  311. f.write(tips_path + "\t" + char_path + "\t" + str(0) + "\n")
  312. def generate_data_mobile(char_num=6, char_shape=(40, 40), image_shape=(160, 260)):
  313. # (40,40) (160, 260)
  314. # (80,80) (360, 590)
  315. bg_paths = glob("../data/base/*")
  316. char_path = "../data/chinese_5710.txt"
  317. with open(char_path, "r") as f:
  318. char_str = f.read()
  319. data_dir = "../data/click/"
  320. # 每个字生成多张图片
  321. for i in range(0, len(char_str)):
  322. if i % 100 == 0:
  323. print("Loop", i)
  324. char = char_str[i]
  325. # 生成带背景图数
  326. image_cnt = 1
  327. char_list = [char] * image_cnt
  328. tips_cnt = 0
  329. image_cnt = 0
  330. tips_list = []
  331. for l in range(50):
  332. # 背景图
  333. bg = cv2.imread(random.sample(bg_paths, 1)[0])
  334. if random.choice([0, 0, 1]):
  335. bg = distort_image(bg)
  336. if random.choice([0, 0, 1]):
  337. bg = flip_image(bg)
  338. # 生成4张tips图,6张带背景的旋转图
  339. image_np, p_list, t_list = char_on_image(bg, char_list, char_shape, image_shape)
  340. tips_list += t_list
  341. for p in p_list:
  342. char_path = str(i) + "_" + str(image_cnt) + "_2" + ".jpg"
  343. cv2.imwrite(data_dir+char_path, image_np[p[0]:p[0]+char_shape[0], p[1]:p[1]+char_shape[1], :])
  344. image_cnt += 1
  345. for tips_image in tips_list[:4]:
  346. tips_path = str(i) + "_" + str(tips_cnt) + "_1" + ".jpg"
  347. cv2.imwrite(data_dir+tips_path, tips_image)
  348. tips_cnt += 1
  349. def generate_data_yolo_char(char_num=6, char_shape=(40, 40), image_shape=(160, 256)):
  350. bg_paths = glob("../data/base/*")
  351. char_path = "../data/chinese_5710.txt"
  352. with open(char_path, "r") as f:
  353. char_str = f.read()
  354. data_dir = "../data/detect/"
  355. # with open(data_dir+"map.txt", "w") as f:
  356. # f.write("")
  357. for i in range(40000, 60000):
  358. if i % 1000 == 0:
  359. print("Loop", i)
  360. bg = cv2.imread(random.sample(bg_paths, 1)[0])
  361. if random.choice([0, 0, 1]):
  362. bg = distort_image(bg)
  363. if random.choice([0, 0, 1]):
  364. bg = flip_image(bg)
  365. char_list = [char_str[x] for x in random.sample(range(len(char_str)), char_num)]
  366. image_np, position_list, tips_image_list = char_on_image(bg, char_list, char_shape, image_shape, 4)
  367. if i < 5000:
  368. tips_image_np, tips_position_list = get_tips_image(tips_image_list, char_shape, image_shape)
  369. image_np_path = str(i) + ".jpg"
  370. cv2.imwrite(data_dir+image_np_path, image_np)
  371. if i < 5000:
  372. tips_image_np_path = str(i) + "_0.jpg"
  373. cv2.imwrite(data_dir+tips_image_np_path, tips_image_np)
  374. # 生成映射数据
  375. with open(data_dir+"map.txt", "a") as f:
  376. box_str = ""
  377. for p in position_list:
  378. box_str += str(p[1]) + "," + str(p[0]) + "," + \
  379. str(p[1]+char_shape[1]) + "," + str(p[0]+char_shape[0]) +\
  380. "," + str(0) + " "
  381. # cv2.rectangle(image_np, (p[1], p[0]), (p[1]+char_shape[1], p[0]+char_shape[0]), (255, 0, 0), 2)
  382. # cv2.imshow("image_np", image_np)
  383. box_str = box_str[:-1]
  384. f.write(image_np_path + " " + box_str + "\n")
  385. if i < 5000:
  386. box_str = ""
  387. for p in tips_position_list:
  388. box_str += str(p[1]) + "," + str(p[0]) + "," + \
  389. str(p[1]+char_shape[1]) + "," + str(p[0]+char_shape[0]) + \
  390. "," + str(0) + " "
  391. # cv2.rectangle(tips_image_np, (p[1], p[0]), (p[1]+char_shape[1], p[0]+char_shape[0]), (255, 0, 0), 2)
  392. # cv2.imshow("tips_image_np", tips_image_np)
  393. # cv2.waitKey(0)
  394. box_str = box_str[:-1]
  395. f.write(tips_image_np_path + " " + box_str + "\n")
  396. def generate_data_yolo_puzzle(image_shape=(160, 256)):
  397. bg_paths = glob("../data/base/*.jpeg")
  398. data_dir = "../data/detect2/"
  399. with open(data_dir+"map.txt", "w") as f:
  400. f.write("")
  401. for i in range(0, 10000):
  402. if i % 1000 == 0:
  403. print("Loop", i)
  404. bg = cv2.imread(random.sample(bg_paths, 1)[0])
  405. if random.choice([0, 0, 1]):
  406. bg = distort_image(bg)
  407. if random.choice([0, 0, 1]):
  408. bg = flip_image(bg)
  409. r = random.randint(35, 60)
  410. puzzle_shape = (r, r)
  411. image_np, position_list = puzzle_on_image(bg, puzzle_shape, image_shape)
  412. image_np_path = str(i) + ".jpg"
  413. cv2.imwrite(data_dir+image_np_path, image_np)
  414. # 生成映射数据
  415. with open(data_dir+"map.txt", "a") as f:
  416. box_str = ""
  417. for p in position_list:
  418. box_str += str(p[1]) + "," + str(p[0]) + "," + \
  419. str(p[1]+puzzle_shape[1]) + "," + str(p[0]+puzzle_shape[0]) + \
  420. "," + str(0) + " "
  421. # cv2.rectangle(image_np, (p[1], p[0]), (p[1]+puzzle_shape[1], p[0]+puzzle_shape[0]), (255, 0, 0), 2)
  422. # cv2.imshow("image_np", image_np)
  423. # cv2.waitKey(0)
  424. box_str = box_str[:-1]
  425. f.write(image_np_path + " " + box_str + "\n")
  426. def generate_data_drag_image(image_shape=(160, 260)):
  427. bg_paths = glob("../data/base/*")
  428. data_dir = "../data/drag/"
  429. with open(data_dir+"map.txt", "w") as f:
  430. f.write("")
  431. for i in range(10000):
  432. if i % 1000 == 0:
  433. print("Loop", i)
  434. bg = cv2.imread(random.sample(bg_paths, 1)[0])
  435. bg = pil_resize(bg, image_shape[0], image_shape[1])
  436. if random.choice([0, 0, 1]):
  437. bg = distort_image(bg)
  438. if random.choice([0, 0, 1]):
  439. bg = flip_image(bg)
  440. image_np, clip_line = get_drag_image(bg)
  441. image_np_path = str(i) + ".jpg"
  442. cv2.imwrite(data_dir+image_np_path, image_np)
  443. # 生成映射数据
  444. with open(data_dir+"map.txt", "a") as f:
  445. f.write(image_np_path + " " + str(clip_line[0][0]) + "\n")
  446. def generate_data_phrase():
  447. data_path = os.path.dirname(os.path.abspath(__file__)) + "/../data/phrase/"
  448. char_path = data_path+"char.txt"
  449. with open(char_path, "r") as f:
  450. char_list = f.readlines()
  451. char_dict = {}
  452. for i in range(len(char_list)):
  453. char_dict[char_list[i]] = i
  454. phrase_list = []
  455. phrase_path = data_path+"phrase3.txt"
  456. with open(phrase_path, "r") as f:
  457. phrase_list += f.readlines()
  458. phrase_path = data_path+"phrase4.txt"
  459. with open(phrase_path, "r") as f:
  460. phrase_list += f.readlines()
  461. phrase_path = data_path+"phrase5.txt"
  462. with open(phrase_path, "r") as f:
  463. phrase_list += f.readlines()
  464. phrase_set = set(phrase_list)
  465. map_path = data_path+"map3.txt"
  466. with open(map_path, "w") as f:
  467. f.write("")
  468. data_list = []
  469. start_time = time.time()
  470. i = 0
  471. negative_way_flag = False
  472. for phrase in phrase_list:
  473. if i % 500000 == 0:
  474. with open(map_path, "a") as f:
  475. f.writelines(data_list)
  476. data_list = []
  477. print("Loop", i, len(phrase_list), time.time()-start_time)
  478. start_time = time.time()
  479. i += 1
  480. # 正样本
  481. index_list = []
  482. for char in phrase[:-1]:
  483. index_list.append(char_dict.get(char+"\n"))
  484. data_list.append(str(index_list) + " 1\n")
  485. # 负样本
  486. if negative_way_flag:
  487. index1 = random.randint(0, len(index_list)-1)
  488. find_flag = False
  489. while not find_flag:
  490. index2 = random.randint(0, len(index_list)-1)
  491. if index1 != index2:
  492. find_flag = True
  493. temp = index_list[index1]
  494. index_list[index1] = index_list[index2]
  495. index_list[index2] = temp
  496. if "".join([char_list[x][:-1] for x in index_list]) + "\n" not in phrase_set:
  497. data_list.append(str(index_list) + " 0\n")
  498. else:
  499. products = list(product(index_list, repeat=len(index_list)))
  500. random.shuffle(products)
  501. negative_cnt = 0
  502. for p in products:
  503. if negative_cnt >= 2:
  504. break
  505. p = list(p)
  506. if len(set(p)) != len(p):
  507. continue
  508. if p != index_list and "".join([char_list[x][:-1] for x in p]) + "\n" not in phrase_set:
  509. data_list.append(str(p) + " 0\n")
  510. negative_cnt += 1
  511. with open(map_path, "a") as f:
  512. f.writelines(data_list)
  513. def generate_data_phrase_raw(word_len=5):
  514. paths = glob("D:/Chinese_corpus/answer/*/*.txt")
  515. phrase_path = "../data/phrase/phrase" + str(word_len) + "_new.txt"
  516. triple_list = []
  517. reg = "[^\u4e00-\u9fa5]"
  518. start_time = time.time()
  519. for i in range(len(paths)):
  520. if i % 1000 == 0:
  521. with open(phrase_path, "w") as f:
  522. f.writelines(triple_list)
  523. print("Loop", i, len(paths), time.time()-start_time)
  524. start_time = time.time()
  525. triple_list = []
  526. with open(paths[i], "rb") as f:
  527. _b = f.read()
  528. try:
  529. text = _b.decode("gbk")
  530. except:
  531. try:
  532. text = _b.decode("gb2312")
  533. except:
  534. try:
  535. text = _b.decode("gb18030")
  536. except:
  537. print(chardet.detect(_b), "is None")
  538. filter_word = ["的"]
  539. for word in filter_word:
  540. text = re.sub(word, "#"*len(word), text)
  541. word_list = jieba.lcut(text, cut_all=False, HMM=True)
  542. for j in range(1, len(word_list)):
  543. current = word_list[j]
  544. current_re = re.search(reg, current)
  545. last = word_list[j-1]
  546. last_re = re.search(reg, last)
  547. if current_re:
  548. continue
  549. if len(current) == word_len:
  550. triple_list.append(current + "\n")
  551. elif len(current) + len(last) == word_len and not last_re:
  552. triple_list.append(last+current + "\n")
  553. triple_list = list(set(triple_list))
  554. print("len(triple_list)", len(triple_list))
  555. with open(phrase_path, "w") as f:
  556. f.writelines(triple_list)
  557. def char_on_image(image_np, char_list, char_shape, image_shape, tip_char_num=1):
  558. position_list = []
  559. for char in char_list:
  560. # 获取单字图片
  561. char_image_pil = get_char_image(char, char_shape)
  562. image_np = pil_resize(image_np, image_shape[0], image_shape[1])
  563. # h, w
  564. fg_w, fg_h = char_image_pil.size[:2]
  565. bg_h, bg_w = image_np.shape[:2]
  566. # 字体放置的位置,且位置不重叠
  567. find_flag = 0
  568. while not find_flag:
  569. position_h = random.randint(0, bg_h-fg_h)
  570. position_w = random.randint(0, bg_w-fg_w)
  571. if len(position_list) < 1:
  572. find_flag = 1
  573. break
  574. for p in position_list:
  575. if get_iou(position_w, position_h, position_w+fg_w, position_h+fg_h,
  576. p[1], p[0], p[1]+fg_w, p[0]+fg_h) > 0:
  577. find_flag = 0
  578. break
  579. else:
  580. find_flag = 1
  581. position_list.append([position_h, position_w])
  582. # 字体添加到背景图上
  583. # image_np = get_image_roi(image_np, char_image_np, position_h, position_w)
  584. image_np = get_image_paste(image_np, char_image_pil, position_h, position_w)
  585. # 生成提示图片
  586. image_list = []
  587. for char in char_list[:tip_char_num]:
  588. char_image_pil = get_char_image(char, char_shape, rotate=False, bg_color=(255, 255, 255, 255))
  589. char_image_np = pil2np_a(char_image_pil)
  590. # char_image_np = pil_resize(char_image_np, char_shape[0], char_shape[1])
  591. image_list.append(char_image_np)
  592. tips_image_np = np.concatenate(image_list, axis=1)
  593. # 加干扰
  594. tips_image_np = create_noise(tips_image_np)
  595. # 切割
  596. image_list = []
  597. for i in range(tip_char_num):
  598. image_list.append(tips_image_np[:, i*char_shape[1]:(i+1)*char_shape[1], :])
  599. return image_np, position_list, image_list
  600. def get_char_image(char, char_shape, rotate=True, bg_color=(0, 0, 0, 0)):
  601. # 创建空图
  602. image_pil = Image.new('RGBA', (80, 80), bg_color)
  603. # 空图上写字
  604. # font_size = 35 # (40, 40)
  605. font_size = 75 # (80, 80)
  606. font_type_list = glob("../font/*")
  607. font_type = random.sample(font_type_list, 1)[0]
  608. font_config = ImageFont.truetype(font_type, int(font_size))
  609. dr = ImageDraw.Draw(image_pil)
  610. fill_color = random_color()
  611. fill_color = (fill_color[0], fill_color[1], fill_color[2])
  612. dr.text((3, -6), char, font=font_config, fill=fill_color)
  613. if rotate:
  614. if random.choice([0, 1]):
  615. angle = random.randint(0, 80)
  616. else:
  617. angle = random.randint(280, 360)
  618. image_pil = image_pil.rotate(angle, expand=False, fillcolor=bg_color)
  619. # image_pil.show("1")
  620. image_pil = image_pil.resize(char_shape)
  621. # cv2.imshow("get_char_image", pil2np(image_pil))
  622. # cv2.waitKey(0)
  623. return image_pil
  624. def get_tips_image(tips_image_list, char_shape, image_shape, roatate=True):
  625. new_list = []
  626. for img in tips_image_list:
  627. if random.choice([0, 0, 1]):
  628. angle = random.randint(0, 360)
  629. img = pil_rotate(img, angle, (255, 255, 255))
  630. new_list.append(img)
  631. tips_image_np = np.concatenate(new_list, axis=1)
  632. new_image = np.full((image_shape[0], image_shape[1], 3), 0, np.uint8)
  633. new_image[:tips_image_np.shape[0], :tips_image_np.shape[1], :] = tips_image_np
  634. position_list = []
  635. for i in range(len(new_list)):
  636. h = 0
  637. w = i*char_shape[1]
  638. position_list.append([h, w])
  639. return new_image, position_list
  640. def get_image_roi(image_bg, image_fg, roi_h, roi_w):
  641. # h, w
  642. fg_h, fg_w = image_fg.shape[:2]
  643. bg_h, bg_w = image_bg.shape[:2]
  644. # roi取值范围
  645. roi = image_bg[roi_h:roi_h+fg_h, roi_w:roi_w+fg_w]
  646. # 获取bg中非fg字体部分的掩码,相当于排除fg的字体部分,只保留bg的除fg字体外的部分
  647. img_fg_gray = cv2.cvtColor(image_fg, cv2.COLOR_BGR2GRAY)
  648. ret, mask = cv2.threshold(img_fg_gray, 0, 255, cv2.THRESH_OTSU)
  649. bg_roi = cv2.bitwise_and(roi, roi, mask=mask)
  650. # 获取fg中字体部分的掩码,相当于排除fg中的白色背景,只保留fg的字体部分
  651. mask_inv = cv2.bitwise_not(mask)
  652. fg_roi = cv2.bitwise_and(image_fg, image_fg, mask=mask_inv)
  653. # 膨胀腐蚀去掉白色颗粒
  654. # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  655. # fg_roi = cv2.erode(fg_roi, kernel)
  656. # fg_roi = cv2.dilate(fg_roi, kernel)
  657. # bg的除字体外背景部分 + fg的字体部分
  658. image_roi = cv2.add(bg_roi, fg_roi)
  659. # 将roi部分放回原bg
  660. image_bg[roi_h:roi_h+fg_h, roi_w:roi_w+fg_w, :] = image_roi
  661. # cv2.imshow("image_fg", image_fg)
  662. # cv2.imshow("get_image_roi", image_bg)
  663. # cv2.waitKey(0)
  664. return image_bg
  665. def get_image_paste(image_bg, image_fg, roi_h, roi_w):
  666. fg_h, fg_w = image_fg.size[:2]
  667. image_bg = cv2.cvtColor(image_bg, cv2.COLOR_BGR2BGRA)
  668. image_bg = np2pil_a(image_bg)
  669. # image_fg = np2pil_a(image_fg)
  670. image_bg.paste(image_fg, (roi_w, roi_h), image_fg)
  671. image_bg = pil2np(image_bg)
  672. # cv2.imshow("get_image_paste", image_bg)
  673. # cv2.waitKey(0)
  674. return image_bg
  675. def random_color(dims=3):
  676. color = [0]*dims
  677. find_flag = 0
  678. while not find_flag:
  679. for dim in range(dims):
  680. color[dim] = random.randint(0, 255)
  681. if color[dim] <= 125:
  682. find_flag = 1
  683. # RGB
  684. # color_list = [
  685. # [207, 91, 85],
  686. # [0, 201, 88],
  687. # [117, 74, 57],
  688. # [210, 210, 27],
  689. # [160, 157, 152],
  690. # [181, 210, 210],
  691. # [27, 112, 107],
  692. # [87, 26, 44],
  693. # [115, 19, 20],
  694. # [161, 210, 68],
  695. # [210, 108, 12],
  696. # [112, 9, 142],
  697. # [50, 41, 84],
  698. # [72, 52, 210],
  699. # [210, 177, 89],
  700. # [148, 200, 89],
  701. # [173, 116, 109],
  702. # [185, 185, 210],
  703. # [181, 7, 210],
  704. # [80, 210, 30],
  705. # [65, 72, 98],
  706. # [210, 123, 109],
  707. # [19, 64, 95],
  708. # [128, 21, 210],
  709. # [129, 137, 60]
  710. # ]
  711. # color = random.sample(color_list, 1)[0]
  712. return tuple(color)
  713. def create_noise(image_np):
  714. ic = ImageCaptcha()
  715. image_pil = np2pil(image_np)
  716. image_pil = ic.create_noise_curve(image_pil, random_color())
  717. image_pil = ic.create_noise_curve(image_pil, random_color())
  718. image_pil = ic.create_noise_dots(image_pil, random_color())
  719. image_np = pil2np(image_pil)
  720. return image_np
  721. def get_iou(x1, y1, x2, y2, a1, b1, a2, b2):
  722. # 相交区域左上角横坐标
  723. ax = max(x1, a1)
  724. # 相交区域左上角纵坐标
  725. ay = max(y1, b1)
  726. # 相交区域右下角横坐标
  727. bx = min(x2, a2)
  728. # 相交区域右下角纵坐标
  729. by = min(y2, b2)
  730. area_n = (x2 - x1) * (y2 - y1)
  731. area_m = (a2 - a1) * (b2 - b1)
  732. w = max(0, bx - ax)
  733. h = max(0, by - ay)
  734. area_x = w * h
  735. return area_x / (area_n + area_m - area_x)
  736. def preprocess_true_boxes(true_boxes, input_shape, anchors, num_classes):
  737. """Preprocess true boxes to training input format
  738. Parameters
  739. ----------
  740. true_boxes: array, shape=(m, T, 5)
  741. Absolute x_min, y_min, x_max, y_max, class_id relative to input_shape.
  742. input_shape: array-like, hw, multiples of 32
  743. anchors: array, shape=(N, 2), wh
  744. num_classes: integer
  745. Returns
  746. -------
  747. y_true: list of array, shape like yolo_outputs, xywh are reletive value
  748. """
  749. # print(true_boxes[..., 4])
  750. # print(num_classes)
  751. assert (true_boxes[..., 4] < num_classes).all(), 'class id must be less than num_classes'
  752. # default setting
  753. num_layers = len(anchors)//3
  754. anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[3, 4, 5], [1, 2, 3]]
  755. true_boxes = np.array(true_boxes, dtype='float32')
  756. input_shape = np.array(input_shape, dtype='int32')
  757. boxes_xy = (true_boxes[..., 0:2] + true_boxes[..., 2:4]) // 2
  758. boxes_wh = true_boxes[..., 2:4] - true_boxes[..., 0:2]
  759. true_boxes[..., 0:2] = boxes_xy/input_shape[::-1]
  760. true_boxes[..., 2:4] = boxes_wh/input_shape[::-1]
  761. m = true_boxes.shape[0]
  762. grid_shapes = [input_shape//{0: 32, 1: 16, 2: 8}[l] for l in range(num_layers)]
  763. y_true = [np.zeros((m, grid_shapes[l][0], grid_shapes[l][1],len(anchor_mask[l]), 5+num_classes),
  764. dtype='float32') for l in range(num_layers)]
  765. # Expand dim to apply broadcasting.
  766. anchors = np.expand_dims(anchors, 0)
  767. anchor_maxes = anchors / 2.
  768. anchor_mins = -anchor_maxes
  769. valid_mask = boxes_wh[..., 0] > 0
  770. for b in range(m):
  771. # Discard zero rows.
  772. wh = boxes_wh[b, valid_mask[b]]
  773. if len(wh) == 0:
  774. continue
  775. # Expand dim to apply broadcasting.
  776. wh = np.expand_dims(wh, -2)
  777. box_maxes = wh / 2.
  778. box_mins = -box_maxes
  779. intersect_mins = np.maximum(box_mins, anchor_mins)
  780. intersect_maxes = np.minimum(box_maxes, anchor_maxes)
  781. intersect_wh = np.maximum(intersect_maxes - intersect_mins, 0.)
  782. intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]
  783. box_area = wh[..., 0] * wh[..., 1]
  784. anchor_area = anchors[..., 0] * anchors[..., 1]
  785. iou = intersect_area / (box_area + anchor_area - intersect_area)
  786. # Find best anchor for each true box
  787. best_anchor = np.argmax(iou, axis=-1)
  788. for t, n in enumerate(best_anchor):
  789. for l in range(num_layers):
  790. if n in anchor_mask[l]:
  791. i = np.floor(true_boxes[b,t,0]*grid_shapes[l][1]).astype('int32')
  792. j = np.floor(true_boxes[b,t,1]*grid_shapes[l][0]).astype('int32')
  793. k = anchor_mask[l].index(n)
  794. c = true_boxes[b, t, 4].astype('int32')
  795. y_true[l][b, j, i, k, 0:4] = true_boxes[b, t, 0:4]
  796. y_true[l][b, j, i, k, 4] = 1
  797. y_true[l][b, j, i, k, 5+c] = 1
  798. return y_true
  799. def get_puzzle(shape=(80, 80)):
  800. # 创建空图
  801. image_pil = Image.new('RGBA', (shape[1], shape[0]), (255, 255, 255, 0))
  802. draw = ImageDraw.Draw(image_pil)
  803. # 居中创建矩形
  804. rec_shape = (40, 40)
  805. left_up_point = [int((shape[0]-rec_shape[0])/2), int((shape[1]-rec_shape[1])/2)]
  806. right_down_point = [left_up_point[0]+rec_shape[0], left_up_point[1]+rec_shape[1]]
  807. # 透明度
  808. flag = random.choice([0, 1])
  809. if flag:
  810. alpha = random.randint(100, 150)
  811. else:
  812. alpha = random.randint(160, 255)
  813. # 背景色
  814. if flag:
  815. r = random.randint(0, 30)
  816. else:
  817. r = random.randint(100, 180)
  818. # r = random.randint(0, 255)
  819. fill_color = (r, r, r, alpha)
  820. # 边缘色
  821. if random.choice([0, 1, 1]):
  822. if flag:
  823. r = random.randint(140, 170)
  824. else:
  825. r = random.randint(70, 100)
  826. outline_color = (r, r, r)
  827. else:
  828. outline_color = (fill_color[0], fill_color[1], fill_color[2])
  829. draw.rectangle((left_up_point[1],
  830. left_up_point[0],
  831. left_up_point[1]+rec_shape[1],
  832. left_up_point[0]+rec_shape[0]),
  833. fill=fill_color,
  834. outline=outline_color)
  835. # 拼图的圆或半圆
  836. radius = random.randint(int(rec_shape[0] / 3 / 2), int(rec_shape[0] / 3 / 1.2))
  837. center_list = [[left_up_point[1], int((right_down_point[0]+left_up_point[0])/2), 1],
  838. [right_down_point[1], int((right_down_point[0]+left_up_point[0])/2), 1],
  839. [int((right_down_point[1]+left_up_point[1])/2), left_up_point[0], 0],
  840. [int((right_down_point[1]+left_up_point[1])/2), right_down_point[0], 0]
  841. ]
  842. circle_num = random.randint(1, 4)
  843. # print("circle_num", circle_num)
  844. center_list = random.sample(center_list, circle_num)
  845. min_w, min_h = left_up_point[1], left_up_point[0]
  846. max_w, max_h = right_down_point[1], right_down_point[0]
  847. for center in center_list:
  848. w, h = center[:2]
  849. is_width = center[2]
  850. # 判断长宽
  851. into_ratio = random.randint(int(1/2*radius), int(3/4*radius))
  852. if is_width:
  853. # 挑选圆是凸还是凹进去
  854. if random.choice([0, 1]):
  855. center = (center[0]+into_ratio, center[1])
  856. else:
  857. center = (center[0]-into_ratio, center[1])
  858. else:
  859. if random.choice([0, 1]):
  860. center = (center[0], center[1]+into_ratio)
  861. else:
  862. center = (center[0], center[1]-into_ratio)
  863. # 判断透明度
  864. color = fill_color
  865. if is_width:
  866. if left_up_point[1] <= center[0] <= right_down_point[1]:
  867. color = (0, 0, 0, 0)
  868. else:
  869. if left_up_point[0] <= center[1] <= right_down_point[0]:
  870. color = (0, 0, 0, 0)
  871. # print("center, color, alpha", center, color, alpha)
  872. draw.ellipse([(center[0]-radius, center[1]-radius),
  873. (center[0]+radius, center[1]+radius)],
  874. fill=color,
  875. outline=outline_color)
  876. # 修补内部圆的边缘颜色
  877. if color[3] == alpha:
  878. if is_width:
  879. if center[0] < w:
  880. draw.rectangle((w,
  881. h-radius,
  882. center[0]+radius,
  883. center[1]+radius),
  884. fill=fill_color)
  885. else:
  886. draw.rectangle((center[0]-radius,
  887. center[1]-radius,
  888. w,
  889. h+radius),
  890. fill=fill_color)
  891. else:
  892. if center[1] < h:
  893. draw.rectangle((w-radius,
  894. h,
  895. center[0]+radius,
  896. center[1]+radius),
  897. fill=fill_color)
  898. else:
  899. draw.rectangle((center[0]-radius,
  900. center[1]-radius,
  901. w+radius,
  902. h),
  903. fill=fill_color)
  904. # 修补外部圆的边缘颜色
  905. else:
  906. if is_width:
  907. if center[0] > w:
  908. draw.rectangle((center[0]-radius,
  909. center[1]-radius,
  910. w,
  911. h+radius),
  912. fill=(0, 0, 0, 0))
  913. else:
  914. draw.rectangle((w,
  915. h-radius,
  916. center[0]+radius,
  917. center[1]+radius),
  918. fill=(0, 0, 0, 0))
  919. else:
  920. if center[1] > h:
  921. draw.rectangle((center[0]-radius,
  922. center[1]-radius,
  923. w+radius,
  924. h),
  925. fill=(0, 0, 0, 0))
  926. else:
  927. draw.rectangle((w-radius,
  928. h,
  929. center[0]+radius,
  930. center[1]+radius),
  931. fill=(0, 0, 0, 0))
  932. # 新增面积
  933. if color[3] == alpha:
  934. if center[0]-radius <= min_w:
  935. min_w = center[0]-radius
  936. if center[0]+radius >= max_w:
  937. max_w = center[0]+radius
  938. if center[1]-radius <= min_h:
  939. min_h = center[1]-radius
  940. if center[1]+radius >= max_h:
  941. max_h = center[1]+radius
  942. image_pil = image_pil.crop([min_w, min_h, max_w+1, max_h+1])
  943. # image_pil.show("2")
  944. return image_pil
  945. def puzzle_on_image(image_np, puzzle_shape, image_shape):
  946. position_list = []
  947. # 获取拼图图片
  948. puzzle_image_pil = get_puzzle()
  949. puzzle_image_pil = puzzle_image_pil.resize(puzzle_shape)
  950. image_np = pil_resize(image_np, image_shape[0], image_shape[1])
  951. # h, w
  952. fg_w, fg_h = puzzle_image_pil.size[:2]
  953. bg_h, bg_w = image_np.shape[:2]
  954. # 拼图放置的位置
  955. position_h = random.randint(0, bg_h-fg_h)
  956. position_w = random.randint(0, bg_w-fg_w)
  957. position_list.append([position_h, position_w])
  958. # for p in position_list:
  959. # cv2.rectangle(image_np, (p[1], p[0]),
  960. # (p[1]+puzzle_shape[1], p[0]+puzzle_shape[0]),
  961. # (0, 0, 255), 1)
  962. # 拼图添加到背景图上
  963. image_np = get_image_paste(image_np, puzzle_image_pil, position_h, position_w)
  964. # cv2.imshow("puzzle_on_image", image_np)
  965. # cv2.waitKey(0)
  966. return image_np, position_list
  967. def distort_image(image_np, hue=.1, sat=1.5, val=1.5):
  968. """
  969. 图像失真
  970. :return:
  971. """
  972. def rand(a=0, b=1):
  973. return np.random.rand()*(b-a) + a
  974. # cv2.imshow("distort_image1", image_np)
  975. hue = rand(-hue, hue)
  976. sat = rand(1, sat) if rand() < .5 else 1/rand(1, sat)
  977. val = rand(1, val) if rand() < .5 else 1/rand(1, val)
  978. image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
  979. x = rgb_to_hsv(image_np/255.)
  980. x[..., 0] += hue
  981. x[..., 0][x[..., 0] > 1] -= 1
  982. x[..., 0][x[..., 0] < 0] += 1
  983. x[..., 1] *= sat
  984. x[..., 2] *= val
  985. x[x > 1] = 1
  986. x[x < 0] = 0
  987. image_np = hsv_to_rgb(x)
  988. image_np = cv2.cvtColor(np.uint8(image_np*255), cv2.COLOR_RGB2BGR)
  989. # cv2.imshow("distort_image2", image_np)
  990. # cv2.waitKey(0)
  991. return image_np
  992. def flip_image(image_np):
  993. # cv2.imshow("flip_image1", image_np)
  994. if random.choice([0, 1]):
  995. # 水平翻转
  996. image_np = cv2.flip(image_np, 1)
  997. else:
  998. # 垂直翻转
  999. image_np = cv2.flip(image_np, 0)
  1000. # cv2.imshow("flip_image2", image_np)
  1001. # cv2.waitKey(0)
  1002. return image_np
  1003. def get_drag_image(image_np):
  1004. h, w = image_np.shape[:2]
  1005. # 取一定高度图片
  1006. clip_h = random.randint(int(1/4*h), int(3/4*h))
  1007. image_clip = image_np[:clip_h, ...]
  1008. # 将图片在一定宽度截断,重新拼接
  1009. clip_w = random.randint(int(1/6*w), int(5/6*w))
  1010. image_w1 = image_clip[:, :clip_w, ...]
  1011. image_w2 = image_clip[:, clip_w:, ...]
  1012. image_new = np.concatenate([image_w2, image_w1], axis=1)
  1013. # 分割线
  1014. clip_line = [(image_w2.shape[1], 0), (image_w2.shape[1], clip_h)]
  1015. # show
  1016. # print(clip_line)
  1017. # cv2.line(image_new, clip_line[0], clip_line[1], (0, 0, 255), 2)
  1018. # cv2.imshow("get_drag_image", image_new)
  1019. # cv2.waitKey(0)
  1020. return image_new, clip_line
  1021. def get_real_data_puzzle(shape=(160, 256)):
  1022. paths = glob("../data/detect2_real/*")
  1023. i = 10000
  1024. for p in paths:
  1025. image = cv2.imread(p)
  1026. image = pil_resize(image, shape[0], shape[1])
  1027. cv2.imwrite("../data/detect2_real/"+str(i)+".jpg", image)
  1028. i += 1
  1029. image = distort_image(image)
  1030. cv2.imwrite("../data/detect2_real/"+str(i)+".jpg", image)
  1031. i += 1
  1032. image = flip_image(image)
  1033. cv2.imwrite("../data/detect2_real/"+str(i)+".jpg", image)
  1034. i += 1
  1035. def read_label_puzzle():
  1036. paths = glob("../data/detect2_real/*.json")
  1037. map_path = "../data/detect2_real/map.txt"
  1038. with open(map_path, "a") as f:
  1039. for p in paths:
  1040. with open(p, "r") as fp:
  1041. _dict = json.loads(fp.read())
  1042. points = _dict.get("shapes")[0].get("points")
  1043. image_path = _dict.get("imagePath")
  1044. ps = [str(int(points[0][0])), str(int(points[0][1])),
  1045. str(int(points[1][0])), str(int(points[1][1]))]
  1046. p_str = ",".join(ps)
  1047. f.write(image_path + " " + p_str + ",0" + "\n")
  1048. def fix_map_txt():
  1049. path = "../data/map.txt"
  1050. with open(path, "r") as f:
  1051. _list = f.readlines()
  1052. with open("../data/map_new.txt", "w") as f:
  1053. new_list = []
  1054. for line in _list:
  1055. ss = line.split(" ")
  1056. ps = ss[-1][:-1].split(",")[:-1]
  1057. if random.choice([0, 1, 1, 1]):
  1058. pix = random.choice([1, 2, 2, 3, 3, 4, 4])
  1059. for i in range(len(ps)):
  1060. if i < 2:
  1061. ps[i] = str(int(ps[i]) - pix)
  1062. else:
  1063. ps[i] = str(int(ps[i]) + pix)
  1064. new_line = ss[0] + " " + ",".join(ps) + ",0\n"
  1065. new_list.append(new_line)
  1066. print("line", line)
  1067. print("new_line", new_line)
  1068. f.writelines(new_list)
  1069. def get_char_map():
  1070. path = "../data/phrase/phrase3.txt"
  1071. with open(path, "r") as f:
  1072. _list = f.readlines()
  1073. path = "../data/phrase/phrase4.txt"
  1074. with open(path, "r") as f:
  1075. _list += f.readlines()
  1076. path = "../data/phrase/phrase5.txt"
  1077. with open(path, "r") as f:
  1078. _list += f.readlines()
  1079. _str = "".join(_list)
  1080. _str = re.sub("\n", "", _str)
  1081. _list = list(set([x+"\n" for x in _str]))
  1082. _list.sort(key=lambda x: x)
  1083. with open("../data/phrase/char.txt", "w") as f:
  1084. f.writelines(_list)
  1085. if __name__ == "__main__":
  1086. # from click_captcha.utils import get_classes, get_anchors
  1087. # annotation_path = '../data/detect/map.txt'
  1088. # log_dir = 'yolo_data/logs/000/'
  1089. # classes_path = 'yolo_data/my_classes.txt'
  1090. # anchors_path = 'yolo_data/tiny_yolo_anchors.txt'
  1091. # class_names = get_classes(classes_path)
  1092. # num_classes = len(class_names)
  1093. # anchors = get_anchors(anchors_path)
  1094. #
  1095. # with open(annotation_path) as f:
  1096. # lines = f.readlines()
  1097. # random.shuffle(lines)
  1098. # input_shape = (160, 256)
  1099. # g = gen_yolo(lines, 10, input_shape, anchors, num_classes)
  1100. # list(g)
  1101. generate_data_phrase()
  1102. # generate_data_yolo_puzzle()
  1103. # gen_yolo_puzzle()
  1104. # _path = "../data/base/0b16d1f1a4e017d4a7ab5779263887f1.jpeg"
  1105. # get_drag_image(cv2.imread(_path))
  1106. # for ii in range(10):
  1107. # im = get_puzzle()
  1108. # with open("../data/chinese.txt", "r") as f:
  1109. # _str = f.read()
  1110. #
  1111. # _list = [c for c in _str]
  1112. # _list = list(set(_list))
  1113. # _str = "".join(_list)
  1114. # with open("../data/chinese.txt", "w") as f:
  1115. # f.write(_str)