【问题标题】:TypeError: get() expected a dict or protobuf message, got <class 'numpy.ndarray'>TypeError: get() 期望一个 dict 或 protobuf 消息,得到 <class 'numpy.ndarray'>
【发布时间】:2021-02-10 09:36:30
【问题描述】:

我正在尝试使用 OpenCV 拍摄图片的一个区域,然后提取该文本。知道如何解决此错误吗?

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)

【问题讨论】:

    标签: python opencv ocr google-vision


    【解决方案1】:

    text_detection 方法需要一个 Cloud Vision Image 任务实例进行分析,您将在其中传递另一种类型的 Image 实例。

    您可以将 OpenCV 图像实例转换为适合 google-vision 客户端方法的实例:

    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)
    

    【讨论】:

    • 工作就像一个魅力。谢谢。除非出于某种原因,即使我导入类型,我也必须将所有 vision.types 替换为 vision_v1 才能使其工作。仅供以后遇到问题的人参考。
    猜你喜欢
    • 2020-02-08
    • 2019-11-06
    • 1970-01-01
    • 2023-01-01
    • 2018-02-14
    • 2014-08-01
    • 2019-12-02
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多