Site icon AranaCorp

Managing an IP Camera with Python

Whether you want to monitor your flat or the progress of your 3D printing, it can be useful to use an IP camera to have the images available from anywhere. In this tutorial, we will use an IP camera from which we will retrieve the images via Python.

This tutorial can be applied on a computer or a Raspberry Pi.

Material

Wansview Q5 Camera Configuration

Follow the guide provided with the camera and software to connect the camera to the Wifi network and synchronise your camera with the software. Once the camera is configured, you can retrieve information such as IP address and ID in the application or software.

In the camera settings, you will find the IP address and MAC address under “Device Information”.

In the “Local Application” menu, you will find the user name and password under “Local Account” and the RTSP stream under “RTSP”:

The url of the RTSP stream is of this form

rtsp://user:password@ipaddress:rtspPort

This last piece of information, which brings together all the others, will allow us to access the camera’s images.

N.B.: Please note that if the camera is powered off, the IP address can be changed during the reboot. You will therefore need to change the IP address in your code.

I won’t go into the details of the configuration as it is unlikely that you will be using the same camera and the guide generally provided is sufficient. However, if you encounter any problems, please feel free to tell us in the comments.

Configuring IP Camera Viewer 4

Generally, each camera comes with its own software and smartphone application with all the necessary features. For practical reasons, you may want to use another software. I will show you how to set up a camera on IP Camera Viewer which works with a multitude of IP camera brands.

The camera is connected to the network and we can retrieve the images using the software. We will now see how to retrieve the images from the camera using a Python script.

Python code to read the RTSP stream

To retrieve the images, we will use the OpenCV library which allows us to create image processing algorithms. To access the RTSP stream, we just need to specify the URL of the camera.

import cv2

def main(args):

	#cap = cv2.VideoCapture(0) #default camera
	cap = cv2.VideoCapture('rtsp://user:password@ipaddress:rtspPort') #IP Camera
	
	while(True):
		ret, frame = cap.read()
		frame=cv2.resize(frame, (960, 540)) 
		cv2.imshow('Capturing',frame)
		
		if cv2.waitKey(1) & 0xFF == ord('q'): #click q to stop capturing
			break

	cap.release()
	cv2.destroyAllWindows()
	return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

Once the script has started, a window will appear with the video from the IP camera’s RTSP stream. To close the window, simply press the “q” key.

Once you have access to the camera images, you can do image processing to recognise objects, detect movement, etc.

Example of application

Colour recognition and monitoring for a future project

Applications

Sources

Exit mobile version