dice_detection module
dice_detection.py
DiceDetection is a Python package for detecting and analyzing dice in images or video streams.
This module provides a DiceDetection class that encapsulates functionalities for dice detection, including blob detection, clustering, and result announcement using text-to-speech.
- Dependencies:
OpenCV (cv2)
NumPy (numpy)
scikit-learn (sklearn)
Calibration (calibration)
Configuration Settings (configs)
Google Text-to-Speech (gtts)
Pygame (pygame)
- Usage:
To use this package, create an instance of the DiceDetection class, calibrate the distance parameter using calibrate_distance, and then use the various methods provided for blob detection, result extraction, and display.
- Example:
Create an instance of DiceDetection
dice_detector = DiceDetection()
Calibrate the distance parameter
dice_detector.calibrate_distance(35)
Get blobs from a frame
frame = cv2.imread(“dice_image.jpg”) blobs = await dice_detector.get_blobs(frame)
Get dice information from blobs
dice_info, num_dice = await dice_detector.get_dice_from_blobs(blobs)
Overlay information on the frame
await dice_detector.overlay_info(frame, dice_info, blobs)
Stop detection when stable state is reached
sum_list, already_printed = await dice_detector.stop_detection(num_dice, sum_list, already_printed, frame)
Announce the result using text-to-speech
await dice_detector.announce_result(num_dice)
Display text on the frame
dice_detector.show_on_image(frame, “Dice detection in progress…”)
- class dice_detection.DiceDetection
Bases:
object
- DiceDetection is a Python package for detecting and
analyzing dice in images or video streams.
- Attributes:
detector: Instance of cv2.SimpleBlobDetector for blob detection. distance_parameter (float): Parameter used for DBSCAN clustering, calibrated based on camera distance.
- async announce_result(number: int) None
Announce the result using text-to-speech.
- Args:
number (int): Result to be announced.
- calibrate_distance(distance: float)
Calibrate the distance parameter after instantiation.
- Args:
distance (float): Distance between the camera and the white table. Range: 30-40 cm.
- async get_blobs(frame: ndarray) list
Detect blobs in the input frame.
- Args:
frame (numpy.ndarray): Input image frame.
- Returns:
list: List of detected blobs.
- async get_dice_from_blobs(blobs: list) -> (<class 'list'>, <class 'int'>)
Extract dice information from detected blobs.
- Args:
blobs (list): List of detected blobs.
- Returns:
tuple: A tuple containing a list of dice information and the number of dice.
- async overlay_info(frame: ndarray, dice: list, blobs: list)
Overlay information on the input frame.
- Args:
frame (numpy.ndarray): Input image frame. dice (list): List of dice information. blobs (list): List of detected blobs.
- show_on_image(frame: ndarray, text: str)
Display text on the input frame.
- Args:
frame (numpy.ndarray): Input image frame. text (str): Text to be displayed.
- async stop_detection(num_dice: int, sum_list: list, already_printed: bool, frame: ~numpy.ndarray, sum: int) -> (<class 'list'>, <class 'bool'>)
Stop dice detection when a stable state is reached.
- Args:
num_dice (int): Number of dice detected. sum_list (list): List to track the sum of dice over time. already_printed (bool): Flag to avoid redundant printing. frame (numpy.ndarray): Input image frame.
- Returns:
tuple: A tuple containing the updated sum_list and already_printed flags.