我有这段代码,mtcnn在图像上检测人脸,在每个人脸周围画一个红色的矩形,并在屏幕上打印。
但我想保存图像,每个面孔周围都有红色的方框。这样我就可以对它做一些预处理了。任何帮助都是好的。
# 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)
发布于 2021-04-28 01:21:03
您可以使用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
https://stackoverflow.com/questions/67284840
复制