如何保存mtcnn检测到的带有红色边框的图像



我有这样的代码,其中mtcnn检测图像上的人脸,在每个人脸周围绘制一个红色矩形,并在屏幕上打印。

代码取自:https://machinelearningmastery.com/how-to-perform-face-detection-with-classical-and-deep-learning-methods-in-python-with-keras/

但我想保存每张脸上都有红色方框的图像。这样我就可以对它进行一些预处理。任何帮助都是好的。

# draw an image with detected objects
def draw_image_with_boxes(filename, result_list):
# load the image
data = pyplot.imread(filename)
# plot the image
pyplot.imshow(data)
# get the context for drawing boxes
ax = pyplot.gca()
# plot each box
for result in result_list:
# get coordinates
x, y, width, height = result['box']
# create the shape
rect = Rectangle((x, y), width, height, fill=False, color='red')
# draw the box
ax.add_patch(rect)
# show the plot
pyplot.show()
filename = 'test1.jpg'
# load image from file
pixels = pyplot.imread(filename)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
faces = detector.detect_faces(pixels)
# display faces on the original image
draw_image_with_boxes(filename, faces)

这个图像,以便我可以在html 上显示它

您可以使用matplotlib.pyplot.savefig。例如:

# save the plot
plt.savefig('image_with_box.jpg')
# show the plot
pyplot.show()

您可以在此处找到更多详细信息:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html

最新更新