Search code examples
pythonopencvartificial-intelligenceconv-neural-networkdlib

Not able to work on face detection of OpenCV in python


I am currently learning Image detection using CNN etc. I found out a good article here which explain the face detection steps using OpenCV. I followed each and every steps. But I am really stuck since hours when trying to test a single sample image. Below is the code I used in google Colab:

import cv2
import matplotlib.pyplot as plt
import dlib
import os
from imutils import face_utils
font = cv2.FONT_HERSHEY_SIMPLEX

cascPath=r'C:\Users\randomUser\Desktop\haarcascade_frontalface_default.xml'
eyePath = r'C:\Users\randomUser\Desktop\haarcascade_eye.xml'
smilePath = r'C:\Users\randomUser\Desktop\haarcascade_smile.xml'
faceCascade = cv2.CascadeClassifier(cascPath)
eyeCascade = cv2.CascadeClassifier(eyePath)
smileCascade = cv2.CascadeClassifier(smilePath)

# even if I use the below path, I am still getting the error.
path = r'C:\Users\randomUser\Desktop\imagedata.jpeg'
gray = cv2.imread('imagedata.jpeg')
plt.figure(figsize=(12,8))
plt.imshow(gray, cmap='gray')
plt.show()

I have downloaded all the default files as mentioned above in my directory location along with the test image imagedata

However, when I am running the first few steps, I am getting the below error :(

I have tried giving physical path but I don't understand what am I missing.

enter image description here

I ran through different articles that explain the nature of the error, but none of them helped so I thought of asking here directly.


Solution

  • The issue that I was facing was because of the google drive path. After researching and using the Image path, I found out that when using colab, and mounting the google drive, even if you give the absolute path, it will add /content at the beginning of the path. Just because of this the path was not correct.