Compare commits

...

2 Commits

Author SHA1 Message Date
Pradana AUMARS 92d131ecd6 Merge pull request 'Update image processing procedure' (#3) from Yun/videocr:master into master
Reviewed-on: #3
2021-07-14 18:48:31 +02:00
Yun b5e6f5a57f Update image processing procedure
Apply threshold after dilution and select only white pixels from result.
Erode afterwards to thin out the text.
2021-07-14 06:22:55 +02:00
1 changed files with 8 additions and 9 deletions

View File

@ -62,15 +62,14 @@ class Video:
if not self.use_fullframe:
# only use bottom half of the frame by default
img = img[self.height // 2:, :]
# dilate and resize
img=cv2.resize(cv2.dilate(img, np.ones((2, 2), np.uint8)), self.resize_dim, interpolation=cv2.INTER_AREA)
# mask to filter out non gray-like pixels/pixels that are not bright enough
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
color_mask = cv2.inRange(hsv, (0, 0, 190), (179, 20, 255))
# apply mask, inverse image so it's black text on white background, add borders to top and bottom
img = cv2.copyMakeBorder(cv2.bitwise_not(cv2.bitwise_and(img, img, mask=color_mask)), 10, 10, 0, 0, cv2.BORDER_CONSTANT, None, (255,255,255))
img = cv2.dilate(img, np.ones((2, 2), np.uint8))
_, img = cv2.threshold(img, 215, 255, cv2.THRESH_BINARY)
color_mask = cv2.inRange(img, (255, 255, 255), (255, 255, 255))
img = cv2.bitwise_and(img, img, mask=color_mask)
img = cv2.erode(img, np.ones((2, 2), np.uint8))
img = cv2.bitwise_not(img)
img = cv2.resize(img, self.resize_dim, interpolation=cv2.INTER_AREA)
img = cv2.copyMakeBorder(img, 20, 20, 0, 0, cv2.BORDER_CONSTANT, None, (255,255,255))
config = '--tessdata-dir "{}" --psm 7 -c preserve_interword_spaces=1'.format(constants.TESSDATA_DIR)
try:
return pytesseract.image_to_data(img, lang=self.lang, config=config)