-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathOpencvDetect.java
More file actions
123 lines (91 loc) · 3.39 KB
/
OpencvDetect.java
File metadata and controls
123 lines (91 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.github.quadflask.smartcrop;
import lombok.extern.slf4j.Slf4j;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class OpencvDetect {
private static OpencvDetect instance = new OpencvDetect();
private static CascadeClassifier cascadeClassifier;
private static String frontalfacePath;
static {
try {
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
System.loadLibrary("lib" + Core.NATIVE_LIBRARY_NAME);
} else if (osName.startsWith("Mac")) {
System.load("/usr/local/lib/lib" + Core.NATIVE_LIBRARY_NAME + ".dylib");
} else if (osName.startsWith("Linux")) {
System.load("/usr/local/lib/lib" + Core.NATIVE_LIBRARY_NAME + ".so");
}
} catch (Throwable t) {
log.error("Load OpenCV lib error.", t);
}
}
public static OpencvDetect getInstance() {
return instance;
}
public Crop[] detectFace(String imagePath) {
Mat image = Imgcodecs.imread(imagePath);
MatOfRect faceDetections = new MatOfRect();
cascadeClassifier.detectMultiScale(image, faceDetections);
Rect[] rects = faceDetections.toArray();
return toCrop(rects);
}
public Crop[] detectFace(byte[] imageBase64) {
try {
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imageBase64));
return detectFace(bi);
} catch (Exception e) {
log.error("dateface error.", e);
}
return null;
}
public Crop[] detectFace(BufferedImage bi) {
try {
Mat image = img2Mat(bi);
MatOfRect faceDetections = new MatOfRect();
cascadeClassifier.detectMultiScale(image, faceDetections);
Rect[] rects = faceDetections.toArray();
return toCrop(rects);
} catch (Throwable t) {
}
return null;
}
/**
* the file is '~/opencvXX/haarcascades/haarcascade_frontalface_alt.xml"'
*
* @param frontalfacePath
*/
public void SetFrontalFacePath(String frontalfacePath) {
this.frontalfacePath = frontalfacePath;
cascadeClassifier = new CascadeClassifier(frontalfacePath);
}
private Crop[] toCrop(Rect[] rect) {
List<Crop> cropList = new ArrayList<>();
if (rect != null) {
Arrays.stream(rect).forEach(r -> cropList.add(new Crop(r.x, r.y, r.width, r.height)));
}
Crop[] crops = new Crop[cropList.size()];
cropList.toArray(crops);
return crops;
}
private Mat img2Mat(BufferedImage im) {
// Convert INT to BYTE
//im = new BufferedImage(im.getWidth(), im.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
// Convert bufferedimage to byte array
byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer()).getData();
// Create a Matrix the same size of image
Mat image = new Mat(im.getHeight(), im.getWidth(), CvType.CV_8UC3);
// Fill Matrix with image values
image.put(0, 0, pixels);
return image;
}
}