Reputation: 31
I'm getting No module named 'pytesseract' error in google collab with following code
import pytesseract import imutils import cv2
pytesseract.pytesseract.tesseract_cmd = r'/content/drive/MyDrive/Automatic_Number_Plate_Recognition_System/Tesseract-OCR/tesseract.exe'
image = cv2.imread("/content/drive/MyDrive/Automatic_Number_Plate_Recognition_System/Cars.png", cv2.IMREAD_COLOR)
image = imutils.resize(image, width=500)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.bilateralFilter(gray_image, 11, 17, 17)
contours, new = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
image1 = image.copy()
cv2.drawContours(image1, contours, -1, (0, 255, 0), 3)
contour = sorted(contours, key=cv2.contourArea, reverse=True)[:30]
count = None
image2 = image.copy()
cv2.drawContours(image2, contour, -1, (0, 255, 0), 3)
# Declaring and initialization of count_n and index and new_image
count_n = 0
index = 21
new_image = []
# Iterating contours
for contour in contours:
perimeter = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.018*perimeter, True)
if len(approx) == 4:
count = approx
x, y, w, h = cv2.boundingRect(contour)
new_image = image[y:y+h, x:x+w]
cv2.imwrite("./" + str(index) + ".png", new_image)
index = index + 1
break
# Drawing Contours to new_image
cv2.drawContours(new_image, [count], -1, (0, 255, 0), 3)
# Showing new_image with label "Image of Number Plate Detected"
cv2.imshow("Image of Number Plate Detected", new_image)
cv2.waitKey(0)
# Initializing img with cropImage.png
img = "./cropImage.png"
# Showing Cropped Image
cv2.imshow("Cropped Image", cv2.imread(img))
# Converting image to string
number = pytesseract.image_to_string(img, lang='eng')
# Filtering the number got from converting image to string
final_number = "".join(number.split()).replace(":", "").replace("-", "").replace(";", "").replace(".", "")
# Printing number detected from image
print("Number on Number Plate : ", final_number)
cv2.waitKey(0)
cv2.destroyAllWindows()
Upvotes: 0
Views: 948
Reputation: 432
You should install pytesseract with pip. You can use the !
magic to run bash in a collab notebook. In the 1st cell:
!pip install pytesseract
then in cell #2:
import pytesseract
import imutils
import cv2
Upvotes: 0