I am trying to detect the face using mtcnn. The main aim is to detect face, crop and save the cropped image as jpg or png file type. The code implemented is below.
from facenet_pytorch import MTCNN
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
img = Image.open("example.jpg")
mtcnn = MTCNN(margin=20, keep_all=True, post_process=False)
faces = mtcnn(img)
print(faces.shape)
This gives the shape
torch.Size([1, 3, 160, 160])
How to save this cropped portion as jpg file.
torch.save(faces, "faces.torch")
that wont be saved as an image, if you want to save it as an image:
img = Image.fromarray(faces.cpu().detach().numpy()[0])
img.save("faces.png")