python - TypeError:crop()从一到两个位置参数,但是给出了三个

标签 python numpy opencv python-imaging-library cv2

我一直在与numpy,PIL和OpenCV一起创建人脸检测和处理系统。我想做的是在边界框周围裁剪(以便进行我尚未编写的进一步处理),所以我写了以下内容:

import cv2
import numpy as np
from PIL import Image 
def main():
    #Creates a camera object
    vidCapture = cv2.VideoCapture(0)
    while 1:
        faceDetector = FaceDetector()
        #stores the current frame into the frame variable
        ret, frame = vidCapture.read()
        #detects any faces and draws bounding boxes around them
        img = faceDetector.find_any_faces(frame)
        processing = Processing()
        #crops everything around the bounding boxes for the faces
        processing.mask(frame, faceDetector.getPoint1(), faceDetector.getPoint2())

class Processing():

    #masks around the bounding boxes for the face
    def mask(self, image, p1, p2):
        #creates a numpy array from the image
        arrimg = np.array(image)
        #creates a PIL Image from the np array
        img = Image.fromarray(arrimg)

        #crops around each bounding box
        cropped_image = img.crop(p1, p2)
        return cropped_image

这将引发一条TypeError错误消息:“crop()从1到2个位置参数,但给出了3个”。从我的发现中,这些通常是因为不包括self作为方法的参数,而是将我的代码包括在内。如果有人可以帮助,那就太好了!

最佳答案

img.crop() 将一个元组作为参数(第二个是已添加的self)。当您传递2时,它将以三个参数结尾,这会给您一个错误:

Image.crop(box=None)

Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

Parameters: box – The crop rectangle, as a (left, upper, right, lower)-tuple.



您可以将元组加在一起以组成一个4元素元组:
img.crop(p1 + p2)

或传播它们:
img.crop((*p1, *p2))

关于python - TypeError:crop()从一到两个位置参数,但是给出了三个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60688467/

相关文章:

python - Numpy bool 掩码——多维和 float

python - 使用 Python 和 NumPy 组合数组

python - 使用两个标志OpenCv的calibrateCamera函数时出错

python - 如何修复此收敛错误? Python 3 统计模型

python - 如何使用 python 打开 SSH 隧道?

python - 如何在视频中添加音频?

c# - 在C#中调整图像大小并将其发送到OpenCV会导致图像失真

python - 在 Django 中基于 IP 地址(国家/地区)重定向域的最佳方法

python - 使用 scipy 的 trapz 函数进行积分

c++ - 使用 OpenCv fisheye::calibrate() 函数时出现段错误