cap=cv2.VideoCapture(video_input_path)
...
vid_writer=cv2.VideoWriter(video_output_path, ...)
# 프레임 하나씩 읽고 쓰기
while True:
# 다음 프레임 유무, 이미지 프레임
hasFrame, img_frame=cap.read()
if not hasFrame:
print('더 이상 처리할 frame이 없습니다')
break
# 가공할 수 있는 새로운 파일 생성
vid_writer.write(img_frame)
cap=cv2.VideoCapture(video_input_path)
cap.get(cv2.CAP_PROP_FRAME_WIDTH)
cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
cap.get(cv2.CAP_PROP_FPS)
while True:
hasFrame, img_frame=cap.read()
if not hasFrame:
print('더 이상 처리할 frame이 없습니다')
break
cap=cv2.VideoCapture(video_input_path)
codec=cv2.VideoWriter_fourcc(*'XVID')
vid_size=(round(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
vid_fps=cap.get(cv2.CAP_PROP_FPS)
vid_writer=cv2.VideoWriter(video_output_path,vid_fps,vid_size)
!mkdir ./data
!wget -O /content/data/Night_Day_Chase.mp4 https://github.com/chulminkw/DLCV/blob/master/data/video/Night_Day_Chase.mp4?raw=true
import cv2
video_input_path = '/content/data/Night_Day_Chase.mp4'
#
video_output_path = '/content/data/Night_Day_Chase_out.mp4'
cap = cv2.VideoCapture(video_input_path)
codec = cv2.VideoWriter_fourcc(*'XVID')
vid_size = (round(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) #(200, 400)
vid_fps = cap.get(cv2.CAP_PROP_FPS)
vid_writer = cv2.VideoWriter(video_output_path, codec, vid_fps, vid_size)
frame_cnt = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print('총 Frame 갯수:', frame_cnt, 'FPS:', round(vid_fps), 'Frame 크기:', vid_size)
총 Frame 갯수: 1383 FPS: 28 Frame 크기: (1216, 516)
import time
green_color=(0, 255, 0)
red_color=(0, 0, 255)
start = time.time()
index=0
while True:
hasFrame, img_frame = cap.read()
if not hasFrame:
print('더 이상 처리할 frame이 없습니다.')
break
index += 1
print('frame :', index, '처리 완료')
cv2.rectangle(img_frame, (300, 100, 800, 400), color=green_color, thickness=2)
caption = "frame:{}".format(index)
cv2.putText(img_frame, caption, (300, 95), cv2.FONT_HERSHEY_SIMPLEX, 0.7, red_color, 1)
vid_writer.write(img_frame)
print('write 완료 시간:', round(time.time()-start,4))
vid_writer.release()
cap.release()
* 출처: 인프런 '딥러닝 컴퓨터 비전 완벽 가이드'