Java Code Examples for org.opencv.core.MatOfRect#toArray()

The following examples show how to use org.opencv.core.MatOfRect#toArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From MOAAP with MIT License 7 votes vote down vote up
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    //Rotating the input frame
    Mat mGray = inputFrame.gray();
    mRgba = inputFrame.rgba();
    if (mIsFrontCamera)
    {
        Core.flip(mRgba, mRgba, 1);
        Core.flip(mGray, mGray, 1);
    }

    //Detecting face in the frame
    MatOfRect faces = new MatOfRect();
    if(haarCascade != null)
    {
        haarCascade.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(200,200), new Size());
    }

    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), new Scalar(100), 3);
    return mRgba;
}
 
Example 2
Source File: DetectObject.java    From classchecks with Apache License 2.0 7 votes vote down vote up
/**
 * 
* @Title: detectMany 
* @Description: 检测多人图片,返回检测的人脸区域对象
* @param mImgSRC
* @return
* MatOfRect
 */
public static MatOfRect detectMany(Mat mImgSRC) {
	
	if(mImgSRC.empty()) {
		LOG.info("检测多人图片检测时没有找到图片");
		return null;
	}
	// 人脸检测器文件的所在路径的文件夹名称数组
	String [] pathKey = {ServletContextHolder.getOpenCVHaarcascades(), "haarcascade_frontalface_alt.xml"};
	CascadeClassifier cascade = new CascadeClassifier(FileUtils.buildFilePath(pathKey));
	if(cascade.empty()) {
		LOG.info("人脸检测级联加载器为null");
		return null;
	}
	// 记录搜索到的人脸区域
	MatOfRect mOfRect = new MatOfRect();
	// 用于计算缩放比例
	int scaledWidth = mImgSRC.width();
	detectManyObject(mImgSRC, cascade, mOfRect, scaledWidth);
	if(mOfRect.toArray().length <= 0) {
		LOG.info("没有检测到人脸...");
		return null;
	}
	return mOfRect;
}
 
Example 3
Source File: FaceDetectionTest.java    From opencv-fun with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void loop (CascadeClassifier classifier, Mat mat, ImgWindow window, VideoCapture video) {
	video.read(mat);
	System.out.println(mat);
	if (!mat.empty()) {
		MatOfRect rects = new MatOfRect();
		long start = System.nanoTime();
		classifier.detectMultiScale(mat, rects);
		System.out.println((System.nanoTime()-start)/1000000000.0);
		window.setImage(mat);
		Graphics2D g = window.begin();
		g.setColor(Color.RED);
		for(Rect r: rects.toArray()) {
			g.drawRect(r.x, r.y, r.width, r.height);
		}
		window.end();
	}
}
 
Example 4
Source File: VideoFaceTests.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * OpenCV-4.0.0 人脸识别
 * 
 * @date: 2019年5月7日12:16:55
 * @param image
 *            待处理Mat图片(视频中的某一帧)
 * @return 处理后的图片
 * @throws IOException
 */
public static Mat getFace(Mat image) throws IOException {
	// 1 读取OpenCV自带的人脸识别特征XML文件
	File faceFile = new ClassPathResourcePatternResolver().getResource("opencv/data/haarcascade_frontalface_alt.xml")
			.getFile();
	CascadeClassifier facebook = new CascadeClassifier(faceFile.getAbsolutePath());
	// 2 特征匹配类
	MatOfRect face = new MatOfRect();
	// 3 特征匹配
	facebook.detectMultiScale(image, face);
	Rect[] rects = face.toArray();
	System.out.println("匹配到 " + rects.length + " 个人脸");
	// 4 为每张识别到的人脸画一个圈
	for (int i = 0; i < rects.length; i++) {
		Imgproc.rectangle(image, new Point(rects[i].x, rects[i].y),
				new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 255, 0));
		Imgproc.putText(image, "Human", new Point(rects[i].x, rects[i].y), Imgproc.FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0,
				new Scalar(0, 255, 0), 1, Imgproc.LINE_AA, false);
		// Mat dst=image.clone();
		// Imgproc.resize(image, image, new Size(300,300));
	}
	return image;
}
 
