我一直在与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()
将单个元组作为参数(第二个是已添加的img.crop()
)。当您传递2时,将以三个参数结尾:
Image.crop(box = None)返回此图像的矩形区域。该框是一个四元组,定义了左,上,右和下像素坐标。参数:box-裁剪矩形,作为(左,上,右,下)元组。
您可以将元组加在一起以组成一个4元素元组:
self
或传播它们:
img.crop(p1 + p2)