django - 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)
这让我认为pdf2image或poppler-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
附加信息:
- Ubuntu 18.04.4 LTS
- Django 3.6.9
- 阿帕奇/2.4.29
谢谢!
解决方案
推荐阅读
- javascript - 带条件的 removeClass
- javascript - 通过多个输入进行数学运算
- c++ - 如何生成展开循环的程序?
- jenkins - 如何在私有云上访问 Kubernetes 上的 Jenkins Dashboard?
- linux - 取消设置 C-Shell 中的所有环境变量
- python - 调整 Pandas 中的数据框列
- python - 将 Beautiful Soup 用于不滚动就无法完全加载的网站
- c - 我在代码中犯了什么错误。它没有给出任何输出?
- python - ValueError:没有足够的值来解包(预期 2,得到 0)
- python - 基于列表的DataFrame列内容的条件替换