Ask Your Question
0

cv.iplimage object has no attribute 'save'

asked 2013-02-21 09:23:11 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

When performing the program, I get the following error: "cv.iplimage object has no attribute 'save'" in module "croppedImage.save(fname+'_crop'+str(n)+ext)". Please, tell me where I go wrong? It is my first python+opencv script, so be tolerant :) code:

#!/usr/bin/python

import cv
import time
import Image
import glob
import os

#Functions
def DetectFace(image, faceCascade, returnImage=False):


    min_size = (20,20)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    cv.EqualizeHist(image, image)

    faces = cv.HaarDetectObjects(
            image, faceCascade, cv.CreateMemStorage(0),
            haar_scale, min_neighbors, haar_flags, min_size
        )

    if faces and returnImage:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)

    if returnImage:
        return image
    else:
        return faces

def pil2cvGrey(pil_im):
    pil_im = pil_im.convert('L')
    cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0]  )
    return cv_im

def cv2pil(cv_im):
    return Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())

def imgCrop(image, cropBox, boxScale=1):
    xDelta=max(cropBox[2]*(boxScale-1),0)
    yDelta=max(cropBox[3]*(boxScale-1),0)

    PIL_box=[cropBox[0]-xDelta, cropBox[1]-yDelta, cropBox[0]+cropBox[2]+xDelta, cropBox[1]+cropBox[3]+yDelta]

    return image.crop(PIL_box)

def faceCrop(imagePattern,boxScale=1):
    faceCascade = cv.Load('haarcascade_frontalface_alt.xml')

    imgList=glob.glob(imagePattern)
    if len(imgList)<=0:
        print 'No Images Found'
        return

    for img in imgList:
        pil_im=Image.open(img)
        cv_im=pil2cvGrey(pil_im)
        faces=DetectFace(cv_im,faceCascade)
        if faces:
            n=1
            for face in faces:
                croppedImage=imgCrop(pil_im, face[0],boxScale=boxScale)
                fname,ext=os.path.splitext(img)
                croppedImage.save(fname+'_crop'+str(n)+ext)
                n+=1
        else:
            print 'No faces found:', img
# MAIN
capture = cv.CaptureFromCAM(0)
faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
while (cv.WaitKey(15)==-1):
    img = cv.QueryFrame(capture)
    img.save(fname+'_captured'+str(n)+ext)
    faceCrop('home/face-control/facedetect/*.jpg',boxScale=1)

P.S. Sorry for my english.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-02-21 13:10:04 -0500

rics gravatar image

You have to use cv2.imwrite(filename,img) or cv.SaveImage(filename,img) as there is no such method as img.save.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-02-21 09:23:11 -0500

Seen: 1,274 times

Last updated: Feb 21 '13