Python | Reproducir un video en modo inverso usando OpenCV

OpenCV (Open Source Computer Vision) es una biblioteca de visión por computadora que contiene varias funciones para realizar operaciones en imágenes o videos. 
 

OpenCV's application areas include :

1) Facial recognition system
2) motion tracking
3) Artificial neural network
4) Deep neural network
5) video streaming  etc.

 
En Python, se puede usar una biblioteca OpenCV llamada CV2 . Python no viene con cv2, por lo que el usuario debe instalarlo por separado. 
 

Para ventanas : 

pip install opencv-python

Para Linux: 

sudo apt-get install python-opencv

La biblioteca OpenCv se puede usar para realizar múltiples operaciones en videos. Intentemos hacer algo interesante usando CV2 . Tome un video como entrada y reprodúzcalo en modo inverso dividiendo el video cuadro por cuadro y almacenando simultáneamente ese cuadro en la lista. Después de obtener la lista de marcos, realizamos una iteración sobre los marcos. Para reproducir video en modo inverso, solo necesitamos iterar en reversa en la lista de cuadros. Utilice el método inverso de la lista para invertir el orden de los fotogramas en la lista.
A continuación se muestra la implementación: 
 

Python3

# Python program to play a video
# in reverse mode using opencv
 
# import cv2 library
import cv2
 
# videoCapture method of cv2 return video object
 
# Pass absolute address of video file
cap = cv2.VideoCapture("video_file_location")
 
# read method of video object will return
# a tuple with 1st element denotes whether
# the frame was read successfully or not,
# 2nd element is the actual frame.
 
# Grab the current frame.
check , vid = cap.read()
 
# counter variable for
# counting frames
counter = 0
 
# Initialize the value
# of check variable
check = True
 
frame_list = []
 
# If reached the end of the video
# then we got False value of check.
 
# keep looping until we
# got False value of check.
while(check == True):
     
    # imwrite method of cv2 saves the
    # image to the specified format.
    cv2.imwrite("frame%d.jpg" %counter , vid)
    check , vid = cap.read()
     
    # Add each frame in the list by
    # using append method of the List
    frame_list.append(vid)
     
    # increment the counter by 1
    counter += 1
 
# last value in the frame_list is None
# because when video reaches to the end
# then false value store in check variable
# and None value is store in vide variable.
 
# removing the last value from the
# frame_list by using pop method of List
frame_list.pop()
 
# looping in the List of frames.
for frame in frame_list:
     
    # show the frame.
    cv2.imshow("Frame" , frame)
     
    # waitkey method to stopping the frame
    # for some time. q key is presses,
    # stop the loop
    if cv2.waitKey(25) and 0xFF == ord("q"):
        break
     
# release method of video
# object clean the input video
cap.release()
 
# close any open windows
cv2.destroyAllWindows()
 
# reverse the order of the element
# present in the list by using
# reverse method of the List.
frame_list.reverse()
 
for frame in frame_list:
    cv2.imshow("Frame" , frame)
    if cv2.waitKey(25) and 0xFF == ord("q"):
        break
 
cap.release()
cv2.destroyAllWindows()

Producción : 
 

Publicación traducida automáticamente

Artículo escrito por ankthon y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *