实时面部情绪识别(一)

news/2024/10/25 13:43:43/

文章目录

  • 实时面部情绪识别(一)
  • 一、linux
    • 一、Tensorflow 安装
      • 1、虚拟环境
      • 2、安装cudnn和cuda
      • 2、校验依赖
    • 二、复现
      • 1、依赖
      • 2、运行
    • 三、常见问题
      • 1、can't open camera by index
  • 二、Windows
    • 一、环境
      • 1、虚拟环境
      • 2、设置镜像(可选)
      • 3、安装cuda和cudnn
    • 二、复现
      • 1、依赖
      • 2、运行
    • 三、常见问题
      • 1、Module 'tensorflow.compat.v2' has no attribute '__internal__'
      • 2、Can't find filter element
      • 3、Cannot dlopen some GPU libraries.

实时面部情绪识别(一)

前言:本文包含在linux 和 Windows实现面部实时的情感识别 原项目在GitHub - otaha178/Emotion-recognition: Real time emotion recognition

实时预测需要GUI和相关摄像头,推荐使用Windows进行。

一、linux

一、Tensorflow 安装

在Ubuntu上使用conda来安装TensorFlow的虚拟环境,可以按照以下步骤操作:

1、虚拟环境

创建新的虚拟环境: 如果你想为TensorFlow创建一个全新的虚拟环境,你可以使用以下命令:

conda create -n tensorflow_zhanyong python=3.8

激活虚拟环境: 使用以下命令来激活刚才创建的虚拟环境:

conda activate tensorflow_zhanyong

安装TensorFlow: 在激活的虚拟环境中,使用以下命令安装TensorFlow:

pip install tensorflow==2.2

www.tensorflow.org:使用 pip 安装 TensorFlow

(详细整理!!!!)Tensorflow与Keras、Python版本对应关系!!!_tensorflow对应python版本-CSDN博客

由于后续需要安装 Keras 2.3.1的版本,故在这里选择Tensorflow 2.2 进行安装

2、安装cudnn和cuda

Tensorflow安装教程(完美安装gpu版本的tensorflow)(Windows,Conda,cuda,cudnn版本对应)_tensorflow-gpu清华源安装-CSDN博客

从源代码构建 | TensorFlow

image-20241024162626094

conda install cudatoolkit=10.1 cudnn=7.6

2、校验依赖

验证安装: 安装完成后,你可以进行简单的测试来验证TensorFlow是否正确安装。在Python环境中输入以下代码:

(tensorflow_zhanyong) zhanyong@532lab:~$ python -c "import tensorflow as tf; print(tf.__version__)"
2.2.0
nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Thu_Nov_18_09:45:30_PST_2021
Cuda compilation tools, release 11.5, V11.5.119
Build cuda_11.5.r11.5/compiler.30672275_0

二、复现

1、依赖

git clone https://github.com/otaha178/Emotion-recognition.git
cd Emotion-recognition/
pip install -r requirements.txt
Successfully built imutils
Installing collected packages: imutils, opencv_python, joblib, scikit_learn, pandas, keras-applications, Keras
Successfully installed Keras-2.3.1 imutils-0.5.3 joblib-1.4.2 keras-applications-1.0.8 opencv_python-4.3.0.38 pandas-0.25.3 scikit_learn-0.22.1

2、运行

