I'm new to pyhton and I'm making a number plate recognition system using haar cascade. My code works fine to detect the number plate and make contours but pytesseract ocr fails to recognise the characters and gives strange results. Please help.
The detected plate using haar cascades
contours made on the detected region
import cv2 import numpy as np import pytesseract plate_cascade = cv2.CascadeClassifier('C:/Users/Jai/Desktop/Test/haarcascade_plate.xml') img = cv2.imread('C:/Users/Jai/Desktop/Test/images/NumberPlates/IMG_20181029_194221.jpg', cv2.IMREAD_COLOR) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = np.array(gray, dtype='uint8') cv2.imshow('gray', gray) plates = plate_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in plates: cv2.rectangle(img, (x,y), (x+w,y+h),(255,0,0),5) roiGray = gray[y:y+h, x:x+w] roiImg = img[y:y+h, x:x+w] roiUGray = cv2.bitwise_not(roiGray) cv2.imshow('img', img) cv2.imshow('roi_gray', roiUGray) ret, thresh = cv2.threshold(roiUGray, 127, 255,0) cv2.imshow("img", img) height, width = thresh.shape newImage = np.zeros((height, width, 3), np.uint8) newImage[:, :] = (0,0,0) im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) num = 0 area = 0 for j in range(len(contours)): if hierarchy[0][j][3] != -1: area = area + cv2.contourArea(contours[j]) num += 1 if num != 0: avgArea = float(area)/float(num) for j in range(len(contours)): if hierarchy[0][j][3] != -1 and cv2.contourArea(contours[j]) > 0.2*avgArea: cv2.drawContours(newImage, contours[j], -1, (255,255,255), 1) blur = cv2.GaussianBlur(newImage, (1, 1), 0) cv2.imshow("roi", blur) ocr_result = pytesseract.image_to_string(blur, lang='eng') print(ocr_result) cv2.waitKey(0) cv2.destroyAllWindows() 

