image_process_test.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # from __future__ import print_function, absolute_import
  2. import torch.utils.data as data
  3. import os
  4. import numpy as np
  5. import cv2,math
  6. def t1():
  7. img = cv2.imread("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\images\\Snipaste_2023-06-12_16-25-38.jpg")
  8. img = cv2.imread("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\Attention_ocr.pytorch\\test_img\\20436312_1683447152.jpg")
  9. # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  10. # img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  11. print('img',img)
  12. cv2.imshow('1',img)
  13. cv2.waitKey(2000)
  14. # 数据增强
  15. # img = img_process(img)
  16. img_h, img_w = img.shape[:2]
  17. inp_h = 32
  18. inp_w = 960
  19. # img = cv2.resize(img, (0,0), fx=self.inp_w / img_w, fy=self.inp_h / img_h, interpolation=cv2.INTER_CUBIC)
  20. # img = np.reshape(img, (self.inp_h, self.inp_w, 1))
  21. # 图片真实长宽比
  22. ratio = img_w / float(img_h)
  23. # 按比例缩放
  24. if math.ceil(inp_h * ratio) > inp_w:
  25. # 如大于默认宽度,则宽度为imgW
  26. resized_w = inp_w
  27. else:
  28. # 如小于默认宽度则以图片真实宽为准
  29. resized_w = int(math.ceil(inp_h * ratio))
  30. # 缩放
  31. img = cv2.resize(img, (resized_w, inp_h))
  32. print('img2',img)
  33. cv2.imshow('2',img)
  34. cv2.waitKey(3000)
  35. img = img.astype(np.float32)
  36. # 标准化
  37. # img = (img/255. - 0.5) / 0.5
  38. img = (img/255. - 0.588) / 0.193
  39. cv2.imshow('33', img)
  40. cv2.waitKey(3000)
  41. img = img.transpose([2, 0, 1])
  42. print('img3',img)
  43. # 对宽度不足的位置,补0
  44. padding_im = np.full((3, inp_h, inp_w),255, dtype=np.float32)
  45. padding_im[:, :, 0:resized_w] = img
  46. print('img4',padding_im)
  47. cv2.imshow('3',padding_im.transpose((1,2,0)))
  48. cv2.waitKey(3000)
  49. def t2():
  50. new = []
  51. with open("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\lib\\dataset\\txt\\train.txt", "r", encoding='utf-8') as f:
  52. for n in f.readlines():
  53. new.append(n)
  54. f.close()
  55. with open("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\lib\\dataset\\txt\\train2.txt", "w", encoding='utf-8') as f:
  56. for n in new[:30000]:
  57. f.write(n)
  58. # f.write("\n")
  59. f.close()
  60. if __name__ == '__main__':
  61. t1()
  62. # t2()
  63. pass