Ask Your Question
0

Can't detect face on iPhone when iPhone is in Landscape mode.

asked 2013-05-13 04:18:21 -0600

nik gravatar image

updated 2013-05-13 04:36:49 -0600

Dear All I am working on face detection application on iPhone. I am creating CvVideoCamera object as follows.

    self.videoCamera = [[FAACvVideoCamera alloc] initWithParentView:self.imageView];
    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    self.videoCamera.defaultFPS = 30;
    self.videoCamera.grayscaleMode = NO;
    self.videoCamera.delegate = self;

Inside the - (void)processImage:(Mat&)image; delegate of CvVideoCamera i am detecting face by using method of CascadeClassifier class as follows.

detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(100, 100));

Strangely this function is only detecting face when i holding iPhone in portrait mode. when i rotate iPhone to landscapeLeft or LandscapeRight face detection fails.

Even i tried with built in openCV class DetectionBasedTracker Here also i am getting same result i.e only detecting face when iPhone is in portrait mode.

What should i do to get face detection working on iPhone in portrait and Landscape .

Thanks in Advance

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-05-16 01:48:19 -0600

nik gravatar image

Finally i found the solution. Need to rotate cv::Mat to portrait orientation.

Here is the my code.

self.videoCamera is the property and also add file "haarcascade_frontalface_alt.xml" to the project.

//CvVideoCamera camera initialization.

- (void)viewDidLoad
{
    NSString *faceCascadePath = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt" ofType:@"xml"];

    if(!face_cascade.load([faceCascadePath UTF8String])) {
        NSLog(@"Could not load face classifier!");
    }

    self.videoCamera = [[CvVideoCamera alloc] initWithParentView:self.imageView];
    self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
    self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
    self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
    self.videoCamera.defaultFPS = 30;
    self.videoCamera.grayscaleMode = NO;
    self.videoCamera.delegate = self;
    [self.videoCamera start];
}

//detect's the face in cv::Mat and displays rect around face.
bool detectAndDisplay( Mat frame )
{
    BOOL bFaceFound = false;
    vector<cv::Rect> faces;
    Mat frame_gray;

    cvtColor(frame, frame_gray, CV_BGRA2GRAY);
    equalizeHist(frame_gray, frame_gray);

    face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(100, 100));

    for(unsigned int i = 0; i < faces.size(); ++i) {
        rectangle(frame, cv::Point(faces[i].x, faces[i].y),
                  cv::Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
                  cv::Scalar(0,255,255));
        bFaceFound = true;
    }
    return bFaceFound;
}

//CvVideoCamera delegate
- (void)processImage:(Mat&)image;
{
    Mat tmpMat;
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    BOOL isInLandScapeMode = NO;
    BOOL rotation = 1;

    //Rotate cv::Mat to the portrait orientation
    if(orientation == UIDeviceOrientationLandscapeRight)
    {
        isInLandScapeMode = YES;
        rotation = 1;
    }
    else if(orientation == UIDeviceOrientationLandscapeLeft)
    {
        isInLandScapeMode = YES;
        rotation = 0;
    }
    else if(orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, rotation);
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, rotation);
        cvtColor(image, image, CV_BGR2BGRA);
        cvtColor(image, image, CV_BGR2RGB);
    }

    if(isInLandScapeMode)
    {
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, rotation);
        cvtColor(image, image, CV_BGR2BGRA);
        cvtColor(image, image, CV_BGR2RGB);
    }

    detectAndDisplay(image);

    if(isInLandScapeMode)
    {
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, !rotation);
        cvtColor(image, image, CV_BGR2RGB);

    }

    else if(orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, !rotation);
        cv::transpose(image, tmpMat);
        cv::flip(tmpMat, image, !rotation);
        cvtColor(image, image, CV_BGR2RGB);
    }
}
edit flag offensive delete link more

Comments

Not working for me.

Flixter gravatar imageFlixter ( 2015-08-01 05:19:46 -0600 )edit

Question Tools

Stats

Asked: 2013-05-13 04:18:21 -0600

Seen: 1,729 times

Last updated: May 16 '13