首页 > 解决方案 > pytesseract 从 pdf 读取自动生成的图像后 Django 应用程序崩溃

问题描述

当用户将文件上传到我的系统时,我会启动一个新线程来扫描它们以找到特定模式,同时处理上传的视图返回并且应用程序被重定向到主页。

thread = threading.Thread(target=fucn1, args=(uuid,))  
thread.start()
return Response(serializer.data, status=status.HTTP_201_CREATED)

在 func1() 中,如果文件是 PDF,我将其分成页面,然后保存并分析每个页面,如下所示:

def func1(uuid):
    documents = Document.objects.filter(model_uuid=uuid)
    result = set()
    for document in documents:
        if extension == 'pdf':
            pages = convert_from_path(filepath,500)
            for i in range(len(pages)):
                page = pages[i]
                img_filepath = local_directory + 'page_' + str(i) + '.jpg'
                page.save(img_filepath, 'JPEG')
                values = analize(img_filepath)
                result = set.union(result, values)
        else:
            values = analize(filepath)
            result = set.union(result, values)


def analize(filepath):
    pattern = "xxxxxxxxxxxx"
    # Load image
    img = cv2.imread(filepath)
    # Extract text from image
    sret = pytesseract.image_to_string(img)
    return re.findall(pattern, sret)

如果输入文件已经是图像(png、jpg、jpeg),则省略分页,一切正常。但是,如果文件是 PDF,系统会在此处崩溃:

sret = pytesseract.image_to_string(img)

这让我认为pdf2imagepoppler-utils存在问题。但是,当输入文件是图像时,此函数也会成功执行。

Apache2 error.log 打印这个:

从守护进程“process_name”收到的截断或过大的响应标头:/home/ubuntu/proejctname/appname/appname/wsgi.py,引用者:https ://xxxxxxxx.es/yyyyyyy/

进口:

from pdf2image import convert_from_path
import boto3
import cv2
import os
import pytesseract
import re
import sys
import numpy as np

附加信息:

谢谢!

标签: djangoopencvwsgipython-tesseractpdf2image

解决方案


推荐阅读