Browse Source

角果识别后台

master
zhangyeguang 2 weeks ago
parent
commit
a720652ebc
  1. 6
      pom.xml
  2. 5
      src/main/java/com/jiagutech/common/exception/BizCode.java
  3. 2
      src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java
  4. 8
      src/main/java/com/jiagutech/controller/RecordController.java
  5. 2
      src/main/java/com/jiagutech/entity/UserEntity.java
  6. 4
      src/main/java/com/jiagutech/record/RecordTaskManager.java
  7. 31
      src/main/java/com/jiagutech/record/RecordTaskProcessor.java
  8. 61
      src/main/java/com/jiagutech/service/RecordServiceImpl.java
  9. 1
      src/main/java/com/jiagutech/service/UserServiceImpl.java
  10. 25
      src/main/java/com/jiagutech/utils/RecognitionUtil.java

6
pom.xml

@ -84,6 +84,12 @@
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>

5
src/main/java/com/jiagutech/common/exception/BizCode.java

@ -24,6 +24,10 @@ public enum BizCode {
FILE_UPLOAD_ERROR(90002, "文件上传失败"),
/**
* 服务器繁忙
*/
SERVER_BUSY(500, "服务器繁忙,请稍后再试"),
USER_NOT_FOUND(40004, "用户不存在"),
@ -48,7 +52,6 @@ public enum BizCode {
USER_PHONE_EXIST(40012, "用户手机号已存在"),
;

2
src/main/java/com/jiagutech/common/exception/GlobalExceptionHandler.java

@ -45,7 +45,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public ResponseEntity<R> handleBusinessException(BusinessException ex) {
return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(R.fail(ex.getCode(), ex.getMessage()), HttpStatus.OK);
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)

8
src/main/java/com/jiagutech/controller/RecordController.java

@ -67,8 +67,9 @@ public class RecordController {
@ApiOperation(value = "更新识别记录内容")
@PutMapping("/updateRecordContent")
public void updateRecord(@RequestBody RecordContentEntity recordContentEntity) {
public R<Void> updateRecord(@RequestBody RecordContentEntity recordContentEntity) {
recordService.updateRecordContent(recordContentEntity);
return R.ok("操作成功");
}
@ -80,13 +81,14 @@ public class RecordController {
@ApiOperation(value = "比例尺设置")
@PutMapping("/scale")
public void updateScale(@RequestBody UpdateScaleRequest request) {
public R<Void> updateScale(@RequestBody UpdateScaleRequest request) {
recordService.updateScale(request);
return R.ok("操作成功");
}
@ApiOperation(value = "删除记录")
@DeleteMapping(value = "/delete/{recordId}")
public R<Void> deleteRecord(Long recordId) {
public R<Void> deleteRecord(@PathVariable Long recordId) {
boolean res = recordService.deleteRecord(recordId);
return res ? R.ok() : R.fail();
}

2
src/main/java/com/jiagutech/entity/UserEntity.java

@ -24,7 +24,7 @@ public class UserEntity {
private int status;
private int role;
private Integer role;
}

4
src/main/java/com/jiagutech/record/RecordTaskManager.java

@ -38,4 +38,8 @@ public class RecordTaskManager {
return taskItems;
}
public static int taskCount() {
return recordTasks.size();
}
}

31
src/main/java/com/jiagutech/record/RecordTaskProcessor.java

@ -7,6 +7,7 @@ import com.jiagutech.entity.RecordEntity;
import com.jiagutech.mapper.RecordContentMapper;
import com.jiagutech.mapper.RecordMapper;
import com.jiagutech.utils.HuaweiObs;
import com.jiagutech.utils.RecognitionUtil;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -15,8 +16,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
@ -50,7 +49,7 @@ public class RecordTaskProcessor {
int rapeNum = 0;
int beakNum = 0;
for (ModelPredictResponse item : predict) {
item.setLength(getMaxLength(item.getPoints()));
item.setLength(RecognitionUtil.getMaxLength(item.getPoints()));
switch (item.getClassify()) {
case 0:
rapeNum++;
@ -64,12 +63,12 @@ public class RecordTaskProcessor {
}
progress += (float) (100 / recordTask.getFiles().length - 10);
recordTask.setProgress(progress);
if (!CollectionUtils.isEmpty(predict)) {
// if (!CollectionUtils.isEmpty(predict)) {
String imageUrl;
try {
log.info("存储图片到oss,file:{}", file.getName());
// imageUrl = huaweiObs.uploadFile(file);
imageUrl = "";
imageUrl = huaweiObs.uploadFile(file);
// imageUrl = "";
progress += 2f;
} catch (Exception e) {
throw new RuntimeException(e);
@ -88,29 +87,11 @@ public class RecordTaskProcessor {
recordContentMapper.insert(recordContentEntity);
log.info("保存处理结果,file:{}", file.getName());
progress += 3f;
}
}
// }
}
private static double calculateDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
private float getMaxLength(int[][] points) {
// 计算四条边的长度
double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB
double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC
double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD
double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA
double max = Math.max(Math.max(ab, bc), Math.max(cd, da));
BigDecimal b = new BigDecimal(max);
// 找出最大边长
return b.setScale(2, RoundingMode.HALF_UP).floatValue();
}
}

61
src/main/java/com/jiagutech/service/RecordServiceImpl.java

@ -1,6 +1,7 @@
package com.jiagutech.service;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.MapUtils;
@ -16,6 +17,8 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiagutech.client.RecognitionModelClient;
import com.jiagutech.common.exception.BizCode;
import com.jiagutech.common.exception.BusinessException;
import com.jiagutech.dto.*;
import com.jiagutech.dto.common.LoginUser;
import com.jiagutech.dto.common.PageRequest;
@ -35,6 +38,8 @@ import com.jiagutech.record.RecordTaskHandler;
import com.jiagutech.record.RecordTaskManager;
import com.jiagutech.record.RecordTaskProcessor;
import com.jiagutech.utils.LoginUtil;
import com.jiagutech.utils.RecognitionUtil;
import jdk.nashorn.internal.ir.IfNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
@ -76,8 +81,17 @@ public class RecordServiceImpl implements RecordService {
return null;
}
/**
* 同时处理的任务数小于等于8个
*
* @param request
* @return
*/
@Override
public String createRecordAsync(CreateRecordRequest request) {
if (RecordTaskManager.taskCount() >= 9) {
throw new BusinessException(BizCode.SERVER_BUSY);
}
String taskId = UUID.randomUUID().toString();
RecordTaskHandler recordTaskHandler = new RecordTaskHandler(customThreadPool, taskProcessor, tmpPath);
recordTaskHandler.createTask(request, taskId);
@ -142,8 +156,13 @@ public class RecordServiceImpl implements RecordService {
List<ModelPredictResponse> recognitionData = record.getRecognitionData();
Scale scaleDto = record.getScale();
//把识别数据进行分类
float size = scaleDto.getSize();
int scale = scaleDto.getScale();
float size = 0;
int scale = 0;
if (scaleDto != null) {
size = scaleDto.getSize();
scale = scaleDto.getScale();
}
for (ModelPredictResponse item : recognitionData) {
float len;
if (size > 0 && scale > 0) {
@ -191,12 +210,14 @@ public class RecordServiceImpl implements RecordService {
}
//创建单元格的合并策略, 前7列数据需要合并
for (int i = 0; i <= 6; i++) {
if (endRowIndex > startRowIndex) {
OnceAbsoluteMergeStrategy mergeStrategy = new OnceAbsoluteMergeStrategy(
startRowIndex,
endRowIndex,
i, i);
mergeStrategies.add(mergeStrategy);
}
}
startRowIndex += len;
avgHandleLen.add(handleLenList.stream().mapToDouble(Float::doubleValue).average().orElse(0.0));
@ -294,6 +315,30 @@ public class RecordServiceImpl implements RecordService {
@Override
public void updateRecordContent(RecordContentEntity recordContentEntity) {
if (CollectionUtil.isNotEmpty(recordContentEntity.getRecognitionData())) {
int handleNum = 0;
int rapeNum = 0;
int beakNum = 0;
for (ModelPredictResponse item : recordContentEntity.getRecognitionData()) {
if (item.getLength() == null || item.getLength() == 0) {
item.setLength(RecognitionUtil.getMaxLength(item.getPoints()));
}
switch (item.getClassify()) {
case 0:
rapeNum++;
break;
case 1:
beakNum++;
break;
default:
handleNum++;
}
}
recordContentEntity.setHandleNum(handleNum)
.setBeakNum(beakNum)
.setSiliquaNum(rapeNum);
}
recordContentMapper.updateById(recordContentEntity);
}
@ -314,9 +359,10 @@ public class RecordServiceImpl implements RecordService {
own.setScale(request.getScale());
own.setScaleType(request.getScaleType());
own.setId(request.getRecordContentId());
other = new RecordContentEntity().setScale(request.getScale());
other = new RecordContentEntity().setScaleType(3).setScale(request.getScale());
queryWrapper.eq("record_id", request.getRecordId());
queryWrapper.eq("scale_type", 3);
queryWrapper.in("scale_type", 0, 1, 3);
queryWrapper.ne("id", request.getRecordContentId());
break;
case 2:
own = recordContentMapper.selectById(request.getRecordContentId());
@ -330,14 +376,16 @@ public class RecordServiceImpl implements RecordService {
break;
case 3:
default:
own = new RecordContentEntity().setScaleType(3);
queryWrapper.eq("record_id", request.getRecordId());
queryWrapper.eq("scale_type", 1);
RecordContentEntity recordContentEntity = recordContentMapper.selectOne(queryWrapper);
if (recordContentEntity != null) {
own = new RecordContentEntity().setScaleType(3);
own.setScale(recordContentEntity.getScale());
}
own.setId(request.getRecordContentId());
} else {
throw new BusinessException(BizCode.General_Failure, "当前无统一的比例尺");
}
}
recordContentMapper.updateById(own);
@ -380,4 +428,5 @@ public class RecordServiceImpl implements RecordService {
}
}

1
src/main/java/com/jiagutech/service/UserServiceImpl.java

@ -61,6 +61,7 @@ public class UserServiceImpl implements UserService {
.setRealName(user.getRealName())
.setUnitName(user.getUnitName())
.setPhone(user.getPhone())
.setRole(0)
.setPassword(BCrypt.hashpw(user.getPassword()));
userMapper.insert(userEntity);
emailUtil.sendEmail(null, user.getPhone(), user.getUnitName(), user.getRealName());

25
src/main/java/com/jiagutech/utils/RecognitionUtil.java

@ -0,0 +1,25 @@
package com.jiagutech.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RecognitionUtil {
private static double calculateDistance(int x1, int y1, int x2, int y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
public static float getMaxLength(int[][] points) {
// 计算四条边的长度
double ab = calculateDistance(points[0][0], points[0][1], points[1][0], points[1][1]); // AB
double bc = calculateDistance(points[1][0], points[1][1], points[2][0], points[2][1]); // BC
double cd = calculateDistance(points[2][0], points[2][1], points[3][0], points[3][1]); // CD
double da = calculateDistance(points[3][0], points[3][1], points[0][0], points[0][1]); // DA
double max = Math.max(Math.max(ab, bc), Math.max(cd, da));
BigDecimal b = new BigDecimal(max);
// 找出最大边长
return b.setScale(2, RoundingMode.HALF_UP).floatValue();
}
}
Loading…
Cancel
Save