[英]cv2 in django 504 gateway timeout
opencv in my project django is very slow, and very times the url is not load and return 504 gateway timeout, and server apache2 is very slow when the url of cv is loading. opencv in my project django is very slow, and very times the url is not load and return 504 gateway timeout, and server apache2 is very slow when the url of cv is loading. Why is that?
这是为什么?
The code does not raise any errors.该代码不会引发任何错误。
This is my code:这是我的代码:
from PIL import Image
import numpy
import cv2
import pytesseract
import os
def get_string(imagen):
# convert the image to a NumPy array and then read it into
# OpenCV format
image = numpy.asarray(bytearray(imagen.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# convert the image to grayscale and flip the foreground
# and background to ensure foreground is now "white" and
# the background is "black"
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
# threshold the image, setting all foreground pixels to
# 255 and all background pixels to 0
thresh = cv2.threshold(
gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# grab the (x, y) coordinates of all pixel values that
# are greater than zero, then use these coordinates to
# compute a rotated bounding box that contains all
# coordinates
coords = numpy.column_stack(numpy.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
# the `cv2.minAreaRect` function returns values in the
# range [-90, 0); as the rectangle rotates clockwise the
# returned angle trends to 0 -- in this special case we
# need to add 90 degrees to the angle
if angle < -45:
angle = -(90 + angle)
# otherwise, just take the inverse of the angle to make
# it positive
else:
angle = -angle
# rotate the image to deskew it
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(
image, M, (w, h),
flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
# load the rotated image and convert it to grayscale
gray_rotated = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
gray_rotated = cv2.threshold(
gray_rotated, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# write the rotated grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "/srv/archivos/prueba_{}".format(imagen.name)
cv2.imwrite(filename, gray_rotated)
text = pytesseract.image_to_string(Image.open(filename), lang='spa')
os.remove(filename)
return text
Thanks and sorry for my English感谢和抱歉我的英语
heyyy.!嘿嘿。! the solutions is in apache2.conf add this line
解决方案在 apache2.conf 添加这一行
WSGIApplicationGroup %{GLOBAL}
More information in this post: https://serverfault.com/questions/844761/wsgi-truncated-or-oversized-response-headers-received-from-daemon-process这篇文章中的更多信息: https://serverfault.com/questions/844761/wsgi-truncated-or-oversized-response-headers-received-from-daemon-process
im very happy!!!我很开心!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.