image.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 9 23:11:51 2020
  5. image
  6. @author: chineseocr
  7. """
  8. import json
  9. import base64
  10. import random
  11. import numpy as np
  12. import six
  13. import cv2
  14. from PIL import Image
  15. from numpy import cos,sin,pi
  16. from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
  17. def plot_lines(img,lines,linetype=2):
  18. tmp = np.copy(img)
  19. for line in lines:
  20. p1,p2 = line
  21. cv2.line(tmp,(int(p1[0]),int(p1[1])),(int(p2[0]),int(p2[1])),(0,0,0),linetype,lineType=cv2.LINE_AA)
  22. return Image.fromarray(tmp)
  23. def base64_to_PIL(string):
  24. try:
  25. # my own train data
  26. string = bytes(string, 'utf-8')
  27. base64_data = base64.b64decode(string)
  28. # with open('temp.jpg', 'wb') as f:
  29. # f.write(base64_data)
  30. # print("base64_to_PIL")
  31. buf = six.BytesIO()
  32. buf.write(base64_data)
  33. buf.seek(0)
  34. img = Image.open(buf).convert('RGB')
  35. return img
  36. except Exception as e:
  37. print(e)
  38. return None
  39. def read_json(p):
  40. with open(p) as f:
  41. jsonData = json.loads(f.read())
  42. shapes = jsonData.get('shapes')
  43. imageData = jsonData.get('imageData')
  44. lines = []
  45. labels = []
  46. for shape in shapes:
  47. lines.append(shape['points'])
  48. [x0, y0], [x1, y1] = shape['points']
  49. label = shape['label']
  50. if label == '0':
  51. if abs(y1-y0) > 500:
  52. label = '1'
  53. # else:
  54. # print("image read_json y<50", x0, y0, x1, y1, label)
  55. elif label == '1':
  56. if abs(x1-x0) > 500:
  57. label = '0'
  58. # else:
  59. # print("image read_json x<50", x0, y0, x1, y1, label)
  60. labels.append(label)
  61. img = base64_to_PIL(imageData)
  62. return img, lines, labels
  63. def rotate(x, y, angle, cx, cy):
  64. """
  65. 点(x,y) 绕(cx,cy)点旋转
  66. """
  67. angle = angle*pi/180
  68. x_new = (x-cx)*cos(angle) - (y-cy)*sin(angle)+cx
  69. y_new = (x-cx)*sin(angle) + (y-cy)*cos(angle)+cy
  70. return x_new, y_new
  71. def box_rotate(box, angle=0, imgH=0, imgW=0):
  72. """
  73. 对坐标进行旋转 逆时针方向 0\90\180\270,
  74. """
  75. x1, y1, x2, y2, x3, y3, x4, y4 = box[:8]
  76. if angle == 90:
  77. x1_, y1_ = y2, imgW-x2
  78. x2_, y2_ = y3, imgW-x3
  79. x3_, y3_ = y4, imgW-x4
  80. x4_, y4_ = y1, imgW-x1
  81. elif angle == 180:
  82. x1_, y1_ = imgW-x3, imgH-y3
  83. x2_, y2_ = imgW-x4, imgH-y4
  84. x3_, y3_ = imgW-x1, imgH-y1
  85. x4_, y4_ = imgW-x2, imgH-y2
  86. elif angle == 270:
  87. x1_, y1_ = imgH-y4, x4
  88. x2_, y2_ = imgH-y1, x1
  89. x3_, y3_ = imgH-y2, x2
  90. x4_, y4_ = imgH-y3, x3
  91. else:
  92. x1_, y1_, x2_, y2_, x3_, y3_, x4_, y4_ = x1, y1, x2, y2, x3, y3, x4, y4
  93. return (x1_, y1_, x2_, y2_, x3_, y3_, x4_, y4_)
  94. def angle_transpose(p, angle, w, h):
  95. x, y = p
  96. if angle == 90:
  97. x, y = y, w-x
  98. elif angle == 180:
  99. x, y = w-x, h-y
  100. elif angle == 270:
  101. x, y = h-y, x
  102. return x, y
  103. def img_argument(img, lines, labels, size=(512, 512)):
  104. w, h = img.size
  105. # 80%几率进行旋转-5~5度
  106. if np.random.randint(0, 100) > 80:
  107. degree = np.random.uniform(-5, 5)
  108. else:
  109. degree = 0
  110. # degree = np.random.uniform(-5,5)
  111. # 旋转线条
  112. newlines = []
  113. for line in lines:
  114. p1, p2 = line
  115. p1 = rotate(p1[0], p1[1], degree, w/2, h/2)
  116. p2 = rotate(p2[0], p2[1], degree, w/2, h/2)
  117. newlines.append([p1, p2])
  118. # img = img.rotate(-degree,center=(w/2,h/2),resample=Image.BILINEAR,fillcolor=(128,128,128))
  119. # 旋转图片
  120. img = img.rotate(-degree, center=(w/2, h/2), resample=Image.BILINEAR)
  121. # 随机选择90度倍数旋转
  122. angle = np.random.choice([0, 90, 180, 270], 1)[0]
  123. newlables = []
  124. # 旋转线条
  125. for i in range(len(newlines)):
  126. p1, p2 = newlines[i]
  127. p1 = angle_transpose(p1, angle, w, h)
  128. p2 = angle_transpose(p2, angle, w, h)
  129. newlines[i] = [p1, p2]
  130. # 旋转角度为90,270度时,横竖线的Label互换
  131. if angle in [90, 270]:
  132. if labels[i] == '0':
  133. newlables.append('1')
  134. else:
  135. newlables.append('0')
  136. else:
  137. newlables.append(labels[i])
  138. # 旋转图片
  139. if angle == 90:
  140. img = img.transpose(Image.ROTATE_90)
  141. elif angle == 180:
  142. img = img.transpose(Image.ROTATE_180)
  143. elif angle == 270:
  144. img = img.transpose(Image.ROTATE_270)
  145. return img, newlines, newlables
  146. def fill_lines(img, lines, linetype=2):
  147. tmp = np.copy(img)
  148. for line in lines:
  149. p1, p2 = line
  150. cv2.line(tmp, (int(p1[0]), int(p1[1])), (int(p2[0]), int(p2[1])),
  151. 255, linetype, lineType=cv2.LINE_AA)
  152. return tmp
  153. def get_img_label_origin(p,size,linetype=1):
  154. img,lines,labels = read_json(p)
  155. img,lines=img_resize_origin(img,lines,target_size=512,max_size=1024)
  156. img,lines,labels =img_argument(img,lines,labels,size)
  157. img,lines,labels=get_random_data(img,lines,labels, size=size)
  158. lines = np.array(lines)
  159. labels = np.array(labels)
  160. labelImg0 = np.zeros(size[::-1],dtype='uint8')
  161. labelImg1 = np.zeros(size[::-1],dtype='uint8')
  162. ind = np.where(labels=='0')[0]
  163. labelImg0 = fill_lines(labelImg0,lines[ind],linetype=linetype)
  164. ind = np.where(labels=='1')[0]
  165. labelImg1 = fill_lines(labelImg1,lines[ind],linetype=linetype)
  166. labelY = np.zeros((size[1],size[0],2),dtype='uint8')
  167. labelY[:,:,0] = labelImg0
  168. labelY[:,:,1] = labelImg1
  169. cv2.imshow("get_img_label", labelImg1)
  170. cv2.waitKey(0)
  171. cv2.imshow("get_img_label", labelImg0)
  172. cv2.waitKey(0)
  173. labelY = labelY>0
  174. return np.array(img),lines,labelY
  175. def gen_origin(paths,batchsize=2,linetype=2):
  176. num =len(paths)
  177. i=0
  178. while True:
  179. #sizes = [512,512,512,512,640,1024] ##多尺度训练
  180. #size = np.random.choice(sizes,1)[0]
  181. size = 640
  182. X = np.zeros((batchsize,size,size,3))
  183. Y = np.zeros((batchsize,size,size,2))
  184. for j in range(batchsize):
  185. if i>=num:
  186. i=0
  187. np.random.shuffle(paths)
  188. p = paths[i]
  189. i+=1
  190. #linetype=2
  191. img,lines,labelImg=get_img_label_origin(p,size=(size,size),linetype=linetype)
  192. cv2.imshow("gen", img)
  193. cv2.waitKey(0)
  194. print("gen image size", img.shape)
  195. X[j] = img
  196. Y[j] = labelImg
  197. yield X,Y
  198. def get_img_label(p, size, linetype=1):
  199. # 读取json格式数据
  200. img, lines, labels = read_json(p)
  201. # if img.size[1] > 1200:
  202. # return np.array([]), np.array([]), np.array([]), np.array([])
  203. height, width = size
  204. # width, height = img.size
  205. # size = np.array(img).shape[:-1]
  206. # print("get_img_label origin image_PIL size", img.size)
  207. # 图片缩放
  208. img, lines = img_resize_by_padding_crop(img, lines, (height, width))
  209. # img, lines = img_resize(img, lines, target_size=size, max_size=1024)
  210. # print("get_img_label train image_PIL size", img.size)
  211. # print("get_img_label train image_np shape", np.array(img).shape)
  212. # 图片增强(各种角度旋转)
  213. # img, lines, labels = img_argument(img, lines, labels, size)
  214. # print("shape3", np.array(img).shape)
  215. # 图片轻微缩放 + 图片失真
  216. # img, lines, labels = get_random_data(img, lines, labels, size=size)
  217. lines = np.array(lines)
  218. labels = np.array(labels)
  219. # size (640, 640) 将size的两个值倒过来
  220. # labelImg0 = np.zeros(size[::-1], dtype='uint8')
  221. # labelImg1 = np.zeros(size[::-1], dtype='uint8')
  222. labelImg0 = np.zeros((height, width), dtype='uint8')
  223. labelImg1 = np.zeros((height, width), dtype='uint8')
  224. # print("get_img_label np zero shape", labelImg0.shape, (height, width))
  225. # 在空图片上画线
  226. ind = np.where(labels == '0')[0]
  227. labelImg0 = fill_lines(labelImg0, lines[ind], linetype=linetype)
  228. ind = np.where(labels == '1')[0]
  229. labelImg1 = fill_lines(labelImg1, lines[ind], linetype=linetype)
  230. # print("label image shape", labelImg0.shape, labelImg1.shape)
  231. # 将只有横竖线的图片堆叠
  232. labelY = np.zeros((height, width, 2), dtype='uint8')
  233. labelY[:, :, 0] = labelImg0
  234. labelY[:, :, 1] = labelImg1
  235. # cv2.imshow("get_img_label", labelImg1)
  236. # cv2.waitKey(0)
  237. # cv2.imshow("get_img_label1", labelImg0)
  238. # cv2.waitKey(0)
  239. # print("get_img_label label col size", labelImg1.shape)
  240. # print("get_img_label label row size", labelImg0.shape)
  241. # 对每个像素点进行二分类
  242. labelY = labelY > 0
  243. return np.array(img), lines, labelY, (height, width)
  244. def get_img_label1(p, linetype=1):
  245. # 读取json格式数据
  246. img, lines, labels = read_json(p)
  247. width, height = img.size
  248. lines = np.array(lines)
  249. labels = np.array(labels)
  250. labelImg0 = np.zeros((height, width), dtype='uint8')
  251. labelImg1 = np.zeros((height, width), dtype='uint8')
  252. # 在空图片上画线
  253. ind = np.where(labels == '0')[0]
  254. labelImg0 = fill_lines(labelImg0, lines[ind], linetype=linetype)
  255. ind = np.where(labels == '1')[0]
  256. labelImg1 = fill_lines(labelImg1, lines[ind], linetype=linetype)
  257. # 将只有横竖线的图片堆叠
  258. labelY = np.zeros((height, width, 2), dtype='uint8')
  259. labelY[:, :, 0] = labelImg0
  260. labelY[:, :, 1] = labelImg1
  261. cv2.imshow("get_img_label", labelImg1)
  262. cv2.waitKey(0)
  263. cv2.imshow("get_img_label", labelImg0)
  264. cv2.waitKey(0)
  265. # 对每个像素点进行二分类
  266. labelY = labelY > 0
  267. return np.array(img), lines, labelY, (height, width)
  268. def rand(a=0., b=1.):
  269. # rand(): [0-1)
  270. return np.random.rand()*(b-a) + a
  271. def get_random_data(image, lines, labels, size=(1024, 1024), jitter=.3,
  272. hue=.1, sat=1.5, val=1.5):
  273. """
  274. random preprocessing for real-time data augmentation
  275. """
  276. iw, ih = image.size
  277. # resize image
  278. w, h = size
  279. new_ar = w/h * rand(1-jitter, 1+jitter) / rand(1-jitter, 1+jitter)
  280. # scale = rand(.2, 2)
  281. scale = rand(0.2, 3)
  282. if new_ar < 1:
  283. nh = int(scale*h)
  284. nw = int(nh*new_ar)
  285. else:
  286. nw = int(scale*w)
  287. nh = int(nw/new_ar)
  288. image = image.resize((nw, nh), Image.BICUBIC)
  289. # 将原图粘贴到另一张空白图上
  290. dx = int(rand(0, w-nw))
  291. dy = int(rand(0, h-nh))
  292. new_image = Image.new('RGB', (w, h), (128, 128, 128))
  293. new_image.paste(image, (dx, dy))
  294. image = new_image
  295. # 图片失真
  296. hue = rand(-hue, hue)
  297. sat = rand(1, sat) if rand() < .5 else 1/rand(1, sat)
  298. val = rand(1, val) if rand() < .5 else 1/rand(1, val)
  299. x = rgb_to_hsv(np.array(image)/255.)
  300. x[..., 0] += hue
  301. x[..., 0][x[..., 0] > 1] -= 1
  302. x[..., 0][x[..., 0] < 0] += 1
  303. x[..., 1] *= sat
  304. x[..., 2] *= val
  305. x[x > 1] = 1
  306. x[x < 0] = 0
  307. # numpy array, 0 to 1
  308. image_data = hsv_to_rgb(x)
  309. N = len(lines)
  310. for i in range(N):
  311. p1, p2 = lines[i]
  312. p1 = p1[0]*nw/iw+dx, p1[1]*nh/ih + dy
  313. p2 = p2[0]*nw/iw+dx, p2[1]*nh/ih + dy
  314. lines[i] = [p1, p2]
  315. return image_data, lines, labels
  316. def gen2(paths, batchsize=2, linetype=2):
  317. num = len(paths)
  318. i = 0
  319. while True:
  320. # 多尺度训练
  321. sizes = [1152, 1024, 1280, 896, 768, 640, 1024]
  322. # size = np.random.choice(sizes, 1)[0]
  323. # height = np.random.choice(sizes, 1)[0]
  324. # width = int(height/1.4)
  325. heights = [1024, 896, 768, 640]
  326. widths = [768, 640, 512, 384]
  327. height = np.random.choice(heights, 1)[0]
  328. width = np.random.choice(widths, 1)[0]
  329. # height = 1024
  330. # width = 768
  331. # size = (1024, 512)
  332. X = np.zeros((batchsize, height, width, 3))
  333. Y = np.zeros((batchsize, height, width, 2))
  334. for j in range(batchsize):
  335. if i >= num:
  336. i = 0
  337. np.random.shuffle(paths)
  338. p = paths[i]
  339. i += 1
  340. # linetype=2
  341. print("gen input size", (width, height))
  342. img, lines, labelImg = get_img_label(p, size=(width, height),
  343. linetype=linetype)
  344. # 高斯模糊
  345. # sigmaX = random.randint(1, 10)
  346. # sigmaY = random.randint(1, 10)
  347. # img = cv2.GaussianBlur(img, (5, 5), sigmaX, sigmaY)
  348. cv2.imshow("gen", img)
  349. cv2.waitKey(0)
  350. print("gen image size", img.shape)
  351. X[j] = img
  352. Y[j] = labelImg
  353. yield X, Y
  354. def gen(paths, batchsize=2, linetype=2):
  355. num = len(paths)
  356. i = 0
  357. while True:
  358. # 多尺度训练
  359. sizes = [1152, 1024, 1280, 896, 768, 640, 1024]
  360. # size = np.random.choice(sizes, 1)[0]
  361. # height = np.random.choice(sizes, 1)[0]
  362. # width = int(height/1.4)
  363. # heights = [1024, 896, 768, 640, 512, 384, 256, 128]
  364. # widths = [1024, 896, 768, 640, 512, 384, 256, 128]
  365. heights = [3008, 2944, 2880, 2816, 2752, 2688, 2624, 2560, 2496, 2432, 2368,
  366. 2304, 2240, 2176, 2112, 2048, 1984, 1920, 1856, 1792, 1728, 1664,
  367. 1600, 1536, 1472, 1408, 1344, 1280, 1216, 1152, 1088, 1024, 960,
  368. 896, 832, 768, 704, 640, 576, 512]
  369. widths = [2048, 1984, 1920, 1856, 1792, 1728, 1664,
  370. 1600, 1536, 1472, 1408, 1344, 1280, 1216, 1152, 1088, 1024, 960,
  371. 896, 832, 768, 704, 640, 576, 512]
  372. height = np.random.choice(heights, 1)[0]
  373. width = np.random.choice(widths, 1)[0]
  374. # height = 1024
  375. # width = 768
  376. # size = (1024, 512)
  377. # print("gen batch shape", height, width)
  378. X = np.zeros((batchsize, height, width, 3))
  379. Y = np.zeros((batchsize, height, width, 2))
  380. for j in range(batchsize):
  381. if i >= num:
  382. i = 0
  383. np.random.shuffle(paths)
  384. p = paths[i]
  385. i += 1
  386. # linetype=2
  387. # print("gen input size", (height, width))
  388. img, lines, labelImg, size = get_img_label(p, size=(height, width),
  389. linetype=linetype)
  390. # if not img.any():
  391. # print("image too large, jump")
  392. # continue
  393. # height, width = size
  394. # X = np.zeros((batchsize, height, width, 3))
  395. # Y = np.zeros((batchsize, height, width, 2))
  396. # if_blur = np.random.choice([0, 1], 1)[0]
  397. # if if_blur:
  398. # # 高斯模糊
  399. # sigmaX = random.randint(1, 3)
  400. # sigmaY = random.randint(1, 3)
  401. # img = cv2.GaussianBlur(img, (5, 5), sigmaX, sigmaY)
  402. # cv2.imshow("gen", img)
  403. # cv2.waitKey(0)
  404. # print("gen image size", img.shape)
  405. # cv2.imshow("gen", labelImg[:, :, 0])
  406. # cv2.waitKey(0)
  407. # print("gen label image size", labelImg[:, :, 0])
  408. X[j] = img
  409. Y[j] = labelImg
  410. yield X, Y
  411. def gen1(paths, batchsize=2, linetype=2):
  412. num = len(paths)
  413. i = 0
  414. while True:
  415. batchsize = 1
  416. for j in range(batchsize):
  417. if i >= num:
  418. i = 0
  419. np.random.shuffle(paths)
  420. p = paths[i]
  421. i += 1
  422. img, lines, labelImg, size = get_img_label(p, linetype=linetype)
  423. height, width = size
  424. X = np.zeros((batchsize, height, width, 3))
  425. Y = np.zeros((batchsize, height, width, 2))
  426. if_blur = np.random.choice([0, 1], 1)[0]
  427. if if_blur:
  428. # 高斯模糊
  429. sigmaX = random.randint(1, 3)
  430. sigmaY = random.randint(1, 3)
  431. img = cv2.GaussianBlur(img, (5, 5), sigmaX, sigmaY)
  432. print("gen image size", img.shape)
  433. cv2.imshow("gen", img)
  434. cv2.waitKey(0)
  435. # cv2.imshow("gen", labelImg[:, :, 0])
  436. # cv2.waitKey(0)
  437. # print("gen label image size", labelImg[:, :, 0].shape)
  438. X[j] = img
  439. Y[j] = labelImg
  440. yield X, Y
  441. def img_resize(im, lines, target_size, max_size=None):
  442. w, h = im.size
  443. w_t, h_t = target_size
  444. print("img_resize", im.size, target_size)
  445. # im_size_min = np.min(im.size)
  446. # im_size_max = np.max(im.size)
  447. im_scale_w = float(w_t)/float(w)
  448. im_scale_h = float(h_t)/float(h)
  449. im = im.resize((int(w*im_scale_w), int(h*im_scale_h)), Image.BICUBIC)
  450. N = len(lines)
  451. for i in range(N):
  452. p1, p2 = lines[i]
  453. p1 = p1[0]*im_scale_w, p1[1]*im_scale_h
  454. p2 = p2[0]*im_scale_w, p2[1]*im_scale_h
  455. lines[i] = [p1, p2]
  456. return im, lines
  457. def img_resize_origin(im,lines,target_size=600,max_size=1500):
  458. w,h = im.size
  459. im_size_min = np.min(im.size)
  460. im_size_max = np.max(im.size)
  461. im_scale = float(target_size)/float(im_size_min)
  462. if max_size is not None:
  463. if np.round(im_scale * im_size_max) > max_size:
  464. im_scale = float(max_size)/float(im_size_max)
  465. im = im.resize((int(w*im_scale),int(h*im_scale)),Image.BICUBIC)
  466. N = len(lines)
  467. for i in range(N):
  468. p1,p2 = lines[i]
  469. p1 = p1[0]*im_scale,p1[1]*im_scale
  470. p2 = p2[0]*im_scale,p2[1]*im_scale
  471. lines[i] = [p1,p2]
  472. return im, lines
  473. def img_resize_by_padding_crop(im, lines, target_size):
  474. w, h = im.size
  475. h_t, w_t = target_size
  476. # print("img_resize_by_padding_crop", im.size, target_size)
  477. # PIL -> CV2
  478. img = cv2.cvtColor(np.asarray(im), cv2.COLOR_RGB2BGR)
  479. # print("img_resize_by_padding_crop image_np shape0", img.shape)
  480. # 图像边缘扩充/裁剪
  481. change_w_flag = 0
  482. change_h_flag = 0
  483. if w_t >= w and h_t >= h:
  484. change_height = int((h_t - h)/2)
  485. change_width = int((w_t - w)/2)
  486. img = cv2.copyMakeBorder(img, change_height, change_height, change_width,
  487. change_width, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  488. change_w_flag = 1
  489. change_h_flag = 1
  490. # print("img_resize_by_padding_crop 1 1")
  491. elif w_t >= w:
  492. change_width = int((w_t - w)/2)
  493. change_height = int((h - h_t)/2)
  494. img = cv2.copyMakeBorder(img, 0, 0, change_width,
  495. change_width, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  496. img = img[change_height:h-change_height, :]
  497. change_w_flag = 1
  498. change_h_flag = -1
  499. # print("img_resize_by_padding_crop 1 -1")
  500. elif h_t >= h:
  501. change_height = int((h_t - h)/2)
  502. change_width = int((w - w_t)/2)
  503. img = cv2.copyMakeBorder(img, change_height, change_height, 0,
  504. 0, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  505. img = img[:, change_width:w-change_width]
  506. change_w_flag = -1
  507. change_h_flag = 1
  508. # print("img_resize_by_padding_crop -1 1")
  509. else:
  510. if abs(h - h_t) % 2 != 0:
  511. change_height = int((h - h_t)/2)
  512. change_height += 1
  513. else:
  514. change_height = int((h - h_t)/2)
  515. if abs(w - w_t) % 2 != 0:
  516. change_width = int((w - w_t)/2)
  517. change_width += 1
  518. else:
  519. change_width = int((w - w_t)/2)
  520. img = img[change_height:h-change_height, change_width:w-change_width]
  521. change_w_flag = -1
  522. change_h_flag = -1
  523. # print("img_resize_by_padding_crop -1 -1")
  524. # print("img_resize_by_padding_crop image_np shape1", img.shape)
  525. # image shape 和 target大小不同
  526. if img.shape[0] < h_t:
  527. img = cv2.copyMakeBorder(img, h_t-img.shape[0], 0, 0,
  528. 0, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  529. if img.shape[1] < w_t:
  530. img = cv2.copyMakeBorder(img, 0, 0, w_t-img.shape[1],
  531. 0, cv2.BORDER_CONSTANT, value=(255, 255, 255))
  532. if img.shape[0] > h_t:
  533. img = img[:h_t, :, :]
  534. if img.shape[1] > w_t:
  535. img = img[:, :w_t, :]
  536. # print("img_resize_by_padding_crop image_np shape2", img.shape)
  537. # CV2 -> PIL
  538. im = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  539. N = len(lines)
  540. for i in range(N):
  541. p1, p2 = lines[i]
  542. p1 = p1[0] + change_w_flag * change_width, p1[1] + change_h_flag * change_height
  543. p2 = p2[0] + change_w_flag * change_width, p2[1] + change_h_flag * change_height
  544. lines[i] = [p1, p2]
  545. return im, lines
  546. if __name__ == '__main__':
  547. _list = []
  548. for i in range(100, 1, -1):
  549. _list.append(i*64)
  550. print(_list)