1. 程式人生 > >MacOS:安裝Miniconda、Python、OpenCV、TensorFlow

MacOS:安裝Miniconda、Python、OpenCV、TensorFlow

一、安裝Miniconda

1、下載 Miniconda(⽹址:https://conda.io/miniconda.html ),它是⼀個 sh ⽂件。開啟 Terminal 應⽤進⼊命令⾏來執⾏這個 sh ⽂件,例如:

sh Miniconda3-latest-Linux-x86_64.sh

安裝時會顯⽰使⽤條款,按“↓”繼續閱讀,按“Q”退出閱讀。之後需要回答下⾯⼏個問題:

Do you accept the license terms? [yes|no]

[no] >>> yes

Do you wish the installer to prepend the Miniconda3 install location to PATH in your /home/your_name/.conda ? [yes|no]

[no] >>> yes

2、安裝完成後,我們需要讓 Conda ⽣效。需要運⾏⼀次 source ~/.bash_profile 或重啟命令⾏應⽤。

二、建立Python環境

1、建立環境:

conda create --name python36 python=3.6

2、啟用環境:

source activate python36

退出環境使用:source deactivate

三、安裝 OpenCV

1、pip install opencv-python

2、pip install opencv-contrib-python

3、測試:

#匯入cv模組
import cv2 as cv
#讀取影象,支援 bmp、jpg、png、tiff 等常用格式
img = cv.imread("./test.jpg")
#建立視窗並顯示影象
cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)

四、安裝 TensorFlow

1、pip install tensorflow

2、測試:

import tensorflow as tf

# 建立2個矩陣,前者1行2列,後者2行1列,然後矩陣相乘:
matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2], [2]])
product = tf.matmul(matrix1, matrix2)

# 上邊的操作是定義圖,然後用會話Session去計算:
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)