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()
I want to draw rectangle boxes in each frame in video and save them.
Above my code, frame = plt.imread(frame)
makes TypeError: Object does not appear to be a 8-bit string path or a Python file-like object
error.
How can I use matplot with cv2? (This code is written in python)