I want to print the rgb values of frames of a video in python

Viewed 36

I want to print the RGB values of frames of a video in python. I tried writing a code in python but the for loop where we print the newframe goes into an infinite loop. Guide me on how shall I overcome this, as an output it should print the RGB value of each pixel. I have attached the code below.

import numpy
import numpy as np
import cv2 as cv

count=0
def messageToBinary(message):
  if type(message) == str:
    return ''.join([ format(ord(i), "08b") for i in message ])
  elif type(message) == bytes or type(message) == np.ndarray:
    return [ format(i, "08b") for i in message ]
  elif type(message) == int or type(message) == np.uint8:
    return format(message, "08b")
  else:
    raise TypeError("Input type not supported")



vidcap = cv.VideoCapture("video.mp4")
if not vidcap.isOpened():
    print("Cannot open")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = vidcap.read()
     # -------------------------------------------------------------> step 2 - split
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv.imshow('frame', gray)
    width, height, d= frame.shape
    print("reshaped...")
    row=int(width*height)
    newframe = frame.reshape(row,3)

    # print(newframe)
    # print(newframe[i][j])
    #the loop below is going into infinite looping  
    for i in range(0,row):
        for j in range(0,3):
                print(newframe[i][j])


    if cv.waitKey(1) == ord('q'):
        break


# When everything is done, release the capture

vidcap.release()
cv.destroyAllWindows()
0 Answers
Related