記一次java調(diào)用python

1.下載

官網(wǎng)下載地址: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命令后沒有正確輸出,設(shè)置下對應(yīng)python版本的環(huán)境變量,將python安裝的目錄和目錄下的 Scripts 目錄添加到 Path 環(huán)境變量中去


image.png

4.設(shè)置pip鏡像

打開我的電腦或者隨便打開一個文件夾,在資源管理器的地址欄輸入%appdata%后回車

image.png

image.png

在打開的文件夾中下新建一個pip文件夾,進入pip文件夾中,新建一個 pip.ini 文件,輸入如下內(nèi)容
[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/"
安裝圖像處理相關(guān)模塊
pip install NumPy SciPy opencv-python matplotlib imutils

5.測試demo,測試cv是否能正常運行,新建文件test.py,內(nèi)容如下。在當前文件下執(zhí)行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程序中調(diào)用示例

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);// 執(zhí)行py文件
            // 用輸入輸出流來截取結(jié)果
            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)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • python3 python官網(wǎng)xp系統(tǒng)下載 python 3.44win7/10下載 python3.54安...
    季花魚__goods閱讀 618評論 0 2
  • Python開發(fā)簡單爬蟲(Python2.X版本,Eclipse工具) 一、爬蟲介紹 爬蟲調(diào)度端:啟動、停止爬蟲,...
    凜0_0閱讀 2,195評論 2 10
  • 國內(nèi)鏡像源 pip如果不配置國內(nèi)鏡像源的話,下載包的速度非常慢,畢竟默認的源在國外呢,國內(nèi)主要的鏡像源有如下,其中...
    pingJava閱讀 2,808評論 0 2
  • python 基于vccode環(huán)境 1. 準備python 這個教程是為沒有使用過python和vccode環(huán)境準...
    cry15閱讀 800評論 0 0
  • 如何將py程序打包成exe py打包成exe如何打的足夠小 Python腳本不能在沒有安裝Python的機器上運行...
    牛二的Homework閱讀 1,875評論 0 5

友情鏈接更多精彩內(nèi)容