我一直在与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时,它将以三个参数结尾,这会给您一个错误:



您可以将元组加在一起以组成一个4元素元组:

img.crop(p1 + p2)

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

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

10-11 22:30
查看更多