MOTIONEYE-LIGHT PAN/TILT
by JJ Slabbert in Circuits > Raspberry Pi
5824 Views, 4 Favorites, 0 Comments
MOTIONEYE-LIGHT PAN/TILT
This Instructable requires Raspberry PI OS Lite (The console version, with no Desktop). It is most suitable for a headless server. For Raspberry PI OS DESKTOP, check https://www.instructables.com/MOTIONEYE-NVR-PANTILT/
motionEye is a web based NVR (Network Video Recorder). It is a web front end for the motion daemon. It is developed in python2.7
More detail on motionEye can be found at:
https://github.com/ccrisan/motioneye
Instalation instructions can be found at:
https://github.com/ccrisan/motioneye/wiki/Installation
motionEye capture video streams via RTSP (Real Time Streaming Protocol) or HTTP. RTSP is a pure streaming protocol and pan/tilt commands can not be send to the camera via RTSP. You may or may not be able to find the HTTP commands for your specific camera's pan / tilt commands in your cameras documentation. To avoid this complexities, it is easier to send ONVIF commands to an ONVIF compliant camera
The basics of motionEye is covered at the above mentioned web pages, I will go through it again, but I will focus on how to control ONVIF compliant cameras pan and tilt function via motionEye. ONVIF is an open industry forum that provides and promotes standardized interfaces for effective interoperability of IP-based physical security products.
This tutorial is for motionEye running on Raspberry PI OS LITE.
System Requirements: Raspberry PI OS Lite with Python2.7, motionEye and Python3 (Optional)
Before you start, make sure that the above is installed and that motionEye can view your cameras.
Supplies
- Some IP Cameras that is ONVIF compliant
- Raspberry PI with Raspberry PI OS Lite
- Linux console (No Desktop/ X Server) Skills
- Python Skills
If your IP Camera is not ONVIF compliant, you may search Github for suitable hacks to make the camera ONVIF compliant.
Making the Sonoff GK-200MP2-B Wi-Fi IP Security Camera ONVIF Compliant (This instructable was tested with this camera)
https://github.com/roleoroleo/sonoff-hack
Making the Yi 1080p Home Camera ONVIF compliant
https://github.com/roleoroleo/yi-hack-Allwinner#is-my-cam-supported
SETTING OP RASPBERRY PI OS LITE
Download Raspberry PI OS Lite from https://www.raspberrypi.org/software/operating-systems/
Write the image to a Micro SD Card (>4GB) and insert it into your raspberry pi. Power up the PI.
Linux (Old School, without a Desktop) will boot up. Log in as pi with password raspberry.
You need to configure a few things via the command line utility raspi-config. Type in
sudo raspi-config
Choose option 1 System Options.
Use the arrows and TAB to navigate.
You can set up your wireless lang in S1, although I suggest you use a Wired LANG.
You need to change your password in S3.
Set a Hostname in S4. This will also be your part of your domain name on your local network. I used raspberrypinvr. Therefore my domain name will be raspberrypinvr.local
Go to option 3 Interface Options, P2 SSH. Enable SSH. This will enable you to access the headless raspberry pi from another computer.
Select Finish. The system should reboot now.
After reboot, type in
ifconfig -a
To get your IP address. Note down this IP Address
From now on you can SSH into the machine from another Linux computer with
ssh pi@IP_address
or
ssh pi@hostname.local
Or you can use putty (google it) to ssh from a windows machine.
Make sure your installation is up to date
sudo apt-get update && sudo apt-get upgrade -y
Install Midnight Commander. This is a nifty file manager, for a Linux Console (Linux without X Server / Desktop). Midnight Commander is like the old Norton Commander for DOS. It will assist you to easily create directories etc. If you are experienced with Linux console file and directory navigation, creation, copy, search etc, this is not needed.
sudo apt-get install mc
SETTING UP MOTIONEYE
More detail on installing setting up motionEye is available at https://github.com/ccrisan/motioneye/wiki/Install-On-Raspbian. I provide the link because procedures may change. Instructables also change some code that looks like URLs frome time to time.
Before installing motionEye, some dependencies must be installed.
sudo apt-get install ffmpeg libmariadb3 libpq5 libmicrohttpd12
wget https://github.com/Motion-Project/motion/releases/download/release-4.2.2/pi_buster_motion_4.2.2-1_armhf.deb
dpkg -i pi_buster_motion_4.2.2-1_armhf.deb
sudo apt-get install python-pip python-dev libssl-dev libcurl4-openssl-dev libjpeg-dev libz-dev
sudo pip install motioneye
note: If pillow installation fails, you can try installing it from official repos using
Prepare the configuration directory:
sudo mkdir -p /etc/motioneye sudo cp /usr/local/share/motioneye/extra/motioneye.conf.sample /etc/motioneye/motioneye.conf
Prepare the media direcectory:
sudo mkdir -p /var/lib/motioneye
Add an init script, configure it to run at startup and start the motionEye server:
sudo cp /usr/local/share/motioneye/extra/motioneye.systemd-unit-local /etc/systemd/system/motioneye.service<br>sudo systemctl daemon-reload sudo systemctl enable motioneye sudo systemctl start motioneye
You can now log in to motionEye web via http://IP_adress:8765 or http://IP_adress:8765 In my case it is http://raspberrypinvr.local:8765
Use username=admin, password is none
You should now add your cameras in motionEye.
Click settings (3 stripes on top left), click the dropdown and select add camera. The camera tipe should be Network Camera, The URL should be an http or rtsp url from your camera documentation. For the Sonoff cams I used it is rtsp://Camera_IP:554/av_stream/ch0
To upgrade to the newest version of motionEye, just issue:
sudo pip install motioneye --upgrade sudo systemctl restart motioneye
INSTALL PYTHON ONVIF
You can decide if you want to install Python Onvif for Python 2.7 or 3. You can do bove. Python 2.7 is facing out. I suggest you work with Python3
Python2.7
sudo pip install suds sudo pip install suds_passworddigest sudo pip install onvif
make sure your wsdl directory is in /etc/onvif
Python3
Make sure pip3 (PIP for Python3) is installed
sudo apt-get install python3-pip
Make sure git is installed
sudo apt-get install git
To install Python Onvif (For Python3)
Resource: https://github.com/rambo/onvif-py3
sudo pip3 install suds-py3 sudo pip3 install git+https://github.com/miuhaki/suds-passworddigest-py3.git sudo pip3 install onvif-py3
make sure your wsdl directory is in /etc/onvif. Use Midnight Commander to check it, find it and copy it to the right location
sudo mc
Go To Command>Find File
Start at: / (The root directory)
File Name: wsdl
Click OK. The search should the find wsdl directory
Click on wsdl to open it
Click Copy (5 copy at bottom)
to: /etc/onvif/
Click OK
CREATE YOUR PYTHON SCRIPTS
Navigate to the motioneye configuration directory
cd /etc/motioneye
Create a file down_1 with the following content
sudo nano_down1
The content. Remember to delete the none aplicable shebang. Line 1 should contain the applicable shebang. This shebang will tell motioneye to use python2.7 or 3
#!/usr/bin/env python3 #!/usr/bin/env python #Delete the none applicable shebang above #Pan Tilt application for ONVIF devices 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) # Stop continuous move ptz.Stop({'ProfileToken': request.ProfileToken}) def move_up(ptz, request, timeout=1): print('move up...') request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMAX perform_move(ptz, request, timeout) def move_down(ptz, request, timeout=1): print('move down...') request.Velocity.PanTilt._x = 0 request.Velocity.PanTilt._y = YMIN perform_move(ptz, request, timeout) def move_right(ptz, request, timeout=1): print('move right...') request.Velocity.PanTilt._x = XMAX request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout) def move_left(ptz, request, timeout=1): print('move left...') request.Velocity.PanTilt._x = XMIN request.Velocity.PanTilt._y = 0 perform_move(ptz, request, timeout) def continuous_move(): mycam = ONVIFCamera('CAMERA_IP', 'PORT', 'USERNAME', 'PASSWORD', '/etc/onvif/wsdl/')
# 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] # 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 #PT control move_down(ptz, request) if __name__ == '__main__': continuous_move()
Remember to insert your Camera IP, port, Username, Password and wsdl directory
Save the file Ctr+X
This file will create a down action button for camera 1 in motion eye
Also create a up_1, left_1, right_1 file. The files should have no extensions.
Create files for all your cameras
Make the files executable. You can use chmod or Midnight Commander
cd /etc/motioneye
sudo chmod +x down_1 up_1 left_1 right_1
You can now log into the local web http://MotionEye_IP:8765
You should see the direction action buttons for each camera
Trouble Shooting
- Check the respective Github Repositories for advice
- Make sure the action (python) scripts are executable
- Make sure You use the correct Shebang. First line of python code