首页 > 解决方案 > OCR 空间 API Python 不打印文本

问题描述

我正在尝试发送图像以ocr.space使用他们的 api 和 python。这是我的代码。代码运行没有错误,但不打印任何内容。为什么?我怎样才能做到print

import requests
import json

def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
    """ OCR.space API request with local file.
        Python3.5 - not tested on 2.7
       :param filename: Your file path & name.
       :param overlay: Is OCR.space overlay required in your response.
                Defaults to False.
       :param api_key: 2fe3ae129e88957
                Defaults to 'helloworld'.
       :param language: Language code to be used in OCR.
                List of available language codes can be found on https://ocr.space/OCRAPI
                Defaults to 'en'.
       :return: Result in JSON format.
    """

    payload = {'isOverlayRequired': overlay,
           'apikey': api_key,
           'language': language,
           }
    with open(filename, 'rb') as f:
        r = requests.post('https://api.ocr.space/parse/image',
                      files={filename: f},
                      data=payload,
                      )
    return r.content.decode()


def ocr_space_url(url, overlay=False, api_key='helloworld', language='eng'):
    """ OCR.space API request with remote file.
        Python3.5 - not tested on 2.7
    :param url: Image url.
    :param overlay: Is OCR.space overlay required in your response.
                Defaults to False.
    :param api_key: 2fe3ae129e88957
                Defaults to 'helloworld'.
    :param language: Language code to be used in OCR.
                List of available language codes can be found on https://ocr.space/OCRAPI
                Defaults to 'en'.
    :return: Result in JSON format.
    """

    payload = {'url': url,
           'isOverlayRequired': overlay,
           'apikey': api_key,
           'language': language,
           }
    m = r.content.decode()
    jsonstr = json.loads(m)
    print (jsonstr["ParsedResults"][0]["ParsedText"])

ocr_space_file(filename='example_image.png', language='eng')

这是我的代码的粘贴箱。

标签: python

解决方案


我修好了它。我修复了代码中的最后一行。这是我新的最后一行 - (ocr_space_file(filename='example_image.png', language='eng'))


推荐阅读