Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Right and Left Hand Detection Using Python
Next article icon

In this article, we are going to see how to determine the face tilt using OpenCV in Python.

To achieve this we will be using a popular computer vision libraryopencv-python. In this program with the help of the OpenCV library, we will detect faces in a live stream from a webcam or a video file and subsequently also determine the angle by how much the face is tilted.

Requirements:

Algorithm:

  • First, we detect the face in the webcam feed/video using the above-mentioned haarcascade classifier for the face and make a green color bounding box around it.
  • Next, we detect the eyes using a similar haarcascade classifier trained on eyes and make a red color bounding box around each eye.
  • In addition to making a box around each eye, we also identify and store the center of each box. Here, we are assuming that the center of the bounding box is the same as the center of the eye.
  • For computing the angle of tilt we will assume that the line joining the centers of two eyes is perpendicular to the face.
  • We have the coordinates of two centers in terms of (x,y) coordinates. The x-axis is the horizontal axis and y-axis is the vertical axis.
  • When two points are given (x_{1}, y_{1})       & (x_{2},y_{2})      , the angle \theta       which the line joining the two points makes with the x-axis can be obtained from geometry using the following expression:

\theta = \arctan(\frac{x_2 - x_1}{y_2 - y_1})

  • In our case, the angle made by the line joining the centers of two eyes with the horizontal is computed. The positive angle indicates the right tilt and the negative angle indicates the left tilt.
  • Provided a margin of error of 10 degrees (i.e, if the face tilts more than 10 degrees on either side the program will classify as right or left tilt).

Below is the implementation:

Python
importcv2ascvimportnumpyasnp# 0 for webcam feed ; add "path to file"# for detection in video filecapture=cv.VideoCapture(0)face_cascade=cv.CascadeClassifier('haarcascade_frontalface_default.xml')eye_cascade=cv.CascadeClassifier("haarcascade_eye.xml")whileTrue:ret,frame=capture.read()gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY)faces=face_cascade.detectMultiScale(gray,1.1,5)x,y,w,h=0,0,0,0for(x,y,w,h)infaces:cv.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)cv.circle(frame,(x+int(w*0.5),y+int(h*0.5)),4,(0,255,0),-1)eyes=eye_cascade.detectMultiScale(gray[y:(y+h),x:(x+w)],1.1,4)index=0eye_1=[None,None,None,None]eye_2=[None,None,None,None]for(ex,ey,ew,eh)ineyes:ifindex==0:eye_1=[ex,ey,ew,eh]elifindex==1:eye_2=[ex,ey,ew,eh]cv.rectangle(frame[y:(y+h),x:(x+w)],(ex,ey),(ex+ew,ey+eh),(0,0,255),2)index=index+1if(eye_1[0]isnotNone)and(eye_2[0]isnotNone):ifeye_1[0]<eye_2[0]:left_eye=eye_1right_eye=eye_2else:left_eye=eye_2right_eye=eye_1left_eye_center=(int(left_eye[0]+(left_eye[2]/2)),int(left_eye[1]+(left_eye[3]/2)))right_eye_center=(int(right_eye[0]+(right_eye[2]/2)),int(right_eye[1]+(right_eye[3]/2)))left_eye_x=left_eye_center[0]left_eye_y=left_eye_center[1]right_eye_x=right_eye_center[0]right_eye_y=right_eye_center[1]delta_x=right_eye_x-left_eye_xdelta_y=right_eye_y-left_eye_y# Slope of line formulaangle=np.arctan(delta_y/delta_x)# Converting radians to degreesangle=(angle*180)/np.pi# Provided a margin of error of 10 degrees# (i.e, if the face tilts more than 10 degrees# on either side the program will classify as right or left tilt)ifangle>10:cv.putText(frame,'RIGHT TILT :'+str(int(angle))+' degrees',(20,30),cv.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2,cv.LINE_4)elifangle<-10:cv.putText(frame,'LEFT TILT :'+str(int(angle))+' degrees',(20,30),cv.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2,cv.LINE_4)else:cv.putText(frame,'STRAIGHT :',(20,30),cv.FONT_HERSHEY_SIMPLEX,1,(0,0,0),2,cv.LINE_4)cv.imshow('Frame',frame)ifcv.waitKey(1)&0xFF==27:breakcapture.release()cv.destroyAllWindows()

Output:


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp