video = 'test.avi'
cap = cv2.VideoCapture(video)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = round(cap.get(cv2.CAP_PROP_FPS))
size = (width, height)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# noinspection PyArgumentList
out = cv2.VideoWriter("output.avi", fourcc, fps, size)
while cap.isOpened():
ret, frame = cap.read()
if frame is None:
print('\nEnd of Video')
break
frame = plt.imread(frame)
##########################################################
im = np.array(Image.open(frame), dtype=np.uint8)
fig, ax = plt.subplots(1)
ax.imshow(im)
bbox = patches.Rectangle((50, 100), 100, 130, linewidth=2, edgecolor='r', facecolor='none')
ax.add_patch(bbox)
font = {'family': 'sans-serif',
# 'color': 'darkred',
'weight': 'book',
'size': 10,
}
plt.text(50, 100, s='labels', color='white', fontdict=font, verticalalignment='top',
bbox={'color': 'red', 'pad': 0})
# plt.show()
fig.savefig(frame)
##########################################################
out.write(frame)
cap.release()
out.release()
我想在视频的每一帧中绘制矩形框并保存它们。
在我的代码上方,frame = plt.imread(frame)
导致TypeError: Object does not appear to be a 8-bit string path or a Python file-like object
错误。
如何在cv2中使用matplot? (此代码是用python编写的)