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)