IP摄像头Python错误

时间:2014-02-12 07:53:54

标签: python c++ opencv video-streaming ip-camera

我正在尝试从IP摄像头访问视频。我正在使用OpenCV和Python这样做。我尝试过的代码如下:

import numpy as np
import cv2
from cv2 import cv

camera=cv.CaptureFromFile("http://root:root@192.168.0.90/axis-cgi/mjpg/video.cgi?resolution=640x480&req_fps=30&.mjpg")
if camera is None:
    print 'Camera is null'
else:
    print 'Camera is not null'
cv.NamedWindow("win")

while True:
    image=cv.QueryFrame(camera)
    cv.ShowImage("win", image)
    k=int(cv.WaitKey(10))
    if k is 27:
        break

在运行此代码时,我得到的输出是:

Image not converted

使用其他方法时,CaptureFromCAM代替CaptureFromFile代码为:

import numpy as np
import cv2
from cv2 import cv

camera=cv.CaptureFromCAM(0)
if camera is None:
    print 'Camera is null'
else:
    print 'Camera is not null'
cv.NamedWindow("win")

while True:
    image=cv.QueryFrame(camera)
    if image is None:
        print 'No conversion to IPL Image'
        break
    else:
        cv.ShowImage("win", image)

当我运行此代码时,我得到的错误是:

ERROR: SampleCB() - buffer sizes do not match
No conversion to IPL Image

我读到了它,并且当缓冲区大小与预期输入大小不匹配时出现SampleCB()错误。我试图改变流分辨率,但似乎没有任何效果。我跟着this线程和this线程。他们提供C ++代码并转换为Python(上面给出的代码)它不起作用。或者线程提供运动检测的代码。我正在使用 Windows 7和Eclipse与Pydev 进行开发。我该怎么办?

2 个答案:

答案 0 :(得分:1)

哦,请坚持使用cv2 API。在当前的OpenCV版本中,旧的cv one不再可用:

import numpy as np
import cv2

cv2.namedWindow("win")
camera = cv2.VideoCapture("http://username:pass@192.168.0.90/axis-cgi/mjpg/video.cgi?resolution=640x480&req_fps=30&.mjpg")
while camera.isOpened():
    ok, image = camera.read()
    if not ok:
        print 'no image read'
        break
    cv2.imshow("win", image)
    k = cv2.waitKey(1) & 0xff
    if k == 27 : break # Esc pressed

答案 1 :(得分:0)

使用python和OpenCV,IPCAM海康威视

查看此示例
import numpy as np 
import cv2

cap = cv2.VideoCapture() 
cap.open("rtsp://USER:PASS@IP:PORT/Streaming/Channels/2")

while(True):
     # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('Salida',frame)

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

# When everything done, release the capture 
cap.release() 
cv2.destroyAllWindows()

在窗口中查看: Get video from IPCAM with python and OpenCV