一直在学习Python的过程中,就对各种版本、包、依赖等等一系列的东西搞得焦头烂额。直到今天找到了Conda。
Conda 是一个开源包管理系统和环境管理系统,用于安装多个版本的软件包及其依赖关系,并在它们之间轻松切换。 它适用于Linux,OS X和Windows,是为Python程序创建的,但可以打包和分发任何软件。
Conda一般分为两个发布版本
Anaconda
是一个开源的Python发行版本,包含了conda、python等180多个科学包及其依赖项。因为包含了大量的科学包,所以Anaconda的安装包比较大。
下载地址:https://www.anaconda.com/download/
Miniconda 是最小的conda安装环境。特点是包需要根据需要自行安装。
下载地址:https://conda.io/miniconda.html
华为Metabook E / Windows 11
设备名称 tdouya-metabook
处理器 11th Gen Intel® Core™ i7-1160G7 @ 1.20GHz 2.11 GHz
机带 RAM 16.0 GB (15.8 GB 可用)
设备 ID
产品 ID
系统类型 64 位操作系统, 基于 x64 的处理器
笔和触控 为 10 触摸点提供笔和触控支持
版本 Windows 11 家庭中文版
版本 21H2
安装日期 2021/12/26
操作系统版本 22000.556
体验 Windows 功能体验包 1000.22000.556.0
使用下载地址: https://conda.io/miniconda.html
测试conda和python显示版本号,安装成功。(下次再写文档,应该连pip的版本号应该一并打出来)
PS:清华大学的镜像站首页是: https://mirrors.tuna.tsinghua.edu.cn/ 里面有非常多的国外镜像。
切换至清华大学镜像站 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
按照说明,执行命令,创建配置文件
根据镜像站说明的内容,清除掉原有内容后,复制镜像站说明中指定的内容
按照命令清除索引缓存
临时使用清华大学镜像升级pip至最新版本(这本身也是使用临时镜像的一种方式)
配置清华大学镜像站为默认地址,查看一下内容。
个别教程中,此文件还有一个trust信息,但是自动生成的代码里面没有,所以我也没有再去追加。整个环境搭建下来,没有任何问题。下面将别人trust的部分写在下面以备查
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
创建了一个用于学习测试用的python3.8版本的环境
切换工作工作环境的命令为:conda create --name <工作环境名称> python=
(base) PS C:\Users\mike_> conda create --name learn_3.8 python=3.8
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: D:\S30-Miniconda\envs\learn_3.8
added / updated specs:
- python=3.8
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2022.3.29 | haa95532_0 122 KB defaults
certifi-2021.10.8 | py38haa95532_2 152 KB defaults
openssl-1.1.1n | h2bbff1b_0 4.8 MB defaults
pip-21.2.2 | py38haa95532_0 1.9 MB defaults
python-3.8.13 | h6244533_0 16.5 MB defaults
setuptools-58.0.4 | py38haa95532_0 779 KB defaults
sqlite-3.38.2 | h2bbff1b_0 807 KB defaults
vc-14.2 | h21ff451_1 8 KB defaults
vs2015_runtime-14.27.29016 | h5e58377_2 1007 KB defaults
wheel-0.37.1 | pyhd3eb1b0_0 33 KB defaults
wincertstore-0.2 | py38haa95532_2 15 KB defaults
------------------------------------------------------------
Total: 26.1 MB
The following NEW packages will be INSTALLED:
done
#
# To activate this environment, use
#
# $ conda activate learn_3.8
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) PS C:\Users\mike_>
切换工作工作环境的命令为:conda activate <工作环境名称>
conda安装包的命令为:conda install <包名称>
(base) PS C:\Users\mike_> conda activate learn_3.8 // 切换至1.3.1中创建的工作环境
(learn_3.8) PS C:\Users\mike_> conda install opencv // 安装opencv
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: D:\S30-Miniconda\envs\learn_3.8
added / updated specs:
- opencv
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB defaults
eigen-3.3.7 | h59b6b97_1 831 KB defaults
glib-2.69.1 | h5dc1a3c_1 1.6 MB defaults
gst-plugins-base-1.18.5 | h9e645db_0 1.7 MB defaults
gstreamer-1.18.5 | hd78058f_0 1.7 MB defaults
hdf5-1.10.6 | h7ebc959_0 7.9 MB defaults
icc_rt-2019.0.0 | h0cc432a_1 6.0 MB defaults
icu-58.2 | ha925a31_3 9.4 MB defaults
intel-openmp-2021.4.0 | haa95532_3556 2.2 MB defaults
jpeg-9d | h2bbff1b_0 283 KB defaults
libffi-3.4.2 | h604cdb4_1 43 KB defaults
libiconv-1.15 | h1df5818_7 626 KB defaults
libogg-1.3.5 | h2bbff1b_1 33 KB defaults
libpng-1.6.37 | h2a8f88b_0 333 KB defaults
libprotobuf-3.5.1 | he0781b1_0 1.6 MB defaults
libtiff-4.2.0 | hd0e1b90_0 786 KB defaults
libvorbis-1.3.7 | he774522_0 202 KB defaults
libwebp-base-1.2.2 | h2bbff1b_0 304 KB defaults
lz4-c-1.9.3 | h2bbff1b_1 132 KB defaults
mkl-2021.4.0 | haa95532_640 114.9 MB defaults
mkl-service-2.4.0 | py38h2bbff1b_0 51 KB defaults
mkl_fft-1.3.1 | py38h277e83a_0 139 KB defaults
mkl_random-1.2.2 | py38hf11a4ad_0 225 KB defaults
numpy-1.16.6 | py38ha4e8547_3 50 KB defaults
numpy-base-1.16.6 | py38h5bb6eb2_3 3.3 MB defaults
opencv-4.5.4 | py38h22b9916_3 23.7 MB defaults
pcre-8.45 | hd77b12b_0 382 KB defaults
qt-5.9.7 | vc14h73c81de_0 72.5 MB defaults
six-1.16.0 | pyhd3eb1b0_1 18 KB defaults
xz-5.2.5 | h62dcd97_0 244 KB defaults
zlib-1.2.11 | hbd8134f_5 114 KB defaults
zstd-1.4.9 | h19a0ad4_0 478 KB defaults
------------------------------------------------------------
Total: 251.7 MB
The following NEW packages will be INSTALLED:
blas anaconda/pkgs/main/win-64::blas-1.0-mkl
eigen anaconda/pkgs/main/win-64::eigen-3.3.7-h59b6b97_1
glib anaconda/pkgs/main/win-64::glib-2.69.1-h5dc1a3c_1
gst-plugins-base anaconda/pkgs/main/win-64::gst-plugins-base-1.18.5-h9e645db_0
gstreamer anaconda/pkgs/main/win-64::gstreamer-1.18.5-hd78058f_0
hdf5 anaconda/pkgs/main/win-64::hdf5-1.10.6-h7ebc959_0
icc_rt anaconda/pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1
icu anaconda/pkgs/main/win-64::icu-58.2-ha925a31_3
intel-openmp anaconda/pkgs/main/win-64::intel-openmp-2021.4.0-haa95532_3556
jpeg anaconda/pkgs/main/win-64::jpeg-9d-h2bbff1b_0
libffi anaconda/pkgs/main/win-64::libffi-3.4.2-h604cdb4_1
libiconv anaconda/pkgs/main/win-64::libiconv-1.15-h1df5818_7
libogg anaconda/pkgs/main/win-64::libogg-1.3.5-h2bbff1b_1
libpng anaconda/pkgs/main/win-64::libpng-1.6.37-h2a8f88b_0
libprotobuf anaconda/pkgs/main/win-64::libprotobuf-3.5.1-he0781b1_0
libtiff anaconda/pkgs/main/win-64::libtiff-4.2.0-hd0e1b90_0
libvorbis anaconda/pkgs/main/win-64::libvorbis-1.3.7-he774522_0
libwebp-base anaconda/pkgs/main/win-64::libwebp-base-1.2.2-h2bbff1b_0
lz4-c anaconda/pkgs/main/win-64::lz4-c-1.9.3-h2bbff1b_1
mkl anaconda/pkgs/main/win-64::mkl-2021.4.0-haa95532_640
mkl-service anaconda/pkgs/main/win-64::mkl-service-2.4.0-py38h2bbff1b_0
mkl_fft anaconda/pkgs/main/win-64::mkl_fft-1.3.1-py38h277e83a_0
mkl_random anaconda/pkgs/main/win-64::mkl_random-1.2.2-py38hf11a4ad_0
numpy anaconda/pkgs/main/win-64::numpy-1.16.6-py38ha4e8547_3
numpy-base anaconda/pkgs/main/win-64::numpy-base-1.16.6-py38h5bb6eb2_3
opencv anaconda/pkgs/main/win-64::opencv-4.5.4-py38h22b9916_3
pcre anaconda/pkgs/main/win-64::pcre-8.45-hd77b12b_0
qt anaconda/pkgs/main/win-64::qt-5.9.7-vc14h73c81de_0
six anaconda/pkgs/main/noarch::six-1.16.0-pyhd3eb1b0_1
xz anaconda/pkgs/main/win-64::xz-5.2.5-h62dcd97_0
zlib anaconda/pkgs/main/win-64::zlib-1.2.11-hbd8134f_5
zstd anaconda/pkgs/main/win-64::zstd-1.4.9-h19a0ad4_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
eigen-3.3.7 | 831 KB | ############################################################################ | 100%
libprotobuf-3.5.1 | 1.6 MB | ############################################################################ | 100%
libvorbis-1.3.7 | 202 KB | ############################################################################ | 100%
gstreamer-1.18.5 | 1.7 MB | ############################################################################ | 100%
done
(learn_3.8) PS C:\Users\mike_>
下列代码第七行提示,无法找到这个包
(learn_3.8) PS C:\Users\mike_> conda install mediapipe
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- mediapipe
Current channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/win-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/win-64
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/noarch
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
(learn_3.8) PS C:\Users\mike_>
之所以1.3.3.1中conda安装失败,原因是conda没有把mediapipe集成进去。此时用pip安装。
仔细看下面安装过程的48-51行,可以发现,在安装过程中,mediapipe的依赖关系还把原本numpy 的1.16.6的版本卸载了,安装了新的1.22.3版本。原本的numpy1.16.6的版本是在装OpenCV的时候安装的。(参考1.3.2中安装过程的第81行)
(learn_3.8) PS C:\Users\mike_> pip install mediapipe
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mediapipe
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/00/46/cb9c3990abd1d862bb120f40e9ce9e27728928e280ec37edd5e538b5997b/mediapipe-0.8.9.1-cp38-cp38-win_amd64.whl (48.5 MB)
|████████████████████████████████| 48.5 MB 6.4 MB/s
Collecting matplotlib
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/83/a5/d079d2287ac7a6389059a0e52537dc2e2ff342580512f42f6c7844c451a0/matplotlib-3.5.1-cp38-cp38-win_amd64.whl (7.2 MB)
|████████████████████████████████| 7.2 MB 6.8 MB/s
Collecting absl-py
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2c/03/e3e19d3faf430ede32e41221b294e37952e06acc96781c417ac25d4a0324/absl_py-1.0.0-py3-none-any.whl (126 kB)
|████████████████████████████████| 126 kB ...
Collecting opencv-contrib-python
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2e/63/c02ffce9f182dd77fad7ee1f333a6a2aca1a5a2c14a683d30b5d2bd8d8db/opencv_contrib_python-4.5.5.64-cp36-abi3-win_amd64.whl (42.2 MB)
|████████████████████████████████| 42.2 MB 6.4 MB/s
Collecting protobuf>=3.11.4
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/07/36/998db8bbde6980eff255a2701e16c871a950cbdc298ed9abe302f9301160/protobuf-3.20.0-cp38-cp38-win_amd64.whl (904 kB)
|████████████████████████████████| 904 kB 6.4 MB/s
Collecting attrs>=19.1.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/be/be/7abce643bfdf8ca01c48afa2ddf8308c2308b0c3b239a44e57d020afa0ef/attrs-21.4.0-py2.py3-none-any.whl (60 kB)
|████████████████████████████████| 60 kB 3.7 MB/s
Requirement already satisfied: numpy in d:\s30-miniconda\envs\learn_3.8\lib\site-packages (from mediapipe) (1.16.6)
Requirement already satisfied: six in d:\s30-miniconda\envs\learn_3.8\lib\site-packages (from absl-py->mediapipe) (1.16.0)
Collecting cycler>=0.10
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting python-dateutil>=2.7
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
|████████████████████████████████| 247 kB ...
Collecting numpy
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fa/f2/f4ec28f935f980167740c5af5a1908090a48a564bed5e689f4b92386d7d9/numpy-1.22.3-cp38-cp38-win_amd64.whl (14.7 MB)
|████████████████████████████████| 14.7 MB ...
Collecting fonttools>=4.22.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b0/5c/5dd502b0e2e0cb2980fc4ed17e970089003e377115abf79b1918097f4996/fonttools-4.31.2-py3-none-any.whl (899 kB)
|████████████████████████████████| 899 kB 6.4 MB/s
Collecting packaging>=20.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
|████████████████████████████████| 40 kB ...
Collecting pyparsing>=2.2.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/80/c1/23fd82ad3121656b585351aba6c19761926bb0db2ebed9e4ff09a43a3fcc/pyparsing-3.0.7-py3-none-any.whl (98 kB)
|████████████████████████████████| 98 kB 435 kB/s
Collecting kiwisolver>=1.0.1
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/97/2d/32ae1ce44d5b514dce568a809b3bed15d60ee1501b9fa9853a0fee705b59/kiwisolver-1.4.2-cp38-cp38-win_amd64.whl (55 kB)
|████████████████████████████████| 55 kB 280 kB/s
Collecting pillow>=6.2.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/cf/81/b40ed97024fa0e7c6305495928996e8f46ab153b07e0f74240f49eec7b7c/Pillow-9.1.0-cp38-cp38-win_amd64.whl (3.3 MB)
|████████████████████████████████| 3.3 MB 6.4 MB/s
Installing collected packages: pyparsing, python-dateutil, pillow, packaging, numpy, kiwisolver, fonttools, cycler, protobuf, opencv-contrib-python, matplotlib, attrs, absl-py, mediapipe
Attempting uninstall: numpy
Found existing installation: numpy 1.16.6
Uninstalling numpy-1.16.6:
Successfully uninstalled numpy-1.16.6
Successfully installed absl-py-1.0.0 attrs-21.4.0 cycler-0.11.0 fonttools-4.31.2 kiwisolver-1.4.2 matplotlib-3.5.1 mediapipe-0.8.9.1 numpy-1.22.3 opencv-contrib-python-4.5.5.64 packaging-21.3 pillow-9.1.0 protobuf-3.20.0 pyparsing-3.0.7 python-dateutil-2.8.2
(learn_3.8) PS C:\Users\mike_>
第一行代码:切换工作目录。工作环境中执行程序并不囿于conda目录
第二行代码:直接执行
(learn_3.8) PS C:\Users\mike_> cd D:\S20-Python3\60-MachineVision
(learn_3.8) PS D:\S20-Python3\60-MachineVision> python 01.py
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
测试代码:
注意:代码24行打开摄像头,田老师笔记本的0号摄像头是后置摄像头,1号摄像头是前置摄像头。 所以执行时24行是1。
此程序在窗口获得焦点的时候按字母‘p’会关闭。单纯点右上角关闭的“x”号是没用滴。
"""
演示Demo
"""
# 导入opencv
import cv2
import numpy as np
import math
# 导入mediapipe:https://google.github.io/mediapipe/solutions/hands
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
# 读取视频流
cap = cv2.VideoCapture(0)
# 获取画面宽度、高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while True:
ret,frame = cap.read()
# 镜像
frame = cv2.flip(frame,1)
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 识别
results = hands.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 如果有结果
if results.multi_hand_landmarks:
# 遍历双手
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# 显示画面
cv2.imshow('demo',frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()