0

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.

   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)
user2356747
  • 65
  • 4
  • 12
  • Possible duplicate of [Detect face then autocrop pictures](https://stackoverflow.com/questions/13211745/detect-face-then-autocrop-pictures) – amitnair92 May 15 '18 at 13:46

1 Answers1

1

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.

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.

jmunsch
  • 16,405
  • 6
  • 74
  • 87
  • the code works.there's no error, but the image that i saved using roi(the face detected),was empty...i hope you could help me in this. roi=saved_image[y0:y1,x0:x1] this the part where i crop the image, so that icould only save the face – user2356747 Apr 01 '14 at 03:27
  • @user2356747 I didn't know the image was empty, or that the code was working. This wasn't mentioned in the question. Sorry. What happens when you run the code with the changes that were suggested? ( e.g. `saved_image[y0:y1,x0:x1]` instead of what is in the code you gave `saved_image[y0:y1,x1:x0]`) Is the image still empty? – jmunsch Apr 01 '14 at 03:49
  • sorry for not mentioning it..i'll try your suggestion. – user2356747 Apr 01 '14 at 03:51