I tried to display the video playback time (OpenCV: Python version)

Trigger

Because I want to do something, I tried to display the video playback time as a process in the previous stage before before.

development of

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()

Supplement

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.

result

output_telop_time.gif

in conclusion

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:

Referenced link

-Concatenation of images in Python -Add text to image

Recommended Posts

I tried to display the video playback time (OpenCV: Python version)
I tried face recognition from the video (OpenCV: python version)
Python OpenCV tried to display the image in text.
I tried to display the time and today's weather w
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried python programming for the first time.
I tried "binarizing" the image with Python + OpenCV
I want to display the progress in Python!
I tried to graph the packages installed in Python
I tried Python on Mac for the first time.
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried python on heroku for the first time
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
I tried to summarize the string operations of Python
I raised the Python version from 2 to 3, but every time I restart the ubuntu terminal, the version remains 2.
I tried "gamma correction" of the image with Python + OpenCV
I tried to illustrate the time and time in C language
[Python] I tried to visualize the follow relationship of Twitter
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to touch Python (installation)
How to get the Python version
I tried to move the ball
I tried to estimate the interval.
How to write offline real time I tried to solve the problem of F02 with Python
I tried to describe the traffic in real time with WebSocket
I tried to solve the ant book beginner's edition with python
I tried to make a regular expression of "time" using Python
I tried to process the image in "sketch style" with OpenCV
I tried to digitize the stamp stamped on paper using OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried to cut out a still image from the video
I tried to improve the efficiency of daily work with Python
I tried to make an image similarity function with Python + OpenCV
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to summarize the umask command
I tried to implement permutation in Python
I tried tensorflow for the first time
I tried to recognize the wake word
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried non-photorealistic rendering with Python + opencv
How to check opencv version in python
I tried to implement ADALINE in Python
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python
Python: I tried the traveling salesman problem
I want to display the progress bar
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
I tried the Python Tornado Testing Framework
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
When I tried to run Python, it was skipped to the Microsoft Store