在第一次和opencv接触后,感触颇多。
anaconda安装cv2过程坎坷,折腾了两个小时。
最后的程序还出现了个神秘错误,不过不影响运行,本小白就写出来记录下。
本程序作用介绍:对视频按照帧数分割成图片。
下面是代码:
import cv2
import os
video_name = '02.mp4'
def save_img():
video_path = 'C:\\Users\\jhon smis\\Desktop\\'
videos = os.listdir(video_path)
for video_name in videos:
file_name = video_name.split('.')[0]
folder_name = video_path + file_name
os.makedirs(folder_name, exist_ok=True)
vc = cv2.VideoCapture(video_path + '/' + video_name)
c = 0
rval = vc.isOpened()
frames= 1
count = 1
while rval:
c = c + 1
rval, frame = vc.read()
pic_path = 'G:\\TUPIAN\\Winter Storm Xylia Time-l-VideoIndirelim.com\\'
if rval:
if frames%10 == 0:
cv2.imwrite(pic_path + str(c) + '.jpg', frame)
cv2.waitKey(1)
count += 1
frames= frames+1
else:
break
vc.release()
print('save_success')
print(folder_name)
程序运行时输出均正常
下面是报错
[ERROR:0] VIDEOIO(cvCreateFileCapture_Images(filename.c_str())): raised OpenCV exception:
OpenCV(3.4.14) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ta6q0f5f\opencv\modules\videoio\src\cap_images.cpp:246: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): C:\Users\jhon smis\Desktop\/Origin.lnk in function 'cv::icvExtractPattern'
[ERROR:0] VIDEOIO(cvCreateFileCapture_Images(filename.c_str())): raised OpenCV exception:
OpenCV(3.4.14) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ta6q0f5f\opencv\modules\videoio\src\cap_images.cpp:246: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): C:\Users\jhon smis\Desktop\/Rockstar Games Launcher.lnk in function 'cv::icvExtractPattern'
错误表现:
会把我桌面上的文件一个一个做出对应的空文件夹
如下图所示:
我的猜测:之前导入视频误把一个长的导入了,结果出现了十几个空文件夹,现在导入了一个一分钟的,只出现了两个,貌似跟帧数有关系。