Example 5
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
void HOGDescriptor() {
    Mat grayMat = new Mat();
    Mat people = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    HOGDescriptor hog = new HOGDescriptor();
    hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());

    MatOfRect faces = new MatOfRect();
    MatOfDouble weights = new MatOfDouble();

    hog.detectMultiScale(grayMat, faces, weights);
    originalMat.copyTo(people);
    //Draw faces on the image
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i < facesArray.length; i++)
        Imgproc.rectangle(people, facesArray[i].tl(), facesArray[i].br(), new Scalar(100), 3);

    //Converting Mat back to Bitmap
    Utils.matToBitmap(people, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 6
Source File: DetectFaceDemo.java    From Java-for-Data-Science with MIT License 6 votes vote down vote up
public void run() {
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  String base = "C:/Books in Progress/Java for Data Science/Chapter 10/OpenCVExamples/src/resources";
  CascadeClassifier faceDetector = 
          new CascadeClassifier(base + "/lbpcascade_frontalface.xml");
  
  Mat image = Imgcodecs.imread(base + "/images.jpg");

  MatOfRect faceVectors = new MatOfRect();
  faceDetector.detectMultiScale(image, faceVectors);

  out.println(faceVectors.toArray().length + " faces found");

  for (Rect rect : faceVectors.toArray()) {
      Imgproc.rectangle(image, new Point(rect.x, rect.y), 
              new Point(rect.x + rect.width, rect.y + rect.height), 
              new Scalar(0, 255, 0));
  }
  Imgcodecs.imwrite("faceDetection.png", image);
}
 
Example 7
Source File: FaceDetectionController.java    From ExoVisix with MIT License 5 votes vote down vote up
/**
 * Method for face detection and tracking
 * 
 * @param frame
 *            it looks for faces in this frame
 */
private void detectAndDisplay(Mat frame)
{
	MatOfRect faces = new MatOfRect();
	Mat grayFrame = new Mat();
	
	// convert the frame in gray scale
	Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
	// equalize the frame histogram to improve the result
	Imgproc.equalizeHist(grayFrame, grayFrame);
	
	// compute minimum face size (20% of the frame height, in our case)
	if (this.absoluteFaceSize == 0)
	{
		int height = grayFrame.rows();
		if (Math.round(height * 0.2f) > 0)
		{
			this.absoluteFaceSize = Math.round(height * 0.2f);
		}
	}
	
	// detect faces
	this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
			new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());
			
	// each rectangle in faces is a face: draw them!
	Rect[] facesArray = faces.toArray();
	for (int i = 0; i < facesArray.length; i++)
	{
		Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(7, 255, 90), 4);
		System.out.println(facesArray[i].tl());	
		System.out.println(facesArray[i].br());	
	}
	
		
	

}
 
Example 8
Source File: DetectActivity.java    From FaceDetectDemo with Apache License 2.0 5 votes vote down vote up
@Override
// 这里执行人脸检测的逻辑, 根据OpenCV提供的例子实现(face-detection)
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();
    // 翻转矩阵以适配前后置摄像头
    if (isFrontCamera) {
        Core.flip(mRgba, mRgba, 1);
        Core.flip(mGray, mGray, 1);
    } else {
        Core.flip(mRgba, mRgba, -1);
        Core.flip(mGray, mGray, -1);
    }
    float mRelativeFaceSize = 0.2f;
    if (mAbsoluteFaceSize == 0) {
        int height = mGray.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
    }
    MatOfRect faces = new MatOfRect();
    if (classifier != null)
        classifier.detectMultiScale(mGray, faces, 1.1, 2, 2,
                new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    Rect[] facesArray = faces.toArray();
    Scalar faceRectColor = new Scalar(0, 255, 0, 255);
    for (Rect faceRect : facesArray)
        Imgproc.rectangle(mRgba, faceRect.tl(), faceRect.br(), faceRectColor, 3);
    return mRgba;
}
 
