[動画を作る , 動画を読み込む] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
Python(パイソン) プログラム作ってみた インデックス へ
-----
2024.9.4 presented in [note] ( //note.com/runningWater/ )
----------
1 はじめに
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。
以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。
しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。
その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。
このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------
2 どのようなものを作ったのか
[動画を作る , 動画を読み込む] ものを、作ってみようと思い、作ってみた。
下記の2モジュールによって、構成されている。
[ TestVideoHandling ] (下記モジュールの動作をテストするためのもの)
[ VideoHandling ]
これらのモジュールは、適宜、下記で説明されているモジュールを、使用している。
[画像処理, OpenCV2 使用 , 機能追加版] , Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた
Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,[エラー処理] を 行う
[ TestVideoHandling ] は、以下のように、動いていく。
(1) 動画データを作成
下記のような処理を、150回、繰り返す。
下記(Fig 1) のイメージデータを読み込み、
そのイメージデータ上に、一定の長さの直線と文字列を書き込み、
それを、1個の動画フレームデータとして、書き出す
このようにして、150個のフレームから成る、動画データが、作成される。
(2) 動画データを読み込み、特定のフレームの内容を、画像データとして、書き出す
(1) で作成された動画データを、最初のフレームから順に、読み込み、特定のフレームにさしかかったら、そのフレームの内容を、画像データとして、出力する。(下記 Fig 2 , Fig 3 , Fig 4 )
以降に、各モジュールの内容を記す。
----------
3 [ TestVideoHandling ] , ファイル名 [ TestVideoHandling.py ]
----------
import ImageDataTwoDimensionHandlingV2
import VideoHandling
#-----------------------------------
def insert_line_and_text_into_image_data ( \
arg_ins_ImageDataTwoDimensionHandlingV2
, arg_frame_counter ) :
module_name = "_____"
function_name = "insert_line_and_text_into_image_data"
for i in range ( 1 , arg_frame_counter ) :
for j in range ( 1 , 10 ) :
arg_ins_ImageDataTwoDimensionHandlingV2 \
.insert_line_figure_into_ImageData ( \
module_name
, function_name
# in the commonly used mathematical XY cordinates
, 100 # arg_From_x_on_ImageData_space
, ( 100 + j ) # arg_From_y_on_ImageData_space
, ( 100 + ( i * 2 ) ) # arg_To_x_on_ImageData_space
, ( 100 + j ) #arg_To_y_on_ImageData_space
, 1 #arg_thickness_of_line_figure
, 180 # arg_color_Blue
, 20 # arg_color_Green
, 20 # arg_color_Red
)
arg_ins_ImageDataTwoDimensionHandlingV2 \
.insert_text_into_ImageData ( \
module_name
, function_name
# in the commonly used mathematical XY cordinates
, 200 # arg_x_on_ImageData_space
, 300 #arg_y_on_ImageData_space
, ( "frame number = " + str ( arg_frame_counter ) )
, 10 # arg_font_color_Blue
, 60 # arg_font_color_Green
, 220 #arg_font_color_Red
)
#======================================
# Test for VideoHandling Class
module_name = "___"
function_name = "Test"
ins_ImageDataTwoDimensionHandlingV2 \
= ImageDataTwoDimensionHandlingV2 \
.ImageDataTwoDimensionHandlingV2 (
module_name
, function_name )
ins_ImageDataTwoDimensionHandlingV2 \
.load_image_data ( module_name
, function_name
, "E:/ForPython/ForTest/Test_6/ImageData_for_Test.png" )
width_of_loaded_image_data \
= ins_ImageDataTwoDimensionHandlingV2 \
.get_width_of_ImageData ( )
height_of_loaded_image_data \
= ins_ImageDataTwoDimensionHandlingV2 \
.get_height_of_ImageData ( )
# --------------------------------------------------
# construct a instance of VideoHandling Class
ins_VideoHandling = VideoHandling \
.VideoHandling ( module_name
, function_name )
ins_VideoHandling \
.prepare_for_output_VideoData ( \
module_name
, function_name
, "E:/ForPython/ForTest/Test_6/Test.mp4"
, width_of_loaded_image_data
, height_of_loaded_image_data
)
# ======================
# ----------- output VideoData -------------
frame_counter = 1
while ( frame_counter <= 150 ) :
ins_ImageDataTwoDimensionHandlingV2 \
.load_image_data ( module_name
, function_name
, "E:/ForPython/ForTest/Test_6/ImageData_for_Test.png" )
insert_line_and_text_into_image_data ( \
ins_ImageDataTwoDimensionHandlingV2
, frame_counter )
ins_VideoHandling \
.write_one_frame_data_for_output_VideoData ( \
module_name
, function_name
, ins_ImageDataTwoDimensionHandlingV2
)
print ( "frame_counter = " , frame_counter )
frame_counter += 1
ins_VideoHandling \
.terminate_writing_for_output_VideoData ( module_name
, function_name )
#================================
# ----------- input VideoData -------------
ins_VideoHandling \
.prepare_for_input_VideoData ( \
module_name
, function_name
, "E:/ForPython/ForTest/Test_6/Test.mp4"
)
process_continue = "Y"
frame_counter = 1
while ( process_continue == "Y" ) :
result_value , ins_ImageDataTwoDimensionHandlingV2 \
= ins_VideoHandling \
.read_one_frame_data_of_VideoData ( \
module_name
, function_name
)
print ( "result_value = " , result_value )
if ( result_value == "E" ) :
# reached to data end
process_continue = "N"
break
width_of_loaded_image_data \
= ins_ImageDataTwoDimensionHandlingV2 \
.get_width_of_ImageData ( )
height_of_loaded_image_data \
= ins_ImageDataTwoDimensionHandlingV2 \
.get_height_of_ImageData ( )
print ( "width_of_loaded_image_data = " , width_of_loaded_image_data )
print ( "height_of_loaded_image_data = " , height_of_loaded_image_data )
print ( "frame_counter = " , frame_counter )
# write image of frame
if ( ( frame_counter == 50 ) \
or \
( frame_counter == 80 ) \
or \
( frame_counter == 110 ) \
) :
ins_ImageDataTwoDimensionHandlingV2 \
.write_loaded_image_data_to_directed_path ( \
module_name
, function_name
, "E:/ForPython/ForTest/Test_6/output" \
+ "_" + str ( frame_counter ) + ".png" )
frame_counter += 1
#------------------------
ins_VideoHandling \
.terminate_reading_for_input_VideoData ( module_name
, function_name )
#==================================================
----------
----------
4 [ VideoHandling ] , ファイル名 [ VideoHandling.py ]
----------
----------
import cv2
import ImageDataTwoDimensionHandlingV2
import ErrorProcess
#===========================================
class VideoHandling :
CV_CLASS_NAME = "VideoHandling"
#------------------------------------------------------------
# definition of constructer
def __init__( self
, arg_requester_module
, arg_requester_function
):
methode_name = "constructer"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_list_of_ErrorMessage = [ " " , " " , " " , " " ]
# -------------------------------------
# for output Video Data,
# the values of the parameters are limited as follows
self.iv_value_of_fourcc = \
cv2.VideoWriter_fourcc ( *'mp4v' )
# fps ( frames per second )
self.iv_value_of_fps = 30
# use color image ( not use gray scale )
self.iv_color_or_grayscale = True
# --- construct instance of ImageDataTwoDimensionHandlingV2
self.iv_ins_ImageDataTwoDimensionHandlingV2_for_reading \
= ImageDataTwoDimensionHandlingV2 \
.ImageDataTwoDimensionHandlingV2 ( \
VideoHandling.CV_CLASS_NAME
, methode_name )
# --------------------------------------
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "self.iv_value_of_fourcc = " , self.iv_value_of_fourcc )
print ( "==================================" )
return
#------------------------------------------------------------
def prepare_for_input_VideoData ( self
, arg_requester_module
, arg_requester_function
, arg_abs_path_of_input_VideoDataFile
) :
methode_name = "prepare_for_input_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_abs_path_of_input_VideoDataFile \
= arg_abs_path_of_input_VideoDataFile
print ( "self.iv_abs_path_of_intput_VideoDataFile = " \
, self.iv_abs_path_of_input_VideoDataFile )
# ------- construct instance of VideoCapture -----------------------
self.iv_ins_VideoCapture \
= cv2.VideoCapture ( \
self.iv_abs_path_of_input_VideoDataFile )
if ( \
( \
self.iv_ins_VideoCapture \
.isOpened ( ) \
) \
== \
False \
) :
#did not opened
self.iv_list_of_ErrorMessage [ 0 ] = \
"******** Error !!!!! did not opened *************"
self.iv_list_of_ErrorMessage [ 1 ] = \
"from upper , arg_requester_module = " + arg_requester_module
self.iv_list_of_ErrorMessage [ 2 ] = \
"from upper , arg_requester_function = " + arg_requester_function
self.iv_list_of_ErrorMessage [ 3 ] = \
"self.iv_abs_path_of_input_VideoDataFile = "\
+ self.iv_abs_path_of_input_VideoDataFile
ErrorProcess.do_error_process (
#arg_requester_module
VideoHandling.CV_CLASS_NAME
#arg_requester_function
, methode_name
#arg_terminate_process_Y_or_N
, "Y"
#arg_self.iv_list_of_ErrorMessage
, self.iv_list_of_ErrorMessage
)
return
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#------------------------------------------------------------
def read_one_frame_data_of_VideoData ( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "read_one_frame_data_of_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
result_of_reading , image_data_readed \
= self.iv_ins_VideoCapture \
.read ( )
if ( result_of_reading == False ) :
#reached to data end
return "E" , ""
self.iv_ins_ImageDataTwoDimensionHandlingV2_for_reading \
.set_image_data ( \
VideoHandling.CV_CLASS_NAME
, methode_name
, image_data_readed
)
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
return "Y" \
, self.iv_ins_ImageDataTwoDimensionHandlingV2_for_reading
#------------------------------------------------------------
def terminate_reading_for_input_VideoData ( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "terminate_reading_for_input_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_ins_VideoCapture \
.release ( )
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#======================================
#------------------------------------------------------------
def prepare_for_output_VideoData ( self
, arg_requester_module
, arg_requester_function
, arg_abs_path_of_output_VideoDataFile
, arg_width_of_frame
, arg_height_of_frame
) :
methode_name = "prepare_for_output_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_abs_path_of_output_VideoDataFile \
= arg_abs_path_of_output_VideoDataFile
self.iv_width_of_frame = arg_width_of_frame
self.iv_height_of_frame = arg_height_of_frame
print ( "self.iv_abs_path_of_output_VideoDataFile = " \
, self.iv_abs_path_of_output_VideoDataFile )
print ( "self.iv_width_of_frame = " , self.iv_width_of_frame )
print ( "self.iv_height_of_frame = " , self.iv_height_of_frame )
# ------- construct instance of VideoWriter -----------------------
self.iv_ins_VideoWriter \
= cv2 \
.VideoWriter ( \
self.iv_abs_path_of_output_VideoDataFile
, self.iv_value_of_fourcc
, self.iv_value_of_fps
, # frame size
( self.iv_width_of_frame , self.iv_height_of_frame )
, self.iv_color_or_grayscale
)
if ( \
( \
self.iv_ins_VideoWriter \
.isOpened ( ) \
) \
== \
False \
) :
#did not opened
self.iv_list_of_ErrorMessage [ 0 ] = \
"******** Error !!!!! did not opened *************"
self.iv_list_of_ErrorMessage [ 1 ] = \
"from upper , arg_requester_module = " + arg_requester_module
self.iv_list_of_ErrorMessage [ 2 ] = \
"from upper , arg_requester_function = " + arg_requester_function
self.iv_list_of_ErrorMessage [ 3 ] = \
"self.iv_abs_path_of_input_VideoDataFile = " \
+ self.iv_abs_path_of_output_VideoDataFile
ErrorProcess.do_error_process (
#arg_requester_module
VideoHandling.CV_CLASS_NAME
#arg_requester_function
, methode_name
#arg_terminate_process_Y_or_N
, "Y"
#arg_self.iv_list_of_ErrorMessage
, self.iv_list_of_ErrorMessage
)
return
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#------------------------------------------------------------
def write_one_frame_data_for_output_VideoData ( self
, arg_requester_module
, arg_requester_function
, arg_ImageDataTwoDimensionHandlingV2
) :
methode_name = "write_one_frame_data_for_output_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
ImageDataTwoDimensionHandlingV2
image_data_of_one_frame \
= arg_ImageDataTwoDimensionHandlingV2 \
.get_loaded_image_data ( \
arg_requester_module
, arg_requester_function )
self.iv_ins_VideoWriter \
.write ( image_data_of_one_frame )
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#------------------------------------------------------------
def terminate_writing_for_output_VideoData ( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "terminate_writing_for_output_VideoData"
print ( "==================================" )
print ( "Enter into , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_ins_VideoWriter \
.release ( )
print ( "==================================" )
print ( "Exit from , Class = " + VideoHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#======================================
----------