python real_time_video.py
[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):File "real_time_video.py", line 29, in <module>frame = imutils.resize(frame,width=300)File "/home/zhanyong/anaconda3/envs/tensorflow_zhanyong/lib/python3.8/site-packages/imutils/convenience.py", line 69, in resize(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

无法打开摄像头:警告 [ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index 显示 opencv 无法通过索引打开 /dev/video0 设备。这通常是因为没有这样的设备,或设备已被其他进程占用。

尝试操作无效的帧:当 cv2.VideoCapture(0) 无法打开视频设备时,它的 read() 方法会返回 False 和一个 None 对象。你的代码试图对这个 None 对象执行 resize 操作,这导致了 AttributeError: 'NoneType' object has no attribute 'shape' 错误。

三、常见问题

1、can’t open camera by index

[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):File "real_time_video.py", line 29, in <module>frame = imutils.resize(frame,width=300)File "/home/zhanyong/anaconda3/envs/tensorflow_zhanyong/lib/python3.8/site-packages/imutils/convenience.py", line 69, in resize(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

无法打开摄像头:警告 [ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index 显示 opencv 无法通过索引打开 /dev/video0 设备。这通常是因为没有这样的设备,或设备已被其他进程占用。

尝试操作无效的帧:当 cv2.VideoCapture(0) 无法打开视频设备时,它的 read() 方法会返回 False 和一个 None 对象。你的代码试图对这个 None 对象执行 resize 操作,这导致了 AttributeError: 'NoneType' object has no attribute 'shape' 错误。

由于linux虚拟机上缺少摄像设备,导致出错。

二、Windows

一、环境

1、虚拟环境

C:\Users\ASUS>conda --version
conda 23.3.1

检查conda是否安装,没有安装可参考Windows下的Anaconda详细安装教程_windows安装anaconda-CSDN博客进行安装

conda create -n tensorflow_zhanyong python=3.8conda activate tensorflow_zhanyong

特别注意,这里需要安装tensorflow-gpu

pip install tensorflow-gpu
Downloading and Extracting PackagesPreparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate tensorflow_zhanyong
#
# To deactivate an active environment, use
#
#     $ conda deactivate

2、设置镜像(可选)

设置全局镜像

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ 
pip config set global.trusted-host mirrors.aliyun.com
(tensorflow_zhanyong) C:\Users\ASUS>pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
Writing to C:\Users\ASUS\AppData\Roaming\pip\pip.ini(tensorflow_zhanyong) C:\Users\ASUS>pip config set global.trusted-host mirrors.aliyun.com
Writing to C:\Users\ASUS\AppData\Roaming\pip\pip.ini

3、安装cuda和cudnn

注意两个都需要安装

可以先使用nvcc --version检查版本 判断是否需要更换cuda版本 建议使用10.1

(tensorflow_zhanyong) C:\Users\ASUS>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Nov_30_19:15:10_Pacific_Standard_Time_2020
Cuda compilation tools, release 11.2, V11.2.67
Build cuda_11.2.r11.2/compiler.29373293_0

CUDA Toolkit 10.1 Original Archive | NVIDIA Developer

https://developer.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.105_418.96_win10.exe

重装完之后需要重启电脑

(tensorflow_zhanyong) E:\20241024EmotionRecognition>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Fri_Feb__8_19:08:26_Pacific_Standard_Time_2019
Cuda compilation tools, release 10.1, V10.1.105

cuDNN Archive | NVIDIA Developer

image-20241024162626094

二、复现

1、依赖

git clone https://github.com/otaha178/Emotion-recognition.git
cd Emotion-recognition/

image-20241024174303512

pip install -r requirements.txt

2、运行

python real_time_video.py

三、常见问题

1、Module ‘tensorflow.compat.v2’ has no attribute ‘internal

在 TensorFlow 2.x 中,Keras 已经集成在 TensorFlow 中,你不需要单独安装 keras 库。你可以在代码中使用 tensorflow.keras 来代替 keras

(tensorflow_zhanyong) E:\20241024EmotionRecognition\Emotion-recognition>python real_time_video.py
Using TensorFlow backend.
2024-10-24 18:27:47.106586: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
Traceback (most recent call last):File "real_time_video.py", line 1, in <module>from keras.preprocessing.image import img_to_arrayFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\__init__.py", line 3, in <module>from . import utilsFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\utils\__init__.py", line 26, in <module>from .vis_utils import model_to_dotFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module>from ..models import ModelFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\models.py", line 10, in <module>from .engine.input_layer import InputFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\engine\__init__.py", line 3, in <module>from .input_layer import InputFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\engine\input_layer.py", line 7, in <module>from .base_layer import LayerFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\engine\base_layer.py", line 12, in <module>from .. import initializersFile "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\initializers\__init__.py", line 124, in <module>populate_deserializable_objects()File "E:\2024.2.19anaconda3\envs\tensorflow_zhanyong\lib\site-packages\keras\initializers\__init__.py", line 49, in populate_deserializable_objectsLOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'(tensorflow_zhanyong) E:\20241024EmotionRecognition\Emotion-recognition>

这个错误是由于你的 TensorFlow 和 Keras 版本不兼容导致的。具体的错误信息指出 tensorflow.compat.v2 模块没有 __internal__ 属性。这个问题通常出现在 Keras 和 TensorFlow 版本不匹配的情况下,特别是当你使用的 TensorFlow 版本可能是较旧的版本时,而它不完全支持某些 Keras 内部调用。

使用 TensorFlow 内置的 Keras: 自 TensorFlow 2.x 起,Keras 被直接集成在 TensorFlow 中,你可以使用 TensorFlow 内置的 Keras,而不再单独安装 Keras。

2、Can’t find filter element

2024-10-24 18:32:37.845901: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1969283b700 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2024-10-24 18:32:37.846237: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2024-10-24 18:32:37.846476: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2024-10-24 18:32:37.846538: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]

这个问题通常发生在 TensorFlow 初始化 GPU 或其他设备时,由于某些依赖项或硬件设置出现问题,导致不能正确加载必要的过滤器或组件。

大概率是安装tensorflow不是gpu版本,应该采取pip install tensorflow-gpu

3、Cannot dlopen some GPU libraries.

 Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.

缺少cudnn或者版本对应错误


http://www.ppmy.cn/news/1541838.html

相关文章

Rust 问号(?)操作符简化错误处理

Rust以其安全性和性能而闻名&#xff0c;它引入了几个特性来确保健壮的错误处理。其中&#xff0c;问号&#xff1f;操作符是Rust语言中的重要工具。它能够简化错误处理&#xff0c;使代码更具可读性和简洁性。 什么是&#xff1f;操作符 在Rust中&#xff0c;错误处理通常使…

计算机组成原理一句话

文章目录 计算机系统概述数据的表示与运算存储系统DRAM固态硬盘芯片片选磁盘RAID多模块存储器cache 指令系统寻址方式指令格式扩展操作码 中央处理器单周期处理器各类部件微程序控制器中断与异常流水线 总线输入输出设备I/O方式 计算机系统概述 指令和数据以同等地位存储在存储…

【C++面试刷题】快排(quick_sort)和堆排(priority_queue)的细节问题

一、快排的快速选择算法两种思路&#xff08;面试会考&#xff09;O(N) 快排的三数取中思路&#xff1a; 重要的是将它三个数进行排序最左为最小&#xff0c;中间为次小&#xff0c;最右为最大的数。&#xff08;错误原因&#xff1a;我刚开始没有将这三个数进行排序&#xff…

设计模式(五)原型模式详解

设计模式&#xff08;五&#xff09;原型模式详解 原型模型简介 定义&#xff1a;原型模型是一种创建型设计模型&#xff0c;它允许通过克隆/复制现有对象来创建新的对象&#xff0c;而无需通过常规的构造函数进行实例化。 这种方式的主要优势是在运行时不需要知道具体的类&a…

E108-GN系列GNSS多模卫星导航定位模块产品说明

E108-GN03和E108-GN04系列系列GNSS多模卫星定位导航模块&#xff0c;具有高性能、高集成度、低功耗、低成本等特点。该系列GNSS多模卫星定位导航模块支持BDS/GPS/GLONASS/GALILEO卫星定位&#xff0c;可多系统联合定位或多系统单独定位&#xff01;米级高精度定位&#xff0c;A…

探索现代软件开发中的持续集成与持续交付(CI/CD)实践

探索现代软件开发中的持续集成与持续交付&#xff08;CI/CD&#xff09;实践 随着软件开发的飞速进步&#xff0c;现代开发团队已经从传统的开发模式向更加自动化和灵活的开发流程转变。持续集成&#xff08;CI&#xff09; 与 持续交付&#xff08;CD&#xff09; 成为当下主…

【MATLAB代码】EKF和CDKF的对比

目录 主要特点 应用场景 运行结果展示 本MATLAB程序实现了扩展卡尔曼滤波&#xff08;EKF&#xff09;与协方差差分卡尔曼滤波&#xff08;CDKF&#xff09;在三维状态估计中的效果对比&#xff0c;为需要高精度定位与动态系统分析的用户提供了一种实用工具。通过直观的结果…

spark sql 广播模式参数

在 Spark SQL 中&#xff0c;广播&#xff08;Broadcast&#xff09;模式常用于处理 Join 操作时的小表与大表的场景&#xff0c;尤其是在小表较小&#xff0c;可以被广播到每个 Executor 时&#xff0c;能够显著提升性能&#xff0c;避免了分布式 Shuffle 的开销。 Spark SQL…