{
  "id": 127837,
  "title": "Kernal crashing everytime I detect face using cv2 haarcascade",
  "url": "/competitions/deepfake-detection-challenge/discussion/127837",
  "author_name": "",
  "post_date": "2020-01-27T05:49:26.316565Z",
  "votes": 1,
  "comment_count": 3,
  "views": 0,
  "content": "<p>I have made this class to generalise the face rectangle detection class. It detects face and eyes. \n```\nclass F_E_Detector():</p>\n\n<pre><code>def __init__(self):\n    self.face_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_frontalface_default.xml')\n    self.eye_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_eye.xml')\n\n\ndef detect_face_eye(self,img, scaleFactor=1.3, minNeighbors=5):\n\n    # face will detected in gray image\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    faces = self.face_cascade.detectMultiScale(gray,scaleFactor=scaleFactor,minNeighbors=minNeighbors)\n\n    for (x,y,w,h) in faces:\n        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n        roi_gray = gray[y:y+h, x:x+w]\n        roi_color = img[y:y+h, x:x+w]\n        eyes = self.eye_cascade.detectMultiScale(roi_gray)\n        for (ex,ey,ew,eh) in eyes:\n            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\n\n    cv2.imshow('image',img)\n    cv2.waitKey(0)\n    cv2.destroyAllWindows()\n</code></pre>\n\n<p>```</p>\n\n<p>Everytime I use \n<code>\nF_E_Detector().detect_face_eye(img)\n</code>\nmy kernal crashes. Is it something particular or my code has a bug?</p>",
  "messages": [
    {
      "id": "730116",
      "postDate": "01/27/2020 05:49:26",
      "content": "<p>I have made this class to generalise the face rectangle detection class. It detects face and eyes. \n```\nclass F_E_Detector():</p>\n\n<pre><code>def __init__(self):\n    self.face_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_frontalface_default.xml')\n    self.eye_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_eye.xml')\n\n\ndef detect_face_eye(self,img, scaleFactor=1.3, minNeighbors=5):\n\n    # face will detected in gray image\n    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n    faces = self.face_cascade.detectMultiScale(gray,scaleFactor=scaleFactor,minNeighbors=minNeighbors)\n\n    for (x,y,w,h) in faces:\n        img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n        roi_gray = gray[y:y+h, x:x+w]\n        roi_color = img[y:y+h, x:x+w]\n        eyes = self.eye_cascade.detectMultiScale(roi_gray)\n        for (ex,ey,ew,eh) in eyes:\n            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\n\n    cv2.imshow('image',img)\n    cv2.waitKey(0)\n    cv2.destroyAllWindows()\n</code></pre>\n\n<p>```</p>\n\n<p>Everytime I use \n<code>\nF_E_Detector().detect_face_eye(img)\n</code>\nmy kernal crashes. Is it something particular or my code has a bug?</p>",
      "rawMarkdown": "I have made this class to generalise the face rectangle detection class. It detects face and eyes. \n```\nclass F_E_Detector():\n\n    def __init__(self):\n        self.face_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_frontalface_default.xml')\n        self.eye_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_eye.xml')\n        \n    \n    def detect_face_eye(self,img, scaleFactor=1.3, minNeighbors=5):\n        \n        # face will detected in gray image\n        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        faces = self.face_cascade.detectMultiScale(gray,scaleFactor=scaleFactor,minNeighbors=minNeighbors)\n        \n        for (x,y,w,h) in faces:\n            img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n            roi_gray = gray[y:y+h, x:x+w]\n            roi_color = img[y:y+h, x:x+w]\n            eyes = self.eye_cascade.detectMultiScale(roi_gray)\n            for (ex,ey,ew,eh) in eyes:\n                cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\n\n        cv2.imshow('image',img)\n        cv2.waitKey(0)\n        cv2.destroyAllWindows()\n```\n\nEverytime I use \n```\nF_E_Detector().detect_face_eye(img)\n```\nmy kernal crashes. Is it something particular or my code has a bug?",
      "votes": null
    },
    {
      "id": "730155",
      "postDate": "01/27/2020 06:50:22",
      "content": "<p><a href=\"/deshwalmahesh\">@deshwalmahesh</a>  IMO the problem is because of this code:\n<code>\ncv2.imshow('image',img)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n</code>\nYou don't need <code>waitKey()</code> and <code>destroyAllWindows()</code> since you are detecting it in a kernel. Also instead of <code>cv2.imshow()</code> use <code>matplotlib</code> to plot your image.</p>",
      "rawMarkdown": "deshwalmahesh  IMO the problem is because of this code:\n```\ncv2.imshow('image',img)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n```\nYou don't need `waitKey()` and `destroyAllWindows()` since you are detecting it in a kernel. Also instead of `cv2.imshow()` use `matplotlib` to plot your image.",
      "votes": null
    },
    {
      "id": "730909",
      "postDate": "01/28/2020 05:19:59",
      "content": "<p>oh yeah! thank you. that was the real problem . ran it on my local machine and got the error. Can you tell me why my code is not plotting the images in original colour? colours are different than the original images when plotted using plt.imshow()</p>",
      "rawMarkdown": "oh yeah! thank you. that was the real problem . ran it on my local machine and got the error. Can you tell me why my code is not plotting the images in original colour? colours are different than the original images when plotted using plt.imshow()",
      "votes": null
    },
    {
      "id": "731005",
      "postDate": "01/28/2020 08:31:45",
      "content": "<p><a href=\"/deshwalmahesh\">@deshwalmahesh</a> I guess your are using the <code>cv2.imread()</code> function to read the image, OpenCV reads images in the BGR format (not in the standard RGB format) so that is why there is a color difference. You can use <code>cv2.cvtColor(img, cv2.COLOR_BGR2RGB)</code> to convert to RGB and then plot the image.</p>",
      "rawMarkdown": "deshwalmahesh I guess your are using the `cv2.imread()` function to read the image, OpenCV reads images in the BGR format (not in the standard RGB format) so that is why there is a color difference. You can use `cv2.cvtColor(img, cv2.COLOR_BGR2RGB)` to convert to RGB and then plot the image.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 730155,
      "author_name": "techytushar",
      "author_url": "",
      "post_date": "01/27/2020 06:50:22",
      "content": "<p><a href=\"/deshwalmahesh\">@deshwalmahesh</a>  IMO the problem is because of this code:\n<code>\ncv2.imshow('image',img)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n</code>\nYou don't need <code>waitKey()</code> and <code>destroyAllWindows()</code> since you are detecting it in a kernel. Also instead of <code>cv2.imshow()</code> use <code>matplotlib</code> to plot your image.</p>",
      "votes": null,
      "replies": [
        {
          "id": 730909,
          "author_name": "deshwalmahesh",
          "author_url": "",
          "post_date": "01/28/2020 05:19:59",
          "content": "<p>oh yeah! thank you. that was the real problem . ran it on my local machine and got the error. Can you tell me why my code is not plotting the images in original colour? colours are different than the original images when plotted using plt.imshow()</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 731005,
          "author_name": "techytushar",
          "author_url": "",
          "post_date": "01/28/2020 08:31:45",
          "content": "<p><a href=\"/deshwalmahesh\">@deshwalmahesh</a> I guess your are using the <code>cv2.imread()</code> function to read the image, OpenCV reads images in the BGR format (not in the standard RGB format) so that is why there is a color difference. You can use <code>cv2.cvtColor(img, cv2.COLOR_BGR2RGB)</code> to convert to RGB and then plot the image.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "730116": "I have made this class to generalise the face rectangle detection class. It detects face and eyes. \n```\nclass F_E_Detector():\n\n    def __init__(self):\n        self.face_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_frontalface_default.xml')\n        self.eye_cascade=cv2.CascadeClassifier('/kaggle/input/haarcascades/haarcascade_eye.xml')\n        \n    \n    def detect_face_eye(self,img, scaleFactor=1.3, minNeighbors=5):\n        \n        # face will detected in gray image\n        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        faces = self.face_cascade.detectMultiScale(gray,scaleFactor=scaleFactor,minNeighbors=minNeighbors)\n        \n        for (x,y,w,h) in faces:\n            img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n            roi_gray = gray[y:y+h, x:x+w]\n            roi_color = img[y:y+h, x:x+w]\n            eyes = self.eye_cascade.detectMultiScale(roi_gray)\n            for (ex,ey,ew,eh) in eyes:\n                cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)\n\n        cv2.imshow('image',img)\n        cv2.waitKey(0)\n        cv2.destroyAllWindows()\n```\n\nEverytime I use \n```\nF_E_Detector().detect_face_eye(img)\n```\nmy kernal crashes. Is it something particular or my code has a bug?",
    "730155": "deshwalmahesh  IMO the problem is because of this code:\n```\ncv2.imshow('image',img)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n```\nYou don't need `waitKey()` and `destroyAllWindows()` since you are detecting it in a kernel. Also instead of `cv2.imshow()` use `matplotlib` to plot your image.",
    "730909": "oh yeah! thank you. that was the real problem . ran it on my local machine and got the error. Can you tell me why my code is not plotting the images in original colour? colours are different than the original images when plotted using plt.imshow()",
    "731005": "deshwalmahesh I guess your are using the `cv2.imread()` function to read the image, OpenCV reads images in the BGR format (not in the standard RGB format) so that is why there is a color difference. You can use `cv2.cvtColor(img, cv2.COLOR_BGR2RGB)` to convert to RGB and then plot the image."
  },
  "source": "meta"
}