本节课程主要内容为开发环境的代建,具体如下:
1)编程环境:Conda环境
2)编程语言:Python3.8
3)库文件:Opencv
4)编程软件:Pycharm软件

常用开发语言—环境介绍(大白的相关课程
image.png
安装的整个步骤,可以按照Conda->Python->算法库->Pycharm的流程。

  • 第一步安装好Conda,建立一个环境管理系统。
  • 在此基础上,第二步,我们可以用Conda命令,新建一个Python3.8的项目环境。
  • 第三步,在这个环境中,安装项目中需要用的一些算法库。
  • 第四步,磨刀不误砍柴工,我们再安装Pycharm软件,在上面三步设置好以后,更便捷的进行编程操作。

环境安装

1、conda的安装

1.1 conda的下载(官网),也可以从大白给的链接进行下载

1.2 conda的安装

① Windows下安装Conda。双击exe文件、同意、所有用户、下一步、将安装路径添加到环境变量
② Mac下安装Conda。安装参考流程: https://zhuanlan.zhihu.com/p/51137150
③ Linux下安装Conda安装参考流程: https://zhuanlan.zhihu.com/p/164886255

2、利用conda安装python

2.1 新建conda环境

通过Anaconda Prompt进入conda的终端;利用代码conda env list查看当前所有的conda环境;键入conda create -n dabai python=3.8即可新建一个名为dabai的conda环境,里面内置的python版本为3.8。
注意:如果长时间一直是安装中的状态,可以按下ctrl+z,可能已经安装好。

2.2 遇到的问题

① 我20年在自己的Windows笔记本上装过conda,但是此后一直都在公司的Linux下进行开发。今天用conda新建项目环境时报错如下:

  1. (F:\Python_software\Anaconda3) C:\Users\Qian0733>conda create -n dabai python=3.8
  2. Fetching package metadata ....
  3. WARNING: The remote server could not find the noarch directory for the
  4. requested channel with url: https://pypi.tuna.tsinghua.edu.cn/simple
  5. It is possible you have given conda an invalid channel. Please double-check
  6. your conda configuration using `conda config --show`.
  7. If the requested url is in fact a valid conda channel, please request that the
  8. channel administrator create `noarch/repodata.json` and associated
  9. `noarch/repodata.json.bz2` files, even if `noarch/repodata.json` is empty.
  10. $ mkdir noarch
  11. $ echo '{}' > noarch/repodata.json
  12. $ bzip2 -k noarch/repodata.json
  13. ..
  14. WARNING: The remote server could not find the noarch directory for the
  15. requested channel with url: http://pypi.douban.com/simple
  16. It is possible you have given conda an invalid channel. Please double-check
  17. your conda configuration using `conda config --show`.
  18. If the requested url is in fact a valid conda channel, please request that the
  19. channel administrator create `noarch/repodata.json` and associated
  20. `noarch/repodata.json.bz2` files, even if `noarch/repodata.json` is empty.
  21. $ mkdir noarch
  22. $ echo '{}' > noarch/repodata.json
  23. $ bzip2 -k noarch/repodata.json
  24. ...
  25. PackageNotFoundError: Packages missing in current channels:
  26. - python 3.8*
  27. We have searched for the packages in the following channels:
  28. - https://pypi.tuna.tsinghua.edu.cn/simple/win-64
  29. - https://pypi.tuna.tsinghua.edu.cn/simple/noarch
  30. - http://pypi.douban.com/simple/win-64
  31. - http://pypi.douban.com/simple/noarch
  32. - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
  33. - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch

通过关键语句a valid conda channel找到问题出在源上(参考文章),因此马上找到知乎上一篇conda换源文章(含Windows、Linux)。
Windows下的换源比较简单,只要在conda终端键入conda config --remove-key channels即可。再通过conda config --show就能看到conda源恢复为默认设置了。设置前后的源如下:

  1. channels: # 以前设置的源
  2. - https://pypi.tuna.tsinghua.edu.cn/simple
  3. - http://pypi.douban.com/simple
  4. - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  1. channels: # 恢复默认的源
  2. - defaults

此后按照教程新建python3.8的代码环境。至此一切顺利···

  1. Fetching package metadata .............
  2. Solving package specifications: .
  3. Package plan for installation in environment F:\Python_software\Anaconda3\envs\dabai:
  4. The following NEW packages will be INSTALLED:
  5. ca-certificates: 2021.10.26-haa95532_2 defaults
  6. certifi: 2021.10.8-py38haa95532_0 defaults
  7. openssl: 1.1.1l-h2bbff1b_0 defaults
  8. pip: 21.2.2-py38haa95532_0 defaults
  9. python: 3.8.12-h6244533_0 defaults
  10. setuptools: 58.0.4-py38haa95532_0 defaults
  11. sqlite: 3.36.0-h2bbff1b_0 defaults
  12. vc: 14.2-h21ff451_1 defaults
  13. vs2015_runtime: 14.27.29016-h5e58377_2 defaults
  14. wheel: 0.37.0-pyhd3eb1b0_1 defaults
  15. wincertstore: 0.2-py38haa95532_2 defaults
  16. Proceed ([y]/n)? y
  17. ca-certificate 100% |###############################| Time: 0:00:00 401.40 kB/s
  18. vs2015_runtime 100% |###############################| Time: 0:00:02 903.77 kB/s
  19. vc-14.2-h21ff4 100% |###############################| Time: 0:00:00 2.76 MB/s
  20. openssl-1.1.1l 100% |###############################| Time: 0:00:03 1.88 MB/s
  21. sqlite-3.36.0- 100% |###############################| Time: 0:00:01 819.81 kB/s
  22. python-3.8.12- 100% |###############################| Time: 0:00:09 2.16 MB/s
  23. certifi-2021.1 100% |###############################| Time: 0:00:00 566.71 kB/s
  24. wheel-0.37.0-p 100% |###############################| Time: 0:00:00 120.19 kB/s
  25. wincertstore-0 100% |###############################| Time: 0:00:00 6.75 MB/s
  26. setuptools-58. 100% |###############################| Time: 0:00:00 1.47 MB/s
  27. pip-21.2.2-py3 100% |###############################| Time: 0:00:02 904.80 kB/s
  28. #
  29. # To activate this environment, use:
  30. # > activate dabai
  31. #
  32. # To deactivate an active environment, use:
  33. # > deactivate
  34. #
  35. # * for power-users using bash, you must source
  36. #

② 不能通过conda activate dabai代码进入新建的环境
报错信息为CommandNotFoundError: 'activate',键入conda显示出相关的所有命令及其作用,具体如下:

  1. usage: conda [-h] [-V] command ...
  2. conda is a tool for managing and deploying applications, environments and packages.
  3. Options:
  4. positional arguments:
  5. command
  6. info Display information about current conda install.
  7. help Displays a list of available conda commands and their help
  8. strings.
  9. list List linked packages in a conda environment.
  10. search Search for packages and display their information. The input
  11. is a Python regular expression. To perform a search with a
  12. search string that starts with a -, separate the search from
  13. the options with --, like 'conda search -- -h'. A * in the
  14. results means that package is installed in the current
  15. environment. A . means that package is not installed but is
  16. cached in the pkgs directory.
  17. create Create a new conda environment from a list of specified
  18. packages.
  19. install Installs a list of packages into a specified conda
  20. environment.
  21. update Updates conda packages to the latest compatible version. This
  22. command accepts a list of package names and updates them to
  23. the latest versions that are compatible with all other
  24. packages in the environment. Conda attempts to install the
  25. newest versions of the requested packages. To accomplish
  26. this, it may update some packages that are already installed,
  27. or install additional packages. To prevent existing packages
  28. from updating, use the --no-update-deps option. This may
  29. force conda to install older versions of the requested
  30. packages, and it does not prevent additional dependency
  31. packages from being installed. If you wish to skip dependency
  32. checking altogether, use the '--force' option. This may
  33. result in an environment with incompatible packages, so this
  34. option must be used with great caution.
  35. upgrade Alias for conda update. See conda update --help.
  36. remove Remove a list of packages from a specified conda environment.
  37. uninstall Alias for conda remove. See conda remove --help.
  38. config Modify configuration values in .condarc. This is modeled
  39. after the git config command. Writes to the user .condarc
  40. file (C:\Users\Qian0733\.condarc) by default.
  41. clean Remove unused packages and caches.
  42. package Low-level conda package utility. (EXPERIMENTAL)
  43. optional arguments:
  44. -h, --help Show this help message and exit.
  45. -V, --version Show the conda version number and exit.
  46. other commands, such as "conda build", are available when additional conda
  47. packages (e.g. conda-build) are installed

确实没有看到与activate有关的命令说明,回头再去新创建的环境说明中看到省略conda即可进入新环境:

  1. # To activate this environment, use:
  2. # > activate dabai

③ python版本环境不匹配
之前新建环境的时候,反馈的信息是新建了一个3.8.12的python环境;但是我用python --version得到的反馈信息是python 3.6.3???whats wrong with this one?

  1. (F:\Python_software\Anaconda3) F:\Python_software\Anaconda3>activate dabai
  2. (dabai) F:\Python_software\Anaconda3>python --version
  3. Python 3.6.3 :: Anaconda, Inc.

该问题在重启电脑后解决

  1. (dabai) C:\Users\Qian0733>where python
  2. F:\Python_software\Anaconda3\envs\dabai\python.exe
  3. C:\Users\Qian0733\AppData\Local\Microsoft\WindowsApps\python.exe
  4. (dabai) C:\Users\Qian0733>python --version
  5. Python 3.8.12

2.3 conda常用的几个命令

  • conda list :查看安装了哪些包
  • conda env list 或 conda info -e :查看当前存在哪些虚拟环境
  • 【Windows下】activate xx: 进入名称叫xx的conda环境,名称这里大家也可以填写自己对应的名称
  • 【Windows下】deactivate: 退出conda环境

3、安装算法库

3.1 OpenCV的安装

在conda终端键入pip install -i [https://pypi.doubanio.com/simple](https://pypi.doubanio.com/simple) opencv-python ,通过豆瓣镜像源网站加速下载该算法包。
3.2 安装验证
通过python交互的方式,输入import cv2看是否报错。

4、安装pycharm

4.1 Pycharm软件的下载

Pycharm的下载链接为: https://www.jetbrains.com/pycharm/download/#section=windows
专业版的功能比较多,不过通常情况下我们只需要选择Community(社区版)即可,免费且功能也够用。

4.2 Pycharm软件的安装

一路“Next”;Create Desktop Shortcut:创建桌面快捷方式;Create Associations:关联文件打开方式,即.py文件是否默认用PyCharm打开。;
Note:选择安装目录,Pycharm需要的内存较多,建议将其安装在D盘或者E盘,不建议放在系统盘C盘:

4.3 如何把conda创建的环境和pycharm联系起来一起用。
pycharm的右下角可以设置代码运行环境和python解释器的版本。点击Add interpreter会跳出一个新界面,在这个界面中①选择最左侧的conda环境——②中间的existing环境——③并找到你在conda终端中配置好的环境地址。
image.pngimage.png

算法实践

可参考大白的视频:https://v.qq.com/x/page/o3301ekee7k.html

1、读取图像(相关知识点)

  • cv2.imread(图像路径,读取模式=0,1,-1) # 读取模式包含:灰度图(1通道);正常图像(3通道);含透明通道的RGBA图像(4通道)
  • OpenCV的图像shape分别是:HWC
  • img = cv2.resize(图像,预期高宽元组)
  • cv2.imshow(“image”, img)
  • cv2.waitKey(0) # 显示图片停顿的时间,如果是0,则一直显示。如果是100,则显示100ms

2、读取视频

  • cap = cv2.VideoCapture(opt.video_path) #用于获取视频的函数,当video_path为数字0时,会调用笔记本本地的摄像头 。如果想用usb摄像头,可以尝试用数字1或者2。
  • while cap.isOpened(): # 判断视频是否是打开状态
  • ret, frame = cap.read() # 利用read函数逐帧读取图像 ;其中ret是布尔值,用于表示是否读取到图像
  • frame_id要在视频打开前设置好,在视频循环时候进行累加,可以作为退出循环(视频读取)的依据。
  • 可以通过imshow的方式展示读取到的视频,设置好停顿时间就能获得流畅的显示体验。