|
@@ -1,28 +1,22 @@
|
|
package cn.com.qmth.am.service.impl;
|
|
package cn.com.qmth.am.service.impl;
|
|
|
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
|
-
|
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import com.qmth.boot.core.ai.client.OcrApiClient;
|
|
|
|
-import com.qmth.boot.core.ai.model.ocr.OcrType;
|
|
|
|
-import com.qmth.boot.core.exception.StatusException;
|
|
|
|
-import com.qmth.boot.core.retrofit.exception.RetrofitResponseError;
|
|
|
|
-import com.qmth.boot.core.retrofit.utils.SignatureInfo;
|
|
|
|
-import com.qmth.boot.core.retrofit.utils.UploadFile;
|
|
|
|
import com.qmth.boot.core.solar.model.OrgInfo;
|
|
import com.qmth.boot.core.solar.model.OrgInfo;
|
|
import com.qmth.boot.core.solar.service.SolarService;
|
|
import com.qmth.boot.core.solar.service.SolarService;
|
|
|
|
|
|
-import cn.com.qmth.am.bean.StudentScoreImageDto;
|
|
|
|
|
|
+import cn.com.qmth.am.bean.OcrDto;
|
|
|
|
+import cn.com.qmth.am.multithread.producer.OcrProducer;
|
|
import cn.com.qmth.am.service.OcrService;
|
|
import cn.com.qmth.am.service.OcrService;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@@ -30,86 +24,41 @@ public class OcrServiceImpl implements OcrService {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(OcrService.class);
|
|
private static final Logger log = LoggerFactory.getLogger(OcrService.class);
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private OcrApiClient ocrApiClient;
|
|
|
|
-
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private SolarService solarService;
|
|
private SolarService solarService;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void ocr() {
|
|
public void ocr() {
|
|
- File dir = new File("d:/ocr");
|
|
|
|
|
|
+ log.warn("OcrService ocr start*************");
|
|
|
|
+ File dir = new File("d:/ocr/data");
|
|
OrgInfo org = solarService.getOrgList().get(0);
|
|
OrgInfo org = solarService.getOrgList().get(0);
|
|
- disposeFile(dir, org);
|
|
|
|
|
|
+ List<OcrDto> files = new ArrayList<>();
|
|
|
|
+ disposeFile(files, dir, org);
|
|
|
|
+ OcrProducer producer = new OcrProducer();
|
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
|
+ param.put("files", files);
|
|
|
|
+ producer.startDispose(4, param, files.size());
|
|
|
|
+ while (true) {
|
|
|
|
+ List<OcrDto> failed = producer.getFaildDto();
|
|
|
|
+ if (CollectionUtils.isEmpty(failed)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ param = new HashMap<>();
|
|
|
|
+ param.put("files", failed);
|
|
|
|
+ producer = new OcrProducer();
|
|
|
|
+ producer.startDispose(4, param, failed.size());
|
|
|
|
+ }
|
|
log.warn("OcrService ocr finish*************");
|
|
log.warn("OcrService ocr finish*************");
|
|
}
|
|
}
|
|
|
|
|
|
- private void disposeFile(File file, OrgInfo org) {
|
|
|
|
|
|
+ private void disposeFile(List<OcrDto> files, File file, OrgInfo org) {
|
|
if (file.isFile() && file.getName().toLowerCase().endsWith(".jpg")) {
|
|
if (file.isFile() && file.getName().toLowerCase().endsWith(".jpg")) {
|
|
- StudentScoreImageDto dto = new StudentScoreImageDto();
|
|
|
|
- dto.setImage(fileToByte(file));
|
|
|
|
- String content = ocrDispose(dto, org);
|
|
|
|
- String name = file.getName().substring(0, file.getName().lastIndexOf("."));
|
|
|
|
- File txt = new File(file.getParentFile().getAbsolutePath() + "/" + name + ".txt");
|
|
|
|
- if (txt.exists()) {
|
|
|
|
- txt.delete();
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- FileUtils.write(txt, content, "utf-8");
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
|
|
+ files.add(new OcrDto(file, org));
|
|
} else {
|
|
} else {
|
|
if (file.isDirectory()) {
|
|
if (file.isDirectory()) {
|
|
for (File subFile : file.listFiles()) {
|
|
for (File subFile : file.listFiles()) {
|
|
- disposeFile(subFile, org);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private byte[] fileToByte(File img) {
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
- try {
|
|
|
|
- BufferedImage bi;
|
|
|
|
- bi = ImageIO.read(img);
|
|
|
|
- ImageIO.write(bi, "jpg", baos);
|
|
|
|
- byte[] bytes = baos.toByteArray();
|
|
|
|
- return bytes;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- } finally {
|
|
|
|
- try {
|
|
|
|
- baos.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private String ocrDispose(StudentScoreImageDto dto, OrgInfo org) {
|
|
|
|
- SignatureInfo signature = SignatureInfo.secret(org.getAccessKey(), org.getAccessSecret());
|
|
|
|
- try {
|
|
|
|
- return ocrApiClient.forImage(signature, OcrType.HANDWRITING, UploadFile.build("image", "", dto.getImage()));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("ocr异常", e);
|
|
|
|
- if (e instanceof RetrofitResponseError) {
|
|
|
|
- RetrofitResponseError tem = (RetrofitResponseError) e;
|
|
|
|
- if (tem.getCode() == 503) {
|
|
|
|
- if (dto.getRetry() <= 3) {
|
|
|
|
- try {
|
|
|
|
- Thread.sleep(3000);
|
|
|
|
- } catch (InterruptedException e1) {
|
|
|
|
- }
|
|
|
|
- dto.setRetry(dto.getRetry() + 1);
|
|
|
|
- return ocrDispose(dto, org);
|
|
|
|
- } else {
|
|
|
|
- throw new StatusException("重试次数过多");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- throw e;
|
|
|
|
|
|
+ disposeFile(files, subFile, org);
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- throw e;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|