Example 9
Source File: ImgprocessUtils.java    From classchecks with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	* @Title: cutOutImage 
	* @Description: 以Rect坐标裁剪图片 
	* @param src
	* @param mOfRect
	* @return
	* List<Mat> 
	 */
	public static List<Mat> cutOutImage(Mat src, MatOfRect mOfRect) {
		
		List<Mat> outs = new ArrayList<Mat>();
		
		if(src.empty() || mOfRect.toArray().length <= 0) {
			return null;
		}
		
		int enlarge = 50; //  将图片的x,y坐标加大
		for(Rect r : mOfRect.toArray()) {
			int enlargeW = (r.x + enlarge) / r.x;
			int enlargeH = (r.y + enlarge) / r.y;
			r.x -= enlarge;
			r.y -= enlarge;
			r.width += (enlargeW * r.width);
			r.height += (enlargeH * r.height);
			
			// 确定人脸在图像中的整个边界,防止人脸在边框之外
			if (r.x < 0) {
				r.x = 0;
			}
			if (r.y < 0) {
				r.y = 0;
			}
			if (r.x + r.width > src.cols()) {
				r.x = src.cols() - r.width;
			}
			if (r.y + r.height > src.rows()) {
				r.y = src.rows() - r.height;
			}
		
			
			Mat cutting = new Mat(src, r); // 裁剪图片
//			System.out.println(cutting);
			outs.add(cutting);
		}
		return outs;
	}
 
Example 10
Source File: DetectObjectTest.java    From classchecks with Apache License 2.0 5 votes vote down vote up
@Test
public void testDetectManyObject() {
	String opencvDLL = "G:/java/JavaProjectRelease/classchecks/src/main/webapp/WEB-INF/dll/x64/opencv_java320.dll";
	System.load(opencvDLL);
	
	String haarcascade = "haarcascade_frontalface_alt.xml";
	
	
	CascadeClassifier cascade = new CascadeClassifier(XMLFilePath + haarcascade);
	
	Mat src = Imgcodecs.imread(imageDir + "/split/14.jpg");
	
	MatOfRect objects = new MatOfRect();
	int scaledWidth = src.width();
	
	DetectObject.detectManyObject(src, cascade, objects, scaledWidth);
	
	Rect [] rects = objects.toArray();
	int i = 0;
	for(Rect r : rects) {
		/*Imgproc.rectangle(src, new Point(r.x-100 , r.y-100 ), 
				new Point(r.x + r.width + 80, 
						r.y + r.height + 80), new Scalar(0, 0, 255), 3);*/
		Imgproc.rectangle(src, r.tl(), 
				r.br(), new Scalar(0, 0, 255), 3);
		/*r.width += 120;
		r.height += 120;
		r.x -= 100;
		r.y -= 100;
		System.out.println(r);
		Mat roi = new Mat(src, r);
		Imgcodecs.imwrite("e:/classchecks/2017417/split/"+i+".jpg", roi);
		i ++;*/
	}
	Imgcodecs.imwrite("e:/classchecks/2017417/dectctManyObject.jpg", src);
	//Imgcodecs.imwrite("e:/classchecks/dectctManyObject.jpg", src);
}
 
