本文整理匯總了Java中org.opencv.objdetect.CascadeClassifier類的典型用法代碼示例。如果您正苦於以下問題:Java CascadeClassifier類的具體用法?Java CascadeClassifier怎麽用?Java CascadeClassifier使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CascadeClassifier類屬於org.opencv.objdetect包,在下文中一共展示了CascadeClassifier類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initClassifier
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
private void initClassifier() {
try {
InputStream is = getResources()
.openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File cascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(cascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
classifier = new CascadeClassifier(cascadeFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
示例2: loop
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
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();
}
}
示例3: run
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
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);
}
開發者ID:PacktPublishing,項目名稱:Machine-Learning-End-to-Endguide-for-Java-developers,代碼行數:21,代碼來源:DetectFaceDemo.java
示例4: main
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
public static void main(String args[]) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceCascade = new CascadeClassifier(xmlFile);
if(!faceCascade.load(xmlFile)) {
System.out.println("Error Loading XML File");
} else {
System.out.println("Success Loading XML");
}
//invokeLater enables *swing threads to operate,
//it is not actually going to run this method later
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FaceTrackMain frame = new FaceTrackMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
示例5: dobj
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
private static Mat dobj(CascadeClassifier objDetector, Mat src) {
final Mat dst = src.clone();
final MatOfRect objDetections = new MatOfRect();
objDetector.detectMultiScale(dst, objDetections);
if (objDetections.toArray().length <= 0) {
return src;
}
for (final Rect rect : objDetections.toArray()) {
Imgproc.rectangle(dst, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 0, 255), 2);
}
return dst;
}
示例6: staticFace
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
public static void staticFace(String input, String output) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
final CascadeClassifier faceDetector = new CascadeClassifier(StaticFacialRecognition.class
.getResource("../../../../../opencv/sources/data/haarcascades_cuda/haarcascade_frontalface_alt.xml")
.getPath().substring(1));
final Mat image = Imgcodecs.imread(input);
// �������� StaticFacialRecognition.class.getResource(input).getPath().substring(1)
final MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
for (final Rect rect : faceDetections.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));
}
System.out.println(String.format("Writing %s", output));
Imgcodecs.imwrite(output, image);
// �������� StaticFacialRecognition.class.getResource("").getPath().substring(1) +
// output
}
示例7: detectLargestObject
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/**
* 檢測一張人臉
* @param img
* @param cascade
* @param largestObject
* @param scaledWidth
*/
public static void detectLargestObject(Mat img, CascadeClassifier cascade,
Rect largestObject, int scaledWidth) {
int flags = Objdetect.CASCADE_FIND_BIGGEST_OBJECT;
Size minFeatureSize = new Size(10, 10);
float searchScaleFactor = 1.1f; // 表示有多少不同大小的人臉要搜索,設置的大檢測更快,但正確率降低
int minNeighbors = 4; // 使檢測的正確率更高,通常設為3
MatOfRect objects = new MatOfRect();
detectObjectsCustom(img, cascade, objects, scaledWidth, flags, minFeatureSize, searchScaleFactor, minNeighbors);
if (!objects.empty()) {
Rect tmpRect = objects.toList().get(0);
largestObject.x = tmpRect.x;
largestObject.y = tmpRect.y;
largestObject.width = tmpRect.width;
largestObject.height = tmpRect.height;
} else {// 返回一個無效的rect
largestObject.x = -1;
largestObject.y = -1;
largestObject.width = -1;
largestObject.height = -1;
}
}
示例8: detectMany
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/**
*
* @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;
}
示例9: detectLeftEye
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/**
* 檢測左眼
* @param faceImg
* @param leftEyePoint
* @param searchedLeftEye
* @param scaledWidth
*/
protected static void detectLeftEye(Mat faceImg, Rect leftEyeRect, int scaledWidth) {
CascadeClassifier cascade = new CascadeClassifier();
cascade.load(DetectorXMLPath + HAARCASCADE_MCS_LEFTEYE);
// 第一次檢測左眼
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, leftEyeRect, scaledWidth);
}
// 如果沒有檢測到眼睛,使用不同的級聯加載器繼續嘗試
if(leftEyeRect.width <= 0) {
cascade.load(DetectorXMLPath+HAARCASCADE_EYE_TREE_EYEGLASSES);
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, leftEyeRect, scaledWidth);
}
}
if(leftEyeRect.width <= 0) {
cascade.load(DetectorXMLPath+HAARCASCADE_EYE);
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, leftEyeRect, scaledWidth);
}
}
}
示例10: detectRightEye
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
protected static void detectRightEye(Mat faceImg, Rect rightEyeRect, int scaledWidth) {
CascadeClassifier cascade = new CascadeClassifier();
cascade.load(DetectorXMLPath+HAARCASCADE_MCS_RIGHTEYE);
// 第一次檢測右眼
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, rightEyeRect, scaledWidth);
}
if(rightEyeRect.width <= 0) {
cascade.load(DetectorXMLPath+HAARCASCADE_EYE_TREE_EYEGLASSES);
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, rightEyeRect, scaledWidth);
}
}
if(rightEyeRect.width <= 0) {
cascade.load(DetectorXMLPath+HAARCASCADE_EYE);
if(!cascade.empty()) {
DetectObject.detectLargestObject(faceImg, cascade, rightEyeRect, scaledWidth);
}
}
}
示例11: FrcFaceDetector
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/**
* Constructor: Create an instance of the object.
*
* @param instanceName specifies the instance name.
* @param classifierPath specifies the file path for the classifier.
* @param videoIn specifies the video input stream.
* @param videoOut specifies the video output stream.
*/
public FrcFaceDetector(
final String instanceName, final String classifierPath, CvSink videoIn, CvSource videoOut)
{
super(instanceName, videoIn, videoOut, NUM_IMAGE_BUFFERS, detectedFacesBuffers);
if (debugEnabled)
{
dbgTrace = new TrcDbgTrace(moduleName, tracingEnabled, traceLevel, msgLevel);
}
//
// Preallocate two MatOfRects for ping pong processing.
//
for (int i = 0; i < detectedFacesBuffers.length; i++)
{
detectedFacesBuffers[i] = new MatOfRect();
}
faceDetector = new CascadeClassifier(classifierPath);
if (faceDetector.empty())
{
throw new RuntimeException("Failed to load Cascade Classifier <" + classifierPath + ">");
}
}
示例12: main
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
public static void main(String[] args) {
// ָ��������ͼƬ·����������ļ�
String inputImagePath = identificate.class.getClassLoader().getResource("hf.jpg").getPath().substring(1);
String outputImageFile = "identificate.png";
String xmlPath = identificate.class.getClassLoader().getResource("cascade_storage.xml").getPath().substring(1);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier(xmlPath);
Mat image = Highgui.imread(inputImagePath);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
// ���������
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255));
}
// д�뵽�ļ�
Highgui.imwrite(outputImageFile, image);
System.out.print("\nOK!");
}
示例13: faceDetect
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
public static Rect[] faceDetect(Mat image) {
// Create a face detector from the cascade file in the resources
// directory.
CascadeClassifier faceDetector = new CascadeClassifier(OPENCV_HAARCASCADES_HOME
+ "haarcascade_frontalface_default.xml");
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections, 1.2, 4, 0,
new org.opencv.core.Size(10, 10), new org.opencv.core.Size(
1000, 1000));
System.out.println(String.format("Detected %s faces",
faceDetections.toArray().length));
return faceDetections.toArray();
}
示例14: initializeOpenCVDependencies
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/** Loads the Classifier file for face detection and initialises a Classifier Variable. **/
private void initializeOpenCVDependencies() {
try {
// Setting up the cascade classifiers
InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_default);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File mCascadeFile = new File(cascadeDir, "haarcascade_frontalface_default.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
// Load the cascade classifier
cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
} catch (Exception e) {
Log.e("OpenCVActivity", "Error loading cascade", e);
}
setUp();
}
示例15: traitDetection
import org.opencv.objdetect.CascadeClassifier; //導入依賴的package包/類
/**
* Detects the traits and returns the number of detections
* @param cascade Classifier for the Detection
* @param trait Name of the trait to be detected
**/
public int traitDetection(CascadeClassifier cascade, String trait) {
MatOfRect traits = new MatOfRect();
// Classifier used for Trait Detection
if (cascade != null) {
// if(cascade == cascadeClassifier2) cascade.detectMultiScale(image, traits, 1.05, 20, 0, new Size(0, 0), new Size(200, 200)); //if eyes
if(trait.equals("eyes")) cascade.detectMultiScale(image, traits, 1.05, 10, 0, new Size(0, 0), new Size(300, 300));// else if(cascade == cascadeClassifier3) cascade.detectMultiScale(image, traits, 1.04, 80, 0, new Size(0, 0), new Size(300, 300)); //if mouth
else if(trait.equals("mouths")) cascade.detectMultiScale(image, traits, 1.45, 10, 0, new Size(0, 0), new Size(200, 200));
else cascade.detectMultiScale(image, traits, 1.05, 6, 0, new Size(lowerLimit1, lowerLimit2), new Size(upperLimit1, upperLimit2)); //faces
}
Rect[] facesArray =traits.toArray();
for (int i = 0; i <facesArray.length; i++) {
Core.rectangle(origIm, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 2);
}
return facesArray.length;
}