Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- cs
- Python
- Object detection
- ubuntu
- 1단계
- 3단계
- ssd
- SWEA
- pytorch
- 그리디
- 딥러닝
- 머신러닝
- 프로그래머스
- AWS
- 백준
- CS231n
- test-helper
- MySQL
- 파이썬
- 이것이 코딩테스트다 with 파이썬
- docker
- STL
- 모두를 위한 딥러닝 강좌 시즌1
- 2단계
- 실전알고리즘
- 코드수행
- 전산기초
- 구현
- C++
- 자료구조 및 실습
Archives
- Today
- Total
곰퓨타의 SW 이야기
[python] opencv video to image 본문
파이썬에서 opencv를 활용하면 여러가지 동영상 이미지를 다룰 수 있다.
동영상을 여러장의 frame으로 변환하고자 하는 경우 다음과 같은 코드를 통해 변환이 가능하다.
import cv2
vidcap = cv2.VideoCapture('/path_input_video.mp4')
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("/path_output_frame/%06d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
print("finish! convert video to frame")
다음과 같은 동영상에 대해 위의 코드를 수행하면, 다음과 같이 여러 장의 frame으로 저장된다.
+++++21.11.03 video(.mov) 가 여러 개인 directory를 input으로 넣어서 video -> image 하고 싶은 경우
import cv2
import argparse
import glob
def video2image(opt):
video_paths = glob.glob(opt.video_dir+"/*.mov")
print(video_paths)
for vp in video_paths:
video_name = vp.split("/")[-1][:-4]
vidcap = cv2.VideoCapture(vp)
success,image = vidcap.read()
count = 0
new_path = '../custom/images/'
while success:
cv2.imwrite(new_path+video_name+"_%04d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
# print('Read a new frame: ', success)
count += 1
print("finish! convert video to frame {name}".format(name=video_name))
print("all convert finish!!")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--video_dir', type=str, help='path to video directory')
opt = parser.parse_args()
video2image(opt)
'Project > python 활용(ex.openCV)' 카테고리의 다른 글
[python] detectron2의 demo.py를 변환하여 txt 파일로 저장하기 (0) | 2021.05.10 |
---|---|
[python] pandas로 labeling type 수정하여 txt 파일 저장하기 (0) | 2021.04.23 |
[python] image size 아는 법 (0) | 2021.04.21 |
[python] ffmpeg를 활용하여 image to video 수행하기 (0) | 2021.04.21 |
[python] openCV로 txt file 활용하여 bounding box, box labeling 하기 (0) | 2021.04.21 |
Comments