先识别数字,后用数字命名,再保存
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
import os
from os.path import join
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
config='--psm 6 --oem 1 -c tessedit_char_whitelist=0123456789'
path = r"D:\BUFFER\Pycharm\OpencvLearn\images\OCR"
determination = "images/ROI_Images"
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
for filename in os.listdir(path): # listdir的参数是文件夹的路径
filenames = path + '\\' + filename
# print(filenames)
img_orig = cv2.imread(filenames, 1)
# print(filenames)
image_RGB = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB)
# cv2.rectangle(image_RGB, (1150,820),(1400,950),(0,0,255),1)
ROI = image_RGB[820:950, 1165:1380]
img = ROI.copy()
# print(pytesseract.image_to_boxes(ROI))
# cv2.imshow("roi", ROI)
# cv2.waitKey(0)
kernel = np.ones((5,5), np.uint8)
open_img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# cv2.imshow("images", open_img)
left_thred = 240
gray_img = cv2.cvtColor(open_img,cv2.COLOR_RGB2GRAY)
ret, thresh1 = cv2.threshold(gray_img, left_thred, 255, cv2.THRESH_BINARY)
thresh1[0:20, 0:100] = 255
thresh1[105:129, 0:210] = 255
blur = cv2.blur(thresh1, (3,3))
# cv2.imshow("thresh",thresh1)
# cv2.waitKey(0)
img_strings = pytesseract.image_to_string(blur)
img_rotate = rotate_bound(img, -5)
ROI = img_rotate[28:113, 15:220]
strings = img_strings[:6]
if len(strings) == 6:
fileName = strings + ".jpg"
# cv2.imwrite('images/ROI_Images/' + fileName,ROI)
cv2.imwrite('images/rcnnROI/'+ fileName,ROI)
print(img_strings[:6])