欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

调用算法,监听算法结束

程序员文章站 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;
            }
        }
    }
}