def detectTextRects(image, imageScale):
# letterBoxes
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
threshold = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (130, 20))
result = cv2.dilate((255 - threshold), kernel)
# // ????????????????
contours = cv2.findContours(result, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
maxValue = 200 * imageScale
minValue = 40 * imageScale
boundRect = []
for points in contours:
appRect = cv2.boundingRect(points) # x y w h
if (appRect[3] > maxValue and appRect[2] > maxValue):
continue
if (appRect[3] < minValue or appRect[2] < minValue):
continue
appRect = list(appRect)
appRect[2] += 60 * imageScale
appRect[3] += 15 * imageScale
appRect[0] -= 30 * imageScale
appRect[1] -= 7.5 * imageScale
boundRect.append(tuple(appRect))
return boundRect
# ??????shell?????
# def image_to_string(img, cleanup=True, plus=''):
# # cleanup?True???????????????
# # plus????tesseract???????
# try:
# subprocess.check_output('tesseract ' + img + ' ' + img + ' ' + plus, shell=True) # ????txt??
# except subprocess.CalledProcessError as e:
# return ""
# text = ''
# with open(img + '.txt', 'r') as f:
# text = f.read().strip()
# if cleanup:
# os.remove(img + '.txt')
# return text
# ?????
评论列表
文章目录