从 Web 请求调用时,OpenCV imread 挂起 [英] OpenCV imread hanging when called from a web request

查看:35
本文介绍了从 Web 请求调用时,OpenCV imread 挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是我在使用 OpenCV 时遇到的最奇怪的错误之一.有很多事情要发生,所以让我尽力解释一下.

  1. 我同时使用 Django Web 框架和 OpenCV (cv2).我正在尝试从 Django 的视图中读取磁盘上的文件.

    imagePath = os.path.dirname(__file__) + "/1.jpg"

    基本上,在与 views.py 文件相同的路径中有一个名为1.jpg"的文件.这就是这段代码所做的一切.很容易.但下一步是事情变得疯狂.

  2. 现在,我想读取位于imagePath"的图像文件.这需要调用 cv2.imread

    image = cv2.imread(imagePath)

    但这就是我的问题开始的地方.不知何故,Apache(或者甚至是 OpenCV,我不知道)开始挂起并且文件永远不会加载.没有错误消息,什么也没有.

做了一些侦探工作,我决定尝试旧版本的 OpenCV(导入 cv).奇怪的是,当我调用 cv.LoadImage(imagePath) 时,Apache 没有挂起,我的图像加载得很好.我完全不知道为什么.

解决我的问题的一个潜在解决方法是使用 PIL.

from PIL import Image将 numpy 导入为 np图像 = Image.open(imagePath)图像 = np.asarray(图像)

再次,使用 PIL Apache 不会挂起,我可以像往常一样处理表示为 numpy 数组的图像,并对其应用任何 cv2 函数.

然而,我不是一个满足于解决方法的人,而且 cv2.imread 挂起的事实真的让我很困扰.

有人遇到过这种情况吗?

在 Python shell 中使用 cv.imread 工作正常,只是来自 Apache 请求导致挂起.

<预><代码>>>>导入 cv2>>>image = cv2.imread("1.jpg")>>>图像.形状(400, 344, 3)>>>

解决方案

我遇到了类似的问题并找到了解决方法 -> 只需添加到您的 apache 配置中:

WSGIScriptAlias application-group=%{GLOBAL}

显然,当您的扩展模块不是设计用于在子解释器中工作时,就会发生这种情况.以上强制它在主解释器中运行.

来源:django apache mod-wsgi 挂起从 .so 文件导入 python 模块http://blog.rtwilson.com/how-to-fix-flask-wsgi-webapp-hanging-when-importing-a-module-such-as-numpy-or-matplotlib/

This is probably one of the strangest errors that I have ever ran into when using OpenCV. There is a lot going on, so let me try to explain this to the best of my ability.

  1. I am using the Django web framework and OpenCV (cv2) together. I am trying to read a file off my disk from a view in Django.

    imagePath = os.path.dirname(__file__) + "/1.jpg"
    

    Basically, in the same path as views.py file there is a file called "1.jpg". That is all this code is doing. Easy enough. But the next step is where things get crazy.

  2. Now, I want to read the image file located at 'imagePath'. This requires a call to cv2.imread

    image = cv2.imread(imagePath)
    

    But this is where my problems start. Somehow, Apache (or maybe even OpenCV, I can't tell) starts hanging and the file is never loaded. There is no error message, no nothing.

Doing some detective work I decided to try out an older version of OpenCV (import cv). Strangely enough, when I call cv.LoadImage(imagePath) Apache does not hang and my image is loaded just fine. I have absolutely no idea why.

A potential work around for my problem is to use PIL.

from PIL import Image
import numpy as np
image = Image.open(imagePath)
image = np.asarray(image)

One again, using PIL Apache does not hang and I can proceed as normal with my image represented as numpy array and apply any of the cv2 functions to it.

However, I'm not one to settle for workarounds and the fact that cv2.imread is hanging really bothers me.

Has anyone ran into this before?

EDIT: Using cv.imread from a Python shell works fine, it's just from an Apache request that the hang happens.

>>> import cv2
>>> image = cv2.imread("1.jpg")
>>> image.shape
(400, 344, 3)
>>> 

解决方案

I had a similar issue and found a fix -> just add to your apache configuration:

WSGIScriptAlias application-group=%{GLOBAL}

Apparently it happens when you have an extension module not designed to work in sub interpreter. The above forces it to run in main interpreter.

Sources: django apache mod-wsgi hangs on importing a python module from .so file http://blog.rtwilson.com/how-to-fix-flask-wsgi-webapp-hanging-when-importing-a-module-such-as-numpy-or-matplotlib/

这篇关于从 Web 请求调用时,OpenCV imread 挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