[英]How to save cropped images?
我想将裁剪后的图像保存在同名的新文件夹中。 我试过这段代码,代码提取人脸,但文件夹中没有保存的图像。 谢谢你。
image_path = glob.glob('/content/drive/MyDrive/ID01/')
def extract_face_from_image(image_path, required_size=(224, 224)):
face_images = []
for filename in os.listdir(image_path):
for img in filename:
image = cv2.imread(os.path.join(image_path, filename))
face_images.append(image)
detector = MTCNN()
faces = detector.detect_faces(image)
for face in faces:
# extract the bounding box from the requested face
x1, y1, width, height = face['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face_boundary = image[y1:y2, x1:x2]
# resize pixels to the model size
face_image = Image.fromarray(face_boundary)
face_image = face_image.resize(required_size)
face_array = asarray(face_image)
face_images.append(face_array)
cv2.imwrite(os.path.join('/content/drive/MyDrive/out/'),str(face), extracted_face)
extracted_face = extract_face_from_image('/content/drive/MyDrive/ID01/')
# cv2.imwrite(os.path.join('/content/drive/MyDrive/out'),str(face), extracted_face)
您的os.path.join
调用中有错字。 您不小心在'/content/drive/MyDrive/out/'
后面加上了右括号。 此外,您在 function 中已经extracted_face
了_face ,您将在其中extracted_face
_face 。 我认为您打算改用face_image
。 该行可能应该是:
cv2.imwrite(os.path.join('/content/drive/MyDrive/out/', str(face)), face_image)
最后,不要忘记从extract_face_from_image()
返回一些东西,否则extracted_face
将只是None
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.