调用算法,监听算法结束
程序员文章站
2022-04-27 17:53:54
...
public class BlockQueueService { private static final Logger logger = LoggerFactory.getLogger(BlockQueueService.class); private static final long MILLILS = 120000; private BlockingQueue<String> receivedImg = new LinkedBlockingQueue<>(); /** * 添加解读任务 * @throws InterruptedException */ public void addUnscrambleTask(String img) throws InterruptedException { receivedImg.put(img); } /** * 启动线程 */ public BlockQueueService() { execAlgorithmThread.start(); } /** * 执行算法的线程 */ Thread execAlgorithmThread = new Thread() { @Override public void run() { while (true) { try { String sheetAnalysisInfos = receivedImg.take(); execAlgorithm(sheetAnalysisInfos); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * 执行算法 * @param */ private void execAlgorithm(String receivedImg) { try { //执行算法解读 doPost(receivedImg); }catch (Exception e){ logger.error("execAlgorithm ocr unscramble Exception" , e); } } }; public void doPost(String filePath){ String xmlDir = filePath.replace(jpgDir,""); xmlDir =rootDir + correctDir +xmlDir.substring(0,xmlDir.lastIndexOf("."))+".xml"; logger.info("filePath的路径 "+ filePath); logger.info("xmlDir的路径" + xmlDir); HashMap map = new HashMap(); map.put("imgPath",filePath); map.put("xmlPath",xmlDir); String url = "http://############/execAlgorithm"; int result = 0; try { result = HttpClient.doPost(url,map); logger.info("------- result ---------- 是否为1"); } catch (Exception e) { e.printStackTrace(); } logger.info("-------execAlgorithm----------判断解析后是否生成对应的XML文件"); if (result==1){ logger.info("解析生成对应的XML文件"); } File file = new File(xmlDir); if (!file.exists()){ logger.info("AI解析未生成对应的XML文件"); writeXml(xmlDir); logger.info("生成对应的错误XML文件"+xmlDir); } logger.info("-------execAlgorithm----------释放资源"); } public void doCmd(String filePath){ String pythonDir = "bone_age_interface.py"; String pythonpath = "/boneage_data/BoneAge/"; String xmlDir = filePath.replace(jpgDir,""); xmlDir =rootDir + correctDir +xmlDir.substring(0,xmlDir.lastIndexOf("."))+".xml"; String cmd = "python" + " " + pythonDir + " " + filePath + " " + xmlDir; logger.info(cmd + "命令"); Runtime run = Runtime.getRuntime(); Process proc = null; try { proc = run.exec("/bin/bash"); } catch (IOException e) { e.printStackTrace(); } if (proc != null) { PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); printWriter.println("cd " + pythonpath); printWriter.println(cmd); printWriter.println("exit"); WorkerThread worker = null; try { worker = new WorkerThread(proc); worker.start(); worker.join(MILLILS); if (worker.exit != null) { logger.info("-------execAlgorithm----------解析完成"); } else { throw new TimeoutException(); } } catch (InterruptedException ex) { worker.interrupt(); Thread.currentThread().interrupt(); ex.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { logger.info("-------execAlgorithm----------判断解析后是否生成对应的XML文件"); File file = new File(xmlDir); boolean result = file.exists(); if (result) { logger.info("解析生成对应的XML文件"); } else { logger.info("解析未生成对应的XML文件"); writeXml(xmlDir); logger.info("生成对应的错误XML文件"+xmlDir); } logger.info("-------execAlgorithm----------释放资源"); printWriter.close(); proc.destroy(); } } } private class WorkerThread extends Thread { private Process process; private Integer exit; private WorkerThread(Process process) { this.process = process; } public void run() { try { logger.info("exit初始化" + exit); exit = process.waitFor(); logger.info("exit在运行完之后" + exit); } catch (InterruptedException ignore) { return; } } } }
上一篇: 咸丰帝资质平平,他为什么能当上皇帝?
下一篇: 兼容所有主流浏览器的 渐变色背景
推荐阅读
-
php 3行代码的分页算法(求起始页和结束页)
-
动画 | 大学四年结束之前必须透彻的排序算法
-
计算机图形学-2.1用中点画线扫描转换算法,绘制任意斜率 的直线。可以通过调用此函数绘制图案(图案中包含各 种斜率)
-
sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用
-
密码监听器注册算法分析
-
php 3行代码的分页算法(求起始页和结束页)
-
动画 | 大学四年结束之前必须透彻的排序算法
-
遨游起始页 php 3行代码的分页算法求起始页和结束页
-
算法设计与分析——全排列问题算法分析(递归调用分析图)
-
3行代码的分页算法(求起始页和结束页)