Code Monkey home page Code Monkey logo

opencv_wrap's Introduction

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

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


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


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

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

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

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

opencv_wrap's People

Contributors

rishi23root avatar

Stargazers

anuj avatar  avatar Roman avatar

Watchers

 avatar anuj avatar

opencv_wrap's Issues

add multi POSE detection

for multi-pose detection, mediapipe doesn't help, therefore using object here human body to get detected frames and then using those roi frames using pose detection individually to get the desired results

updates for base lib

cv2 decorator base

  • fix returning data from all of the function
  • introduce new util to detect on a single frame and use that in cam, video etc

cv2 decorator utils

  • face center
  • crop image
  • Create a bounding box for the objects
  • a representer function that will take all the data the user wants to see while working on some project
    • show data in different labels or sections

for testing and showcasing

  • take videos from dir
  • take frames from the dir
  • take cam feed

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.