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

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

问题描述

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

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. 我正在使用Django Web框架和OpenCV(cv2)在一起。我试图从Django的视图中读取我的磁盘上的文件。

  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"

基本上,与views.py文件相同的路径有文件称为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.

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

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

image = cv2.imread(imagePath)

但这是我的问题开始的地方。不知何故,Apache(或者甚至OpenCV,我不能告诉)开始挂起,文件从未加载。没有错误消息,没有任何东西。

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.

做一些侦探工作我决定尝试一个较旧版本的OpenCV (进口cv)。奇怪的是,当我调用cv.LoadImage(imagePath)Apache不挂起,我的图像加载就好了。我绝对不知道为什么。

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.

我的问题的一个潜在的工作是使用PIL。

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)

再一次,使用PIL Apache不挂起,可以像我的图像一样正常进行,以numpy数组为代表,并应用任何cv2函数。

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.

但是,我不是一个解决方案的解决方案那个cv2.imread挂起真的很麻烦我。

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

有没有人碰过这个?

编辑:使用来自Python shell的cv.imread工作正常,只是从Apache请求发出挂起。

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)
>>> 


推荐答案

错误

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

正确

from os.path import abspath, join, dirname

imagePath = abspath( join(dirname(__file__), "1.jpg") )

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

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