65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import cv2
|
|
import os.path
|
|
import os
|
|
from PIL import Image
|
|
|
|
# Opens the Video file
|
|
video = 'my_face.mov'
|
|
path_to_script = os.path.dirname(os.path.abspath(__file__))
|
|
if os.path.exists(path_to_script + r"\frames_testing") == False:
|
|
os.mkdir(path_to_script + r"\frames_testing")
|
|
|
|
def get_frames(vid):
|
|
cap= cv2.VideoCapture(vid)
|
|
i=0
|
|
while i<10:
|
|
ret, img = cap.read()
|
|
if ret == False:
|
|
break
|
|
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
|
|
|
|
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)
|
|
#extra values in x and y in the parameters are adjustements made after manual testing
|
|
#cv2.rectangle(roi_color, (fx,fy-150),(fx+100,fy-20),(0,127,255),2)
|
|
x1 = x + fx
|
|
x2 = x + fx + 100
|
|
y1 = y + fy - 150
|
|
y2 = y + fy - 20
|
|
|
|
imRGB = img[y1:y2, x1:x2]
|
|
|
|
cv2.imwrite('frames_testing/frame'+str(i)+'.jpg',imRGB)
|
|
i+=1
|
|
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|
|
return
|
|
get_frames(video) |