简体   繁体   English

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

[英]how to crop the detected face in opencv and save roi as image in opencv python

Im using opencv in python and this is my code in detecting the face and saving the face..but it does not save the roi(the face detected),i've been having trouble doing this.please help me how to fix this. 我在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)

Do you mean?: 你的意思是?:

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

The indentation above and below the while statement seems incorrect. while语句上方和下方的缩进似乎不正确。

Triple quotes should only be used temporarily for block quotes as they can cause problems. 三重引号仅应暂时用于块引号,因为它们可能会引起问题。

Maybe use # instead: 也许使用#代替:

#x0,y0=x,y
#x1,y1=x+w,y+h

Unless that is how the help for that function is suppose to read. 除非那样,否则应该阅读该功能的帮助。

Including errors in your question would be helpful too. 在您的问题中包括错误也将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM