【2022年】解决tensorflow.python.framework.errors

【2022年】解决tensorflow.python.framework.errors,第1张

报错截图:

原代码:
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)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/727650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存