私的AI研究会 > 動画保存考察
OpenVINO™ Toolkit バージョン「2021.2」環境で動画保存ができない原因を探る。
動画ファイルのフレームごとにカウント数を表示しながら保存する。
import cv2 class App: def __init__(self): self.windowName_in = "VIDEO IN" self.windowName_out = "VIDEO OUT" cv2.namedWindow(self.windowName_in) cv2.namedWindow(self.windowName_out) self.frame_num = 0 def openVideo(self): def getFps(): fps = self.src.get(cv2.CAP_PROP_FPS) frame_time = int(1 / fps * 1000 ) return fps, frame_time self.src = cv2.VideoCapture('bird.mp4') if not self.src.isOpened(): print("FILE OPEN FAILED..") import sys sys.exit() self.fps, self.frame_time = getFps() print('FPS = ', self.fps) print('FRAME_TIME = ', self.frame_time) def playVideo(self): def initFrame(): retval, frame = self.src.read() h, w, channels = frame.shape return h, w, frame def initWriter(): # fourcc = cv2.VideoWriter_fourcc(*'XVID') # rec = cv2.VideoWriter('video_out.mp4', fourcc, self.fps, (w, h)) print("w,h = ", w, h) fourcc = cv2.VideoWriter_fourcc('m','p','4','v') rec = cv2.VideoWriter('video_out.mp4', fourcc, self.fps, (w, h)) return rec h, w, frame = initFrame() rec = initWriter() print("!!!!!!!!!!!",rec) while True: retval, frame = self.src.read() if frame is None: print("FRAME IS NONE..") break # 何かフィルター # img_out = frame.copy() # テキスト描画 cv2.putText(img_out, "frame: " + str(self.frame_num) + "", (int(w/2), 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2, 8) cv2.imshow(self.windowName_in, frame) cv2.imshow(self.windowName_out, img_out) rec.write(img_out) key = cv2.waitKey(self.frame_time) if key == 27: print("ESC CLICKED..") break self.frame_num += 1 def closeVideo(self): cv2.destroyAllWindows() self.src.release() def run(self): self.openVideo() self.playVideo() self.closeVideo() class Util: def msg(): print("hoge") if __name__ == '__main__': App().run()
pi@raspberrypi-mas:~/workspace/test $ python3 video-inout.py [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (919) open OpenCV | GStreamer warning: unable to query duration of stream [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (956) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=0, duration=-1 FPS = 29.97002997002997 FRAME_TIME = 33 w,h = 720 480 [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (1601) open OpenCV | GStreamer warning: cannot link elements !!!!!!!!!!! <VideoWriter 0xb2b39b70> FRAME IS NONE..※ 動画ファイルは作成できていない。
mizutu@ubuntu2004dk:~/workspace/test$ python3 video-inout.py FPS = 29.97002997002997 FRAME_TIME = 33 w,h = 720 480 !!!!!!!!!!! <VideoWriter 0x7ff6b9b40370> FRAME IS NONE..※ 動画ファイルは正常に作成できる。「video_out.mp4」
import cv2 import numpy as np # 0=内蔵カメラ cap = cv2.VideoCapture(0) fps = int(cap.get(cv2.CAP_PROP_FPS)) w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') out = cv2.VideoWriter('output.mp4', fourcc, fps, (w, h)) while True: ret, frame = cap.read() cv2.imshow('frame', frame) out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() out.release() cv2.destroyAllWindows()
pi@raspberrypi-mas:~/workspace/test $ python3 video-cam.py [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (1601) open OpenCV | GStreamer warning: cannot link elements※ 動画ファイルは作成できていない。
import cv2 cap = cv2.VideoCapture("videotestsrc ! videoconvert ! appsink") while True: ret, img = cap.read() if not ret: break cv2.imshow("",img) key = cv2.waitKey(1) if key==27: #[esc] key break
pi@raspberrypi-mas:~/workspace/test $ python3 video-test.py [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (956) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=0, duration=-1
mizutu@ubuntu2004dk:~/workspace/test$ python3 video-test.py [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (956) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=0, duration=-1
# -*- coding: utf-8 -*- import cv2 print("---start---") #動画ファイルを読み込む video = cv2.VideoCapture('./video/movie-test.mp4') # 幅と高さを取得 width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) size = (width, height) #総フレーム数を取得 frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) #フレームレート(1フレームの時間単位はミリ秒)の取得 frame_rate = int(video.get(cv2.CAP_PROP_FPS)) print(frame_rate, size, frame_count) ### # 保存用 fmt = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') writer = cv2.VideoWriter('./result/outtest.mp4', fmt, frame_rate, size) while True: ret, frame = video.read() if ret: ### ここに加工処理などを記述する ### writer.write(frame) # 1フレーム画像表示 cv2.imshow('test window', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break writer.release() video.release() cv2.destroyAllWindows() print("---end---")
pi@raspberrypi-mas:/ $ cd ~/cq pi@raspberrypi-mas:~/cq $ python3 movietest.py ---start--- [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (919) open OpenCV | GStreamer warning: unable to query duration of stream [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (956) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=0, duration=-1 29 (720, 480) -1 (python3:1327): GStreamer-CRITICAL **: 09:31:32.071: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (1601) open OpenCV | GStreamer warning: cannot link elements ---end---※ 動画ファイルは作成できていない。
mizutu@ubuntu2004dk:~/workspace/test$ cd ../cq mizutu@ubuntu2004dk:~/workspace/cq$ python3 movietest.py ---start--- 29 (720, 480) 793 (python3:2062): GStreamer-CRITICAL **: 09:34:51.163: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed [ WARN:0] global ../opencv/modules/videoio/src/cap_gstreamer.cpp (1601) open OpenCV | GStreamer warning: cannot link elements ---end--- mizutu@ubuntu2004dk:~/workspace/cq$ python3 movietest.py ---start--- 29 (720, 480) 793 ---end---※ 動画ファイルは正常に作成できる。「result/outtest.mp4」
pi@raspberrypi-mas:~/work/test $ python3 video-inout.py Xlib: extension "RANDR" missing on display ":1.0". FPS = 29.97002997002997 FRAME_TIME = 33 w,h = 720 480 !!!!!!!!!!! <VideoWriter 0xacd59af0> FRAME IS NONE..※ 動画ファイルは正常に作成できる。「video_out.mp4」
pi@raspberrypi-mas:~/work/test $ python3 video-cam.py Xlib: extension "RANDR" missing on display ":1.0". pi@raspberrypi-mas:~/work/test $ ls bird.mp4 output.mp4 video-cam.py video-inout.py video-test.py※ 動画ファイルは正常に作成できる。「output.mp4」
pi@raspberrypi-mas:~/work/test $ python3 video-test.py [ERROR:0] global /home/pi/opencv-python/opencv/modules/videoio/src/cap.cpp (116) open VIDEOIO(CV_IMAGES): raised OpenCV exception: OpenCV(4.1.1) /home/pi/opencv-python/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): videotestsrc ! videoconvert ! appsink in function 'icvExtractPattern'※ GStreamerがないので当然実行できない。
pi@raspberrypi-mas:/ $ cd ~/cq pi@raspberrypi-mas:~/work/cq $ python3 movietest.py ---start--- 29 (720, 480) 793 Xlib: extension "RANDR" missing on display ":1.0". ---end--- pi@raspberrypi-mas:~/work/cq $ pi@raspberrypi-mas:~/work/cq $ ls result outtest.mp4※ 動画ファイルは正常に作成できる。「result/outtest.mp4」
pi@raspberrypi-mas:~/work/test $ python3 Python 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> print(cv2.getBuildInformation())
General configuration for OpenCV 4.1.1 ===================================== Version control: 4.1.1 Platform: Timestamp: 2019-09-08T08:03:44Z Host: Linux 4.19.58-v7+ armv7l CMake: 3.13.4 CMake generator: Unix Makefiles CMake build tool: /usr/bin/make Configuration: Release CPU/HW features: Baseline: VFPV3 NEON requested: DETECT required: VFPV3 NEON C/C++: Built as dynamic libs?: NO C++ Compiler: /usr/bin/c++ (ver 8.3.0) C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG C Compiler: /usr/bin/cc C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -mfpu=neon -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG Linker flags (Release): -Wl,--gc-sections Linker flags (Debug): -Wl,--gc-sections ccache: NO Precompiled headers: NO Extra dependencies: /usr/lib/arm-linux-gnueabihf/liblapack.so /usr/lib/arm-linux-gnueabihf/libcblas.so /usr/lib/arm-linux-gnueabihf/libatlas.so ade /usr/lib/arm-linux-gnueabihf/libQtGui.so /usr/lib/arm-linux-gnueabihf/libQtTest.so /usr/lib/arm-linux-gnueabihf/libQtCore.so /usr/lib/arm-linux-gnueabihf/libwebp.so /usr/lib/arm-linux-gnueabihf/libpng.so /usr/lib/arm-linux-gnueabihf/libz.so /usr/lib/arm-linux-gnueabihf/libtiff.so /usr/lib/arm-linux-gnueabihf/libjasper.so /usr/lib/arm-linux-gnueabihf/libjpeg.so /usr/lib/arm-linux-gnueabihf/libImath.so /usr/lib/arm-linux-gnueabihf/libIlmImf.so /usr/lib/arm-linux-gnueabihf/libIex.so /usr/lib/arm-linux-gnueabihf/libHalf.so /usr/lib/arm-linux-gnueabihf/libIlmThread.so dl m pthread rt 3rdparty dependencies: ittnotify libprotobuf quirc tegra_hal OpenCV modules: To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio Disabled: world Disabled by dependency: - Unavailable: java js python2 ts Applications: - Documentation: NO Non-free algorithms: NO GUI: QT: YES (ver 4.8.7 EDITION = OpenSource) QT OpenGL support: NO GTK+: NO VTK support: NO Media I/O: ZLib: /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.11) JPEG: /usr/lib/arm-linux-gnueabihf/libjpeg.so (ver 62) WEBP: /usr/lib/arm-linux-gnueabihf/libwebp.so (ver encoder: 0x020e) PNG: /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.6.36) TIFF: /usr/lib/arm-linux-gnueabihf/libtiff.so (ver 42 / 4.0.10) JPEG 2000: /usr/lib/arm-linux-gnueabihf/libjasper.so (ver 1.900.1) OpenEXR: /usr/lib/arm-linux-gnueabihf/libImath.so /usr/lib/arm-linux-gnueabihf/libIlmImf.so /usr/lib/arm-linux-gnueabihf/libIex.so /usr/lib/arm-linux-gnueabihf/libHalf.so /usr/lib/arm-linux-gnueabihf/libIlmThread.so (ver 2.2.1) HDR: YES SUNRASTER: YES PXM: YES PFM: YES Video I/O: DC1394: NO FFMPEG: YES avcodec: YES (58.35.100) avformat: YES (58.20.100) avutil: YES (56.22.100) swscale: YES (5.3.100) avresample: NO GStreamer: NO v4l/v4l2: YES (linux/videodev2.h) Parallel framework: pthreads Trace: YES (with Intel ITT) Other third-party libraries: Lapack: YES (/usr/lib/arm-linux-gnueabihf/liblapack.so /usr/lib/arm-linux-gnueabihf/libcblas.so /usr/lib/arm-linux-gnueabihf/libatlas.so) Eigen: NO Custom HAL: YES (carotene (ver 0.0.1)) Protobuf: build (3.5.1) OpenCL: YES (no extra features) Include path: /home/pi/opencv-python/opencv/3rdparty/include/opencl/1.2 Link libraries: Dynamic load Python 3: Interpreter: /usr/bin/python3 (ver 3.7.3) Libraries: /usr/lib/arm-linux-gnueabihf/libpython3.7m.so (ver 3.7.3) numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.16.2) install path: python Python (for build): /usr/bin/python2.7 Java: ant: NO JNI: NO Java wrappers: NO Java tests: NO Install to: /home/pi/opencv-python/_skbuild/linux-armv7l-3.7/cmake-install -----------------------------------------------------------------
※ GStreamer はインストールされていない。