You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.4 KiB
110 lines
3.4 KiB
import cv2
|
|
from PIL import Image
|
|
import pytesseract
|
|
|
|
card_name_roi = { 'x':44, 'y':32, 'w':360, 'h':40 }
|
|
|
|
def grayscale(image):
|
|
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
def noise_removal(image):
|
|
import numpy as np
|
|
kernel = np.ones((1, 1), np.uint8)
|
|
image = cv2.dilate(image, kernel, iterations=1)
|
|
kernel = np.ones((1, 1), np.uint8)
|
|
image = cv2.erode(image, kernel, iterations=1)
|
|
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
|
|
image = cv2.medianBlur(image, 3)
|
|
return (image)
|
|
|
|
def thin_font(image):
|
|
import numpy as np
|
|
image = cv2.bitwise_not(image)
|
|
kernel = np.ones((2,2),np.uint8)
|
|
image = cv2.erode(image, kernel, iterations=1)
|
|
image = cv2.bitwise_not(image)
|
|
return (image)
|
|
|
|
def thick_font(image):
|
|
import numpy as np
|
|
image = cv2.bitwise_not(image)
|
|
kernel = np.ones((2,2),np.uint8)
|
|
image = cv2.dilate(image, kernel, iterations=1)
|
|
image = cv2.bitwise_not(image)
|
|
return (image)
|
|
|
|
file_path = "data/WTR/WTR003.png"
|
|
|
|
img = cv2.imread(file_path)
|
|
inverted_image = cv2.bitwise_not(img)
|
|
cv2.imwrite("temp/inverted.jpg", inverted_image)
|
|
|
|
gray_image = grayscale(img)
|
|
cv2.imwrite("temp/gray.jpg", gray_image)
|
|
|
|
thresh, im_bw = cv2.threshold(gray_image, 210, 230, cv2.THRESH_BINARY)
|
|
cv2.imwrite("temp/bw_image.jpg", im_bw)
|
|
|
|
no_noise = noise_removal(im_bw)
|
|
cv2.imwrite("temp/no_noise.jpg", no_noise)
|
|
|
|
eroded_image = thin_font(no_noise)
|
|
cv2.imwrite("temp/eroded_image.jpg", eroded_image)
|
|
|
|
dilated_image = thick_font(no_noise)
|
|
cv2.imwrite("temp/dilated_image.jpg", dilated_image)
|
|
|
|
im = Image.open(file_path)
|
|
ocr_result = pytesseract.image_to_string(im)
|
|
print("Unaltered:")
|
|
print (ocr_result)
|
|
print("===========================")
|
|
|
|
inverted_im = Image.open("temp/inverted.jpg")
|
|
inverted_image_ocr = pytesseract.image_to_string(inverted_im)
|
|
print("Inverted:")
|
|
print (inverted_image_ocr)
|
|
print("===========================")
|
|
|
|
greyscale_im = Image.open("temp/gray.jpg")
|
|
greyscale_im_ocr = pytesseract.image_to_string(greyscale_im)
|
|
print("Greyscale:")
|
|
print (greyscale_im_ocr)
|
|
print("===========================")
|
|
|
|
bw_im = Image.open("temp/bw_image.jpg")
|
|
bw_im_ocr = pytesseract.image_to_string(bw_im)
|
|
print("Black and White:")
|
|
print (bw_im_ocr)
|
|
print("===========================")
|
|
|
|
no_noise_im = Image.open("temp/no_noise.jpg")
|
|
no_noise_im_ocr = pytesseract.image_to_string(no_noise_im)
|
|
print("No Noise:")
|
|
print (no_noise_im_ocr)
|
|
print("===========================")
|
|
|
|
eroded_image_im = Image.open("temp/eroded_image.jpg")
|
|
eroded_image_im_ocr = pytesseract.image_to_string(eroded_image_im)
|
|
print("Eroded:")
|
|
print (eroded_image_im_ocr)
|
|
print("===========================")
|
|
|
|
dilated_image_im = Image.open("temp/dilated_image.jpg")
|
|
dilated_image_im_ocr = pytesseract.image_to_string(dilated_image_im)
|
|
print("Dilated:")
|
|
print (dilated_image_im_ocr)
|
|
print("===========================")
|
|
|
|
def testArea(image, x,y,w,h):
|
|
roi = image[y:y+h, x:x+w]
|
|
inverted_image = cv2.bitwise_not(roi)
|
|
cv2.imwrite("temp/inverted.jpg", inverted_image)
|
|
gray_image = grayscale(roi)
|
|
cv2.imwrite("temp/gray.jpg", gray_image)
|
|
cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 2)
|
|
ocr_result = pytesseract.image_to_string(gray_image)
|
|
print(ocr_result)
|
|
|
|
testArea(img, card_name_roi['x'], card_name_roi['y'], card_name_roi['w'], card_name_roi['h'])
|
|
cv2.imwrite("temp/markup.jpg", img)
|