OCR空间响应解析结果[0]错误

2024-09-29 23:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写一个程序,它扫描收据,并依赖于使用OCRSpace API的OCR响应。在过去的几百次尝试中,它工作得非常好,但是当从iphone而不是电脑将图像上传到我的flask服务器时,图像内容没有OCR结果。我曾尝试在他们的网站上使用相同的图像,它给出了正常的响应,但在我的flask应用程序中它返回

parsed_results = result.get("ParsedResults")[0]
TypeError: 'NoneType' object is not subscriptable

我正在使用代码:

img = cv2.imread(file_path)

                height, width, _ = img.shape
                roi = img[0: height, 0: width]
                _, compressedimage = cv2.imencode(".jpg", roi, [1, 90])
                file_bytes = io.BytesIO(compressedimage)
                url_api = "https://api.ocr.space/parse/image"
                result = requests.post(url_api,
                                files = {os.path.join(r'PATH', file_name): file_bytes},
                                data = {"apikey": "KEY",
                                        "language": "eng",
                                        #"OCREngine": 2,
                                        "isTable": True})
                result = result.content.decode()
                result = json.loads(result)
                parsed_results = result.get("ParsedResults")[0]
                global OCRText
                OCRText = parsed_results.get("ParsedText")

提前谢谢你的帮助


Tags: path图像apiflaskimggetresultparsed
2条回答

iPhones and iPads as of iOS 11 use HEIF as standard; there are no incompatibilities when transferring to PC or sending e.g. by sharing, as the images are converted to the widely supported JPEG format; however, incompatibilities arise when using cloud services e.g. Google Photos.

High Efficiency Image File Format (HEIF)

As@rob247发布的iPhone默认使用HEIF格式(官方链接here) 因此,当您将照片上载到脚本时,请尝试在使用之前将其转换为JPEG,因为opencv不支持*heif、*avif、*heic,但请参见问题#14534。如果您喜欢其他格式,请在opencvimread查看支持的格式列表

相关问题 更多 >

    热门问题