Skip to main content

A library for drawing custom bounding boxes with rounded corners and zones.

Project description

ZDraw

ZDraw is a Python library for drawing custom bounding boxes with rounded corners on images using OpenCV. It supports dynamic adjustment of corner radius and border thickness, along with options for adding labels to the bounding boxes.

Features

  • Draws rounded rectangles on images.
  • Dynamically adjusts corner radius and border thickness based on box dimensions.
  • Add custom labels with solid background and rounded edges.
  • Assign random colors to classes for better visualization.

Installation

pip install zdraw

Usage Example

import cv2
from zdraw import ZDraw
# Initialize ZDraw with an optional class color map
# Initialize ZDraw
zdraw = ZDraw()

# Load an image
frame = cv2.imread("image.jpg")
frame = cv2.resize(frame, (800, 600), interpolation=cv2.INTER_AREA)
# Define bounding box coordinates and class
x1, y1, x2, y2 = 100, 150, 400, 300
class_name = "DynamicObject"

# Line Points
line_x1, line_y1, line_x2, line_y2 = 50, 50, 200, 50
line_points = [(line_x1, line_y1), (line_x2, line_y2)]

## Shape Points

# Triangle
tri_x1, tri_y1, tri_x2, tri_y2 = 250, 100, 350, 200
triangle_points = [(tri_x1, tri_y1), (tri_x2, tri_y1), (tri_x2, tri_y2)]

# Rectangle / Square
rect_x1, rect_y1, rect_x2, rect_y2 = 50, 300, 200, 400
rectangle_points = [(rect_x1, rect_y1), (rect_x2, rect_y1), (rect_x2, rect_y2), (rect_x1, rect_y2)]

# Polygon
poly_x1, poly_y1, poly_x2, poly_y2 = 250, 300, 350, 400
polygon_points = [
    (poly_x1, poly_y1),
    (poly_x2, poly_y1),
    (poly_x2, poly_y2),
    (poly_x1, poly_y2 - 50),
    (poly_x1, poly_y2 - 100),
]

frame_l = zdraw.ZDrawShape(frame.copy(), line_points, shape="line")
frame_t = zdraw.ZDrawShape(frame.copy(), triangle_points, shape="triangle")
frame_r = zdraw.ZDrawShape(frame.copy(), rectangle_points, shape="rectangle")
frame_p = zdraw.ZDrawShape(frame.copy(), polygon_points, shape="polygon")

# Draw bounding box with label
original_frame, modified_frame = zdraw.ZDrawRect(frame.copy(), x1, y1, x2, y2, class_name, return_original_frame=True)

# Display the result
cv2.imshow("Original Frame", original_frame)
cv2.imshow("Modified Frame BBox with Label", modified_frame)
cv2.imshow("Modified Frame  Line", frame_l)
cv2.imshow("Modified Frame Triangle", frame_t)
cv2.imshow("Modified Frame Rectangle", frame_r)
cv2.imshow("Modified Frame Polygon", frame_p)
cv2.waitKey(0)
cv2.destroyAllWindows()

API Reference

ZDraw

__init__(class_colors={})

  • Initializes the ZDraw object.

  • Parameters:

    • class_colors (dict): A dictionary mapping class names to RGB tuples.

get_color_for_class(class_name)

  • Returns a random color for a given class if not already assigned.

__draw_rec__(frame, top_left, bottom_right, border_color, fill_color)

  • Draws a rounded rectangle on the frame, private.

ZDrawRect(frame, x1, y1, x2, y2)

  • Draws a bounding box with rounded corners and a label.

ZDrawShape(frame, [point1, point2, ...])

  • Draws a shape for creating zones and lines on the image.

Dependencies

  • Python 3.8 or higher
  • OpenCV (opencv-python)
  • NumPy

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

zdraw-0.1.3.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

zdraw-0.1.3.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file zdraw-0.1.3.1.tar.gz.

File metadata

  • Download URL: zdraw-0.1.3.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for zdraw-0.1.3.1.tar.gz
Algorithm Hash digest
SHA256 c110646948618e447026cdc6d4c81db474aeb053b5a08788b0ec5cae78ce828e
MD5 67aeba4b400522a664bd4a74154aaa77
BLAKE2b-256 4c6a3cafa6b25d88bb7ccb56842abffc8f84595c1b4736ad3111875654b78f5e

See more details on using hashes here.

File details

Details for the file zdraw-0.1.3.1-py3-none-any.whl.

File metadata

  • Download URL: zdraw-0.1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for zdraw-0.1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 995bb47ff7f304d94adfcbeb035e1e382c3e9e2d61ca223eeb0004a27ba3986f
MD5 cb6894c695653642bdddf069c8cb101d
BLAKE2b-256 0bde29f8c7352752d188de4ec03a2dd38d2d7f9d65510ca9cec0d21a7e078a06

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