java调用python的py文件

java调用py文件出现空值,但是在PyCharm运行正常,求指教
java代码


package org.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PythonFaceUtil {
    //调用自己的python路径
    String exe = "C:\\Users\\iamat\\AppData\\Local\\Programs\\Python\\Python39\\python.exe";
   //要调用的python函数脚本
    String command = "J:\\face\\src\\main\\webapp\\res\\python_face\\Face_detection.py";
    public String FaceDetection(String FaceImage) throws IOException, InterruptedException {
        String[] cmdArr = new String[] {exe, command, FaceImage};//将参数封装
        Process process = Runtime.getRuntime().exec(cmdArr);//采用Process调用,Runtime.getRuntime().exec(cmdArr)用于调用外部可执行程序或系统命令
        //python默认编码格式是(‘zh_CN’, ‘cp936’),而cp36就是编码GB2312。所以java端将输入流的字符编码设置成Gb2312
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"gb2312"));
        String line = null;
        line = in.readLine();
        System.out.println(line);
        while (line != null) {
            if(line.equals("图中有1个人脸")){
                in.close();
                process.waitFor();
                return "图中有1个人脸";
            }else if(line.equals("图中有多个人脸")) {
                in.close();
                process.waitFor();
                return "图中有多个人脸";
            }else{
                in.close();
                process.waitFor();
                return "图中没有人脸";
            }
        }
        in.close();
        int result = process.waitFor();
        System.out.println("执行结果:" + result);
        return "调用异常";
    }
  
    public static void main(String args[]) throws IOException, InterruptedException {
//        人脸检测
        String FaceImage="J:\\face\\src\\main\\webapp\\res\\facePhoto\\2bb607a8-9201-4ca2-a2ed-a98d52da23ea.gif";
        PythonFaceUtil pythonFaceUtil=new PythonFaceUtil();
        String res=pythonFaceUtil.FaceDetection(FaceImage);
        System.out.println(res);

    }
}

运行结果

img

python代码

from sys import argv

import cv2


def Face_detection():
    # dir = r"F:\my_project\opencv\face_recognize"
    # 人脸彩色图
    image = cv2.imread("3.jpg")
    # print(argv[1])
    # image = cv2.imread(argv[1])
    # 加载分类器模型
    faceCascade = cv2.CascadeClassifier(
        r"J:\\face\\src\\main\\webapp\\res\\python_face\\haarcascade_frontalface_default.xml")
    # 彩色图转为灰度图片,可用0作为实参替代
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 它可以检测出图片中所有的人脸,并将人脸用vector保存各个人脸的坐标、大小(用矩形表示),并返回numpy向量数组
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.15,
        minNeighbors=5,
        minSize=(5, 5)
    )
  
    # 判断是否有人脸
    if len(faces) >= 1:
        # 逐个标注人脸
        for (x, y, w, h) in faces:
            # 矩形标注
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
        if len(faces) > 1:
            return "图中有多个人脸"
        else:
            return "图中有1个人脸“

    else:
        return "图中没有人脸"


a = Face_detection()
print(a)



img

你确定成功链接上了Python服务了,里面的这个方法运行了么,写个print看看有没有输出先