【问题标题】:Not able to work on face detection of OpenCV in python无法在 python 中对 OpenCV 进行人脸检测
【发布时间】:2020-07-29 06:07:06
【问题描述】:

我目前正在学习使用 CNN 等进行图像检测。我发现了一篇很好的文章 here,它解释了使用 OpenCV 进行人脸检测的步骤。我遵循了每一个步骤。但是在尝试测试单个示例图像时,我真的被困了几个小时。下面是我在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()

我已经在我的目录位置下载了上面提到的所有默认文件以及测试图像imagedata

但是,当我运行前几个步骤时,我收到以下错误:(

我尝试过提供物理路径,但我不明白我错过了什么。

我浏览了不同的文章来解释错误的性质,但没有一篇有帮助,所以我想直接在这里问。

【问题讨论】:

  • 嗨,我现在唯一想到的就是尝试从plt.imshow() 中删除cmap='gray'。我知道pngjpg 图像处理整数数组作为TIFF 格式的数据图像处理浮点数。
  • 已经尝试过了...但不起作用:(

标签: python opencv artificial-intelligence conv-neural-network dlib


【解决方案1】:

你应该:

print( gray.shape )

读完之后。因为您很可能正在阅读一个不存在的文件,该文件将所有代码呈现在该文件之下。

【讨论】:

  • 我被困在其中。我已经根据屏幕截图提供了文件的物理位置。当我走到那条路时,我在那里找到了图像。但我仍然无法弄清楚它是如何不加载图像的。我给出的路径不正确吗?
【解决方案2】:

我想我发现了错误:

# 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

用 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

【讨论】:

  • 那也行不通...我已经添加了路径和图像文件名...这些都不起作用:(
  • 哦,不,我认为这是问题所在 :(
  • 尝试使用其他包(如 PIL)打开图像怎么样?
【解决方案3】:

我面临的问题是因为谷歌驱动器路径。研究使用Image路径后发现,在使用colab,挂载google驱动时,即使给出绝对路径,也会在路径的开头加上/content。正因为如此,路径不正确。

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 2013-03-23
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2017-05-29
    • 2015-12-18
    • 2013-08-29
    相关资源
    最近更新 更多