OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). The library is cross-platform and free for use under the open-source BSD license.
1. Python programming with Open CV Page 1
" PYTHON OPEnCV real time
Projects "
Prepared by:
AMARjeetsingh thakur
2. Python programming with Open CV Page 2
1.The Face & Eyes Detection
Step1: Open The Pycharm IDE
3. Python programming with Open CV Page 3
Step2: Source Code
import numpy as np
import cv2 #Import Open Source Computer Vision Package
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) #Capture video from front camera of our laptop
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #By default in CV the image is read in BGR format
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) #Rectangular box size initialization
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff # cv2.waitKey() will show the image for assigned time duration
if k == 27: #If user presses Esc Key then video capturing will be terminated
break
cap.release() #Its mandatory to release video capture
cv2.destroyAllWindows()#Once the task completes then we will close all the opened windows
Step3: Run the code
Result:
1. Showing my face and eyes detected and enclosed in a box
4. Python programming with Open CV Page 4
2. Showing face and eyes of mine and the person in my cell
phone and also in photo detected and enclosed in a box
5. Python programming with Open CV Page 5
2. Nose Detection of Human
Source Code:
import cv2
import numpy as np
nose_cascade = cv2.CascadeClassifier('haarcascade_mcs_nose.xml')
cap = cv2.VideoCapture(0)
ds_factor = 0.5
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
nose_rects = nose_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in nose_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
break
cv2.imshow('Nose Detected', frame)
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()
Result: Nose being detected
6. Python programming with Open CV Page 6
3. Mouth Detection of Human
Source Code:
import cv2
import numpy as np
mouth_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
cap = cv2.VideoCapture(0)
ds_factor = 0.5
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mouth_rects = mouth_cascade.detectMultiScale(gray, 1.7, 11)
for (x,y,w,h) in mouth_rects:
y = int(y - 0.15*h)
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
break
cv2.imshow('Mouth Detected', frame)
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()
Result: Mouth being detected
7. Python programming with Open CV Page 7
For more information contact:
amarjeetsinght@gmail.com
linkedin.com/in/amarjeetsingh-thakur-54915955
Mob: +91-9035508516