Git Product home page Git Product logo

concurrent-videocapture's Introduction

concurrent-videocapture

Opencv VideoCapture that runs concurrently in a thread for faster processing

Installation

You can install concurrent_videocapture from pip using

pip install concurrent_videocapture

or using

pip install git+https://github.com/charlielito/concurrent-videocapture

Usage

It follows the same api than cv2.VideoCapture, so the change in code is minimal. The following example just opens the Camera 0 and shows the image in a cv2 window while simulating a heavy image processing function with time.sleep.

import cv2
import time
from concurrent_videocapture import ConcurrentVideoCapture
# Use it a the standard cv2.VideoCapture Class
cap = ConcurrentVideoCapture(0)
while True:
    init = time.time()
    grabbed, frame = cap.read()

    # Simulate heavy image processing function
    time.sleep(0.3)
    if not grabbed:
        break
    cv2.imshow("video", frame)
    key = cv2.waitKey(1)
    if key == 27:  # ESC
        break
    fps = 1/(time.time() - init)
    print('Fps: {}'.format(fps))
cap.release()

Additional parameters

Sometimes it is useful to perform some preprocessing to the image in the thread where the camera is read. For that we can use in the constructor the parameter transform_fn, which is a function that is applied to each frame. The flag return_rgb is for converting each frame to RGB since opencv by default returns the frames in BGR format. The next example will flip horizontally the image and return it as RGB:

import cv2
from concurrent_videocapture import ConcurrentVideoCapture

cap = ConcurrentVideoCapture(0, transform_fn=lambda image:cv2.flip(image, 1), return_rgb=True)
while True:
    grabbed, frame = cap.read()

    if not grabbed:
        break
    cv2.imshow("video", frame)
    key = cv2.waitKey(1)
    if key == 27:  # ESC
        break
cap.release()

concurrent-videocapture's People

Contributors

charlielito avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.