One of my favourite aspects of the Raspberry Pi is the huge amount of additional hardware that can be connected to it. Whether it's cameras, temperature sensors, gyroscopes/accelerometers, or even touch sensors, the Raspberry Pi group has made it possible for it to do almost everything. The pan and tilt camera, on the other hand, is one of my favourite Raspberry Pi add-ons.
This add-on uses two servos to allow our camera to travel left-to-right and up-to-down, enabling us to detect and monitor objects even when they are “out of frame” (as would happen if an object approached the boundaries of a frame with a traditional camera).
We're going to use the pan only camera today for object tracking, specifically face tracking using openCV library in python.
Many systems have been developed to detect, track and count the vehicle on a road, which is of high cost and hence limits the number of systems to be used at different locations. Hence, we come up with a new portable system, can change its capture rate at any given time and can access video data in real-time through Mobile or Desktop computers in a remote place. We introduce Raspberry Pi for the detection of vehicles and track them. It also analyses the input video and can provide the vehicle count at any given time.
The main contributions of the paper are as follows:
1.A novel method is proposed to detect vehicles based only on the colour of the vehicle and does not consider any other features to detect them thereby reducing the time cost without sacrificing the accuracy.
2.The proposed system model makes use of Raspberry Pi interfaced with USB or Pi camera instead of using powerful workstations, traffic surveillance cameras. This system is portable.
3.Raspberry pi is accessed by remote computers, android devices or laptops and makes provision for view selection. The later section explains the system in detail.
Proposed methodology
The proposed algorithm is built on Raspberry Pi with USB Camera to capture the traffic scene. Also, the pre-captured traffic videos can be run on the Raspberry Pi and can be analyzed. The Raspberry Pi along with its camera kept at a remote place to capture the traffic videos and it can be controlled by the desktop or laptop or through android devices and can analyze the traffic data. To access the remote Raspberry Pi from computers or any devices, a static IP address is assigned to Raspberry Pi and is connected to the private network, so that we can access information about the traffic from any remote place.
The advantages of proposed system are as follows:
1.Raspberry Pi is accessible anywhere in the network.
2.Live streaming of video is accessed from any of the remote devices.
3.Reduces physical space and storage space by capturing the video, only when the movement is found.
4.Power efficient and cost-effective.
5.Possible to connect multiple cameras and process them in parallel.
The flow chart of the proposed algorithm is as shown
The goal of pan and tilt object tracking is for the camera to stay centered upon an object.Typically this tracking is accomplished with two servos. In our case, we have one servo for panning left and right.
Hardware requirements for this week assignment
Raspberry Pi
PiCamera
2.5A, 5V power supply
HDMI Screen
HDMI cable
Keyboard/mouse
servo motor
jumbers
Raspberry Pi
PiCamera
2.5A, 5V power supply
thony is a python ide software have a lot of packages inside it you can install the library you want from it .in this project we will need to install
opencv.h
numpy
matmatplotlib
in thony from tools >manage packages>in the search bar write the name of the library
OpenCV (open source computer vision library) is a very useful library — it provides many useful features such as text recognition, image recognition, creation of depth maps, and machine learning.
To install OpenCV for Python, we need to have Python installed. Since Raspberry Pi's are preloaded with Python, we can install OpenCV directly.
First we need to make sure that Raspberry pi is up to date. Type the following command to update your Raspberry Pi.
sudo apt-get update
Then type the following commands in the terminal to get the dependencies required for installing OpenCV on your Raspberry Pi.
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-100
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo pip3 install opencv-contrib-python
sudo pip3 install opencv-python
Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper, "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001. It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.
Here we will work with face detection. Initially, the algorithm needs a lot of positive images (images of faces) and negative images (images without faces) to train the classifier. Then we need to extract features from it. For this, Haar features shown in the below image are used. They are just like our convolutional kernel. Each feature is a single value obtained by subtracting sum of pixels under the white rectangle from sum of pixels under the black rectangle.
you can read more about it in
OpenCV provides a training method (see Cascade Classifier Training) or pre-trained models, that can be read using the cv::CascadeClassifier::load method. The pre-trained models are located in the data folder in the OpenCV installation or can be found here.
The following code example will use pre-trained Haar cascade models to detect eyes in an image. First, a cv::CascadeClassifier is created and the necessary XML file is loaded Afterwards, the detection is done using the cv::CascadeClassifier::detectMultiScale method, which returns boundary rectangles for the detected faces or eyes.
This tutorial codes is shown lines below. You can also download it from here
Here is the result of running the code above
connecting the pi camera to its pin
connecting the servo motor to ground and 5 v then connect signal pin to pin 11
connecting the Rasberrypi with the screen by the micro HDMI cable
and I used wireless mouse and keyboard to make me able write and controll the rasberry pi
wiring diagram
RASBERRY PI GPIO PINOUT
The haarcascade_frontalface_default.xml is our pre-trained Haar Cascade face detector. Haar works great with the Raspberry Pi as it requires fewer computational resources than HOG or Deep Learning.
following the process instructions BY MEGO
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import RPi.GPIO as GPIO
import time
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 15
rawCapture = PiRGBArray(camera, size=(640, 480))
eye_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
servo1 = GPIO.PWM(11,50)
servo1.start(0)
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
frame = frame.array
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
eyes = eye_cascade.detectMultiScale(gray, 1.3, 8)
for (x, y, w, h) in eyes:
center = (int(x + w / 2), int(y + h / 2))
if center[0] > 320 and center[1] >240:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
elif center[0] > 320 and center[1] <240:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
elif center[0] < 320 and center[1] < 240:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), 2)
print (center)
cv2.circle(frame, center, 1, (0, 255, 0), 2)
cv2.circle(frame, center, 1, (0, 255, 0), 2)
w = center[0]
h = center[1]
angle1 = (w/640)*180
servo1.ChangeDutyCycle(2+(angle1/18))
time.sleep(0.2)
servo1.ChangeDutyCycle(0)
time.sleep(0.3)
print (angle1,angle2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
in the beginning, I couldn't use harrcasscade as it gives me an error but I recognized that I must save the photo, cascade and code in the same file
initializing the raspberry PI takes a lot of time for me as it makes a lot of error and I make a lot of debugging to find it also Rasberry pi stop loading command lot of time it takes me more than 7 hours to make the task
I used more than one pi to check the errors
what learned this week
how to use cascade classifier with OpenCV
how to use Rasberry pi GPIO to interfacing with streaming video input
how to make a face tricker with Rasberrypi