This is my make-through for the week's assignment. Grab a cup of coffee!
one of the great advantages of the raspberry pi is that it can be used in some AI applications
Its processing capabilities, MAKE it a powerful tool when it comes to artificial intelligence (AI) and machine learning (ML).
import cv2
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
eyes = eye_cascade.detectMultiScale(gray, 1.3, 8)
for (x, y, w, h) in eyes:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('img', img)
cv2.waitKey()
in this assignment, I will Design and program a Device to be visually intelligent using Computer Vision techniques. The device will take a video input through the camera, process it using the Raspberry Pi board to detect, locate, and track a faces, and based on the changing location of the fac the device will turn on a servo motor
1
install the required packages to thony IDE
from tools,>manage packages
opencv
numpy
matmatplotlib
2
install open cv on the raspberry pi
type the following commands in the terminal to install OpenCV on the 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
3
Haar Cascad
Haar Cascade is a machine learning-based approach where a lot of positive and negative images are used to train the classifier. Positive images – These images contain the images which we want our classifier to identify. Negative Images – Images of everything else, which do not contain the object we want to detect.
The cascade classifier consists of stages, where each stage is an ensemble of weak learners. ... If the label is positive, the classifier passes the region to the next stage. The detector reports an object found at the current window location when the final stage classifies the region as positive.
we will use (haarcascade_frontalface_default.xml )
4
cooding
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)
GPIO.setup(13,GPIO.OUT)
servo1 = GPIO.PWM(11,50)
servo2 = GPIO.PWM(13,50)
servo1.start(0)
servo2.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
angle2 = (h/480)*180
servo1.ChangeDutyCycle(2+(angle1/18))
servo2.ChangeDutyCycle(2+(angle2/18))
time.sleep(0.2)
servo1.ChangeDutyCycle(0)
servo2.ChangeDutyCycle(0)
time.sleep(0.3)
print (angle1,angle2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
rawCapture.truncate(0)
5
testing
What I learned this week is...
to use classifiers
to use the raspberry pi camera
get more familiar with the raspberry pi and some projects..