如何使用opencv将帧写入文件夹

how to write frames into folder using opencv

从实时网络摄像头视频中提取帧并共同保存 space。但是如何使用 opencv

在特定文件夹中写入帧

代码:

import cv2
# Opens the inbuilt camera of laptop to capture video.
cap = cv2.VideoCapture(0)
i = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    
    # This condition prevents from infinite looping
    # incase video ends.
    if ret == False:
        break
    # Save Frame by Frame into disk using imwrite method
    cv2.imwrite('Frame'+str(i)+'.jpg', frame)
    i += 1

cap.release()
cv2.destroyAllWindows()

我试过这样但我不会为我工作

out_path = "/home/fraction/Desktop/extract_webcam_frames/frames"
path = out_path 
cv2.imwrite(os.path.join(path +  str(i)+'.jpg' , frame)
i += 1

如何使用opencv解决这个问题python

路径要写对

out_path = "/home/fraction/Desktop/extract_webcam_frames/frames"
frame_name = 'Frame'+str(i)+'.jpg'
cv2.imwrite(os.path.join(out_path, frame_name), frame)
i += 1

在这种情况下,您可以使用 f-string,它在 Python 3.6+ 中可用。

out_path = f"/home/fraction/Desktop/extract_webcam_frames/frames/Frame{i}.jpg"
cv2.imwrite(out_path, frame)
i += 1