相关文章推荐

Getting Started with the Camera and Capturing still Jpeg Image.

I recently purchase an IP camera with the hope that I can connect it to a PC and create a python script.

I did search online how I can access this camera model wnc-01. And the popular way is via “ispyconnect.com”.

I hit on google search bar “wnc-01 camera ispy” and only got a jpg access. reference adamas jpg.JPG This works pretty ok if you want still jpg image but not for me that whats video stream.

The url to use is “ http://192.168.1.88/tmpfs/auto.jpg&#8221 ;

I then tried to access it via OpenCV in a python script.

cv2.imread

However OpenCV command will not work directly since this is a http request.

I need to do urlopen() function.

This is a working code to access the jpeg image.

import urllib import cv2 import numpy as np while 1: url = "http://192.168.1.30/tmpfs/auto.jpg" #IP address needs to be fixed! url_response = urllib.urlopen(url) img_array = np.array(bytearray(url_response.read()), dtype=np.uint8) img = cv2.imdecode(img_array, -1) cv2.imshow('URL Image', img) cv2.waitKey()

Accessing the Video Stream of the Camera in Real-time.

I have to proceed how I can access it in real-time.

I tried to make it work using onvif.

The model of my IP cam is wnc-01. It is onvif compliant so I tried to check if it is true and not Chinese marketing gimmick using the application “Onvif Device Manager”.

It is very important to note the user and password of the camera. I leave it on default user: admin and default password: admin for me not to forget during development.

It works like a charm and i can stream live video and ptz control works as well.

The next challenge it to make a python script so that I can make my own software.

After trying few scripts. This one works on live viewing.

import cv2, time cap = cv2.VideoCapture("rtsp://admin:[email protected]:554/11") time.sleep(2) while(True): ret, frame = cap.read() print ret if ret == 1: cv2.imshow('frame',frame) else: print "no video" if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

Working on the Control of the IP Cam via PTZ.

Now I have the stream for video. I need to work on controlling the camera movement via PTZ.

Google search again helped me to try something.

I found out that I need the python package onvif.

Since I am working on Anaconda Distribution on all my python projects, ideally I must use conda install to do add it but onvif package is not in conda yet so i need to do it in via pip.

Few things to do next is run cmd in admin rights then enter the following.

conda install pip

This command will update the packages inside my PC.

Next is to enter command

pip install onvif

This will start to download the onvif package.

I then need to make a python script to test the IP Camera movements.

There’s a github repo that has a ptz example. This is what I need.

https://github.com/quatanium/python-onvif/blob/master/examples/continuous_move.py

As always, python scripts you find online will not always work like a charm.

I encounter the error below whenever I start to initialize my ONVIF Camera.

Something is missing so I perform a search on my drive C:/.

The wsdl folder is located on Anaconda2 folder when onvif python package was extracted, it must be on the site-packages forlder.

I then move it from

C:\ProgramData\Anaconda2\wsdl

to the site-packages folder.

C:\ProgramData\Anaconda2\Lib\site-packages\wsdl

It now compiles but i still have error.

What I did is just comment it out to remove the errors that avoid me to run the script. It was used twice on the sample script so I have to comment them both.

After those fixes I was able to run the script but still it is not working. Then I realized I have to update the ip addres, user name, password and the port used on the sample script from github.

With the correct IP Address, login credentials and port, I was able to move the camera. But another challenge arrived, it wont stop. When I send a command to go left, it will continue to go left until it reaches the physical limit. This is because I removed the

ptz.Stop({'ProfileToken': request.ProfileToken})

function that causes error on my first attempt to run it.

I searched online again. I found out that a timeout will make the movement to stop if  “ptz.Stop” is removed.  However, this is not true in my testing.

It took me almost 6 hours trying to make it work. I am already slowly starting to abandon the idea of using the ptz to control the camera and just use Arduino with two servo to do pan and tilting feature. I do not like the arduino approach since I will waste the built-in capability of the IP camera.

More struggle and TAE ahead. (TAE = Trial and Error).

I discovered that it was the profile token for the ptz.Stop that is failing so I focused on it. The light arrived after reading many documentation of Onvif standard. I tried to put parameter to the ptz.Stop and it worked!

I need to replace

ptz.Stop({'ProfileToken': request.ProfileToken}) request.PanTilt = 1 # Stop continuous move ptz.Stop(request)

The modified script works now basic movement. I just set the timeout to 2 and it is now stopping after 2 second of sleep.

This is the final code.

from time import sleep from onvif import ONVIFCamera XMAX = 1 XMIN = -1 YMAX = 1 YMIN = -1 def perform_move(ptz, request, timeout): # Start continuous move ptz.ContinuousMove(request) # Wait a certain time sleep(timeout) request.PanTilt = 1 # Stop continuous move ptz.Stop(request)#{'ProfileToken': request.ProfileToken}) def move_up(ptz, request, timeout=2): print 'move up...' request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMAX perform_move(ptz, request, timeout) def move_down(ptz, request, timeout=2): print 'move down...' request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMIN perform_move(ptz, request, timeout) def move_right(ptz, request, timeout=2): print 'move right...' request.Velocity.PanTilt._x = XMAX request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout) def move_left(ptz, request, timeout=2): print 'move left...' request.Velocity.PanTilt._x = XMIN request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout) def continuous_move(): mycam = ONVIFCamera('192.168.1.30', 8080, 'admin', 'admin') # Create media service object media = mycam.create_media_service() # Create ptz service object ptz = mycam.create_ptz_service() # Get target profile media_profile = media.GetProfiles()[0]; print media_profile # Get PTZ configuration options for getting continuous move range request = ptz.create_type('GetConfigurationOptions') request.ConfigurationToken = media_profile.PTZConfiguration._token ptz_configuration_options = ptz.GetConfigurationOptions(request) request = ptz.create_type('ContinuousMove') request.ProfileToken = media_profile._token # ptz.Stop({'ProfileToken': media_profile._token}) # Get range of pan and tilt # NOTE: X and Y are velocity vector global XMAX, XMIN, YMAX, YMIN XMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Max XMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].XRange.Min YMAX = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Max YMIN = ptz_configuration_options.Spaces.ContinuousPanTiltVelocitySpace[0].YRange.Min # move right move_right(ptz, request) # move left move_left(ptz, request) # Move up move_up(ptz, request) # move down move_down(ptz, request) if __name__ == '__main__': continuous_move()

We have three scripts here.

1. Access the jpeg shot.

2. Stream video via OpenCV.

3. Control camera via Python Onvif PTZ.

I am now thinking to buy lots of this cheap IP Camera.

I can now integrate this code on future projects that will use IP Camera.

Useful links.

The Python package of onvif
https://pypi.org/project/onvif/

Onvif list of available configurations and operations.
https://www.onvif.org/onvif/ver20/util/operationIndex.html

I write articles and make easy to follow guides and tutorials about Internet of Things - LoRa LoRaWAN WiFi Bluetooth BLE Cellular IoT NB-IoT LTE-M Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
 
推荐文章