diff --git a/OCR/EasyOCR_2.py b/OCR/EasyOCR_2.py new file mode 100644 index 0000000..b0b24e2 --- /dev/null +++ b/OCR/EasyOCR_2.py @@ -0,0 +1,55 @@ +import cv2 +from matplotlib import pyplot as plt +import numpy as np +import easyocr +import imutils +import random + +img = cv2.imread("Test5.jpg") #read image +plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) +plt.title('Original Image') +plt.show() + +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert image to gray +bfilter = cv2.bilateralFilter(gray, 11, 17, 17) #Noise reduction +plt.imshow(cv2.cvtColor(bfilter, cv2.COLOR_BGR2RGB)) #show processed image +plt.title('Processed Image') +plt.show() + +edged = cv2.Canny(bfilter, 30, 200) #Edge detection +plt.imshow(cv2.cvtColor(edged, cv2.COLOR_BGR2RGB)) +plt.title('Edged Processed Image') +plt.show() + +keypoints = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #Find contours +contours = imutils.grab_contours(keypoints) #Grab contours +contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10] #Sort contours + +#Loop over our contours to find the best possible approximate contour of 10 contours +location = None +for contour in contours: + approx = cv2.approxPolyDP(contour, 10, True) + if len(approx) == 4: + location = approx + break + +print("Location: ", location) + +mask = np.zeros(gray.shape, np.uint8) #create blank image with same dimensions as the original image +new_image = cv2.drawContours(mask, [location], 0,255, -1) #Draw contours on the mask image +new_image = cv2.bitwise_and(img, img, mask=mask) #Take bitwise AND between the original image and mask image + +plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)) #show the final image +plt.show() + +(x,y) = np.where(mask==255) #Find the co-ordinates of the four corners of the document +(x1, y1) = (np.min(x), np.min(y)) #Find the top left corner +(x2, y2) = (np.max(x), np.max(y)) #Find the bottom right corner +cropped_image = gray[x1:x2+1, y1:y2+1] #Crop the image using the co-ordinates + +plt.imshow(cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)) #show the cropped image +plt.show() + +reader = easyocr.Reader(['en']) #create an easyocr reader object with english as the language +result = reader.readtext(cropped_image) #read text from the cropped image +print(result) diff --git a/OCR/Test.jpg b/OCR/Test.jpg new file mode 100644 index 0000000..253d22b Binary files /dev/null and b/OCR/Test.jpg differ diff --git a/OCR/Test2.jpg b/OCR/Test2.jpg new file mode 100644 index 0000000..f010d68 Binary files /dev/null and b/OCR/Test2.jpg differ diff --git a/OCR/Test3.jpg b/OCR/Test3.jpg new file mode 100644 index 0000000..11f7428 Binary files /dev/null and b/OCR/Test3.jpg differ diff --git a/OCR/Test4.jpg b/OCR/Test4.jpg new file mode 100644 index 0000000..e188371 Binary files /dev/null and b/OCR/Test4.jpg differ diff --git a/OCR/Test5.jpg b/OCR/Test5.jpg new file mode 100644 index 0000000..d16be7e Binary files /dev/null and b/OCR/Test5.jpg differ