Showing posts with label #frames. Show all posts
Showing posts with label #frames. Show all posts

Friday, December 20, 2019

Reading Image frame by frame from Saved videoes or Camera Using opencv Python

One of my friend was asking about reading image frames from videoes, so I thought a quick block may be very helpful for beginners. It is actually very easy, just follow the bellow steps:


Step 1. Installations



a. Install python




If you still do not have python in your system , please install python


For linux: sudo apt-get update $ sudo apt-get install python3.6

For windows: download the installation file from python website and follow the instructions



b. Install Opencv



For linux:

sudo pip3 install opencv-python
 
For Windows:

pip3 install opencv-python






Step2: Reading saved or camera video



- At first need to import the opencv



- Read the video either from camera or saved video


- While frame is available show the frame and save it. Finally release the camera and window which we used to show.


Hope you liked this post. I am posting the script below so that you can just copy paste. leave your feedback below.

import cv2

#if reading from saved video, need to specify where the file is saved
#cap = cv2.VideoCapture('D:\project\spoof\classification\test_video\test.avi')

#if reading from camera, camera id is 0 here
cap = cv2.VideoCapture(0)

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

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    cv2.imwrite('savedImage.jpg', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()