Example 11
Source File: FaceDetection.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public Eyes getEyes(Mat img){
    double halfWidth = img.cols() / 2;
    double height = img.rows();
    double[] values = new double[4];
    values[0] = 0;
    values[1] = 0;
    values[2] = halfWidth;
    values[3] = height;
    Rect rightHalf = new Rect(values);
    values[0] = halfWidth;
    Rect leftHalf = new Rect(values);
    MatOfRect rightEyes = new MatOfRect();
    MatOfRect leftEyes = new MatOfRect();

    Mat rightHalfImg = img.submat(rightHalf);
    rightEyeDetector.detectMultiScale(rightHalfImg, rightEyes);
    Mat leftHalfImg = img.submat(leftHalf);
    leftEyeDetector.detectMultiScale(leftHalfImg, leftEyes);

    if (rightEyes.empty() || leftEyes.empty() || rightEyes.toArray().length > 1 || leftEyes.toArray().length > 1){
        return null;
    }

    Rect rightEye = rightEyes.toArray()[0];
    Rect leftEye = leftEyes.toArray()[0];

    MatOfFloat rightPoint = new MatOfFloat(rightEye.x + rightEye.width / 2, rightEye.y + rightEye.height / 2);
    MatOfFloat leftPoint = new MatOfFloat(img.cols() / 2 + leftEye.x + leftEye.width / 2, leftEye.y + leftEye.height / 2);

    MatOfFloat diff = new MatOfFloat();
    Core.subtract(leftPoint, rightPoint, diff);
    double angle = Core.fastAtan2(diff.toArray()[1], diff.toArray()[0]);
    double dist = Core.norm(leftPoint, rightPoint, Core.NORM_L2);
    Eyes eyes = new Eyes(dist, rightPoint, leftPoint, angle);
    return eyes;
}
 
Example 12
Source File: FaceDetectionActivity.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        mRgba = inputFrame.rgba();
        mGray = inputFrame.gray();

        if (mAbsoluteFaceSize == 0) {
            int height = mGray.rows();
            if (Math.round(height * mRelativeFaceSize) > 0) {
                mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
            }
            mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
        }

        MatOfRect faces = new MatOfRect();

        if (mDetectorType == JAVA_DETECTOR) {
            if (mJavaDetector != null)
                mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                        new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
        }
        else if (mDetectorType == NATIVE_DETECTOR) {
            if (mNativeDetector != null)
                mNativeDetector.detect(mGray, faces);
        }
        else {
            Log.e(TAG, "Detection method is not selected!");
        }

        Rect[] facesArray = faces.toArray();
        for (int i = 0; i < facesArray.length; i++)
            Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);

        return mRgba;
    }
 
Example 13
Source File: FaceDetection.java    From AndroidFaceRecognizer with MIT License 5 votes vote down vote up
private static Rect detectEyes(DetectionBasedTracker detector, Mat image, boolean left){
	Rect result = null;
	if(detector == null){
		return result;
	}
	detector.setMinFaceSize(30);
	MatOfRect eyes = new MatOfRect();
	detector.detect(image, eyes);
	Rect[] eyesArray = eyes.toArray();
	int possibleEyeIndex = -1;
	int minEyeRectSize = 1000000;
	 for (int j = 0; j < eyesArray.length; j++){
		 if(left && (eyesArray[j].x + eyesArray[j].width) > (image.cols()/2)) {
			 continue;
		 }
		 if(!left && (eyesArray[j].x + eyesArray[j].width) < (image.cols()/2)) {
			 continue;
		 }
		 if(eyesArray[j].x > 0
				 && eyesArray[j].y > (image.rows() /4) && (eyesArray[j].y + eyesArray[j].height) < (image.rows() /2)){
			 int rectSize = eyesArray[j].height * eyesArray[j].width;
			 if(rectSize < minEyeRectSize){
				 possibleEyeIndex = j;
				 rectSize = minEyeRectSize;
			 }
		 }
	 }
	 if(possibleEyeIndex != -1) {
		 result = eyesArray[possibleEyeIndex];
	 }
	 return result;
}
 
