Developer Notes

Here, we dive into ColorDetect’s inner definitions and working. Found a bug or feature request you would like to address? Take a look at the Contribution guidelines and feel free to submit a pull. The project source is hosted on Github

Module ColorDetect

Defines ColorDetect class

For example:

>>> from colordetect import ColorDetect
>>> user_image = ColorDetect("<path_to_image>")
# where color_count is the target most dominant colors to be found. Default set to 5
>>> colors =  user_image.get_color_count(color_count=5)
>>> colors
# alternatively, save these RGB values to the image
>>> user_image.write_color_count()
>>> user_image.save_image("<storage_path>","<image_file_name>")
# Image processed and saved successfully
class colordetect.color_detect.ColorDetect(image, resize_h: Optional[int] = None)[source]

Bases: object

Detect and recognize the number of colors in an image

get_color_count(color_count: int = 5, color_format: str = 'human_readable') dict[source]

Count the number of different colors

color_count: int

The number of most dominant colors to be obtained from the image

color_format:str

The format to return the color in. Options

  • hsv - (60°,100%,100%)

  • rgb - rgb(255, 255, 0) for yellow

  • hex - #FFFF00 for yellow

  • human_readable - yellow for yellow

Returns

color description

get_segmented_image(lower_bound: tuple, upper_bound: tuple, erode_iterations: int = 3, dilate_iterations: int = 3, use_grab_cut: bool = True, gc_iterations: int = 3) tuple[source]

Get image masks from an image

lower_bound: tuple

A lower color range from which to look from

upper_bound: tuple

The higher RGB color range from which to look from

erode_iterations: int

The number of times to perform erosion of the image

dilate_iterations: int

The number of times dilation is applied.

use_grab_cut: bool

A boolean indicating whether grabCut will be applied to the image. This is True by default.

gc_iterations: int

Number of iterations the algorithm should make before returning the result

Returns

output_image, gray, segmented, mask

save_image(location: str = '.', file_name: str = 'out.jpg')[source]

Save the resultant image file to the local directory

location: str

The file location of the image

file_name:str

The name of the new image

write_color_count(left_margin: int = 10, top_margin: int = 20, font: int = 0, font_color: tuple = (0, 0, 0), font_scale: float = 1.0, font_thickness: float = 1, line_type: int = 1, save: bool = False)[source]

Write the number of colors found to the image

left_margin: int

Text spacing from the left

top_margin: int

Text spacing from the top

font: int

Font to use in text. Look up acceptable values from python-opencv

font_color:

RGB tuple of text font color

font_scale:

Size of the text to be written

font_thickness:

Thickness of the text

line_type: int = 1,

write_text(text: str = '', left_margin: int = 10, top_margin: int = 20, font: int = 0, font_color: tuple = (0, 0, 0), font_scale: float = 1.0, font_thickness: float = 1.0, line_type: int = 1, line_spacing: int = 0)[source]

Write text onto an image

Parameters

text: str

The text to be written onto the image

line_spacing:int

The spacing between lines

left_margin: int

Text spacing from the left

top_margin: int

Text spacing from the top

font: int

Font to use in text. Look up acceptable values from python-opencv

font_color:

RGB tuple of text font color

font_scale:

Size of the text to be written

font_thickness: float = 1.0

Thickness of the text

line_type: int = 1,

Space betweeen the lines

Returns

Module VideoColor

Defines VideoColor class

Usage:

>>> from colordetect import VideoColor
>>> user_video = VideoColor("<path_to_video>")
# where frame_color_count is the target most dominant colors to be found. Default set to 5
>>> colors =  user_video.get_video_frames(frame_color_count=7)
>>> colors
# alternatively shorten the dictionary to get a specific number of sorted colors from the whole lot
>>> from colordetect import col_share
>>> top_colors = col_share.sort_order(object_description=colors, key_count=8)
class colordetect.video_color_detect.VideoColor(video)[source]

Bases: object

Detect and recognize the number of colors in a video

get_time_frame_color(color_count: int = 5, color_format: str = 'rgb', time: int = 1000) tuple[source]

Get color from a specific time in the video

Parameters

time: int

Time to get color from in video in milliseconds

color_count: int

Number of colors to return at the given time frame

color_format:str

The format to return the color in. Options

  • hsv - (60°,100%,100%)

  • rgb - rgb(255, 255, 0) for yellow

  • hex - #FFFF00 for yellow

return

(image, color_description)

get_video_frames(frame_color_count: int = 5, color_format: str = 'rgb', progress: bool = False) dict[source]

Get image frames and their colors from the video

frame_color_count: int

The number of most dominant colors to be obtained from a single frame

color_format:str

The format to return the color in. Options

  • hsv - (60°,100%,100%)

  • rgb - rgb(255, 255, 0) for yellow

  • hex - #FFFF00 for yellow

return

color_description dictionary

Module ColShare

Global methods non-object specific

Usage:

>>> from colordetect import col_share
# show a progress bar for a process
>>> col_share.progress_bar("<current_process_position>", "<total_process_length>", "<process_description>")
# sort a dictionary by value to required length or in specific order
>>> col_share.sort_order('<dictionary>', "<items_to_return>", "<order>")
colordetect.col_share.is_url(url: str) bool[source]

Check if the string parsed is a URL

url: str

A string to be checked

colordetect.col_share.progress_bar(position: int, total_length: int, post_text: str = 'Color Detection')[source]

Display a progress bar of video processing

position: int

Current position of process

total_length: int

Total length of process

post_text: str

Text to display along with progress bar

colordetect.col_share.sort_order(object_description: dict, key_count: int = 5, ascending: bool = True)[source]

Sort items in a dictionary according to value

object_description: dict

A dictionary whose values need sorting

key_count: int

The number of items to return from the sort

ascending: bool

The order to perform the dictionary sort. By default, set to True.

Returns

A sorted dictionary with specific number