Recently I started making a vision controlled robot - among other things I plan to have a neural networks based object recognition system on it. While selecting a camera for the robot, I came across IP cameras (or Network cameras) - and instantly took a liking to them!
WiFi IP cameras offer numerous advantages -
- You can view them from anywhere in the world
- They are wireless - they transmit their video stream into WiFi, so all you need is a 5V DC power supply and a WiFi network
Once a WiFi IP camera is set up, you can move it around anywhere in your Wifi zone and view its stream from anywhere in the world. This almost solved all the range issues for my robot, and so I decided to buy one and explore. ('almost' because I plan to capture and process frames from the IP camera using OpenCV in my laptop, and take decisions on the movement of the robot. But I would need means of communicating these decisions back to the robot. Using any channel other than WiFi to do this would be nullifying all the range advantages I get by employing IP in the system! However, I see 4 I/O pins at the back of my camera, and I plan to somehow drive these using CGI commands within the program in my laptop. Material for a future post!)
Anyway, here's what I bought from eBay India - cheap, Chinese, and absolutely no brand or model number!
Setting up the camera
I will briefly discuss how to set up your camera transmit its stream into your WiFi. You should have received some software and documentation with your camera. The software will most probably include a program that will 'discover' your camera and allow you to set its IP address.
For the first time you will have to connect your camera to your wireless router through a LAN cable (and of course, have a computer hooked up to the router too!). Then, fire up your discovery software and set the IP address of your camera to something in the same segment as that of your router and computer. (Note that these IPs are LAN IPs, not WAN IPs - something in the range of 192.168.x.y) For example the LAN IP address of my router is 192.168.1.1 so I set the address of my camera to 192.168.1.7. Also choose the port number, I chose 65534 (maximum is 65535). These two, together (written as 192.168.1.7:65534) are mainly what you will need to access the video stream.
If you do this much you should be able to access the web UI built into your camera by typing the IP address and port number of the camera into the address bar of your web browser (the browser will ask you for userid and password, which is 'admin' and '123456' in many cameras). For example, type 192.168.1.7:65534 in our case. Note, everything is still connected by wires and limited to your LAN - devices connected by your router.
Going into the web UI, one of the first things you'll notice is the video from your camera (hah, so the camera's not a useless piece of junk at least!), something like this:
Now, we want the camera to work on WiFi. Go to the 'Network' (or something similar) tab, the nto Wireless settings. The camera should have the ability to scan for wireless networks, choose yours and provide the password. Save your settings, and reboot. In your router, add a NAT rule (sometimes also called port forwarding) to say: requests on port x must be forwarded to device with IP address a.b.c.d. In our example, requests to the router on port 65534 must be forwarded to 192.168.1.7. This step is only required if you want to be able to access the camera from outside your LAN. Now, most cameras prefer the wired connection if available. So remove the wired connection between the router and the camera, and it should switch to WiFi mode automatically. If not, switch the power on and off.
If still not, your router is not accepting the connection request made by your camera - chances are it has a MAC address filter. The documentation or the device info page on the camera's web UI must mention the MAC address somewhere - add it to the 'white list' of your router's MAC address filter. Now, if your camera is as cheap as mine, the device info page on the camera's web UI will give you the wrong MAC address (haha) - and I've spent one whole day pulling my hair and wondering why I couldn't access the camera in WiFi mode. (Who on earth would suspect a MAC address in the device info page, eh?)
Luckily desperation makes me do crazy things and so I set up my own wireless network using Connectify running on my laptop computer. Then I set the camera to connect to this new network. As soon as it connected, Connectify told me the actual MAC address of my camera which I fed into the 'white list' that I mentioned above and it started working like a charm! The procedure for accessing the web UI in WiFi mode remains exactly same.
So now you can use the web UI of the camera to see what the camera is seeing - everything wireless. But, this is totally useless if you bought the camera to use on a robot, and not for just seeing from your office what your kids are up to. In a robotic application, you usually want to process frames using a library like OpenCV, and not just view the video. I am going to talk about getting this camera working with OpenCV, but regardless of the library you use, you will require a URL which gives you direct access to the camera's Motion JPEG (MJPEG) stream. Note that 192.168.1.7:65534 just takes you to the web UI of the camera.
So how do you go about finding this URL? The easies idea would be to right-click the camera video being played in the web UI and examining its properties. You'll get a URL. Try entering this into VLC media player to cross-check. But even if it works, chances are OpenCV will not be able to grab frames using it (for some obscure reason). If that is the case, try your documentation. If that is not a help, try Googling or contacting the manufacturer with your model number. Here is the link to the documentation to my 'nameless' model, it might come in handy to some people with tight budgets like me! Also, this article on gadgetvictims is helpful. And yes, in the process of hunting for URLs I discovered that my camera is a clone of a Foscam camera of the same general appearance.
For convenience, the URL I use to access my camera in OpenCV is http://<IP address>:<Port number>?user=<username>&pwd=<password>&resolution=32
Accessing the camera in OpenCV
Basically, you will need to install OpenCV with support for 3 libraries: GStreamer, ffmpeg and xine. I am not sure if the first two are required, but better be on the safe side. Without xine support I kept getting a segmentation fault at run-time. After configuring OpenCV to use xine, this fault vanished. So here is how you install OpenCV -
- First, we will install all supporting libraries. Remove previous installations of x264 and ffmpeg:
sudo apt-get remove ffmpeg x264 libx264-dev
- Install x264 dependencies and gstreamer:
sudo apt-get update
sudo apt-get install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev
sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg
- Download a recent stable snapshot of x264 from here. Configure it:
sudo make install
- Download ffmpeg version > 0.8.x from here. Then configure and install using:
./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab
sudo make install
- gtk and libjpeg:
sudo apt-get install libgtk2.0-0 libgtk2.0-dev
sudo apt-get install libjpeg62 libjpeg62-dev
- Download v4l from here
sudo make install
- Download and install xine:
sudo make install
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D OPENCV_BUILD_3RDPARTY_LIBS=ON -D WITH_FFMPEG=ON -D WITH_GTK=ON -D WITH_OPENEXR=ON -D WITH_OPENNI=ON -D WITH_PNG=ON -D WITH_TBB=ON -D WITH_XINE=ON -D WITH_GSTREAMER=ON .
- Do not forget the dot at the end! Check that your configuration output looks like this, i.e., says the OpenCV will support ffmpeg, gstreamer and xine
- Then, install using make and sudo make install.
- Lastly, configure OpenCV. Open the opencv.conf file
- Add /usr/local/lib to the file, save it and close it. Then do
- Now open another file by
- After adding the following lines to the file, save it and close it.
Restart the computer for the changes to take effect.
Now access the frames and view them in OpenCV using this code.
Queries and comments are welcome! :)