Example 14
Source File: HaarCascadeOp.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@Override
public List<CVParticle> execute(CVParticle input) throws Exception {
	ArrayList<CVParticle> result = new ArrayList<CVParticle>();
	Frame frame = (Frame)input;
	if(frame.getImageType().equals(Frame.NO_IMAGE)) return result;

	MatOfByte mob = new MatOfByte(frame.getImageBytes());
	Mat image = Highgui.imdecode(mob, Highgui.CV_LOAD_IMAGE_COLOR);
	
	/*
	mob = new MatOfByte();
	Highgui.imencode(".png", image, mob);
	BufferedImage bi = ImageUtils.bytesToImage(mob.toArray());
	ImageIO.write(bi, "png", new File("testOutput/"+sf.getStreamId()+"_"+sf.getSequenceNr()+".png"));
	*/
	
	MatOfRect haarDetections = new MatOfRect();
	haarDetector.detectMultiScale(image, haarDetections, scaleFactor, minNeighbors, flags, new Size(minSize[0], minSize[1]), new Size(maxSize[0], maxSize[1]));
	ArrayList<Descriptor> descriptors = new ArrayList<Descriptor>();
	for(Rect rect : haarDetections.toArray()){
		Rectangle box = new Rectangle(rect.x, rect.y, rect.width, rect.height);
		descriptors.add(new Descriptor(input.getStreamId(), input.getSequenceNr(), box, 0, new float[0]));
	}
	
	Feature feature = new Feature(input.getStreamId(), input.getSequenceNr(), name, 0, descriptors, null);
	if(outputFrame){
		frame.getFeatures().add(feature);
		result.add(frame);
	}else{
		result.add(feature);
	}
	return result;
}
 
Example 15
Source File: CascadeClassifierProcessor.java    From Camdroid with Apache License 2.0 5 votes vote down vote up
protected void execute() {

            if (!classifierPath.equals(lastUsedClassifierPath)) {
                detector.load(classifierPath);
                lastUsedClassifierPath = classifierPath;
            }

            if (detector.empty()) {
                return;
            }

            out = gray();

            double min = (double) this.in.height() * object_min_size / 100;
            double max = (double) this.in.height() * object_max_size / 100;

            MatOfRect rects = new MatOfRect();
            detector.detectMultiScale(out, rects,
                    1 + (double) scale_factor / 100, min_neighbors, 0,
                    new Size(min, min), new Size(max, max));

            for (Rect rect : rects.toArray()) {
                Imgproc.rectangle(out, rect.tl(), rect.br(),
                        new Scalar(255, 0, 0), 1);
            }

        }
 
Example 16
Source File: ObjectDetector.java    From OpenCV-android with Apache License 2.0 5 votes vote down vote up
/**
 * 目标检测
 *
 * @param gray   灰度图像
 * @param object 识别结果的容器
 * @return 检测到的目标位置集合
 */
public Rect[] detectObject(Mat gray, MatOfRect object) {
    // 使用Java人脸检测
    mCascadeClassifier.detectMultiScale(
            gray, // 要检查的灰度图像
            object, // 检测到的人脸
            1.1, // 表示在前后两次相继的扫描中,搜索窗口的比例系数。默认为1.1即每次搜索窗口依次扩大10%;
            mMinNeighbors, // 默认是3 控制误检测,表示默认几次重叠检测到人脸,才认为人脸存在
            Objdetect.CASCADE_SCALE_IMAGE,
            getSize(gray, mRelativeObjectWidth, mRelativeObjectHeight), // 目标最小可能的大小
            gray.size()); // 目标最大可能的大小

    return object.toArray();
}
 
Example 17
Source File: EyesDetectionInteractorImpl.java    From Image-Detection-Samples with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Build a template from a specific eye area previously substracted
 * uses detectMultiScale for this area, then uses minMaxLoc method to
 * detect iris from the detected eye</p>
 *
 * @param area Preformatted Area
 * @param size minimum iris size
 * @param grayMat image in gray
 * @param rgbaMat image in color
 * @param detectorEye Haar Cascade classifier
 * @return built template
 */
