如何在Linux上让Face-recognition脚本调用独立NVIDIA显卡
问题
我正在使用face_recognition库,调用CNN模型进行人脸检测:
locations = face_recognition.face_locations(frame, model='cnn')
想让脚本用独立显卡而非集成显卡运行,但尝试optirun python main.py和primusrun python main.py都没用,执行nvidia-smi查看显卡使用情况时,只有Xorg在占用显存:
nvidia-smi ... |=======================================================================================| | 0 N/A N/A 7626 G /usr/lib/Xorg 5MiB | +---------------------------------------------------------------------------------------+
显然脚本没用到独立显卡。我的Linux基础薄弱,但想深入了解系统,麻烦帮忙解决。
我的脚本如下:
def detected(): # data = pickle.loads(open('user_1_encodings.pickle', 'rb').read()) cams = cv2.VideoCapture(0) not_identify = [] while True: ret, frame = cams.read() locations = face_recognition.face_locations(frame, model='cnn') encodings = face_recognition.face_encodings(frame, locations) for face_encoding, face_location in zip(encodings, locations): # user_encodings = user_data['encodings'] # result = face_recognition.compare_faces(user_encodings, face_encoding) result = False green = [0, 255, 0] blue = [255, 0, 0] red = [0, 0, 255] if any(result): print(result) match = user_data['name'] left_top = (face_location[3], face_location[0]) right_bottom = (face_location[1], face_location[2]) cv2.rectangle(frame, left_top, right_bottom, green, 2) text_x = face_location[3] text_y = face_location[0] - 10 font_scale = 0.5 font_thickness = 1 font_face = cv2.FONT_HERSHEY_COMPLEX_SMALL text_size, _ = cv2.getTextSize(match, font_face, font_scale, font_thickness) text_width, text_height = text_size rect_left_top = (text_x-1, text_y+5) rect_right_bottom = (text_x + text_width+5, text_y - text_height-5) cv2.rectangle(frame, rect_left_top, rect_right_bottom, (255, 255, 255), -1) cv2.putText(frame, match, (text_x, text_y), font_face, font_scale, (255, 0, 0), font_thickness) else: print('NOT OK') print(face_location) match = 'Unknown' top, right, bottom, left = face_location face = frame[top:bottom, left:right] pil_image = Image.fromarray(face) print(pil_image) not_identify.append(pil_image) print(not_identify) if len(not_identify) > 3: # if result is False: not_identify = [] else: data = result not_identify = [] left_top = face_location[3], face_location[0] right_bottom = (face_location[1], face_location[2]) cv2.rectangle(frame, left_top, right_bottom, red, 2) text_x = face_location[3] text_y = face_location[0] - 10 font_scale = 0.5 font_thickness = 1 font_face = cv2.FONT_HERSHEY_COMPLEX_SMALL text_size, _ = cv2.getTextSize(match, font_face, font_scale, font_thickness) text_width, text_height = text_size rect_left_top = (text_x-1, text_y+5) rect_right_bottom = (text_x + text_width+5, text_y - text_height-5) cv2.rectangle(frame, rect_left_top, rect_right_bottom, (255, 255, 255), -1) cv2.putText(frame, match, (text_x, text_y), font_face, font_scale, (255, 0, 0), font_thickness) cv2.imshow('scan', frame) k = cv2.waitKey(20) if k == ord('q'): break if __name__ == '__main__': detected()
基准测试程序用optirun运行时能正常调用独立显卡,说明驱动安装没问题:
+---------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=======================================================================================| | 0 N/A N/A 6351 G /usr/lib/Xorg 24MiB | | 0 N/A N/A 6355 G glxspheres64 2MiB | +---------------------------------------------------------------------------------------+
解决方法
1. 检查dlib是否支持CUDA
face_recognition底层依赖dlib,CNN人脸检测需要dlib编译时启用CUDA支持。先运行以下代码确认:
import dlib print(dlib.DLIB_USE_CUDA)
如果输出False,说明当前dlib没有CUDA支持,必须重新编译安装。
2. 重新编译安装带CUDA支持的dlib和face_recognition
- 先卸载现有版本:
pip uninstall -y dlib face_recognition
- 安装编译依赖(Ubuntu/Debian系):
sudo apt install build-essential cmake libopenblas-dev liblapack-dev libx11-dev libgtk-3-dev python3-dev
- 编译安装dlib(确保CUDA已正确安装,执行
nvcc --version能输出版本信息):
pip install dlib --no-binary :all:
编译过程会自动检测CUDA环境,只要CUDA正常,就会启用CUDA支持。
- 重新安装face_recognition:
pip install face_recognition
3. 优化optirun/primusrun的运行方式
- 先进入optirun的shell环境再运行脚本,确保环境变量正确传递:
optirun bash python main.py
- 或者指定Python解释器的完整路径,避免调用到错误的版本(比如虚拟环境内的Python):
optirun /usr/bin/python3 main.py # 虚拟环境示例 optirun ~/venvs/face_detect/bin/python main.py
4. 验证GPU使用情况
运行脚本后,再次执行nvidia-smi,如果看到python进程占用GPU显存,就说明成功调用了独立显卡。也可以用nvidia-smi dmon实时监控GPU使用率变化。
内容的提问来源于stack exchange,提问作者Roman
相关产品推荐
相关产品推荐

