如何在opencv中裁剪检测到的人脸并在opencv python中将roi保存为图像

2024-06-25 06:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中使用opencv,这是我检测人脸和保存人脸的代码。但是它不能保存roi(检测到的人脸),我一直在做这件事这个。拜托帮我解决这个问题。在

   TRAINSET = "data/lbpcascades/lbpcascade_frontalface.xml"
   DOWNSCALE = 4

   cam = cv2.VideoCapture(0) #capture a video

   cv2.namedWindow("preview")
   classifier = cv2.CascadeClassifier(TRAINSET) 

   Compare_images=[]
   for file in os.listdir("images"):
       if file.endswith(".jpg"):
          Compare_images.append(file)
while True: # try to get the first frame
    _, frame = cam.read() 

key = cv2.waitKey(20)
if(key==32):

    print "Name of Image:"

    n= raw_input()

    value=len(Compare_images)
    cv2.imwrite('images/image'+str(n)+'.jpg', frame)
    saved_image=cv2.imread("images/image"+str(n)+".jpg")
    minisize = (saved_image.shape[1]/DOWNSCALE,saved_image.shape[0]/DOWNSCALE)
    miniframe = cv2.resize(saved_image, minisize)
    faces = classifier.detectMultiScale(miniframe)
    for f in faces:
        x, y, w, h = [ v*DOWNSCALE for v in f ]     
        print x 
        print y,w,h      

        x0,y0=int(x),int(y)
        x1,y1=int(x+w),int(y+h)
        print x0,y0,y1,y0

        image = cv2.rectangle(saved_image, (x0,y0), (x1,y1), (0,0,255),2)

        roi=saved_image[y0:y1,x1:x0]#crop 
        cv2.imwrite('roi.jpg',roi)
        cv2.imshow("adsa", saved_image) 


cv2.putText(frame, "Press ESC to close.", (5, 25),
            cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("preview", frame)

Tags: imagecv2frame人脸intcomparejpgimages
1条回答
网友
1楼 · 发布于 2024-06-25 06:27:29

你是说?公司名称:

.
.
.
print x0,y0,x1,y1
.
.
.
roi=saved_image[y0:y1,x0:x1]

while语句上下的缩进似乎不正确。在

三重引号只能临时用于块引号,因为它们可能会导致问题。在

也许用#代替:

^{pr2}$

除非函数的帮助就是这样读的。在

在你的问题中包含错误也会很有帮助。在

相关问题 更多 >