@@ -100,13 +100,8 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.testng</groupId>
|
<groupId>org.testng</groupId>
|
||||||
<artifactId>testng</artifactId>
|
<artifactId>testng</artifactId>
|
||||||
<version>7.6.0</version>
|
<version>7.5</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-nop</artifactId>
|
|
||||||
<version>1.7.25</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,36 +1,20 @@
|
|||||||
package cn.heavenlybook;
|
package cn.heavenlybook;
|
||||||
|
|
||||||
import ai.djl.ModelException;
|
import ai.djl.ModelException;
|
||||||
import ai.djl.inference.Predictor;
|
|
||||||
import ai.djl.modality.Classifications;
|
import ai.djl.modality.Classifications;
|
||||||
import ai.djl.modality.cv.Image;
|
import ai.djl.modality.cv.Image;
|
||||||
import ai.djl.modality.cv.ImageFactory;
|
import ai.djl.modality.cv.ImageFactory;
|
||||||
import ai.djl.modality.cv.output.BoundingBox;
|
|
||||||
import ai.djl.modality.cv.output.DetectedObjects;
|
|
||||||
import ai.djl.modality.cv.output.Rectangle;
|
|
||||||
import ai.djl.repository.zoo.Criteria;
|
|
||||||
import ai.djl.repository.zoo.ModelZoo;
|
|
||||||
import ai.djl.repository.zoo.ZooModel;
|
|
||||||
import ai.djl.translate.TranslateException;
|
import ai.djl.translate.TranslateException;
|
||||||
import cn.heavenlybook.utils.AnimalsClassification;
|
import cn.heavenlybook.utils.AnimalsClassification;
|
||||||
import cn.heavenlybook.utils.FaceDetection;
|
|
||||||
import cn.heavenlybook.utils.OpenCVImageUtil;
|
import cn.heavenlybook.utils.OpenCVImageUtil;
|
||||||
import lombok.extern.java.Log;
|
import lombok.extern.java.Log;
|
||||||
import org.bytedeco.javacv.CanvasFrame;
|
|
||||||
import org.bytedeco.javacv.Frame;
|
import org.bytedeco.javacv.Frame;
|
||||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||||
import org.bytedeco.javacv.OpenCVFrameGrabber;
|
import org.bytedeco.javacv.OpenCVFrameGrabber;
|
||||||
import org.bytedeco.opencv.opencv_core.Mat;
|
import org.bytedeco.opencv.opencv_core.Mat;
|
||||||
import org.bytedeco.opencv.opencv_core.Point;
|
|
||||||
import org.bytedeco.opencv.opencv_core.Rect;
|
|
||||||
import org.bytedeco.opencv.opencv_core.Scalar;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.bytedeco.opencv.global.opencv_imgproc.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ht
|
* @author ht
|
||||||
@@ -38,97 +22,22 @@ import static org.bytedeco.opencv.global.opencv_imgproc.*;
|
|||||||
@Log
|
@Log
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException, ModelException, TranslateException {
|
public static void main(String[] args) throws IOException, ModelException, TranslateException {
|
||||||
float shrink = 0.5f;
|
|
||||||
float threshold = 0.7f;
|
|
||||||
|
|
||||||
Criteria<Image, DetectedObjects> criteria = new FaceDetection().criteria(shrink, threshold);
|
|
||||||
|
|
||||||
// 开启摄像头,获取图像(得到的图像为frame类型,需要转换为mat类型进行检测和识别)
|
// 开启摄像头,获取图像(得到的图像为frame类型,需要转换为mat类型进行检测和识别)
|
||||||
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
|
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
|
||||||
|
|
||||||
grabber.start();
|
grabber.start();
|
||||||
|
|
||||||
// Frame与Mat转换
|
// Frame与Mat转换
|
||||||
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
|
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
|
||||||
// 新建一个预览窗口
|
|
||||||
CanvasFrame canvas = new CanvasFrame("人脸检测");
|
|
||||||
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
canvas.setVisible(true);
|
|
||||||
canvas.setFocusable(true);
|
|
||||||
// 窗口置顶
|
|
||||||
if (canvas.isAlwaysOnTopSupported()) {
|
|
||||||
canvas.setAlwaysOnTop(true);
|
|
||||||
}
|
|
||||||
Frame frame;
|
Frame frame;
|
||||||
|
// 获取图像帧
|
||||||
try (ZooModel<Image, DetectedObjects> model = ModelZoo.loadModel(criteria);
|
while ((frame = grabber.grab()) != null) {
|
||||||
Predictor<Image, DetectedObjects> predictor = model.newPredictor()) {
|
// 将获取的frame转化成mat数据类型
|
||||||
// 获取图像帧
|
Mat img = converter.convert(frame);
|
||||||
while (canvas.isVisible() && (frame = grabber.grab()) != null) {
|
BufferedImage buffImg = OpenCVImageUtil.mat2BufferedImage(img);
|
||||||
|
Image image = ImageFactory.getInstance().fromImage(buffImg);
|
||||||
// 将获取的frame转化成mat数据类型
|
Classifications classifications = AnimalsClassification.predict(image);
|
||||||
Mat img = converter.convert(frame);
|
Classifications.Classification bestItem = classifications.best();
|
||||||
BufferedImage buffImg = OpenCVImageUtil.mat2BufferedImage(img);
|
log.info(bestItem.getClassName() + " : " + bestItem.getProbability());
|
||||||
|
log.info(classifications.toJson());
|
||||||
Image image = ImageFactory.getInstance().fromImage(buffImg);
|
|
||||||
|
|
||||||
// region 动物识别
|
|
||||||
Classifications classifications = AnimalsClassification.predict(image);
|
|
||||||
|
|
||||||
Classifications.Classification bestItem = classifications.best();
|
|
||||||
System.out.println(bestItem.getClassName() + " : " + bestItem.getProbability());
|
|
||||||
// List<Classifications.Classification> items = classifications.items();
|
|
||||||
// List<String> names = new ArrayList<>();
|
|
||||||
// List<Double> probs = new ArrayList<>();
|
|
||||||
// for (int i = 0; i < items.size(); i++) {
|
|
||||||
// Classifications.Classification item = items.get(i);
|
|
||||||
// names.add(item.getClassName());
|
|
||||||
// probs.add(item.getProbability());
|
|
||||||
// }
|
|
||||||
|
|
||||||
log.info(classifications.toJson());
|
|
||||||
// endregion
|
|
||||||
|
|
||||||
int imageWidth = image.getWidth();
|
|
||||||
int imageHeight = image.getHeight();
|
|
||||||
|
|
||||||
DetectedObjects detections = predictor.predict(image);
|
|
||||||
List<DetectedObjects.DetectedObject> items = detections.items();
|
|
||||||
|
|
||||||
// 遍历人脸
|
|
||||||
for (DetectedObjects.DetectedObject item : items) {
|
|
||||||
BoundingBox box = item.getBoundingBox();
|
|
||||||
Rectangle rectangle = box.getBounds();
|
|
||||||
int x = (int) (rectangle.getX() * imageWidth);
|
|
||||||
int y = (int) (rectangle.getY() * imageHeight);
|
|
||||||
Rect face =
|
|
||||||
new Rect(
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
(int) (rectangle.getWidth() * imageWidth),
|
|
||||||
(int) (rectangle.getHeight() * imageHeight));
|
|
||||||
|
|
||||||
// 绘制人脸矩形区域,scalar色彩顺序:BGR(蓝绿红)
|
|
||||||
rectangle(img, face, new Scalar(0, 0, 255, 1));
|
|
||||||
|
|
||||||
int posX = Math.max(face.tl().x() - 10, 0);
|
|
||||||
int posY = Math.max(face.tl().y() - 10, 0);
|
|
||||||
// 在人脸矩形上面绘制文字
|
|
||||||
putText(
|
|
||||||
img,
|
|
||||||
"Face",
|
|
||||||
new Point(posX, posY),
|
|
||||||
FONT_HERSHEY_COMPLEX,
|
|
||||||
1.0,
|
|
||||||
new Scalar(0, 0, 255, 2.0));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 显示视频图像
|
|
||||||
canvas.showImage(frame);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas.dispose();
|
|
||||||
grabber.close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,25 +59,27 @@ public final class AnimalsClassification {
|
|||||||
return new Classifications(names, probs);
|
return new Classifications(names, probs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Criteria<Image, Classifications> criteria;
|
||||||
|
static ZooModel<Image, Classifications> rotateModel;
|
||||||
|
static Predictor<Image, Classifications> classifier;
|
||||||
|
|
||||||
public static Classifications classfier(Image img)
|
public static Classifications classfier(Image img)
|
||||||
throws IOException, ModelException, TranslateException {
|
throws IOException, ModelException, TranslateException {
|
||||||
|
|
||||||
Criteria<Image, Classifications> criteria =
|
criteria =
|
||||||
Criteria.builder()
|
criteria == null
|
||||||
.optEngine("PaddlePaddle")
|
? Criteria.builder()
|
||||||
.setTypes(Image.class, Classifications.class)
|
.optEngine("PaddlePaddle")
|
||||||
.optModelUrls("https://aias-home.oss-cn-beijing.aliyuncs.com/models/animals.zip")
|
.setTypes(Image.class, Classifications.class)
|
||||||
// .optModelUrls("/Users/calvin/model/animals/")
|
.optModelUrls("https://aias-home.oss-cn-beijing.aliyuncs.com/models/animals.zip")
|
||||||
.optModelName("inference")
|
// .optModelUrls("/Users/calvin/model/animals/")
|
||||||
.optTranslator(new AnimalTranslator())
|
.optModelName("inference")
|
||||||
.optProgress(new ProgressBar())
|
.optTranslator(new AnimalTranslator())
|
||||||
.build();
|
.optProgress(new ProgressBar())
|
||||||
|
.build()
|
||||||
try (ZooModel<Image, Classifications> rotateModel = ModelZoo.loadModel(criteria)) {
|
: criteria;
|
||||||
try (Predictor<Image, Classifications> classifier = rotateModel.newPredictor()) {
|
rotateModel = rotateModel == null ? ModelZoo.loadModel(criteria) : rotateModel;
|
||||||
Classifications classifications = classifier.predict(img);
|
classifier = classifier == null ? rotateModel.newPredictor() : classifier;
|
||||||
return classifications;
|
return classifier.predict(img);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ public final class LightAnimalsClassification {
|
|||||||
Classifications.Classification item = items.get(i);
|
Classifications.Classification item = items.get(i);
|
||||||
double prob = item.getProbability();
|
double prob = item.getProbability();
|
||||||
probArr[i] = prob;
|
probArr[i] = prob;
|
||||||
if (prob > max) max = prob;
|
if (prob > max) {
|
||||||
|
max = prob;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < items.size(); i++) {
|
for (int i = 0; i < items.size(); i++) {
|
||||||
@@ -69,7 +71,7 @@ public final class LightAnimalsClassification {
|
|||||||
.optProgress(new ProgressBar())
|
.optProgress(new ProgressBar())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (ZooModel rotateModel = ModelZoo.loadModel(criteria)) {
|
try (ZooModel<Image, Classifications> rotateModel = ModelZoo.loadModel(criteria)) {
|
||||||
try (Predictor<Image, Classifications> classifier = rotateModel.newPredictor()) {
|
try (Predictor<Image, Classifications> classifier = rotateModel.newPredictor()) {
|
||||||
Classifications classifications = classifier.predict(img);
|
Classifications classifications = classifier.predict(img);
|
||||||
return classifications;
|
return classifications;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
package cn.heavenlybook;
|
||||||
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Reference in New Issue
Block a user