Skip to main content

opencv_wrap

A collection of decorators for opencv and helper functions for multiple opencv tasks.

Working with opencv can be quite a hussel, a lot of boiler code, nested functions for specific use cases, this package is designed to make it easier to work with opencv, while focusing on the main task in hand. best for prototyping and quick testing. second part is speed and performance, this package is designed to be fast and efficient.


Built with ☕ by [@rishi23root](https://github.com/rishi23root/)

rishi23root/opencv_wrap/

GitHub stars PyPI GitHub PyPI - Python Version Say Thanks!

Installation

pip install opencv-wrap

Very basic example of reading camera feed and displaying it. with just 5 lines of code. 😎

from opencv_wrap import cv2Decorator

@cv2Decorator.TotalTimeTaken(show=True)
@cv2Decorator.AccessCamOrVideo(show=True)
@cv2Decorator.CalculateFps(draw=True)
def all_actions(**kwargs):
    return kwargs

all_actions()

Advance example of face detection and smart viewer. with just 23 lines of code. 😊

from opencv_wrap import cv2Decorator
import cv2
from opencv_wrap.detectors import Face
from opencv_wrap.utils.helper import show_all_frames, clipImage

@cv2Decorator.DetectInEachFrame(detector=Face(verbose=True),name="face")
@cv2Decorator.TotalTimeTaken(show=True)
@cv2Decorator.AccessCamOrVideo(show=False,videoPath="./opencv_wrap/testMedia/test.mp4")  # path to video
@cv2Decorator.CalculateFps(draw=False)
@cv2Decorator.MirrorFrame()
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_RGB2BGR, frameName="bgr_frame")
def all_actions(**kwargs):
    mainFrameCopy = kwargs["frame"].copy()
    processed = kwargs["face"].detect(kwargs["bgr_frame"])
    face_coordinate = kwargs["face"].getDetectionBox(
        processed, kwargs["frame"], draw=True
    )
    kwargs["face"].getLandmarks(processed, kwargs["frame"], draw=True)
    kwargs["detected"] = [clipImage(mainFrameCopy, i) for i in face_coordinate]
    show_all_frames(kwargs, keysToShow=["frame", "detected"])
    return kwargs

all_actions()
image

image


Features with decorators

from opencv_wrap import cv2Decorator

@cv2Decorator.TotalTimeTaken(show=True)
...
  • TotalTimeTaken

  • CalculateFps

  • MirrorFrame

  • ConvertCOLOR

  • AccessCamOrVideo

  • DetectInEachFrame

Utils to help you with opencv tasks

from opencv_wrap.utils import DetectorClass
from opencv_wrap.utils.helper import detectionBox

Detector Parent

DetectorClass is a base class for all the detectors. provide some basic functions like Singleton and isVerbose.

Helper functions

  • saveFrames

  • detectionBox

  • detectionBox

  • resizeImage

  • clipImage

  • added_title

  • combine_images

Detection Classes

from opencv_wrap.detectors import Face , Hand, Pose
  • Face detection

  • Hand detection

  • Pose detection

  • eye detection (yet to be added)

you can reconstruct the detector classes as per your need. 😊

like extend the class and add more functions to it. like action of certain detections.

example, blur everything but face. can be useful when you want to hide the background and just fucus on the object, here Face.

import cv2
from opencv_wrap import cv2Decorator
from opencv_wrap.detectors import Face

class FaceExtented(Face):
    def blurEverytingButFace(self, frame, face_coordinate):
        # make a copy of the frame
        frameCopy = frame.copy()
        frame = cv2.blur(frame, (50,50))
        for (x, y, w, h) in face_coordinate:
            frame[y : y + h, x : x + w] = frameCopy[y : y + h, x : x + w]
        return frame

@cv2Decorator.DetectInEachFrame(detector=FaceExtented(verbose=True),name="face")
@cv2Decorator.AccessCamOrVideo(show=True,videoPath="./opencv_wrap/testMedia/test.mp4")
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_RGB2BGR, frameName="bgr_frame")
def all_actions(\*\*kwargs):
    processed = kwargs["face"].detect(kwargs["bgr_frame"])
    face_coordinate = kwargs["face"].getDetectionBox(
    processed, kwargs["frame"], draw=False,padding_ratio=0.4)
    kwargs["frame"] = kwargs["face"].blurEverytingButFace(kwargs["frame"], face_coordinate)
    return kwargs

all_actions()
image

image


OPEN FOR CONTRIBUTIONS 🤝

Steps to start contributing

  1. Star the repo 🌟

  2. Fork the repo 👨‍💻

  3. Clone the repo 📂

  4. Create a new issue 🔖

  5. Make changes 📜

  6. Push the changes 🚀

  7. Create a pull request 🌐


More Usage Examples

Example 1 : Reading a single frame from the directory