@NonNull
private static Mat buildTemplate(Rect area, final int size,
                                 @NonNull Mat grayMat,
                                 @NonNull Mat rgbaMat,
                                 CascadeClassifier detectorEye) {
    Mat template = new Mat();
    Mat graySubMatEye = grayMat.submat(area);
    MatOfRect eyes = new MatOfRect();
    Rect eyeTemplate;
    detectorEye.detectMultiScale(graySubMatEye, eyes, 1.15, 2,
            Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                    | Objdetect.CASCADE_SCALE_IMAGE, new Size(EYE_MIN_SIZE, EYE_MIN_SIZE),
            new Size());

    Rect[] eyesArray = eyes.toArray();
    if (eyesArray.length > 0) {
        Rect e = eyesArray[0];
        e.x = area.x + e.x;
        e.y = area.y + e.y;
        Rect eyeRectangle = getEyeArea((int) e.tl().x,
                (int) (e.tl().y + e.height * 0.4),
                e.width,
                (int) (e.height * 0.6));
        graySubMatEye = grayMat.submat(eyeRectangle);
        Mat rgbaMatEye = rgbaMat.submat(eyeRectangle);


        Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(graySubMatEye);

        FaceDrawerOpenCV.drawIrisCircle(rgbaMatEye, minMaxLoc);
        Point iris = new Point();
        iris.x = minMaxLoc.minLoc.x + eyeRectangle.x;
        iris.y = minMaxLoc.minLoc.y + eyeRectangle.y;
        eyeTemplate = getEyeArea((int) iris.x - size / 2,
                (int) iris.y
                        - size / 2, size, size);
        FaceDrawerOpenCV.drawEyeRectangle(eyeTemplate, rgbaMat);
        template = (grayMat.submat(eyeTemplate)).clone();
    }
    return template;
}
 
Example 18
Source File: ShowCameraViewActivity.java    From FaceT with Mozilla Public License 2.0 4 votes vote down vote up
@Override
    public Mat onCameraFrame(CvCameraViewFrame cvCameraViewFrame) {

        mRgba = cvCameraViewFrame.rgba();
        mGray = cvCameraViewFrame.gray();


        if (mAbsoluteFaceSize == 0) {
            int height = mGray.rows();
            if (Math.round(height * mRelativeFaceSize) > 0) {
                mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
            }
        }


        MatOfRect faces = new MatOfRect();

        if (mDetectorType == JAVA_DETECTOR) {
            if (mJavaDetector != null) {
                mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                        new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());

            }
        } else {
            Log.e(TAG, "Detection method is not selected!");
        }

        Rect[] facesArray = faces.toArray();
        for (int i = 0; i < facesArray.length; i++) {
            Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
            face_middle_x = (int) facesArray[i].tl().x + facesArray[i].width / 2;
            face_middle_y = (int) facesArray[i].tl().y + facesArray[i].height / 2;
            Log.d("face middle : ", face_middle_x + "," + face_middle_y);
            Log.d(TAG, "faces array " + String.valueOf(i));

        }
//        if(facesArray.length > 0) {
//            //auto save face when it detects the face
//            //Thread t = getBaseContext().getMainLooper().getThread();
//            Thread t = new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_S");
//                    String currentDateandTime = sdf.format(new Date());
//                    String saveDir = Environment.getExternalStorageDirectory().getPath() + "/DCIM/OCV/FDSave";
//                    File dirCheck = new File(saveDir);
//                    if(!dirCheck.exists()) {
//                        dirCheck.mkdirs();
//                    }
//                    String fileName = saveDir + "/" + currentDateandTime + ".jpg";
//                    try {
//                        mOpenCvCameraView.takePicture(fileName);
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }
//                }
//            });
//            t.start();
//        }
//
//        if (mIsColorSelected) {
//            mDetector.process(mRgba);
//            List<MatOfPoint> contours = mDetector.getContours();
//            Log.e(TAG, "Contours count: " + contours.size());
//            Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);
//
//            Mat colorLabel = mRgba.submat(4, 68, 4, 68);
//            colorLabel.setTo(mBlobColorRgba);
//
//            Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
//            mSpectrum.copyTo(spectrumLabel);
//        }

        return mRgba;
    }
 
