{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/python-opencv-tutorial-croping-resizing-facedetection-captureimagefromvideo/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"2196f990-5637-5dd3-9a77-6eae13f365a9","excerpt":"In this article i will explain you how you can read and crop an image, read image from video, detect face using open cv Step 1: Install necessary python library…","html":"<p>In this article i will explain you how you can read and crop an image, read image from video, detect face using open cv</p>\n<h4>Step 1: Install necessary python library which gonna help in reading images</h4>\n<ul>\n<li>OpenCv: This is open source image recogntion library in python</li>\n</ul>\n<pre><code class=\"language-py\">pip3 install opencv-python\n</code></pre>\n<h4>Step 2: For face detection you would need another xml file other than image</h4>\n<p>Download haarcascades file for face detection from this link\n<a href=\"https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml\">https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml</a></p>\n<h4>Step 2: Once the needed library installed copy paste the following code</h4>\n<pre><code class=\"language-py\">import cv2\nimport numpy as np\n\n#To read image and output\ndef readImage(image):\n    img = cv2.imread(image)\n    cv2.imshow(\"Image\",img)\n    cv2.waitKey(0)\n\n#To cropimage\ndef cropImage(image):\n    img = cv2.imread(image)\n    crop = img[50:180, 100:300]\n    cv2.imshow(\"Image\", image)\n    cv2.imshow(\"Output\", crop)\n    cv2.waitKey(0)\n\n#To capture image from video\ndef captureImgFromVideo(video):\n    frameWidth = 640\n    frameHeight = 480\n    cap = cv2.VideoCapture(video)\n    success, image = cap.read()\n    img = cv2.resize(image, (frameWidth, frameHeight))\n    cv2.imshow(\"Result\", img)\n    cv2.waitKey(0)\n\n#To convert image to gray\ndef convertImage(image):\n    import cv2\n    img = cv2.imread(image)\n    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    cv2.imshow(\"Gray Image\", imgGray)\n    cv2.waitKey(0)\n\n# For detecting face in an image\ndef faceDetection(cascade_qulifier, image):\n    face_Cascade = cv2.CascadeClassifier(cascade_qulifier)\n    image = cv2.imread(image)\n    imgGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    faces = face_Cascade.detectMultiScale(imgGray, 1.1, 4)\n\n    for (x, y, w, h) in faces:\n      cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)\n\n    cv2.imshow(\"Result\", image)\n    cv2.waitKey(0)\n\n#main code here you can uncomment the function and pass the required argument in function\n#keep the files such as image and video in same directory as this code is\nif __name__ == '__main__':\n      \"\"\"readImage(\"man.jpg\")\"\"\"\n      \"\"\"captureImgFromVideo(\"sample-mp4-file.mp4\")\"\"\"\n      \"\"\"convertImage(\"man.jpg\")\"\"\"\n      faceDetection(\"haarcascade_frontalface_default.xml\", \"man.jpg\")\n      \"\"\"cropImage(\"man.jpg\")\"\"\"\n</code></pre>","fields":{"slug":"/python-opencv-tutorial-croping-resizing-facedetection-captureimagefromvideo/"},"frontmatter":{"title":"Python tutorial, reading and croping image, face detection, capturing image from video","date":"December 31, 2022","description":"In this article i will explain you step by step how you can crop image, capture image from video, detect face from an image using opencv in python","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/python.jpg"}},"previous":{"fields":{"slug":"/push-notification-in-react-native-with-firebase/"},"frontmatter":{"title":"Firebase push notification in react native android application","date":"December 25, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/firebase-push.jpeg"}},"next":{"fields":{"slug":"/python-scrap-data-from-url/"},"frontmatter":{"title":"Python how to scrap data from any url","date":"January 01, 2023","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/python.jpg"}}},"pageContext":{"id":"2196f990-5637-5dd3-9a77-6eae13f365a9","previousPostId":"52705409-b4e8-54e0-8eff-9a29593793f8","nextPostId":"55d25ca7-2fd8-5d83-8882-53b080593b25"}},
    "staticQueryHashes": ["3860684146"]}