A library for drawing custom bounding boxes with rounded corners and zones.
Project description
ZRect
ZRect 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
ZDrawobject. -
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zdraw-0.1.3.tar.gz.
File metadata
- Download URL: zdraw-0.1.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
654a6403b55976f45525e080825df1098ae900c5f387141f0b0f9bcebdf6c4a8
|
|
| MD5 |
a9c6cc70608b1a73f3f4a53c9176ecab
|
|
| BLAKE2b-256 |
4d925e6920933ecb07a544f655d05123af0891b42e027b4640596f624e19e03e
|
File details
Details for the file zdraw-0.1.3.0-py3-none-any.whl.
File metadata
- Download URL: zdraw-0.1.3.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56e05fe3823108f6af59d4b2589632a4f799fede42220557d804f92b91eee286
|
|
| MD5 |
9eccac39e8d8f6ec8bf213cad20b21bf
|
|
| BLAKE2b-256 |
65d443e956afebfb506b7ffb2b07a9f7938ed76072fafd45f8d8a141e438bc8e
|
File details
Details for the file zdraw-0.1.3-py3-none-any.whl.
File metadata
- Download URL: zdraw-0.1.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78f6a6c6c3f2843167a28ce389ef3b20e4283b1d8fe0efd7f05003dace9960cc
|
|
| MD5 |
1bfc92b98cd3d417846581cbb613ca8e
|
|
| BLAKE2b-256 |
39e72ddfaeac1282ebc950f5a54d226df616b45fc1dbdaae597e9d7ff0c1f40d
|