任务

我之前跑了一个CNN情绪识别的开源代码,现在我想尝试把他用到我的另一个项目里。但当时那个项目是用python3.8的虚拟环境跑的,我现在的项目的虚拟环境是python3.10,所以就会出点问题。这就是为什么我现在坚持什么项目都用python3.10跑通,并记录排坑博客的原因!方便再次用到。

踩坑回顾

CV2包版本问题

症状描述

我在python3.10环境下,pip3 install一通,直接默认最新的 版本。原本运行没问题的代码,换新环境就找不到元素了:

AttributeError: module 'cv2' has no attribute 'haarcascades'

肯定是包版本问题。要么改版本,要么看新版本是不是同样的功能换了使用方法,如果新版本干脆没这个功能了,就比较棘手,可能得改版本。我不想改版本,就先网上搜下。这篇博客参考回答给了我答案。

解决方法

把源代码中的

face_path = cv2.haarcascades + 'haarcascade_frontalface_alt.xml'

改为

face_path = cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml'

就行了。

模型文件路径问题

症状描述

运行到加载模型这一步报错:

OSError: No file or directory found at ./model/CNN-Emotion-Model

原项目中,脚本和模型的目录在同一个目录中,python也在这个目录下运行,所以是没问题的。但在新项目中,python不在这个目录下运行,所以相对路径的根不再是那个目录,而是python脚本的目录,自然找不到模型文件。这应该是挺常见的问题,但我工程实践太少了,现在才知道怎么处理:

  • 要么用绝对路径:这太傻了,下次项目移植岂不更麻烦
  • 要么保留这一模块的目录结构(也就是保留了相对路径关系),让脚本自己运行时确定自己的绝对路径,一拼接就没问题了:这是个好办法

解决办法

把源代码:

model_path = "./model/CNN-Emotion-Model"

改为:

current_dir = os.path.dirname(os.path.realpath(__file__))
model_path = os.path.join(current_dir, 'model/CNN-Emotion-Model')

就好了。

tensorflow版本问题

症状描述

运行报错很长,可把我吓了一跳。和运行原项目的提示信息一对比,发现主要变化是这几个部分:

2024-02-02 21:58:21.583946: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-02-02 21:58:21.583973: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-02-02 21:58:21.584533: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered

2024-02-02 21:58:24.336529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 6100 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3070 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6
2024-02-02 21:58:25.693790: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:447] Loaded runtime CuDNN library: 8.8.1 but source was compiled with: 8.9.4.  CuDNN library needs to have matching major version and equal or higher minor version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
2024-02-02 21:58:25.694223: W tensorflow/core/framework/op_kernel.cc:1839] OP_REQUIRES failed at conv_ops_impl.h:1199 : UNIMPLEMENTED: DNN library is not found.
Traceback (most recent call last):
  File "/home/lcy-magic/RaceCar_Demo/RaceCar_ShareControl/evaluate/run-realtime.py", line 85, in <module>
    main()
  File "/home/lcy-magic/RaceCar_Demo/RaceCar_ShareControl/evaluate/run-realtime.py", line 58, in main
    emotion = drawFace(frame, frame_grey)
  File "/home/lcy-magic/RaceCar_Demo/RaceCar_ShareControl/evaluate/run-realtime.py", line 36, in drawFace
    emotion = emotion_model.predict(face/255)
  File "/home/lcy-magic/anaconda3/envs/CARDEMO/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/lcy-magic/anaconda3/envs/CARDEMO/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 53, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error:

  File "/home/lcy-magic/anaconda3/envs/CARDEMO/lib/python3.10/site-packages/keras/src/layers/convolutional/base_conv.py", line 262, in convolution_op

DNN library is not found.
	 [[{{node sequential_11/conv2d_57/Conv2D}}]] [Op:__inference_predict_function_1627]

看起来也是版本问题,网上搜了几个方法都不太管用,无奈只好给tensorflow降版本了。

解决办法

通过pip3 list查看原项目的tensorflow版本为tensorflow 2.13.1,而现在项目的版本为tensorflow 2.15.0.post1
于是降版本:

pip3 install tensorflow==2.13.1

再次运行,完美成功:
在这里插入图片描述

其他

我现在很想知道有没有对正负面情绪进行实时打分的算法,特别是基于微表情的,跑的这个情绪识别的开源算法只有做出比较刻意的表情才能被明显识别,较小的情绪变化他捕捉不到,而且波动严重,选取最大值作为主要情绪的识别是合格的,但我想要个能给舒适度打分的算法。如果有知道的大哥,麻烦指个路,多谢多谢。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