java调用python函数没有返回数据_Java 执行 Python 脚本,脚本测试没问题,集成项目中,获取不到数据?...

描述你的问题

现在是有个需求就是针对上传的人脸图片去做人脸检测,人脸检测功能是通过java这边去调用python程序,看到普遍有两方式,第一种是:通过Jython.jar提供的类库实现 ,第二种方式是:通过Runtime.getRuntime()开启进程来执行脚本文件,现在采用的是第二种方式,测试的时候没问题,将其执行到项目中,通过 http 接口调用测试,立马返回数据,且为空的。

测试通过

服务器上,通过java测试单个class文件,正常返回数据(7s的时间,由于服务器性能比较差),集成在项目中,通过post 测试,立马返回数据,且数据为空,后面尝试设置当前线程 sleep 10s,还是空数据

root@smartconstruction:/zzt/face-detect# java PyExecuteCmd

main stringbuilder [18.621605661813042, 1]

public class PyExecuteCmd {

private static final String cmd = "python3 /zzt/face-detect/picdim.py ";

public static void main(String[] args) {

File file = new File("/zzt/face-detect/156654987537916313.jpg");

String[] ret = execute(file, 12312412L);

System.out.println(Thread.currentThread().getName() + " stringbuilder " + Arrays.toString(ret));

}

/**

* Get PictureQuality HumanNum for example 12.944183177157162 1

*

* @param file

* @param fileNameCrc

* @return 12.944183177157162 1

*/

public static String[] execute(File file, Long fileNameCrc) {

if (Objects.isNull(file)) {

return null;

}

String absolutePath = file.getAbsolutePath();

String command = cmd + absolutePath;

Process proc;

try {

proc = Runtime.getRuntime().exec(command);

BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));

String line = null;

StringBuilder stringBuilder = new StringBuilder();

while ((line = in.readLine()) != null) {

stringBuilder.append(line).append(" ");

}

in.close();

proc.destroy();

String[] resultArr = stringBuilder.toString().split(" ");

return resultArr;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue项目调用Node.js的child_process模块来运行Python脚本,您可以按照以下步骤进行操作: 1. 在Vue项目的根目录下,创建一个新的JavaScript文件(例如:utils.js)用于编写调用child_process模块的代码。 2. 在utils.js文件引入child_process模块: ```javascript const { exec } = require('child_process'); ``` 3. 编写一个函数,用于执行Python脚本,例如: ```javascript function runPythonScript(scriptPath, args) { return new Promise((resolve, reject) => { const pythonProcess = exec(`python ${scriptPath} ${args}`, (error, stdout, stderr) => { if (error) { reject(error); } else { resolve(stdout); } }); }); } ``` 4. 在需要调用child_process模块的组件,引入utils.js文件: ```javascript import { runPythonScript } from './path/to/utils.js'; ``` 5. 在组件的方法调用runPythonScript函数执行Python脚本,例如: ```javascript methods: { async executePythonScript() { try { const result = await runPythonScript('path/to/python/script.py', 'arguments'); console.log(result); } catch (error) { console.error(error); } } } ``` 6. 在需要执行Python脚本的地方,调用executePythonScript方法即可。 确保将'path/to/python/script.py'替换为您的Python脚本文件的实际路径,并在需要时传递任何必要的参数。 这样,您就可以在Vue项目调用Node.js的child_process模块来运行Python脚本了。请注意,在使用child_process模块时要小心处理安全和错误处理,以避免潜在的安全问题和意外错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值