@cv2Decorator.DetectInEachFrame(
    detector=cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_default.xml"),
    name='face')
@cv2Decorator.MirrorFrame()
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_BGR2GRAY)
def all_actions(**kwargs):
    frame = kwargs['frame']
    # detect face from trainerd data and detectMultiScale use to deteat every size of face
    face_coordinate = kwargs['face'].detectMultiScale(kwargs['greyScale'],1.3,5)
    detectionBox(detectedArr=face_coordinate, frame=frame)
    return kwargs

frame = cv2.imread('./opencv_wrap/testMedia/test.jpg')

kwargs = all_actions(frame=frame)
cv2.imshow('frame',kwargs['frame'])
key = cv2.waitKey(0)

Example 2 : Reading cam and detecting Hand in each frame

@cv2Decorator.DetectInEachFrame(
    detector=Hand(verbose=True),
    name="hand",
)
@cv2Decorator.TotalTimeTaken(show=True)
@cv2Decorator.AccessCamOrVideo(show=False, fps=12)
@cv2Decorator.CalculateFps(draw=True)
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_RGB2BGR, frameName="bgr_frame")
def all_actions(**kwargs):
    mainFrameCopy = kwargs["frame"].copy()
    processed = kwargs["hand"].detect(kwargs["bgr_frame"])
    face_coordinate = kwargs["hand"].getDetectionBox(
        processed, kwargs["frame"], draw=True
    )
    kwargs["hand"].getLandmarks(processed, kwargs["frame"],draw=True)
    # print(len(face_coordinate))
    kwargs["detected"] = [clipImage(mainFrameCopy, i) for i in face_coordinate]
    show_all_frames(kwargs, keysToShow=["frame", "detected"])
    return kwargs


kwargs = all_actions()
image

image

Example 3 : Reading video and detecting Pose in each frame

@cv2Decorator.DetectInEachFrame(
    detector=Pose(verbose=True),
    name="pose",
)
@cv2Decorator.TotalTimeTaken(show=True)
@cv2Decorator.AccessCamOrVideo(show=False, videoPath="./opencv_wrap/testMedia/test.mp4", fps=12)
@cv2Decorator.CalculateFps(draw=True)
@cv2Decorator.MirrorFrame()
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_BGR2GRAY)
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_RGB2BGR, frameName="bgr_frame")
def all_actions(**kwargs):
    mainFrameCopy = kwargs["frame"].copy()
    processed = kwargs["pose"].detect(kwargs["bgr_frame"])
    face_coordinate = kwargs["pose"].getDetectionBox(
        processed, kwargs["frame"], draw=True
    )
    kwargs["pose"].getLandmarks(processed, kwargs["frame"],draw=True)

    kwargs["detected"] = [clipImage(mainFrameCopy, i) for i in face_coordinate]
    show_all_frames(kwargs, keysToShow=["frame", "detected"])
    return kwargs


all_actions()
image

image

Example 4 : Reading video and saving each frame in a folder

from opencv_wrap import cv2Decorator
from opencv_wrap.utils.helper import saveFrame

@cv2Decorator.AccessCamOrVideo(show=True, videoPath="./opencv_wrap/testMedia/test.mp4", )
def all_actions(**kwargs):
    saveFrame(kwargs['frame'],kwargs['frame_count'],destination='./output')
    return kwargs

all_actions()
image

image

Example 5 : Reading video and show converted frame in smart view

@cv2Decorator.TotalTimeTaken(show=True)
@cv2Decorator.AccessCamOrVideo(show=False, videoPath="./opencv_wrap/testMedia/test.mp4", fps=12)
@cv2Decorator.CalculateFps(draw=True)
@cv2Decorator.MirrorFrame()
@cv2Decorator.ConvertCOLOR(converter=cv2.COLOR_BGR2GRAY)
def all_actions(**kwargs):
    show_all_frames(kwargs,keysToShow=['frame','greyScale','mirror_frame'])
    return kwargs

all_actions()

image

Future Updates

  • ☐ Face recognition

  • ☐ Eye detection

  • ☐ Object detection

  • ☐ Image classification

  • ☐ segmentation (decorator)

  • ☐ making whole program faster by atleast 10x using cython

Metadata

Release files for opencv-wrap 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opencv-wrap 0.1.2
File Size Uploaded Provenance Actions
opencv_wrap-0.1.2.tar.gz 18.1 kB Without provenance Details for opencv_wrap-0.1.2.tar.gz

Release files / opencv_wrap-0.1.2.tar.gz

Download URL opencv_wrap-0.1.2.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
59c4bad4ad1dc5d33f6120ec8191fd945ba3c1b040fb4edc5b842c59766b4821
BLAKE2b-256 checksum
How to use checksums
db0b11c465b6a6d5f15acebc879743408c2a34305133f2fd34f0fd4f34eb1aab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Security for opencv-wrap 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page