Loading... **微软系统代码开发环境配置下载链接教程。** * [Download Visual Studio Code – Mac, Linux, Windows](https://code.visualstudio.com/Download) * [Download Python | Python.org](https://www.python.org/downloads/) * [Free Download | Anaconda](https://www.anaconda.com/download) * [Download CMake](https://cmake.org/download/) * [TONGYI Lingma – Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=Alibaba-Cloud.tongyi-lingma) * [Chinese Language Pack for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=MS-CEINTL.vscode-language-pack-zh-hans) * [opencv/data/haarcascades at 4.x · opencv/opencv · GitHub](https://github.com/opencv/opencv/tree/4.x/data/haarcascades) ## 国内源 添加国内(清华)镜像源: ``` conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes ``` ``` pip install tensorflow==2.3 -i https://mirrors.aliyun.com/pypi/simple pip install tensorflow -i https://mirrors.aliyun.com/pypi/simple https://pypi.tuna.tsinghua.edu.cn/simple https://pypi.mirrors.ustc.edu.cn/simple http://pypi.douban.com/simple/ https://mirrors.aliyun.com/pypi/simple/ http://pypi.hustunique.com/ http://pypi.hustunique.com/ http://mirrors.sohu.com/Python/ https://mirror.baidu.com/pypi/simple ``` 添加国内(清华/阿里)镜像源: ``` pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip config set global.index-url https://mirrors.aliyun.com/pypi/simple pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 更新查看版本安装库 ``` pip install --upgrade pip python -m pip install --upgrade pip python -V pip -V pip install opencv-python pip install dlib pip uninstall opencv-python pip uninstall opencv-python-headless python -m pip install --upgrade pip pip install opencv-contrib-python pip3 install mediapipe ``` ## 更新环境 ``` #查看版本 conda --version #查看路径 where conda #查看配置 conda config --show #初始化 conda init #查看现有源 conda config --show-sources #删除默认源 conda config --remove channels defaults #搜索时显示通道地址 conda config --set show_channel_urls yes #升级 conda update conda #清理 conda clean --all ``` ## 创建环境 ``` #创建环境默认安装在你 conda 目录下的 envs 文件目录下。 conda create -n feeday python=3.8 #激活环境 activate feeday #列出所有的环境 当前激活的环境会标*。 conda info -e #切换到另一个环境 conda activate feeday #注销当前环境 deactivate #复制环境 conda create -n feeday --clone feeday2 #删除环境 conda remove -n feeday --all #查看已安装包 conda list #安装包 conda install -c conda-forge python-opencv conda install beautifulsoup4 pip install beautifulsoup4 #移除包 conda remove beautifulsoup4 ``` ## 编辑器配置环境 编辑器中文乱码:搜索配置勾选改为在终端输出 ``` run code in terminal ``` 系统添加环境变量 ``` F:\Program Files\CMake\bin ``` ``` F:\Program Files\anaconda3\Library\bin F:\Program Files\anaconda3\Scripts F:\Program Files\anaconda3 ``` ``` C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin ``` ``` C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.x.xxxx\bin\Hostx64\x64 ``` ## 人脸和手指检测代码 ``` import cv2 import mediapipe as mp import os from datetime import datetime # 加载级联分类器 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 打开默认摄像头 cap = cv2.VideoCapture(0) # 如果不存在名为 'png' 的文件夹,则创建一个 output_folder = 'png' if not os.path.exists(output_folder): os.makedirs(output_folder) # 初始化MediaPipe Hand模型 mp_hands = mp.solutions.hands hands = mp_hands.Hands() while True: # 从摄像头读取一帧 ret, frame = cap.read() # 将帧转换为灰度图 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 在灰度图中检测人脸 faces = face_cascade.detectMultiScale(gray, 1.3, 5) # 在人脸周围画矩形并保存它们 for i, (x, y, w, h) in enumerate(faces): # 扩展人脸区域边界框 #x -= 10 #y -= 10 #w += 10 #h += 10 cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # 使用基于当前时间的时间戳保存检测到的人脸 now = datetime.now() timestamp = now.strftime("%Y%m%d%H%M%S") face_filename = os.path.join(output_folder, f'face_{timestamp}_{i + 1}.png') cv2.imwrite(face_filename, frame[max(0, y):min(frame.shape[0], y+h), max(0, x):min(frame.shape[1], x+w)]) # 将图像转换为RGB rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 使用MediaPipe Hand模型检测手 results = hands.process(rgb_frame) # 如果检测到手 if results.multi_hand_landmarks: # 遍历每只检测到的手 for i, hand_landmarks in enumerate(results.multi_hand_landmarks): # 获取手部关键点的坐标 landmarks = [(int(point.x * frame.shape[1]), int(point.y * frame.shape[0])) for point in hand_landmarks.landmark] # 计算手指区域的边界框 min_x, min_y = min(landmarks, key=lambda x: x[0])[0], min(landmarks, key=lambda x: x[1])[1] max_x, max_y = max(landmarks, key=lambda x: x[0])[0], max(landmarks, key=lambda x: x[1])[1] # 扩展边界框 min_x -= 20 min_y -= 20 max_x += 20 max_y += 20 # 截取手指区域的图像 finger_region = frame[max(0, min_y):min(frame.shape[0], max_y), max(0, min_x):min(frame.shape[1], max_x)] # 生成当前时间的字符串形式 current_time = datetime.now().strftime("%Y%m%d%H%M%S") # 保存截取的图像,以时间命名 hand_filename = os.path.join(output_folder, f"hand_{current_time}_{i}.png") cv2.imwrite(hand_filename, finger_region) # 绘制手部关键点 for cx, cy in landmarks: cv2.circle(frame, (cx, cy), 5, (255, 0, 0), -1) # 显示处理后的帧 cv2.imshow('检测', frame) # 如果按下 'q' 键,退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭所有窗口 cap.release() cv2.destroyAllWindows() ``` 最后修改:2024 年 10 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