报错截图:

在这里插入图片描述

原代码:

import cv2

from mtcnn import MTCNN

detector = MTCNN()

img = cv2.imread('./Lena3.jpg')

output = detector.detect_faces(img)

print(output)

在网上搜了半天,大部分是说GPU被占用,或者是向量维数不正确什么的。但是我的问题并不在这里。

解决方式:

加入Tensorflow显存设置。

import cv2
import tensorflow.compat.v1 as tf  
from mtcnn import MTCNN

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.6
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)

detector = MTCNN()

img = cv2.imread('./Lena3.jpg')

output = detector.detect_faces(img)

print(output)

结果:

加入显存设置后正常运行。

在这里插入图片描述
p.s.玩一玩MTCNN人脸检测

import cv2
import tensorflow.compat.v1 as tf
from mtcnn import MTCNN

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.6
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)

detector = MTCNN()
img = cv2.imread('./Lena3.jpg')
output = detector.detect_faces(img)
print(output)

x,y,width,height = output[0]['box']
left_eye_X,left_eye_Y = output[0]['keypoints']['left_eye']
right_eye_X,right_eye_Y = output[0]['keypoints']['right_eye']
nose_X,nose_Y = output[0]['keypoints']['nose']
mouth_left_X,mouth_left_Y = output[0]['keypoints']['mouth_left']
mouth_right_X,mouth_right_Y = output[0]['keypoints']['mouth_right']

# opencv的三色顺序为BGR (Blue,Green,Red)
cv2.rectangle(img,pt1=(x,y),pt2=(x+width,y+height),color=(0,255,0),thickness=2)
cv2.circle(img,center=(left_eye_X,left_eye_Y),color=(0,0,255),thickness=2,radius=1)
cv2.circle(img,center=(right_eye_X,right_eye_Y),color=(0,0,255),thickness=2,radius=1)
cv2.circle(img,center=(nose_X,nose_Y),color=(255,0,0),thickness=2,radius=1)
cv2.circle(img,center=(mouth_left_X,mouth_left_Y),color=(255,0,0),thickness=2,radius=1)
cv2.circle(img,center=(mouth_right_X,mouth_right_Y),color=(255,0,0),thickness=2,radius=1)
cv2.imshow('result',img)
# cv2.imwrite('./result.png',img)
cv2.waitKey(0)

在这里插入图片描述

GitHub 加速计划 / te / tensorflow
184.55 K
74.12 K
下载
一个面向所有人的开源机器学习框架
最近提交(Master分支:3 个月前 )
a49e66f2 PiperOrigin-RevId: 663726708 3 个月前
91dac11a This test overrides disabled_backends, dropping the default value in the process. PiperOrigin-RevId: 663711155 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