gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:23:50 25 4
gpt4 key购买 nike

我一直在与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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com