1

I am trying to access a video from an IP camera. I am using OpenCV and Python to do so. The code that I have tried is given below:

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

On running this code the output that I am getting is:

Image not converted

On using another method, CaptureFromCAM instead of CaptureFromFile the code is:

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)

When I run this code the error I am getting is:

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

I read about it, and the SampleCB() error is in the case when the buffer size does not match the expected input size. I tried to change the streaming resolution, but nothing seems to work. I followed this thread and this thread. They are giving the C++ code and on conversion to Python (the code given above) it does not work. Or the thread gives the code for motion detection. I am using Windows 7 and Eclipse with Pydev for development. What do I do?

Community
  • 1
  • 1
praxmon
  • 5,009
  • 22
  • 74
  • 121
  • http://stackoverflow.com/a/11449901/773226 – Anoop K. Prabhu Feb 12 '14 at 08:08
  • @AnoopK.Prabhu Same SampleCB() error – praxmon Feb 12 '14 at 08:09
  • @PrakharMohanSrivastava Please find and post a link to the manual for your camera. You may be using the wrong parameters in your URL? Also, this sounds like one small detail is probably wrong, so [here is a chat room](http://chat.stackoverflow.com/rooms/info/47550/http-stackoverflow-com-questions-21721813-ip-camera-python-error?tab=general) for anyone to discuss it so we don't need to use comments for discussion. – KobeJohn Feb 15 '14 at 11:16
  • Prakhar told me the camera is M1103 so [this should be the manual](http://www.axis.com/en/files/manuals/um_m1103_46766_en_1206.pdf). – KobeJohn Feb 17 '14 at 04:38
  • @PrakharMohanSrivastava I saw your other question about putting your video into tkinter. Does that mean that this part is working now? If so, could you accept berak's answer or post your own answer with the solution? I know getting started with OpenCV can be frustrating so this might help a lot of other people in the future. – KobeJohn Feb 24 '14 at 12:35
  • @kobejohn No, sadly this code is not working thus I did not accept, I wrote other code which is working. I am using urllib to strip the packets and view the images using opencv. Wait, the code is in the answer to the question given: http://stackoverflow.com/questions/21702477/ip-camera-capture-using-opencv-and-python – praxmon Feb 25 '14 at 04:08

2 Answers2

1

Oh, please stick with the cv2 API. The old cv one is no more available in current OpenCV versions:

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
berak
  • 39,159
  • 9
  • 91
  • 89
  • The program terminates without giving any output? Are you sure your code is correct? Or do I need to add something? – praxmon Feb 12 '14 at 09:16
  • it could not open your url then – berak Feb 12 '14 at 09:22
  • I checked using my web browser, it is opening the url. Can something else go wrong? – praxmon Feb 12 '14 at 09:25
  • Yes, it seems there is a problem with the url. `camera.isOpened` is false. The thing is it is working perfectly when I am pasting the link on the browser. Any other ideas? – praxmon Feb 13 '14 at 12:57
  • @amitnair92 sadly no, I could not solve it and then moved on to something else. – praxmon Jul 12 '18 at 16:51
0

Look this example with python and OpenCV, IPCAM hikvision

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

See in the window: Get video from IPCAM with python and OpenCV

  • Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Jun 05 '18 at 14:52