使用python版本的opencv保存从视频文件中抽取出来的图像帧,就是在磁盘上看不到图片文件。进过测试,发现imwrite的参数中若包括了windows目录的磁盘路径(如D:)则无法保存成功。
程序一:
cap = cv2.VideoCapture("D:/项目/video/front.avi")
index = 0
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('iframe', frame)
cv2.imwrite(str(index)+".jpg", frame)
index += 1
if index > 10:
break
程序一可以在当前目录保存图片,但是无法指定目录。
程序二:
cap = cv2.VideoCapture("D:/项目/video/front.avi")
index = 0
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('iframe', frame)
cv2.imwrite("D:/项目/video/" + str(index)+".jpg", frame)
index += 1
if index > 10:
break
程序二无法在指定目录生成图片,磁盘上没保存任何jpg文件
程序三:
cap = cv2.VideoCapture("D:/项目/活体检测/python_liveness/video/front.avi")
index = 0
pwd = os.getcwd()
os.chdir("D:/项目/活体检测/python_liveness/video/")
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('iframe', frame)
cv2.imwrite(str(index)+".jpg", frame)
index += 1
if index > 10:
break
os.chdir(pwd)
程序三可以在指定目录保存图片。
程序四:
cap = cv2.VideoCapture("D:/项目/活体检测/python_liveness/video/front.avi")
index = 0
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('iframe', frame)
cv2.imwrite("video/" + str(index)+".jpg", frame)
index += 1
if index > 10:
break
程序四可以在指定相对目录生成图片。
个人怀疑是“D:”字符的原因。