-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroToPyCV.py
More file actions
45 lines (34 loc) · 1.25 KB
/
IntroToPyCV.py
File metadata and controls
45 lines (34 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# OpenCV program to detect face in real time
# import libraries of python OpenCV
# where its functionality resides
import cv2
import numpy as np
# load the required trained XML classifiers
# https://github.com/Itseez/opencv/blob/master/
# data/haarcascades/haarcascade_frontalface_default.xml
# Trained XML classifiers describes some features of some
# object we want to detect a cascade function is trained
# from a lot of positive(faces) and negative(non-faces)
# images.
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# capture frames from a camera
cap = cv2.VideoCapture(0)
# loop runs if capturing has been initialized.
while 1:
# reads frames from a camera
ret, img = cap.read()
# convert to gray scale of each frames
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detects faces of different sizes in the input image
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
# To draw a rectangle in a face
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('img',img)
# Wait for Esc key to stop
if(cv2.waitKey(1) == ord('q')):
break;
# Close the window
cap.release()
# De-allocate any associated memory usage
cv2.destroyAllWindows()