Ubuntu22.04.1下运行OpenCV人脸识别报错:缺失Qt Wayland插件
Ubuntu 22.04.1下OpenCV+face_recognition人脸识别代码报错解决
问题描述
在Ubuntu 22.04.1系统运行基于OpenCV和face_recognition的人脸识别代码时,持续出现如下报错:qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "/usr/local/lib/python3.10/dist-packages/cv2/qt/plugins"
已尝试谷歌搜索解决方案,用Pacman安装插件未成功,怀疑插件已安装但路径错误,或需要替代实现方法。
完整代码如下:
import cv2 import os import face_recognition import numpy as np # Get a reference to webcam #0 (the default one) video_capture = cv2.VideoCapture(0) #loop over all the images in the folder and put them in a list known_face_encodings = [] known_face_names = [] for file in os.listdir("known/"): if file.endswith(".jpg"): #load the image known_image = face_recognition.load_image_file("known/" + file) #get the face encoding known_face_encoding = face_recognition.face_encodings(known_image)[0] #add the encoding to the list known_face_encodings.append(known_face_encoding) #add the name to the list known_face_names.append(file[:-4]) # Initialize some variables face_locations = [] face_encodings = [] face_names = [] process_this_frame = True #loop over the frames while True: # Grab a single frame of video ret, frame = video_capture.read() # Resize frame of video to 1/4 size for faster face recognition processing small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) rgb_small_frame = small_frame[:, :, ::-1] # Only process every other frame of video to save time if process_this_frame: # Find all the faces and face encodings in the current frame of video face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: # See if the face is a match for the known face(s) matches = face_recognition.compare_faces(known_face_encodings, face_encoding) name = "Unknown" # If a match was found in known_face_encodings, just use the first one. if True in matches: first_match_index = matches.index(True) name = known_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame # Display the results for (top, right, bottom, left), name in zip(face_locations, face_names): # Scale back up face locations since the frame we detected in was scaled to 1/4 size top *= 4 right *= 4 bottom *= 4 left *= 4 # Draw a box around the face cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # Display the resulting image cv2.imshow('Video', frame) #save the image cv2.imwrite("Tested/test.jpg", frame)
解决方案
方法1:强制使用X11平台绕过Wayland问题
Ubuntu 22.04默认使用Wayland,但OpenCV的Qt插件可能存在兼容性问题,可强制指定使用X11(xcb)平台:
- 代码内设置:在代码最开头添加以下两行:
import os os.environ['QT_QPA_PLATFORM'] = 'xcb'
- 命令行运行时设置:执行脚本时直接指定环境变量:
QT_QPA_PLATFORM=xcb python your_script_name.py
方法2:修复Wayland插件路径
如果系统已安装Wayland插件但路径不匹配,可通过软链接修复:
- 查找系统中Wayland插件的位置:
find /usr -name "libqwayland-egl.so" - 将找到的插件路径链接到OpenCV的Qt插件目录:
sudo ln -s /path/to/libqwayland-egl.so /usr/local/lib/python3.10/dist-packages/cv2/qt/plugins/platforms/
(替换/path/to/libqwayland-egl.so为实际查到的路径)
方法3:移除Qt窗口显示(非必要时)
如果代码仅需要保存识别后的图片,不需要实时显示窗口,可直接注释掉cv2.imshow('Video', frame)这一行,彻底规避Qt相关问题。
内容的提问来源于stack exchange,提问作者tjmc608
相关产品推荐
相关产品推荐

