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

How to save the image with the red bounding boxes on it detected by 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)

This image so that i can display it on an 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