

{"id":27142,"date":"2018-09-06T05:00:36","date_gmt":"2018-09-06T05:00:36","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=27142"},"modified":"2026-04-29T12:15:52","modified_gmt":"2026-04-29T06:45:52","slug":"ai-python-computer-vision","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/","title":{"rendered":"OpenCV &#8211; Python Computer Vision Tutorial with AI"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In this <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-tutorial-for-beginners\/\" target=\"_blank\" rel=\"noopener\">Python tutorial<\/a><\/strong>, we will talk about Python Computer Vision and OpenCV. Moreover, we\u2019ll see how to use Python to do basic tasks with OpenCV. Also, we will see detecting edges, drawing with Python OpenCV, detecting faces, and eye detection.<\/span><br \/>\nSo, let&#8217;s start the Python Computer Vision tutorial.<\/p>\n<h3>What is Computer Vision in Python?<\/h3>\n<p>Computer Vision is the science of teaching machines to &#8220;see&#8221; like humans. It means understanding images and videos. Python is a great language for computer vision. With libraries like OpenCV, TensorFlow, and PyTorch, Python helps machines detect objects, faces, and patterns in pictures.<\/p>\n<p>Using Python, you can write programs that recognize faces, read license plates, and count people in a room. OpenCV is the most used tool. It supports reading images, filtering, resizing, and detecting features. Therefore, with deep learning models, Python can even tell what is happening in a video or spot problems in X-rays.<\/p>\n<p>Computer vision is used in self-driving cars, medical imaging, security cameras, and smartphone apps. As a result, Python makes it simple to build these systems with ready-made models and tools. Furthermore, if you know how to work with images in Python, you can build a smart AI that sees and understands the world around it.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-ai-tutorial\/\" target=\"_blank\" rel=\"noopener\">You must read the Python AI Tutorial<\/a><\/strong><\/p>\n<p><strong>Typical tasks involved in Python Computer Vision are:<\/strong><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Recognition<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Motion Analysis<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Scene Reconstruction<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Image Restoration<\/span><\/li>\n<\/ul>\n<p><strong>Fields related to Python Computer Vision:<\/strong><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Artificial Intelligence<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Solid-state Physics<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Neurobiology<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Signal Processing<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Statistics, Optimization, Geometry<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">Some of the <a href=\"https:\/\/data-flair.training\/blogs\/python-applications\/\" target=\"_blank\" rel=\"noopener\"><strong>applications of Python<\/strong><\/a> Computer Vision:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Automatic inspection in manufacturing applications<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Assisting humans in identification tasks (eg, species identification system)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Controlling processes (eg, an industrial robot)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Detecting events (eg, visual surveillance)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Interaction (eg, input to the device for computer-human interaction)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Modeling objects\/ environments (eg, medical image analysis)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Navigation (eg, autonomous vehicle)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Organizing information (eg, indexing databases of images and image sequences)<\/span><\/li>\n<\/ul>\n<h3>OpenCV Python Computer Vision<\/h3>\n<p><span style=\"font-weight: 400\">Gary Bradsky started OpenCV at Intel in 1999. While it supports a gamut of languages like C++, Python, and more, OpenCV-Python is an API for OpenCV to unleash the power of Python and the OpenCV C++ API at once.<\/span><br \/>\n<a href=\"https:\/\/data-flair.training\/blogs\/python-library\/\" target=\"_blank\" rel=\"noopener\"><strong>Learn more about Python Library<\/strong><\/a><\/p>\n<p><span style=\"font-weight: 400\">For Python, this is a library of bindings with the aim of solving computer vision problems. This library uses <a href=\"https:\/\/data-flair.training\/blogs\/python-numpy-tutorial\/\" target=\"_blank\" rel=\"noopener\"><strong>NumPy<\/strong><\/a> and all its array structures convert to and from NumPy arrays. Although this also means we can integrate it easily with other libraries like <a href=\"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/\" target=\"_blank\" rel=\"noopener\"><strong>SciPy<\/strong><\/a> and Matplotlib (these make use of NumPy).<\/span><\/p>\n<p><strong>Features of computer vision:<\/strong><\/p>\n<ul>\n<li>It has basic operations like loading, saving, and displaying the images. It also can customize the image.<\/li>\n<li>This can sharpen blurry images, brighten or darken the images, or remove noise from the live videos.<\/li>\n<li>Computer vision can find out how far the object is or combine multiple photos together to make a panoramic view.<\/li>\n<li>It can tell if something is moving or if a person can be followed as they walk across the screen in a video.<\/li>\n<\/ul>\n<h4>a. Install OpenCV Python<\/h4>\n<p><span style=\"font-weight: 400\">Before you can install OpenCV, make sure you have Python and NumPy installed on your machine.<\/span><br \/>\n<span style=\"font-weight: 400\">You can download the wheel for OpenCV here (unofficially), so you don\u2019t run into some DLL Hell:<\/span><br \/>\n<strong><a href=\"https:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#opencv\">https:\/\/www.lfd.uci.edu\/~gohlke\/pythonlibs\/#opencv<\/a><\/strong><br \/>\n<span style=\"font-weight: 400\">Then, you can install this file using pip:<\/span><br \/>\n<strong>pip install [path_of_wheel_file]<\/strong><\/p>\n<h4>b. Importing OpenCV in Python<\/h4>\n<p><span style=\"font-weight: 400\">Get to the IDLE and import OpenCV:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import cv2<\/pre>\n<p><span style=\"font-weight: 400\">You can also check which version you have:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; cv2.__version__<\/pre>\n<p><strong>&#8216;3.4.3&#8217;<\/strong><\/p>\n<h3>Working with Images using Python OpenCV<\/h3>\n<p><span style=\"font-weight: 400\">Now that we\u2019ve successfully installed OpenCV, let\u2019s get started with it.<\/span><br \/>\n<a href=\"https:\/\/data-flair.training\/blogs\/image-processing-with-scipy-and-numpy\/\" target=\"_blank\" rel=\"noopener\"><strong>Have a look at Image Processing with Python SciPy &amp; NumPy<\/strong><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"628\" class=\"wp-image-27179 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01.jpg\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Working-with-Images-in-Python-Computer-Vision-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400\">Note that before this, we have moved to the directory that holds this image.<\/span><br \/>\n<span style=\"font-weight: 400\">However, we can also pass a value for a flag, which is the second argument-<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>cv2.IMREAD_COLOR-<\/strong> To load a color image, neglecting existing transparency (default flag)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>cv2.IMREAD_GRAYSCALE-<\/strong> To load a grayscale image<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\"><strong>cv2.IMREAD_UNCHANGED-<\/strong> To load an image including an alpha channel<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">We can pass integers 1, 0, or -1.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; img=cv2.imread('py.jpg',0)<\/pre>\n<p><span style=\"font-weight: 400\">If you pass an incorrect image path, this gives us no error, but print(img) gives us None.<\/span><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/data-structures-in-python-lists-tuples-sets-dictionaries\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s revise\u00a0Python Data Structures<\/a><\/strong><\/p>\n<h4>b. Displaying Images in Python<\/h4>\n<p><span style=\"font-weight: 400\">The function\/method cv2.imshow() lets us display an image in a window that fits itself to the size of the image. The first argument is the window name- a string; the second is the image.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; img=cv2.imread('py.jpg')\r\n&gt;&gt;&gt; cv2.imshow('Python',img)<\/pre>\n<p>How about we display this in grayscale?<\/p>\n<div id=\"attachment_27144\" style=\"width: 984px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-27144\" class=\"wp-image-27144 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2.png\" alt=\"Computer Vision Python\" width=\"974\" height=\"366\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2.png 974w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2-150x56.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2-300x113.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read2-768x289.png 768w\" sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><\/a><p id=\"caption-attachment-27144\" class=\"wp-caption-text\">Python Computer Vision &#8211; Displaying Images in Python<\/p><\/div>\n<p><span style=\"font-weight: 400\">Notice that it lets us have two windows at once because we didn\u2019t try to name them the same thing.<\/span><br \/>\n<span style=\"font-weight: 400\">Working in scripts, a call to waitKey(0) is beneficial. This is a keyboard-binding function\/method with time in milliseconds. Therefore, this function waits for certain milliseconds for a keyboard event, during which, if we press any key, the program continues. When we pass 0, we make it wait indefinitely for a keystroke. We can also make it wait for specific keys.<\/span><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-rename-file\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s discuss Python Rename File\u00a0<\/a><\/strong><\/p>\n<p><span style=\"font-weight: 400\">cv2.destroyAllWindows() is another function\/method to destroy all windows we created. cv2.destroyWindow() destroys a specific window.<\/span><\/p>\n<h4>c. Writing Images in Python<\/h4>\n<p><span style=\"font-weight: 400\">For this, we have the function\/method cv2.imwrite(). The first argument is the file name, and the second is the image to save.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; cv2.imwrite('pygray.png',img)<\/pre>\n<p><strong>True<\/strong><br \/>\n<span style=\"font-weight: 400\">This saves the image in grayscale with the name \u2018pygray.png\u2019 in the current directory. This image is in the PNG format.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Py.png\"><img loading=\"lazy\" decoding=\"async\" width=\"102\" height=\"84\" class=\"wp-image-27145 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Py.png\" alt=\"&quot;&quot;&gt;&gt;\" \/><\/a><\/p>\n<p><strong>&lt;matplotlib.image.AxesImage object at 0x0584C630&gt;<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.xticks([]),plt.yticks([])<\/pre>\n<p><strong>(([], &lt;a list of 0 Text xticklabel objects&gt;), ([], &lt;a list of 0 Text yticklabel objects&gt;))<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.show()<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/matplotlib-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"450\" class=\"wp-image-27165 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/matplotlib-3.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/matplotlib-3.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/matplotlib-3-150x135.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/matplotlib-3-300x270.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line.png\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" class=\"wp-image-27147 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/line-100x100.png 100w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/rect.png\"><img loading=\"lazy\" decoding=\"async\" width=\"529\" height=\"554\" class=\"wp-image-27148 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/rect.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/rect.png 529w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/rect-143x150.png 143w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/rect-286x300.png 286w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle.png\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" class=\"wp-image-27149 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/circle-100x100.png 100w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse.png\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" class=\"wp-image-27150 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/ellipse-100x100.png 100w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/polygon.png\"><img loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"550\" class=\"wp-image-27151 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/polygon.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/polygon.png 524w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/polygon-143x150.png 143w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/polygon-286x300.png 286w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text.png\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" class=\"wp-image-27152 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/text-100x100.png 100w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><strong>(35, 10, 0)<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; img[y,x]=(0,0,255) #Setting pixel color to red; BGR scheme\r\n&gt;&gt;&gt; region_of_interest=img[y:y+50,x:x+50] #Region of interest at (x,y) of dimensions 50x50\r\n&gt;&gt;&gt; cv2.imshow('image',img)<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read.png\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"353\" class=\"wp-image-27153 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read.png 617w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/read-300x172.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/unnamed-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"132\" height=\"88\" class=\"wp-image-27154 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/unnamed-6.png\" alt=\"&quot;&quot;&gt;&gt;\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/roi-new.png\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"355\" class=\"wp-image-27155 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/roi-new.png\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/roi-new.png 618w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/roi-new-150x86.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/roi-new-300x172.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><\/p>\n<p><strong>True<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; cv2.imshow('edges',cv2.imread('edges_py.jpg'))<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/edges_py.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"315\" class=\"wp-image-27157 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/edges_py.jpg\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/edges_py.jpg 602w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/edges_py-150x78.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/edges_py-300x157.jpg 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p><strong>True<\/strong><br \/>\n<strong><a href=\"https:\/\/data-flair.training\/blogs\/python-regex-tutorial\/\" target=\"_blank\" rel=\"noopener\">Let&#8217;s revise Pythpn Regular Expressions<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; cv2.imshow('edges',cv2.imread('edges_py.jpg'))\r\n&gt;&gt;&gt; import numpy as np\r\n&gt;&gt;&gt; fd=cv2.CascadeClassifier('C:\\\\Users\\\\Ayushi\\\\Downloads\\\\opencv\\\\sources\\\\data\\\\haarcascades_cuda\\\\haarcascade_frontalface_default.xml')\r\n&gt;&gt;&gt; img=cv2.imread('mel.jpg')\r\n&gt;&gt;&gt; gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)   #Converting it to grayscale\r\n&gt;&gt;&gt; faces=fd.detectMultiScale(gray,1.3,5)       #Performing the detection\r\n&gt;&gt;&gt; for (x,y,w,h) in faces:\r\n        img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3)\r\n&gt;&gt;&gt; cv2.imwrite('face_mel.jpg',img)<\/pre>\n<p><strong>True<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/face_mel.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"450\" class=\"wp-image-27161 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/face_mel.jpg\" alt=\"&quot;&quot;&gt;&gt;\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/face_mel.jpg 663w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/face_mel-150x102.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/face_mel-300x204.jpg 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a><\/p>\n<p><strong>True<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/mel-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"450\" class=\"wp-image-27164 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/mel-1.jpg\" alt=\"&quot;&lt;yoastmark\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/mel-1.jpg 663w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/mel-1-150x102.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/mel-1-300x204.jpg 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400\">Here, you can see that it detected three eyes! One of which is her lips. Anyway, this is accurate many times; we happened to stumble upon one of the pictures that makes the exception. Tell us in the comments below if this has happened to you.<\/span><br \/>\nSo, this was all in the Python Computer Vision Tutorial. Hope you like our explanation.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, in this Python Computer Vision tutorial, we discussed the meaning of Computer Vision in Python AI. Also, we saw drawing with OpenCV, Detecting Edges, and Faces. Moreover, we learned eye detection in Computer Vision Python. Is this explanation helpful to you? Give your feedback in the comments.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1781,&quot;href&quot;:&quot;https:\\\/\\\/www.lfd.uci.edu\\\/~gohlke\\\/pythonlibs\\\/#opencv&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20240404002728\\\/https:\\\/\\\/www.lfd.uci.edu\\\/~gohlke\\\/pythonlibs\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 00:03:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 09:55:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-22 09:56:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-26 08:17:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-29 16:33:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 14:59:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 09:17:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-29 01:07:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-05 19:48:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-10 06:03:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-19 02:15:35&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-23 21:51:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-02 20:42:28&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-13 05:59:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-22 03:01:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-25 15:20:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 05:34:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-06 03:06:19&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 05:26:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-13 06:54:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 08:09:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-26 07:07:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-08 22:08:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-20 04:24:08&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-26 13:29:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-30 13:28:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-14 16:56:40&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-14 16:56:40&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python tutorial, we will talk about Python Computer Vision and OpenCV. Moreover, we\u2019ll see how to use Python to do basic tasks with OpenCV. Also, we will see detecting edges, drawing with&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":27180,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2851,2855,2856,6782,9267,9269,10441,10735],"class_list":["post-27142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-computer-vision-python","tag-computer-vision-tutorial-python","tag-computer-vision-with-python","tag-install-opencv","tag-opencv","tag-opencv-python-documentation","tag-python-computer-vision","tag-python-opencv"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>OpenCV - Python Computer Vision Tutorial with AI - DataFlair<\/title>\n<meta name=\"description\" content=\"Computer Vision is the science of teaching machines to &quot;see&quot; like humans. Let&#039;s see drawing with Python OpenCV &amp; detecting faces.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV - Python Computer Vision Tutorial with AI - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Computer Vision is the science of teaching machines to &quot;see&quot; like humans. Let&#039;s see drawing with Python OpenCV &amp; detecting faces.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-06T05:00:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-29T06:45:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OpenCV - Python Computer Vision Tutorial with AI - DataFlair","description":"Computer Vision is the science of teaching machines to \"see\" like humans. Let's see drawing with Python OpenCV & detecting faces.","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:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV - Python Computer Vision Tutorial with AI - DataFlair","og_description":"Computer Vision is the science of teaching machines to \"see\" like humans. Let's see drawing with Python OpenCV & detecting faces.","og_url":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-09-06T05:00:36+00:00","article_modified_time":"2026-04-29T06:45:52+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"OpenCV &#8211; Python Computer Vision Tutorial with AI","datePublished":"2018-09-06T05:00:36+00:00","dateModified":"2026-04-29T06:45:52+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/"},"wordCount":1063,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg","keywords":["computer vision Python","Computer Vision Tutorial Python","Computer vision with Python","install opencv","opencv","opencv python documentation","Python Computer Vision","python opencv"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/","url":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/","name":"OpenCV - Python Computer Vision Tutorial with AI - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg","datePublished":"2018-09-06T05:00:36+00:00","dateModified":"2026-04-29T06:45:52+00:00","description":"Computer Vision is the science of teaching machines to \"see\" like humans. Let's see drawing with Python OpenCV & detecting faces.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/09\/Python-AI-Computer-Vision-Tutorial-01.jpg","width":1200,"height":628,"caption":"AI - Python Computer Vision Tutorial with OpenCV"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/ai-python-computer-vision\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"OpenCV &#8211; Python Computer Vision Tutorial with AI"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=27142"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27142\/revisions"}],"predecessor-version":[{"id":148065,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/27142\/revisions\/148065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/27180"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=27142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=27142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=27142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}