Example 19
Source File: Training.java    From marvel with MIT License 4 votes vote down vote up
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    if (mAbsoluteFaceSize == 0) {
        int height = mGray.rows();
        if (Math.round(height * mRelativeFaceSize) > 0) {
            mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
        }
        //  mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
    }

    MatOfRect faces = new MatOfRect();

    if (mDetectorType == JAVA_DETECTOR) {
        if (mJavaDetector != null)
            mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
    }
    else if (mDetectorType == NATIVE_DETECTOR) {
        /*if (mNativeDetector != null)
            mNativeDetector.detect(mGray, faces);*/
    }
    else {
        Log.e(TAG, "Detection method is not selected!");
    }

    Rect[] facesArray = faces.toArray();

    if ((facesArray.length==1)&&(faceState==TRAINING)&&(countImages<MAXIMG)&&(!text.equals("")))
    {


        Mat m;
        Rect r=facesArray[0];


        m=mRgba.submat(r);
        mBitmap = Bitmap.createBitmap(m.width(),m.height(), Bitmap.Config.ARGB_8888);


        Utils.matToBitmap(m, mBitmap);

        Message msg = new Message();
        String textTochange = "IMG";
        msg.obj = textTochange;
        mHandler.sendMessage(msg);
        if (countImages<MAXIMG)
        {
            fr.add(m, text);
            countImages++;
        }

    }
    for (int i = 0; i < facesArray.length; i++)
        Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);

    return mRgba;
}
 
Example 20
Source File: FXController.java    From Face-Recognition with Apache License 2.0 4 votes vote down vote up
/**
	 * Method for face detection and tracking
	 * 
	 * @param frame
	 *            it looks for faces in this frame
	 */
	private void detectAndDisplay(Mat frame)
	{
		MatOfRect faces = new MatOfRect();
		Mat grayFrame = new Mat();
		
		// convert the frame in gray scale
		Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
		// equalize the frame histogram to improve the result
		Imgproc.equalizeHist(grayFrame, grayFrame);
		
		// compute minimum face size (20% of the frame height, in our case)
		if (this.absoluteFaceSize == 0)
		{
			int height = grayFrame.rows();
			if (Math.round(height * 0.2f) > 0)
			{
				this.absoluteFaceSize = Math.round(height * 0.2f);
			}
		}
		
		// detect faces
		this.faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | Objdetect.CASCADE_SCALE_IMAGE,
				new Size(this.absoluteFaceSize, this.absoluteFaceSize), new Size());
				
		// each rectangle in faces is a face: draw them!
		Rect[] facesArray = faces.toArray(); 
		for (int i = 0; i < facesArray.length; i++) {
			Imgproc.rectangle(frame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0), 3);

			// Crop the detected faces
			Rect rectCrop = new Rect(facesArray[i].tl(), facesArray[i].br());
			Mat croppedImage = new Mat(frame, rectCrop);
			// Change to gray scale
			Imgproc.cvtColor(croppedImage, croppedImage, Imgproc.COLOR_BGR2GRAY);
			// Equalize histogram
			Imgproc.equalizeHist(croppedImage, croppedImage);
			// Resize the image to a default size
			Mat resizeImage = new Mat();
			Size size = new Size(250,250);
			Imgproc.resize(croppedImage, resizeImage, size);
			
			// check if 'New user' checkbox is selected
			// if yes start collecting training data (50 images is enough)
			if ((newUser.isSelected() && !newname.isEmpty())) {
				if (index<20) {
					Imgcodecs.imwrite("resources/trainingset/combined/" +
					random + "-" + newname + "_" + (index++) + ".png", resizeImage);
				}
			}
//			int prediction = faceRecognition(resizeImage);
			double[] returnedResults = faceRecognition(resizeImage);
			double prediction = returnedResults[0];
			double confidence = returnedResults[1];
			
//			System.out.println("PREDICTED LABEL IS: " + prediction);
			int label = (int) prediction;
			String name = "";
			if (names.containsKey(label)) {
				name = names.get(label);
			} else {
				name = "Unknown";
			}
			
			// Create the text we will annotate the box with:
//            String box_text = "Prediction = " + prediction + " Confidence = " + confidence;
            String box_text = "Prediction = " + name + " Confidence = " + confidence;
            // Calculate the position for annotated text (make sure we don't
            // put illegal values in there):
            double pos_x = Math.max(facesArray[i].tl().x - 10, 0);
            double pos_y = Math.max(facesArray[i].tl().y - 10, 0);
            // And now put it into the image:
            Imgproc.putText(frame, box_text, new Point(pos_x, pos_y), 
            		Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0, 2.0));
		}
	}