这可能吗?
Import os
Import cv2
For (root, dirs, files) in os.walk(directory):
For file in files:
If file.endswith(.mp4)
Return file
Cam = cv2.VideoCapture (file)
我想使用opencv播放视频,但是我不想输入文件名。我想将视频添加到opencv将访问视频的目录中。
错误是“断言失败”。
请帮助。
答案 0 :(得分:0)
请不要在语句中使用大写字母...希望这样可以工作:
import os
def find_mp4(directory):
for root, dirs, files in os.walk(directory, topdown=False):
for name in files:
if name.endswith('.mp4'):
return os.path.join(root, name)
directory = '.'
mp4_file_name = find_mp4(directory)
答案 1 :(得分:0)
import cv2
import os
import cv2
for root, dirs, filename in os.walk('videos'):
for fil in filename:
if fil.endswith('.mp4'):
ref = os.path.join(root,fil) # find the files and then join it with the path and assign it to a variable
print(ref)
cap = cv2.VideoCapture(ref)
while True:
ret, frame = cap.read()
cv2.imshow("output", frame)
if cv2.waitKey(25) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()