[英]Cant open video with videoCapture opencv
I have following code which reads frames from video file and stores as jpg.我有以下代码,它从视频文件中读取帧并存储为 jpg。 The code works fine if I read frames from camera directly but for video file it doesn't read the frames.
如果我直接从相机读取帧,则代码可以正常工作,但对于视频文件,它不会读取帧。
cap = cv2.VideoCapture('C:/Users/lostpanda.mp4')
#cap = cv2.VideoCapture(0)
count = 0
while cap.isOpened():
ret,frame = cap.read()
print(ret,frame)
cv2.imshow('window-name', frame)
name = 'C:/Users/video_testing/video-frames/' + str(count) + '.jpg'
#cv2.imwrite("frame%d.jpg" % count, frame)
cv2.imwrite(name,frame)
count = count + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break
Thanks谢谢
You can try this instead:你可以试试这个:
import cv2
import os
# Read the video from specified path
cam =cv2.VideoCapture(r"C:/Users/lostpanda.mp4")
try:
# creating a folder named data
if not os.path.exists('video-frames'):
os.makedirs('video-frames')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
# reading from frame
ret,frame = cam.read()
if ret:
# if video remains continue creating images
name = './video-frames/frame' + str(currentframe)+ '.jpg'
print ('Creating...' + name)
# write extracted images
cv2.imwrite(name, frame)
#Counter to show number of frames that are being created
currentframe += 1
else:
break
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.