vedant aher
vedant aher

Reputation: 31

import pytesseract error in google collab

I'm getting No module named 'pytesseract' error in google collab with following code

AUTOMATIC NUMBER PLATE RECOGNITION SYSTEM

import pytesseract import imutils import cv2

pytesseract.pytesseract.tesseract_cmd = r'/content/drive/MyDrive/Automatic_Number_Plate_Recognition_System/Tesseract-OCR/tesseract.exe'

Reading image

image = cv2.imread("/content/drive/MyDrive/Automatic_Number_Plate_Recognition_System/Cars.png", cv2.IMREAD_COLOR)

Showing readed image

cv2.imshow("Image",image)

Resizing readed image

image = imutils.resize(image, width=500)

Converting it to gray scale image

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Edging the gray image

edged = cv2.bilateralFilter(gray_image, 11, 17, 17)

Finding contours from edged

contours, new = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

Copying image to image1

image1 = image.copy()

Drawing Contours

cv2.drawContours(image1, contours, -1, (0, 255, 0), 3)

Sorting Contours

contour = sorted(contours, key=cv2.contourArea, reverse=True)[:30]

Declaring count and initializing it to None

count = None

Copying image to image2

image2 = image.copy()

Drawing Contours

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

Answers (1)

Tumo Masire
Tumo Masire

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

Related Questions