python cv2.Videocapture()不起作用,cap.isOpened()返回false

时间:2017-02-13 18:22:21

标签: python-2.7 opencv

cv2.Videocapture()在使用网络摄像头时工作正常但在尝试从硬盘驱动器读取时显示错误cap.isOpened()返回false

import cv2
import numpy as np
background=cv2.imread('background.png')
cap = cv2.VideoCapture('car video.mp4')
cap.open('car video.mp4')
print cap.isOpened()
while 1:
    ret,img=cap.read()
    cv2.imshow('a',img)
    print img.shape


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

显示此错误

    cv2.imshow('a',img)
    error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:266: error:       (-215)        size.width>0 && size.height>0 in function cv::imshow

我的opencv版本3.0.0,python 2.7,windows10 32位

4 个答案:

答案 0 :(得分:4)

你需要ffmpeg编解码器才能运行视频

答案 1 :(得分:1)

我不确定您是否正确编写了文件名。我从未见过像' car video.mp4' 这样的文件目录。当您使用零基索引时,您的网络摄像头和cv2.VideoCapture工作正常;但VideoCapture无法读取像' car(space)video.mp4' 这样的文件。工作代码是这样的;

import numpy as np
import cv2

cap = cv2.VideoCapture('video.mp4')

while(cap.isOpened()):

    ret, frame = cap.read()

    if ret==True:

        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()

答案 2 :(得分:1)

在anaconda3虚拟环境中使用opencv时遇到相同的错误。 我检查了当前opencv版本的buildinformation,并将ffmpeg标记为“否”

要解决此问题

  1. 我从conda环境中卸载了opencv(conda卸载opencv)
  2. 使用conda-forge频道安装了最新的ffmpeg(conda install -c conda-forge ffmpeg)

    名称版本建立渠道

    ffmpeg 4.0.2 ha6a6e2b_0 conda-forge

  3. 然后使用conda-forge通道再次安装opencv(conda install -c conda-forge opencv)

    名称版本建立渠道

opencv 3.4.1 py36_blas_openblash829a850_201 [blas_openblas] conda-forge

执行此操作后重新启动python控制台并导入cv2。

答案 3 :(得分:0)

尝试

import java.util.logging.Logger
import io.grpc.{Server, ServerBuilder}
import org.apache.spark.ml.tuning.CrossValidatorModel
import org.apache.spark.sql.SparkSession
import testproto.test.{Email, EmailLabel, RouteGuideGrpc}
import scala.concurrent.{ExecutionContext, Future}

object HelloWorldServer {
  private val logger = Logger.getLogger(classOf[HelloWorldServer].getName)

  def main(args: Array[String]): Unit = {
    val server = new HelloWorldServer(ExecutionContext.global)
    server.start()
    server.blockUntilShutdown()
  }
  private val port = 50051
}

class HelloWorldServer(executionContext: ExecutionContext) {
  self =>
  private[this] var server: Server = null

  private def start(): Unit = {
    server = ServerBuilder.forPort(HelloWorldServer.port).addService(RouteGuideGrpc.bindService(new RouteGuideImpl, executionContext)).build.start
    HelloWorldServer.logger.info("Server started, listening on " + HelloWorldServer.port)
    sys.addShutdownHook {
      System.err.println("*** shutting down gRPC server since JVM is shutting down")
      self.stop()
      System.err.println("*** server shut down")
    }
  }

  private def stop(): Unit = {
    if (server != null) {
      server.shutdown()
    }
  }

  private def blockUntilShutdown(): Unit = {
    if (server != null) {
      server.awaitTermination()
    }
  }

  private class RouteGuideImpl extends RouteGuideGrpc.RouteGuide {
    override def getLabel(request: Email): Future[EmailLabel] = {
      val replay = EmailLabel(emailId = request.emailId, label = "aaaaa")
      Future.successful(replay)
    }
  }
}

对我有用