opencv
opencv-图像部分
opencv显示图像
cv2. namedWindow( "just for test" , 0 )
cv2. imshow( "just for test" , index_img)
cv2. waitKey( 0 )
Opencv转PIL.Image
Image. fromarray( cv2. cvtColor( cv_img, cv2. COLOR_BGR2RGB) )
PIL. Image转Opencv
cv2. cvtColor( numpy. asarray( Img_img) , cv2. COLOR_RGB2BGR)
opencv- 视频部分
opencv读取视频
import cv2video_stream = cv2. VideoCapture( video_path)
读取视频的相关信息
帧率
fps = int ( round ( cap. get( cv2. CAP_PROP_FPS) ) )
分辨率-宽度
width = int ( cap. get( cv2. CAP_PROP_FRAME_WIDTH) )
分辨率-高度
height = int ( cap. get( cv2. CAP_PROP_FRAME_HEIGHT) )
总帧数
frame_counter = int ( cap. get( cv2. CAP_PROP_FRAME_COUNT) ) video_frames = [ ]
while True : still_reading, frame = video_stream. read( ) if not still_reading: video_stream. release( ) break video_frames. append( frame) opencv将图片写成视频
import cv2fourcc = cv2. VideoWriter_fourcc( * 'mp4v' )
videowriter = cv2. VideoWriter( args. video_save_path, fourcc, 25 , size)
for mask_name in sorted ( os. listdir( args. image_mask_folder) ) : mask_path = os. path. join( args. image_mask_folder, mask_name) mask = cv2. imread( mask_path) mask[ mask != ( 0 , 0 , 0 ) ] = 255 mask[ mask == ( 0 , 0 , 0 ) ] = 0 videowriter. write( mask)
videowriter. release( )
时间time模块
输出格式化的时间
import timecur_time = time. strftime( "%Y%m%d%H%M" , time. localtime( ) )
python运行程序模块
指定程序运行的根目录
export PYTHONPATH= . /
subprocess执行的代码模块
video_process_cmd = [ 'ffmpeg' , '-i' , input_path, '-vcodec' , 'libx264' , '-b:v' , str ( modify_bitrate) , '-bufsize' , str ( modify_bitrate) , '-maxrate' , str ( modify_bitrate) , '-c:a' , 'copy' , output_path, "-y"
]
subprocess. run( video_process_cmd)