12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # from __future__ import print_function, absolute_import
- import torch.utils.data as data
- import os
- import numpy as np
- import cv2,math
- def t1():
- img = cv2.imread("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\images\\Snipaste_2023-06-12_16-25-38.jpg")
- img = cv2.imread("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\Attention_ocr.pytorch\\test_img\\20436312_1683447152.jpg")
- # img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
- print('img',img)
- cv2.imshow('1',img)
- cv2.waitKey(2000)
- # 数据增强
- # img = img_process(img)
- img_h, img_w = img.shape[:2]
- inp_h = 32
- inp_w = 960
- # img = cv2.resize(img, (0,0), fx=self.inp_w / img_w, fy=self.inp_h / img_h, interpolation=cv2.INTER_CUBIC)
- # img = np.reshape(img, (self.inp_h, self.inp_w, 1))
- # 图片真实长宽比
- ratio = img_w / float(img_h)
- # 按比例缩放
- if math.ceil(inp_h * ratio) > inp_w:
- # 如大于默认宽度,则宽度为imgW
- resized_w = inp_w
- else:
- # 如小于默认宽度则以图片真实宽为准
- resized_w = int(math.ceil(inp_h * ratio))
- # 缩放
- img = cv2.resize(img, (resized_w, inp_h))
- print('img2',img)
- cv2.imshow('2',img)
- cv2.waitKey(3000)
- img = img.astype(np.float32)
- # 标准化
- # img = (img/255. - 0.5) / 0.5
- img = (img/255. - 0.588) / 0.193
- cv2.imshow('33', img)
- cv2.waitKey(3000)
- img = img.transpose([2, 0, 1])
- print('img3',img)
- # 对宽度不足的位置,补0
- padding_im = np.full((3, inp_h, inp_w),255, dtype=np.float32)
- padding_im[:, :, 0:resized_w] = img
- print('img4',padding_im)
- cv2.imshow('3',padding_im.transpose((1,2,0)))
- cv2.waitKey(3000)
- def t2():
- new = []
- with open("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\lib\\dataset\\txt\\train.txt", "r", encoding='utf-8') as f:
- for n in f.readlines():
- new.append(n)
- f.close()
- with open("C:\\Users\\Administrator\\Desktop\\OCR_pytorch\\CRNN_Chinese_Characters_Rec\\lib\\dataset\\txt\\train2.txt", "w", encoding='utf-8') as f:
- for n in new[:30000]:
- f.write(n)
- # f.write("\n")
- f.close()
- if __name__ == '__main__':
- t1()
- # t2()
- pass
|