import os
import cv2
import dlib
while(True):
if(os.path.exists("test/timestamp_micro.jpg")):
path = "test/timestamp_micro.jpg"
img = cv2.imread(path)#读取照片
if img is None:
print(1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#灰度处理
detector = dlib.get_frontal_face_detector()#人脸分类器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")#人脸检测器
dets = detector(img, 1)
for face in dets:
shape = predictor(img, face) # 寻找人脸的68个标定点
height = []
wide = []
# 遍历所有点,找出左上角与右下角坐标
for point in shape.parts():
height.append(point.x)
wide.append(point.y)
x1 = min(height) # 左上角
y1 = min(wide) # 左上角
y2 = max(wide) # 右下角
x2 = max(height) # 右下角
img = img[y1:y2, x1:x2]#截取图片
cv2.imwrite("test\detect.jpg", img)
os.remove(path)
我想要设计一个不断截取输入照片人脸部分的程序,但是只能成功运行一次,第二次就会报错告知我读取的img为空……
查询有关问题得出的解答都是路径含有中文或者格式不对,但是仔细检查也没能找出问题并且若是真的这些有问题为什么第一遍还是能够成功执行,而到第二遍读取才失败,希望能得到解答