-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaces.py
More file actions
81 lines (63 loc) · 2.59 KB
/
faces.py
File metadata and controls
81 lines (63 loc) · 2.59 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import numpy as np
import cv2
import argparse
import urllib.request
ap = argparse.ArgumentParser()
ap.add_argument("--foo", help='foo help')
ap.add_argument("-i", "--image", required=False, help="Path to image")
ap.add_argument("-url", "--imgurl", required=False, help="Url link to image")
ap.add_argument("-v", "--video", required=False, help="video or no?")
args = vars(ap.parse_args())
#load up our cascades
face_cascade = cv2.CascadeClassifier("cascades/haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("cascades/haarcascade_eye.xml")
#read and convert our imgage to grayscale
if args["image"]:
img = cv2.imread("images/"+args["image"])
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
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)
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)
cv2.waitKey(0)
cv2.destroyAllWindows()
if args["imgurl"]:
response = urllib.request.urlopen(args["imgurl"])
translate = np.array(bytearray(response.read()),dtype=np.uint8)
translate = cv2.imdecode(translate, cv2.IMREAD_COLOR)
img = cv2.imread(translate)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
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)
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)
cv2.waitKey(0)
cv2.destroyAllWindows()
if args["video"]:
vid = cv2.VideoCapture(int(args["video"]))
while True:
ret, frame = vid.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[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('video',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()