I started working with OpenCV and cameras last year and I had some trouble getting started.  I thought it might help some others if I posted what I had to go through to get this to work.  I found that most people use USB cameras for use with OpenCV.  While this is easier, it wouldn’t work in the application I was wanting to use cameras.  I decided to use some IP cameras.  I found these on Amazon.  They were relatively inexpensive, HD, and outdoor rated.  Once I got it I had to fight with it to get OpenCV to read it.

The camera arrived with no documentation.  The company’s website was no help.  I was able to log into the camera to do things, but I couldn’t pull up a stream.  The best troubleshooting tool for this was VLC.  WIth VLC I should have been able to open a ‘RTSP’ stream from the camera, but I couldn’t.  It was frustrating.  Eventually I just sat there reading the reviews on Amazon hoping someone else had this issue and actually posted the information I needed.  About 11 pages in I found the RTSP addresses.

 

 

Main Stream – rtsp://IP:554/11 (1920×1080)
Sub Stream – rtsp://IP:554/12 (640×360)

Now that I have the ‘RTSP’ addresses I was about to open the streams with VLC and then I started working on opening it with Python.  Go ahead and try to pip install the OpenCV package.

pip install opencv-python

After installing it try to import it into python.  If you have any issues you should try to install the package manually using Christoph Gohlke Wheel Collection and pip installing the downloaded file.

import cv2

The IP address for my camera in the example is ‘192.168.0.101’ and so the stream address will be ‘rtsp://192.168.0.101:554/12’.  I like to take the time to break the commands down as much as I can to make the script as portable as I can.  This is why I break it down into different variables.

#IP Camera Information
scheme = '192.168.1.'
stream = '12'

Now we need to start capturing the stream.

#'101' is the last octet of the IP for the stream.
host = scheme+'101' 
cap = cv2.VideoCapture('rtsp://'+host+':554/'+stream) 

Next we start a ‘While True’ loop for displaying the stream.  It is important to start it in a loop so that you can break the loop to release the stream on demand.

The ‘cv2.imshow’ command shows the video stream.

Here is the anatomy of the ‘cv2.imshow’ function.  ‘cv2.imshow’ takes two arguments.  The first is the name you want to show on the top of the window.  You can change this to whatever you would like, but you have to have it.  The second is the object you stored the video capture stream in.  In this example it is called ‘frame’.

Then the script looks for a key press.  So when the escape key (b27) then it releases the captured stream and then runs ‘cv2.destroyAllWindows()’.  If you don’t have the part in your script you may end up having the stream causing a lot of lag on your PC until the stream is force closed or the stream dies from natural causes.

while True:
    _, frame = cap.read()



    #Place options to overlay on the video here.
    #I'll go over that later.



    cv2.imshow(('Camera'+str(camera_id)), frame01)

    k = cv2.waitKey(0) & 0xFF
    if k == 27: #esc key ends process
        cap.release()
        break
cv2.destroyAllWindows()