package com
.gapf
.supervise
.web
.controller
.v1
;
import com
.alibaba
.fastjson
.JSON
;
import com
.gapf
.common
.config
.GapfConfig
;
import com
.gapf
.management
.utils
.FaceUtils
;
import com
.gapf
.management
.utils
.StringUtil
;
import lombok
.extern
.slf4j
.Slf4j
;
import org
.opencv
.core
.Point
;
import org
.opencv
.core
.*
;
import org
.opencv
.imgcodecs
.Imgcodecs
;
import org
.opencv
.imgproc
.Imgproc
;
import org
.opencv
.objdetect
.CascadeClassifier
;
import org
.opencv
.videoio
.VideoCapture
;
import org
.springframework
.core
.io
.ClassPathResource
;
import org
.springframework
.core
.io
.Resource
;
import org
.springframework
.util
.ResourceUtils
;
import org
.springframework
.web
.bind
.annotation
.GetMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RestController
;
import javax
.imageio
.ImageIO
;
import javax
.swing
.*
;
import java
.awt
.*
;
import java
.awt
.image
.BufferedImage
;
import java
.io
.File
;
import java
.io
.IOException
;
import java
.text
.SimpleDateFormat
;
import java
.util
.Date
;
import java
.util
.HashMap
;
import java
.util
.List
;
import java
.util
.Map
;
@Slf4j
@RestController
@RequestMapping("/{version}")
public class FaceController extends JPanel{
private static final long serialVersionUID
= 1L
;
private static BufferedImage mImg
;
private static SimpleDateFormat sdf
= new SimpleDateFormat("yyyymmddHHmmss");
public static final String FACE_PATH
= "/face/";
private static String format
= "jpg";
static {
System
.loadLibrary(Core
.NATIVE_LIBRARY_NAME
);
}
private static BufferedImage
mat2BI(Mat mat
) {
int dataSize
= mat
.cols() * mat
.rows() * (int) mat
.elemSize();
byte[] data
= new byte[dataSize
];
mat
.get(0, 0, data
);
int type
= mat
.channels() == 1 ?
BufferedImage
.TYPE_BYTE_GRAY
: BufferedImage
.TYPE_3BYTE_BGR
;
if (type
== BufferedImage
.TYPE_3BYTE_BGR
) {
for (int i
= 0; i
< dataSize
; i
+= 3) {
byte blue
= data
[i
+ 0];
data
[i
+ 0] = data
[i
+ 2];
data
[i
+ 2] = blue
;
}
}
BufferedImage image
= new BufferedImage(mat
.cols(), mat
.rows(), type
);
image
.getRaster().setDataElements(0, 0, mat
.cols(), mat
.rows(), data
);
return image
;
}
public void paintComponent(Graphics g
) {
if (mImg
!= null
) {
g
.drawImage(mImg
, 0, 0, mImg
.getWidth(), mImg
.getHeight(), this);
}
}
public static String
detectFace(Mat img
,CascadeClassifier faceDetector
) {
System
.out
.println("Running DetectFace ... ");
Mat greyScaleImg
= new Mat();
Imgproc
.cvtColor(img
, greyScaleImg
, Imgproc
.COLOR_RGB2GRAY
,0);
Mat smallImg
=img
.clone();
Imgproc
.resize(greyScaleImg
,smallImg
,new Size(img
.width()*0.2,img
.height()*0.2));
Mat qualityImg
= new Mat();
Imgproc
.equalizeHist(smallImg
,qualityImg
);
MatOfRect faceDetections
= new MatOfRect();
faceDetector
.detectMultiScale(qualityImg
, faceDetections
);
Rect
[] rects
= faceDetections
.toArray();
if (rects
!= null
&& rects
.length
>= 1) {
String name
= sdf
.format(new Date());
for (Rect rect
: rects
) {
Imgproc
.rectangle(img
, new Point(rect
.x
, rect
.y
), new Point(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
),
new Scalar(0, 0, 255), 2);
}
String filePath
= GapfConfig
.getUploadPath()+FACE_PATH
+ File
.separator
+ name
+ "." + format
;
File f
= new File(filePath
);
BufferedImage bufferedImage
= mat2BI(img
);
try {
ImageIO
.write(bufferedImage
, format
, f
);
} catch (IOException e
) {
e
.printStackTrace();
}
return filePath
;
} else {
return null
;
}
}
private static void save(Mat img
, Rect rect
, String outFile
){
Mat sub
= img
.submat(rect
);
Mat mat
= new Mat();
Size size
= new Size(300, 300);
Imgproc
.resize(sub
, mat
, size
);
Imgcodecs
.imwrite(outFile
, mat
);
}
@GetMapping("/face/search")
public void searchFace(String rtsp
) {
VideoCapture capture
= new VideoCapture();
try {
capture
.open(rtsp
);
if (!capture
.isOpened()) {
throw new Exception("camera not found!");
}
FaceController panel
= new FaceController();
Mat capImg
= new Mat();
while (capture
.isOpened()) {
capture
.read(capImg
);
String altPath
= ResourceUtils
.getFile("classpath:static/haarcascade_frontalface_alt.xml").getPath();
Resource resource
= new ClassPathResource(altPath
);
String path
= resource
.getFile().getPath();
CascadeClassifier faceDetector
= new CascadeClassifier(path
);
String imageUrl
= detectFace(capImg
,faceDetector
);
if(imageUrl
!=null
){
String file
= FaceUtils
.uploadFile(imageUrl
);
String mapStr
= FaceUtils
.searchFace(file
);
float score
= 0f;
if(!StringUtil
.isBlank(mapStr
)){
HashMap hashMap
= JSON
.parseObject(mapStr
, HashMap
.class);
Map
<String, Map> bodyMap
= (Map
<String, Map>) hashMap
.get("body");
Map
<String, Map> dataMap
= (Map
<String, Map>) hashMap
.get("data");
List
<Map
<String, Map>> listMap
= (List
<Map
<String, Map>>) dataMap
.get("matchList");
List
<Map
<String, Object>> facelistMap
= (List
<Map
<String, Object>>) listMap
.get(0).get("faceItems");
Map
<String, Object> resultMap
= (Map
<String, Object>) facelistMap
.get(0);
score
= Float
.valueOf(resultMap
.get("score").toString());
if(score
>0.6){
System
.out
.println("-----------------"+resultMap
.get("entityId"));
break;
}
}
}
panel
.repaint();
}
capture
.release();
} catch (Exception e
) {
e
.printStackTrace();
} finally {
System
.out
.println("--done--");
capture
.release();
}
}
@GetMapping("/face/search1")
public void searchFace1(String rtsp
) throws IOException
{
String path
= ResourceUtils
.getFile("classpath:static/haarcascade_frontalface_alt.xml").getPath();
System
.out
.println(path
);
}
public static void main(String
[] args
) throws IOException
{
}
}