简体   繁体   English

无法在 python 中对 OpenCV 进行人脸检测

[英]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.我目前正在学习使用 CNN 等进行图像检测。我在这里找到了一篇很好的文章,它解释了使用 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:以下是我在 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.我试过给出物理路径,但我不明白我错过了什么。

在此处输入图像描述

I ran through different articles that explain the nature of the error, but none of them helped so I thought of asking here directly.我浏览了不同的文章来解释错误的性质,但没有一篇有帮助,所以我想直接在这里问。

You should:你应该:

print( gray.shape )

after you read it.读完之后。 Because most likely you're reading a non-existent file that renders all code below that moot.因为您很可能正在阅读一个不存在的文件,该文件将所有代码呈现在该文件之下。

I think I found the error:我想我发现了错误:

# This is what you have
path = r'C:\Users\randomUser\Desktop\imagedata.jpeg'
gray = cv2.imread('imagedata.jpeg')

# This is what you should have
path = r'C:\Users\randomUser\Desktop\imagedata.jpeg'
gray = cv2.imread(path) # <-- you weren't using the path of the image

Opening the image with PIL?用 PIL 打开图像?

from PIL import Image

path = r'C:\Users\randomUser\Desktop\imagedata.jpeg'
gray = Image.open(path).convert("L") # L to open the image in gray scale

not sure if this works:S不确定这是否有效:S

Let me know if that helped: :D让我知道这是否有帮助::D

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.研究使用Image路径后发现,在使用colab,挂载google驱动时,即使给出绝对路径,也会在路径的开头加上/content Just because of this the path was not correct.正因为如此,路径不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM