记一次java调用python

1.下载

官网下载地址:https://www.python.org/downloads/
python安装包镜像下载地址: https://registry.npmmirror.com/binary.html?path=python/

2.安装

image.png

image.png

image.png

3.验证安装

image.png

如果运行python命令后没有正确输出,设置下对应python版本的环境变量,将python安装的目录和目录下的 Scripts 目录添加到 Path 环境变量中去


image.png

4.设置pip镜像

打开我的电脑或者随便打开一个文件夹,在资源管理器的地址栏输入%appdata%后回车

image.png

image.png

在打开的文件夹中下新建一个pip文件夹,进入pip文件夹中,新建一个 pip.ini 文件,输入如下内容
[global]
index-url = https://pypi.douban.com/simple

或者通过命令生成

配置pip镜像
python -m pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.extra-index-url "http://mirrors.aliyun.com/pypi/simple/
http://pypi.hustunique.com/ https://pypi.mirrors.ustc.edu.cn/simple/"
安装图像处理相关模块
pip install NumPy SciPy opencv-python matplotlib imutils

5.测试demo,测试cv是否能正常运行,新建文件test.py,内容如下。在当前文件下执行python test.py

import cv2 as cv
print(cv.getVersionString())
image = cv.imread("1.jpg")
print(image.shape)
cv.imshow("image",image)
cv.waitKey()

6java程序中调用示例

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.crfsdi.prospectai.bean.response.rockcore.DrillImgCombineDto;
import com.crfsdi.prospectai.service.DrillImgCombineService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@Service
public class DrillImgConbineServideImpl implements DrillImgCombineService {
    @Override
    public String Combine(DrillImgCombineDto requestDto) {
        StringBuilder ret;
        ret = new StringBuilder();
        try {
            // 下载原文件
            String filePath = "https://******/2683_1627726752632.jpg";
            //HttpUtil.downloadFile(filePath, "C:\\Users\\JH\\Desktop\\123\\dist");
            HttpUtil.downloadFile(filePath, "/");

            Process proc;
            String[] cmdArr = new String[]{"D:\\python\\python.exe",
                    "./DrillImgCombine.py",
                    Base64.encodeBase64String(Base64.encodeBase64String(JSONUtil.toJsonStr(requestDto).getBytes("utf-8")).getBytes("utf-8"))};

            proc = Runtime.getRuntime().exec(cmdArr);// 执行py文件
            // 用输入输出流来截取结果
            BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            String error;
            while ((error = stdError.readLine()) != null) {
                System.err.println("=====+++++"+error);
            }


            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gbk"));
            String line = null;
            while ((line = in.readLine()) != null) {
                ret.append(line);
                System.out.println("----+++++"+line);
            }
            in.close();
            proc.waitFor();
            // TODO 上传拼接处理文件。
            // TODO 保存入库
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return ret.toString();
    }

}
# -*- coding: UTF-8 -*-
import os,sys
import base64
import json
import cv2 as cv
import numpy as np
#import scipy.spatial
from imutils import perspective
imgPath = "D:\\python\\pyTest\\"
print(sys.argv[1])
jsonObj = base64.b64decode(base64.b64decode(sys.argv[1]))
print(jsonObj)

parms = json.loads(jsonObj)

# load the notecard code image, clone it, and initializeNo module named
#scipy.spatial the 4 points
# that correspond to the 4 corners of the notecard
notecard = cv.imread(imgPath + "1.jpg")
clone = notecard.copy()
pts = np.array(parms['pts'])
# loop over the points and draw them on the cloned image
for (x, y) in pts:
    cv.circle(clone, (x, y), 5, (0, 255, 0), -1)
# apply the four point tranform to obtain a "birds eye view" of
# the notecard
warped = perspective.four_point_transform(notecard, pts)
# show the original and warped images
cv.namedWindow('Original',cv.WINDOW_NORMAL)
cv.imshow("Original", clone)
cv.namedWindow('Warped',cv.WINDOW_NORMAL)
cv.imshow("Warped", warped)
cv.imwrite(imgPath + "2.jpg",warped)
#height, width, channels = warped.shape
height=2000
width=int(height/2)
img2= cv.resize(warped,(height,width))
cv.imshow("img2", img2)
cv.imwrite(imgPath + "3.jpg",img2)
img3=img2[0:int(width/5),:]
img4=img2[int(width/5):int(2*width/5),:]
img5=img2[int(2*width/5):int(3*width/5),:]
img6=img2[int(3*width/5):int(4*width/5),:]
img7=img2[int(4*width/5):width,:]
cv.imshow("img3", img3)
cv.imshow("img4", img4)
cv.imshow("img5", img5)
cv.imshow("img6", img6)
cv.imshow("img7", img7)
inputs = np.hstack((img3,img4,img5,img6,img7))
cv.imshow("inputs", inputs)
cv.imwrite(imgPath + "4.jpg",inputs)
cv.waitKey(0)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • python3 python官网xp系统下载 python 3.44win7/10下载 python3.54安...
    季花鱼__goods阅读 3,630评论 0 2
  • Python开发简单爬虫(Python2.X版本,Eclipse工具) 一、爬虫介绍 爬虫调度端:启动、停止爬虫,...
    凛0_0阅读 6,423评论 2 10
  • 国内镜像源 pip如果不配置国内镜像源的话,下载包的速度非常慢,毕竟默认的源在国外呢,国内主要的镜像源有如下,其中...
    pingJava阅读 7,528评论 0 2
  • python 基于vccode环境 1. 准备python 这个教程是为没有使用过python和vccode环境准...
    cry15阅读 4,086评论 0 0
  • 如何将py程序打包成exe py打包成exe如何打的足够小 Python脚本不能在没有安装Python的机器上运行...
    牛二的Homework阅读 5,773评论 0 5

友情链接更多精彩内容