简体   繁体   中英

IOS IT is possible CvPhotoCamera [OpenCV] we can detect face

In openCV i have to detect face and eyes . but my code is not working , here is my code please check , this is not working . But same code i am trying in CvVideoCamera then my code is working properly . only one problem eyes is not detection .

@property (nonatomic, strong) CvPhotoCamera* photoCamera;
 cv::CascadeClassifier faceCascade

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *pathToFaceCascade = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt" ofType:@"xml"];
    if (pathToFaceCascade == nil) {
        [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot find path for resource haarcascade_frontalface_alt.xml" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
        return;
    }

    if (!faceCascade.load([pathToFaceCascade UTF8String])) {
        [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot load haarcascade_frontalface_alt.xml" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
        return;
    }




    photoCamera = [[CvPhotoCamera alloc]
                   initWithParentView:imageView];
    photoCamera.delegate = self;
    photoCamera.defaultAVCaptureDevicePosition =
    AVCaptureDevicePositionFront;
    photoCamera.defaultAVCaptureSessionPreset =
    AVCaptureSessionPresetPhoto;
    photoCamera.defaultAVCaptureVideoOrientation =
    AVCaptureVideoOrientationPortrait;
    [photoCamera start];

    [self.view addSubview:imageView];



       // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)photoCamera:(CvPhotoCamera*)camera
      capturedImage:(UIImage *)image;
{
    [camera stop];
    imageView.image = image;

    // [takePhotoButton setEnabled:NO];
    // [startCaptureButton setEnabled:YES];
}
-(IBAction)takePhotoButtonPressed:(id)sender;
{
    [photoCamera takePicture];



}

#ifdef __cplusplus
//...Working Good
-(void)processImage:(Mat &)image
{
    Mat bwImage;
    cvtColor(image, bwImage, CV_BGRA2GRAY);

    equalizeHist(bwImage, bwImage);

    std::vector<cv::Rect> faces;
    faceCascade.detectMultiScale(bwImage, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));

    char buffer[64];
    static int number = 0;
    sprintf(buffer, "(%d, %d) - Detected: %ld %d", bwImage.cols, bwImage.rows, faces.size(), number++);
    String str(buffer);
    putText(image, str, cvPoint(20, 20), 0, 0.5, cvScalar(255, 255, 255, 255));

    for (int i = 0; i < faces.size(); i++) {
        cv::rectangle(image, cvPoint(faces[i].x, faces[i].y), cvPoint(faces[i].x + faces[i].width, faces[i].y + faces[i].height), cvScalar(0, 255, 255, 255), 2.0);
    }


}

As I know, in OpenCV there is no facial landmarks detection. You can use dlib for it. Here is very useful project: https://github.com/zweigraf/face-landmarking-ios

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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