[Python] OpenCV ret=False, is it reading the video, or not?

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to detect edges of a video file (640x640 9sn.) and save the result. I followed OpenCV documents and some other examples. Most of the examples that I found was reading from camera.

    Here is my code. I checked cap.isOpened(), it returns True but ret does False and frame is NoneType object. What is confusing is that I'm having the gray array which depends the condition if ret == True. How can I get gray matrix if ret = False?

    (I installed ffmpeg pip install ffmpeg-python)

    (andy.avi was saved in folder but it's broke, empty)

    import cv2
    import numpy as np


    cap = cv2.VideoCapture("...\\video.mp4")

    while(cap.isOpened()):
    ret, frame = cap.read()

    frame_width = int(cap.get(3))
    frame_height = int(cap.get(4))

    size = (frame_width, frame_height)

    result = cv2.VideoWriter('andy.avi',
    cv2.VideoWriter_fourcc(*'DIVX'),
    30, size)

    if ret == True:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 45, 90)
    result.write(edges)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break
    else:
    break

    cap.release()
    result.release()
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página