Because I want to do something, I tried to display the video playback time as a process in the previous stage before before.
import cv2
import numpy as np
if __name__ == '__main__':
cap = cv2.VideoCapture('one_minutes.mp4')
cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
telop_height = 50
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
writer = cv2.VideoWriter('telop_time.mp4',fourcc, fps, (cap_width, cap_height + telop_height))
count = 0
try :
while True:
if not cap.isOpened():
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
ret, frame = cap.read()
if frame is None:
break
telop = np.zeros((telop_height, cap_width, 3), np.uint8)
telop[:] = tuple((128,128,128))
images = [frame, telop]
frame = np.concatenate(images, axis=0)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "{:.4f} [sec]".format(round(count/fps, 4)),
(cap_width - 250, cap_height + telop_height - 10),
font,
1,
(0, 0, 255),
2,
cv2.LINE_AA)
writer.write(frame)
count += 1
except cv2.error as e:
print(e)
writer.release()
cap.release()
telop = np.zeros((telop_height, cap_width, 3), np.uint8)
telop[:] = tuple((128,128,128))
images = [frame, telop]
frame = np.concatenate(images, axis=0)
Here, we are creating a space to display characters below. Create an image filled with a single color with telop = np.zeros ((telop_height, cap_width, 3), np.uint8) telop [:] = tuple ((128,128,128))
, and ʻimages = [frame, telop ] frame = np.concatenate (images, axis = 0) uses
numpy` to synthesize images vertically.
cv2.putText
does not support Japanese.
If you want to display it, you have to use github or Pillow.
Searching for issue, it seems that the freetype module can be used, but only in C ++: weary:
-Concatenation of images in Python -Add text to image
Recommended Posts