Search code examples
pythonopencvocrgoogle-vision

TypeError: get() expected a dict or protobuf message, got <class 'numpy.ndarray'>


I'm trying to take a region of a picture using OpenCV, and then extracting that text. Any idea how to fix this error?

roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
cv2.imshow('image2', roi)
cv2.waitKey(0)

response = client.text_detection(image=roi)
texts = response.text_annotations
string = texts[0].description

print(string)

Solution

  • text_detection method expects an Cloud Vision Image task instance for analization, where you're passing Image instance of another type.

    You can convert OpenCV image instance to the one suitable for google-vision client methods:

    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    success, encoded_image = cv2.imencode('.jpg', roi)
    roi_image = encoded_image.tobytes()
    roi_image = vision.types.Image(content=roi_image)
    response = client.text_detection(image=roi_image)