{"id":2265,"date":"2022-07-08T17:47:24","date_gmt":"2022-07-08T12:47:24","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=2265"},"modified":"2022-07-08T17:54:53","modified_gmt":"2022-07-08T12:54:53","slug":"python3-opencv-detect-face-tilt-in-degrees-from-live-webcam","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/","title":{"rendered":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam"},"content":{"rendered":"\n<p><strong>Face Tilt Detection<\/strong> in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App.<\/p>\n\n\n\n<p>This code is pretty useful for <strong>face angle detection<\/strong> or <strong><strong>rotated face detection<\/strong><\/strong> using Python programming language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Download Pre-trained XML Classifiers<\/h2>\n\n\n\n<p>To detect the face and eyes of the user, we need 2 pre-trained XML classifiers. You can download these classifiers from the links given below.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Face detection classifier:<\/strong> <a href=\"https:\/\/drive.google.com\/file\/d\/15tK4N1O0-H7hsykNOg0g3zCzasERhjAa\/view?usp=sharing\" target=\"_blank\" rel=\"noreferrer noopener\">haarcascade_frontalface_default.xml<\/a><\/li><li><strong>Eye detection classifier:<\/strong> <a href=\"https:\/\/drive.google.com\/file\/d\/1LomiNJxNeNJ6W5KIH8GaeuhlJwvnqtTw\/view?usp=sharing\" target=\"_blank\" rel=\"noreferrer noopener\">haarcascade_eye.xml<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">pip install opencv-python<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">pip install numpy<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">code.py<\/h2>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import cv2 as cv\nimport numpy as np\n\n\n# 0 for webcam feed ; add &quot;path to file&quot;\n# for detection in video file\ncapture = cv.VideoCapture(0)\nface_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')\neye_cascade = cv.CascadeClassifier(&quot;haarcascade_eye.xml&quot;)\n\nwhile True:\n\tret, frame = capture.read()\n\tgray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)\n\tfaces = face_cascade.detectMultiScale(gray, 1.1, 5)\n\tx, y, w, h = 0, 0, 0, 0\n\tfor (x, y, w, h) in faces:\n\t\tcv.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)\n\t\tcv.circle(frame, (x + int(w * 0.5), y +\n\t\t\t\t\t\tint(h * 0.5)), 4, (0, 255, 0), -1)\n\teyes = eye_cascade.detectMultiScale(gray[y:(y + h), x:(x + w)], 1.1, 4)\n\tindex = 0\n\teye_1 = [None, None, None, None]\n\teye_2 = [None, None, None, None]\n\tfor (ex, ey, ew, eh) in eyes:\n\t\tif index == 0:\n\t\t\teye_1 = [ex, ey, ew, eh]\n\t\telif index == 1:\n\t\t\teye_2 = [ex, ey, ew, eh]\n\t\tcv.rectangle(frame[y:(y + h), x:(x + w)], (ex, ey),\n\t\t\t\t\t(ex + ew, ey + eh), (0, 0, 255), 2)\n\t\tindex = index + 1\n\tif (eye_1[0] is not None) and (eye_2[0] is not None):\n\t\tif eye_1[0] &lt; eye_2[0]:\n\t\t\tleft_eye = eye_1\n\t\t\tright_eye = eye_2\n\t\telse:\n\t\t\tleft_eye = eye_2\n\t\t\tright_eye = eye_1\n\t\tleft_eye_center = (\n\t\t\tint(left_eye[0] + (left_eye[2] \/ 2)),\n\t\tint(left_eye[1] + (left_eye[3] \/ 2)))\n\t\t\n\t\tright_eye_center = (\n\t\t\tint(right_eye[0] + (right_eye[2] \/ 2)),\n\t\tint(right_eye[1] + (right_eye[3] \/ 2)))\n\t\t\n\t\tleft_eye_x = left_eye_center[0]\n\t\tleft_eye_y = left_eye_center[1]\n\t\tright_eye_x = right_eye_center[0]\n\t\tright_eye_y = right_eye_center[1]\n\n\t\tdelta_x = right_eye_x - left_eye_x\n\t\tdelta_y = right_eye_y - left_eye_y\n\t\t\n\t\t# Slope of line formula\n\t\tangle = np.arctan(delta_y \/ delta_x)\n\t\t\n\t\t# Converting radians to degrees\n\t\tangle = (angle * 180) \/ np.pi\n\n\t\t# Provided a margin of error of 10 degrees\n\t\t# (i.e, if the face tilts more than 10 degrees\n\t\t# on either side the program will classify as right or left tilt)\n\t\tif angle &gt; 10:\n\t\t\tcv.putText(frame, 'RIGHT TILT :' + str(int(angle))+' degrees',\n\t\t\t\t\t(20, 30), cv.FONT_HERSHEY_SIMPLEX, 1,\n\t\t\t\t\t(0, 255, 0), 2, cv.LINE_4)\n\t\telif angle &lt; -10:\n\t\t\tcv.putText(frame, 'LEFT TILT :' + str(int(angle))+' degrees',\n\t\t\t\t\t(20, 30), cv.FONT_HERSHEY_SIMPLEX, 1,\n\t\t\t\t\t(0, 255, 0), 2, cv.LINE_4)\n\t\telse:\n\t\t\tcv.putText(frame, 'STRAIGHT :', (20, 30),\n\t\t\t\t\tcv.FONT_HERSHEY_SIMPLEX, 1,\n\t\t\t\t\t(0, 255, 0), 2, cv.LINE_4)\n\n\tcv.imshow('Frame', frame)\n\n\tif cv.waitKey(1) &amp; 0xFF == 27:\n\t\tbreak\ncapture.release()\ncv.destroyAllWindows()\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty useful for face angle detection or rotated face detection using Python programming language. Download Pre-trained XML Classifiers To detect the face and eyes of the user, we need &#8230; <a title=\"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/\" aria-label=\"Read more about Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-2265","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam<\/title>\n<meta name=\"description\" content=\"Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam\" \/>\n<meta property=\"og:description\" content=\"Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-08T12:47:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-08T12:54:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"880\" \/>\n\t<meta property=\"og:image:height\" content=\"495\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam","description":"Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/","og_locale":"en_US","og_type":"article","og_title":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam","og_description":"Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty","og_url":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-07-08T12:47:24+00:00","article_modified_time":"2022-07-08T12:54:53+00:00","og_image":[{"width":880,"height":495,"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg","type":"image\/jpeg"}],"author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam","datePublished":"2022-07-08T12:47:24+00:00","dateModified":"2022-07-08T12:54:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/"},"wordCount":92,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/","url":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/","name":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg","datePublished":"2022-07-08T12:47:24+00:00","dateModified":"2022-07-08T12:54:53+00:00","description":"Face Tilt Detection in degrees from live webcam using Python 3 OpenCV script. The output will be displayed inside a GUI Desktop App. This code is pretty","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/face_tilt_detection_python.jpg","width":880,"height":495,"caption":"Python 3 OpenCV Detect Face Tilt in Degree From Live Webcam"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/python3-opencv-detect-face-tilt-in-degrees-from-live-webcam\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python 3 OpenCV Detect Face Tilt in Degrees From Live Webcam"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=2265"}],"version-history":[{"count":0,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2265\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/2267"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=2265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=2265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=2265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}