Skip to main content

Library to help the drawing process with OpenCV. Thought to add labels to the images. Classification of images, etc.

Project description

cv2_tools

Library to help the drawing process with OpenCV. Thought to add labels to the images. Classification of images, etc.

image

Image generated with Intel Openvino Toolkit and drawed with opencv-draw-tools v0.1.9

Installation

Pre-requisites

You will need to install:

  • opencv >= 3.6.2
  • numpy >= 1.13.3
  • python-constraint >= 1.4.0

You can simply execute: pip install -r requirements.txt

Finally you can install the library with:

pip install opencv-draw-tools-fernaperg

When you install opencv-draw-tools, it will automatically download numpy but not opencv becouse in some cases you will need another version.

Usage

Test

import cv2_tools

print('Name: {}\nVersion:{}\nHelp:{}'.format(cv2_tools.name,cv2_tools.__version__,cv2_tools.help))
webcam_test()

Oriented Object Programming method

Firstly create a SelectorCV2 object. You can pass it optional parameters to configure the output.

from cv2_tools.Selection import SelectorCV2
selector = SelectorCV2(color=(200,90,0), filled=True)

Also you can configure it later using the method (all optional parameters):

selector.set_properties()

Now, each time you want to add a selected zone call the method:

"""
Coordinates:

(x1,y1)____(x2,y1)
   |          |
   |          |
(x1,y2)____(x2,y2)

Tags (optional parameter):
* It could be a normal string
* A string with '\n'
* A list of strings
* None / '' / [] / False
"""
selector.add_zone((x1,y1,x2,y2),tags=tag)

Finally, when you want to draw all the rectangles execute:

edited_frame = selector.draw(frame)

If you want to use the same object multiple times you can easily change the content inside it:

# This method could help change rectangles to
selector.set_range_valid_rectangles( origin, destination)

# This method could help if you know exactly the indexes that you want to keep
# Default = [], so if you just want to clean the buffer call this method without parameters
set_valid_rectangles(indexes)

If you want, you can see the example detect_faces.py, it also use an open source library called face_recognition.

Manual method

import opencv_draw_tools as cv2_tools


"""
  Draw better rectangles to select zones.
  Keyword arguments:
  frame -- opencv frame object where you want to draw
  position -- touple with 4 elements (x1, y1, x2, y2)
              This elements must be between 0 and 1 in case it is normalized
              or between 0 and frame height/width.
  tags -- list of strings/tags you want to associate to the selected zone (default [])
  tag_position -- position where you want to add the tags, relatively to the selected zone (default None)
                  If None provided it will auto select the zone where it fits better:
                      - First try to put the text on the Bottom Rigth corner
                      - If it doesn't fit, try to put the text on the Bottom Left corner
                      - If it doesn't fit, try to put the text Inside the rectangle
                      - Finally if it doesn't fit, try to put the text On top of the rectangle
  alpha -- transparency of the selected zone on the image (default 0.9)
           1 means totally visible and 0 totally invisible
  color -- color of the selected zone, touple with 3 elements BGR (default (110,70,45) -> dark blue)
           BGR = Blue - Green - Red
  normalized -- boolean parameter, if True, position provided normalized (between 0 and 1) else you should provide concrete values (default False)
  thickness -- thickness of the drawing in pixels (default 2)
  filled -- boolean parameter, if True, will draw a filled rectangle with one-third opacity compared to the rectangle (default False)
  peephole -- boolean parameter, if True, also draw additional effect, so it looks like a peephole
"""
frame = cv2_tools.select_zone(frame, position, tags=[])

Example with Webcam

import opencv_draw_tools as cv2_tools
cv2_tools.webcam_test()

See webcam_test() code:

def webcam_test():
    """Reproduce Webcam in real time with a selected zone."""
    print('Launching webcam test')
    cap = cv2.VideoCapture(0)
    f_width = cap.get(3)
    f_height = cap.get(4)
    window_name = 'opencv_draw_tools'
    while True:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        if ret:
            keystroke = cv2.waitKey(1)
            position = (0.33,0.2,0.66,0.8)
            tags = ['MIT License', '(C) Copyright\n    Fernando\n    Perez\n    Gutierrez']
            frame = select_zone(frame, position, tags=tags, color=(130,58,14), thickness=2, filled=True, normalized=True)
            cv2.imshow(window_name, frame)
            # True if escape 'esc' is pressed
            if keystroke == 27:
                break
    cv2.destroyAllWindows()
    cv2.VideoCapture(0).release()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cv2_tools-1.0.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cv2_tools-1.0.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file cv2_tools-1.0.1.tar.gz.

File metadata

  • Download URL: cv2_tools-1.0.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for cv2_tools-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f60fec56e3acb9c2de5e44ef47dbbabc9ec913496b77e1ada79d2e5c095a0f0c
MD5 2f33c6d0a5f4195813748a71c121c908
BLAKE2b-256 42e39b2ed6307b51b1bccb398b9d44425020006f098e0fb7c5d9bcd825f46958

See more details on using hashes here.

File details

Details for the file cv2_tools-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cv2_tools-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for cv2_tools-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8aa28e922a8f392aec42c32403914efbcd24259ccf5f964ed8b230b1e951ba47
MD5 3ec1b453e570eb73d823b2954bb9fe52
BLAKE2b-256 c745ba531c59a32e74662490d922110b00264fb3eb969eac5d9ff736aa846dd6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page