英文:
How to fix - AttributeError: module 'cv2' has no attribute 'legacy'
问题
我正在运行一段来自一位程序员的人脸识别代码,这是一段时间以前的代码,我在Google Colab上运行过。原因是我的计算机没有GPU,而Colab有。
我现在不再与那位程序员合作,现在尝试在一台带有GPU的计算机上运行该代码。
我已经设置好了运行代码所需的所有软件包(python 3.10、cuda、cudnn、opencv-contrib-python==4.5.5.62
、opencv-python==4.7.0.72
、tensorflow==2.10 等),唯一改变的是将tensorflow版本从2.12 降级到2.10 以便在Windows上运行。
现在我收到以下错误消息,之前从未收到过,指出cv2没有'legacy'属性。我在网上搜索过,只看到cv2已经从版本4.5开始将某些内容移到了'legacy'。有人知道如何解决这个问题吗?
我拥有的代码在Colab上运行正常。该脚本确实能够识别GPU。
感谢所有的帮助。
错误信息:
在第10行,第1列
----> 1 dict_with_frames, fps = main(videoname=video_name, x_frame_to_check=frame_to_check, database_folder=database_folder, tracker_type=tracker_type,path_to_the_video=path_to_the_video, output_folder=output_folder, frame_of_early_stop=stop_frame, frame_of_start=frame_of_start )
3 save_the_dict(dict_with_frames, output_path='Database/', output_file_name='dict_with_frames.txt')
5 #gpu_or_cpu = 'cpu.config.yml' - for cpu
6 #gpu_or_cpu = 'gpu.config.yml' - for gpu
在第8行,第88列,在main(videoname, x_frame_to_check, database_folder, tracker_type, path_to_the_video, output_folder, frame_of_early_stop, frame_of_start)中
84 ret, frame = cap.read()
85 if frame_id % x_frame_to_check == 0:
86
87 # multi object tracker initialization
---> 88 multi_tracker = cv2.legacy.MultiTracker_create()
89 dict_with_objects = {}
90 print(f'Frame number {frame_id}/{number_of_frames}')
属性错误: 模块 'cv2' 没有属性 'legacy'
我尝试在网上搜索相关信息,但关于cv2.legacy的内容很少。能查到的信息是,版本4.5之后,跟踪模块被移到了cv2.legacy,但那才是正常工作的部分。我刚刚又在Colab上运行了该文件(tensorflow 2.12),它正常工作。
英文:
I am running a facial recognition code that I have from a programmer a while back that I ran on google colab. Reason was that I didn't have gpu on my computer and colab has.
I am not working with the programmer anymore and am now trying to run the code on a computer with gpu.
I finished setting up all packages needed for running the code (python 3.10, cuda, cudnn, opencv-contrib-python==4.5.5.62
, opencv-python==4.7.0.72
, tensorflow==2.10 etc.) and the only thing I changed was to downgrade the tensorflow from version 2.12 to 2.10 so it will work on windows.
I am now receiving the following error which I haven't received before stating that cv2 has no attribute 'legacy'. I searched online but only saw a mention that cv2 has move things to legacy from version ~4.5. Anyone know how to get around this?
The code I have is working on colab. The script does manage to recognize gpu.
Thanks to all reference.
Error:
Cell In[10], line 1
----> 1 dict_with_frames, fps = main(videoname=video_name, x_frame_to_check=frame_to_check, database_folder=database_folder, tracker_type=tracker_type,path_to_the_video=path_to_the_video, output_folder=output_folder, frame_of_early_stop=stop_frame, frame_of_start=frame_of_start )
3 save_the_dict(dict_with_frames, output_path='Database/', output_file_name='dict_with_frames.txt')
5 #gpu_or_cpu = 'cpu.config.yml' - for cpu
6 #gpu_or_cpu = 'gpu.config.yml' - for gpu
Cell In[8], line 88, in main(videoname, x_frame_to_check, database_folder, tracker_type, path_to_the_video, output_folder, frame_of_early_stop, frame_of_start)
84 ret, frame = cap.read()
85 if frame_id % x_frame_to_check == 0:
86
87 # multi object tracker initialization
---> 88 multi_tracker = cv2.legacy.MultiTracker_create()
89 dict_with_objects = {}
90 print(f'Frame number {frame_id}/{number_of_frames}')
AttributeError: module 'cv2' has no attribute 'legacy'
I tried searching about it online but not much comes up to do with cv2.legacy. What does come up is that after version 4.5 the tracking module was moved to the cv2.legacy, but that is what works.
I just ran the file again, as it is (tensorflow 2.12), on colab and it is working.
答案1
得分: 0
通常情况下出现的问题都是一样的:多个opencv-python*软件包之间发生了冲突。尤其是如果版本甚至都不匹配,但匹配的版本仍然可能会发生冲突。
- 删除所有的
opencv-python*
软件包 - 安装 仅一个 软件包
所有这些软件包都包含基本模块。*-contrib
软件包不是附加组件,它是完整的。
任何依赖于 opencv-python
的其他软件包都将使用 opencv-contrib-python
很好,即使Python软件包的依赖关系不能建模。
英文:
it's usually the same issue: multiple opencv-python* packages conflict with each other. Especially if the versions don't even match, but matching versions may still conflict.
- Remove all
opencv-python*
packages - Install exactly one package
All of them contain the base modules. The *-contrib
package is not an add-on, it's complete.
Any other package depending on opencv-python
will be perfectly happy with opencv-contrib-python
, even if Python package dependencies can't model that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论