【问题标题】:Error trying to use blur with Haarcascades尝试对 Haarcascades 使用模糊时出错
【发布时间】:2020-08-19 20:18:08
【问题描述】:

我正在尝试自己做一个项目,我认为我可以完成这项工作,但是这个问题发生了,而且……我不知道发生了什么。该项目的目的是模糊照片甚至视频上的眼睛。

import matplotlib.pyplot as plt
import numpy as np
import cv2

people = cv2.imread('Computer-Vision-with-Python/DATA/people.jpg',0)
people2 = cv2.imread('Computer-Vision-with-Python/DATA/people2.jpg')

def display(img, cmap='gray'):
    fig=plt.figure(figsize=(12,10))
    ax = fig.add_subplot(111)
    ax.imshow(img,cmap='gray')

eye_cascade = cv2.CascadeClassifier('Computer-Vision-with-Python/DATA/haarcascades/haarcascade_eye.xml')

def detect_eye(img):
    face_img = img.copy()

    face_rects = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2,minNeighbors=6)

    for (x,y,w,h) in face_rects:
        cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,255,255),10)

    return face_img

def detect_and_blur_eye(img):

    eye_img = img.copy()
    roi = img.copy()

    eye_rects = eye_cascade.detectMultiScale(eye_img,scaleFactor=1.2, minNeighbors=6) 

    for (x,y,w,h) in eye_rects: 
        print (x,y,w,h)
        roi = roi[y:y+h,x:x+w]
        blurred_roi = cv2.medianBlur(roi,7)

        eye_img[y:y+h,x:x+w] = blurred_roi

    return eye_img

results = detect_and_blur_eye(people)

之后,我收到此错误:


TypeError                                 Traceback (most recent call last)
<ipython-input-259-f561e117d7f8> in <module>
----> 1 results = detect_and_blur_eye(people)

<ipython-input-258-2ab32e080f88> in detect_and_blur_eye(img)
     12         blurred_roi = cv2.medianBlur(roi,7)
     13 
---> 14         eye_img[y:y+h,x:x+w] = blurred_roi
     15 
     16     return eye_img

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

不知道怎么回事!请在这种情况下指导我。提前致谢。

编辑

我能够发现一些可以引导我解决问题的方法。我更改了函数,以便获得一些值。

def detect_and_blur_eye(img):

    eye_img = img.copy()
    roi = img.copy()

    eye_rects = eye_cascade.detectMultiScale(eye_img,scaleFactor=1.2, minNeighbors=6) 

    for (x,y,w,h) in eye_rects: 
        print (type(eye_rects))
        print (x,y,w,h)
        roi = roi[y:y+h,x:x+w]
        print(roi.shape)
        #blurred_roi = cv2.medianBlur(roi,7)

        #eye_img[y:y+h,x:x+w] = blurred_roi

    return eye_img

results = detect_and_blur_eye(people)

它还给了我这个:

<class 'numpy.ndarray'>
1182 414 45 45
(45, 45)
<class 'numpy.ndarray'>
595 427 56 56
(0, 0)
<class 'numpy.ndarray'>
512 430 57 57
(0, 0)
<class 'numpy.ndarray'>
270 470 60 60
(0, 0)
<class 'numpy.ndarray'>
349 475 56 56
(0, 0)
<class 'numpy.ndarray'>
981 375 62 62
(0, 0)
<class 'numpy.ndarray'>
842 389 50 50
(0, 0)
<class 'numpy.ndarray'>
762 391 50 50
(0, 0)
<class 'numpy.ndarray'>
1072 390 54 54
(0, 0)
<class 'numpy.ndarray'>
1238 399 48 48
(0, 0)

所以,由于某种原因,我的形状是 (0,0)

【问题讨论】:

标签: python python-3.x opencv haar-classifier


【解决方案1】:

是的,像@jtlz2 所说的你的问题是由于在图像中找不到眼睛,因此没有类型,当你尝试分配它时会引发错误。可以有两种解决方法,或者您使用更好的分类器来填充查找眼睛,但是如果您想将它用于视频,您将需要实时并且不能使用 CNN。另一种方法是 dlib 面部关键点。除此之外,如果您只想继续使用 haar-cascades,您可以这样做:

try:
    print (x,y,w,h)
    roi = roi[y:y+h,x:x+w]
    blurred_roi = cv2.medianBlur(roi,7)

    eye_img[y:y+h,x:x+w] = blurred_roi
except:
    pass

这样如果有任何错误,你的眼睛不会模糊,但程序不会抛出错误。

【讨论】:

  • 很抱歉我没听懂你想说的。
  • 我刚刚编辑了这篇文章并提供了一些有用的信息,也许它可以提供帮助!感谢您的宝贵时间
  • 你真的不应该使用一个空的 except 语句——也许至少打印一个警告? :)
【解决方案2】:

我能够发现问题所在。在函数中,我需要在获得另一个值之前重置 ROI,而我没有这样做。

def detect_and_blur_eye(img):

    eye_img = img.copy()
    roi = img.copy()

    eye_rects = eye_cascade.detectMultiScale(eye_img,scaleFactor=1.2, minNeighbors=6,minSize=(5,5)) 

    for (x,y,w,h) in eye_rects:
        roi = img.copy()
        print (type(eye_rects))
        print (x,y,w,h)
        roi = roi[y:y+h,x:x+w]
        print(roi.shape)
        blurred_roi = cv2.medianBlur(roi,21)
        eye_img[y:y+h,x:x+w] = blurred_roi

    return eye_img

这行得通!

【讨论】:

  • 如果图像(帧)中没有眼睛怎么办?然后会发生什么?当您开始将其应用于视频时,必然会发生这种情况
  • 为什么不直接做 roi = img[y:y+h,x:x+w] 呢?您也可以将img 直接传递给检测器,而不是每次都复制一份
猜你喜欢
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 2020-12-21
相关资源
最近更新 更多