【问题标题】:TypeError: crop() takes from one to two positional arguments but three were givenTypeError:crop() 采用一到两个位置参数,但给出了三个
【发布时间】:2020-06-26 12:46:30
【问题描述】:

我一直在使用 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 作为方法的参数,但我在代码中包含了我的参数。如果有人可以提供帮助,那就太好了!

【问题讨论】:

标签: python numpy opencv python-imaging-library cv2


【解决方案1】:

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

Image.crop(box=None)

从此图像返回一个矩形区域。该框是一个定义左、上、右和下像素坐标的 4 元组。

参数:box – 裁剪矩形,为(左、上、右、下)元组。

你可以把你的元组加在一起来组成一个 4 元素的元组:

img.crop(p1 + p2)

或传播它们:

img.crop((*p1, *p2))

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2022-12-12
    相关资源
    最近更新 更多