package com.xfjf.framework.test;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mytest.cvcuda;
import com.mytest.imgprecess;
import com.mytest.test;
import com.xfjf.framework.dto.ai.MatYoloForwardPoint;
import com.xfjf.framework.entity.dto.ArAigorithmModelDTO;
import com.xfjf.framework.util.ONNXModelInferenceUtil;
import com.xfjf.framework.util.StringUtils;
import com.xfjf.framework.util.http.HttpUtils;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.*;
import java.lang.reflect.Type;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SocketServer20230903 {
private final static String SOAP_BEGIN = "<SOAP-ENV:Envelope";
private final static String SOAP_END = "</SOAP-ENV:Envelope>";
public static List<cvcuda> cvcudaList = new ArrayList<>();
private static imgprecess instance;
public static synchronized imgprecess getInstance() {
if (instance == null) {
instance = new imgprecess();
}
return instance;
}
public static synchronized List<cvcuda> getCvcudaListInstance() {
if (StringUtils.isEmpty(cvcudaList) ) {
obtainModel();
}
return cvcudaList;
}
public static List<ArAigorithmModelDTO> arAigorithmModelDTOS = new ArrayList<>();
public static List<ArAigorithmModelDTO> obtainModel() {
System.out.println("初始化模型数据");
cvcudaList.clear();
Map<String, Object> params = new HashMap<String, Object>();//请求参数
try {
Gson gson = new Gson();
String url = "http://127.0.0.1:8080/system/aigorithmmodel/queryArAigorithmModelAndClass";
String json= HttpUtils.sendRequst(url, params,"GET");
Type listType = new TypeToken<List<ArAigorithmModelDTO>>() {}.getType();
arAigorithmModelDTOS = gson.fromJson(json, listType);
for(ArAigorithmModelDTO aigorithmModelDTO : arAigorithmModelDTOS){
cvcuda yolo_cuda = new cvcuda();
int use_gpu = 1;
int w = 1280;
int h = 736;
int num_class = aigorithmModelDTO.getModelClassDTOList().size();
float confidence = 0.5f;
yolo_cuda.loadModel(aigorithmModelDTO.getFilePath(),use_gpu,confidence,w,h,num_class);
cvcudaList.add(yolo_cuda);
}
return arAigorithmModelDTOS;
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<>();
}
private ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
SocketServer20230903 socketServer = new SocketServer20230903();
getCvcudaListInstance();
getInstance();
socketServer.start();
}
public SocketServer20230903() throws IOException {
serverSocket = new ServerSocket(18889);
}
public void start() throws IOException {
while (true) {
try {
Socket socket = serverSocket.accept();
System.out.println("dddddddddddddd");
new SocketThread(socket).start();
} catch (IOException e) {
}
}
}
class SocketThread extends Thread {
private MatDisplay matDisplay;
private final Socket socket;
private final OutputStream out;
private final InputStream inputStream;
private final AtomicInteger frameCount;
private AtomicInteger countBytesRead ;
private final ByteArrayOutputStream byteArrayOutputStream;
public SocketThread(Socket socket) throws IOException {
this.socket = socket;
this.frameCount = new AtomicInteger(0);
this.out = socket.getOutputStream();
this.inputStream = socket.getInputStream();
this.countBytesRead = new AtomicInteger(0);
this.byteArrayOutputStream = new ByteArrayOutputStream();
matDisplay = new MatDisplay();
}
public void run() {
try {
Integer bufferSize = 0 ;
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
String receivedData = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
// System.out.println(receivedData);
if (receivedData.indexOf(SOAP_BEGIN) != -1 && receivedData.indexOf(SOAP_END) != -1) {
Pattern pattern = Pattern.compile("<SOAP-ENV:Envelope>(\\d+)</SOAP-ENV:Envelope>");
Matcher matcher = pattern.matcher(receivedData);
while (matcher.find()) {
bufferSize = Integer.parseInt(matcher.group(1));
countBytesRead.set(0);
}
continue;
}
synchronized (this) {
byteArrayOutputStream.write( buffer, 0, bytesRead);
countBytesRead.addAndGet(bytesRead);
if(countBytesRead.get() == bufferSize){
Mat mat = ONNXModelInferenceUtil.identification_(byteArrayOutputStream.toByteArray());
if (mat != null && !mat.empty()) {
// String filename = "d://a/received_image"+ frameCount +".jpg";
// Imgcodecs.imwrite(filename, mat);
matDisplay.displayMat(mat);
try{
long startTime = System.currentTimeMillis();
MatYoloForwardPoint matYoloForwardPoint = test.recognitionUserGpu(mat,cvcudaList
,arAigorithmModelDTOS,instance);
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - startTime;
System.out.println("推理耗时"+elapsedTime);
Gson gson = new Gson();
String json = gson.toJson(matYoloForwardPoint);
synchronized (out) {
out.write(json.getBytes());
// out.write("2".getBytes());
}
}catch (Exception e){
e.getStackTrace();
}
}
byteArrayOutputStream.reset();
frameCount.incrementAndGet();
countBytesRead.set(0);
}
}
}
} catch (Exception e) {
e.getStackTrace();
System.out.println(e.getMessage());
} finally {
synchronized (socket) {
if (socket != null && !socket.isClosed()) {
try {
MatYoloForwardPoint matYoloForwardPoint = new MatYoloForwardPoint();
Gson gson = new Gson();
String json = gson.toJson(matYoloForwardPoint);
synchronized (out) {
out.write(json.getBytes());
}
socket.close();
} catch (IOException e) {
e.getStackTrace();
}
}
}
}
}
}
private String queryThreadId() {
return Long.toString(Thread.currentThread().getId());
}
}
代码里long startTime = System.currentTimeMillis();
MatYoloForwardPoint matYoloForwardPoint = test.recognitionUserGpu(mat,cvcudaList
,arAigorithmModelDTOS,instance);
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - startTime;
System.out.println("推理耗时"+elapsedTime); 这段很慢,有优化的办法吗
评论区