使用cv2.imread加载图像

时间:2018-09-20 06:39:32

标签: python python-3.x opencv flask cv2

当我尝试使用cv2.imread从路径加载图像时,它显示为“ none”。 我创建了UI以使用flask上传图像。

这就是我所做的:

UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__))
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])

app = Flask(__name__)
#app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/')
def uploadfile():
    return render_template('home.html')

@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
    target = os.path.join(UPLOAD_FOLDER, 'static/')
    upload = os.path.join(target, 'uploads/')
    if not os.path.isdir(target):
        os.mkdir(target)
        os.mkdir(upload)
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(upload, filename))

    image=cv2.imread('/home/user/image/static/uploads/filename')
    print("image",image)

我的图像未加载到“图像”中。打印结果是

image,None ,

2 个答案:

答案 0 :(得分:2)

<filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowGenericHttpRequests</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.allowSubdomains</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, HEAD, POST, DELETE, PUT, OPTIONS</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Content-Type, X-Requested-With</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>X-Test-1, X-Test-2</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.maxAge</param-name> <param-value>-1</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 产生的NoneType错误中,大约95%来自无效的文件路径 (OpenCV: Resolving NoneType errors)。也就是说,路径规范错误或缺少某些内容,或者文件名不完整-可能缺少扩展名( png jpg 等)

current list of supported image file types包括:

  • Windows位图-* .bmp,*。dib
  • JPEG文件-* .jpeg,*。jpg,*。jpe
  • JPEG 2000文件-* .jp2
  • 便携式网络图形-* .png
  • WebP-* .webp
  • 便携式图像格式-* .pbm,*。pgm,*。ppm * .pxm,*。pnm
  • 太阳栅格-* .sr,*。ras
  • TIFF文件-* .tiff,*。tif
  • OpenEXR图像文件-* .exr
  • 辐射HDR-* .hdr,*。pic
  • Gdal支持的栅格和矢量地理空间数据

如果支持图像类型,请检查路径和文件名是否正确。

答案 1 :(得分:0)

我的路径不正确

image=cv2.imread(os.path.join(upload,filename))

以上解决了我的问题