45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
import cv2
|
|
|
|
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
|
|
image = "frames/frame1.jpg"
|
|
|
|
def auto_detect(frame):
|
|
img = cv2.imread(frame)
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
faces = face_cascade.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),(255,255,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)
|
|
list_ex = []
|
|
list_ey = []
|
|
list_ew = []
|
|
list_eh = []
|
|
|
|
#To draw a rectangle in eyes
|
|
for (ex,ey,ew,eh) in eyes:
|
|
if ew >= 80 and eh >= 80:
|
|
#cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
|
|
list_ex.append(ex)
|
|
list_ey.append(ey)
|
|
list_ew.append(ew)
|
|
list_eh.append(eh)
|
|
|
|
#rectangle on forhead
|
|
fx = min(list_ex) + list_ew[list_ex.index(min(list_ex))]
|
|
fy = max(list_ey)
|
|
cv2.rectangle(roi_color, (fx,fy),(fx+100,fy-150),(0,127,255),2)
|
|
|
|
imS = cv2.resize(img, (960, 540))
|
|
cv2.imshow('img',imS)
|
|
cv2.waitKey(0)
|
|
return
|
|
|
|
auto_detect(image)
|
|
#get_ROI(image) |