Is there any method to extract still frames from a live video

Question:

I am working on a sign language project I just wanted to know how can I extract still frames from a live video. Is there any built-in method or i have to code any logic for it.

Asked By: user12075100

||

Answers:

OpenCV allows you receive a call back for each frame from the video source and you can analyse, modify and/or save that frame as you want.

A simple example using Python might be (based on tutorial here: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html)

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    # Here we just save the frame to a file
    cv2.imwrite('frame_'+str(i)+'.jpg',frame)
    i+=1

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

For a more complex example, this time in Android, the following code will save the frame to an image file if it detects a particular color (based on the OpenCV Color Blob detection example:

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();

        if (mIsColorSelected) {
            mDetector.process(mRgba);
            List<MatOfPoint> contours = mDetector.getContours();
            Log.e(TAG, "Contours count: " + contours.size());
            Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);

            Mat colorLabel = mRgba.submat(4, 68, 4, 68);
            colorLabel.setTo(mBlobColorRgba);

            Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
            mSpectrum.copyTo(spectrumLabel);

            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String testFilename = "TestOCVwrite.png";
            File testFile = new File(path, testFilename);
            String testFullfileName = testFile.toString();
            Log.i(TAG, "Writing to filename: " + testFilename);
            Boolean writeResult = Imgcodecs.imwrite(testFullfileName, mRgba);
            if (!writeResult) {
                Log.i(TAG, "Failed to write to filename: " + testFilename);
            }
        }

        return mRgba;
    }
Answered By: Mick
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.