API documentation for Count-Cups developers.
Count-Cups provides a Python API for programmatic access to core functionality.
Configuration management using Pydantic settings.
Application settings class.
from app.core.config import settings
# Access settings
print(settings.app_name)
print(settings.detection_engine)
print(settings.default_theme)Key Attributes:
app_name: Application nameapp_version: Application versiondetection_engine: Detection engine (heuristics/mediapipe)default_theme: Default themecamera_index: Camera device index
Database operations and data persistence.
Database connection and operations.
from app.core.db import Database
db = Database()
# Get daily stats
stats = db.get_daily_stats(date.today())
# Add sip event
db.add_sip_event(sip_event)
# Get cup profiles
profiles = db.get_cup_profiles()Key Methods:
get_daily_stats(date): Get daily statisticsadd_sip_event(event): Add sip eventget_cup_profiles(): Get all cup profilescreate_cup_profile(profile): Create cup profile
Data models and business entities.
Cup profile model.
from app.core.models import CupProfile
profile = CupProfile(
name="Coffee Mug",
size_ml=250,
sips_per_cup=10
)Sip event model.
from app.core.models import SipEvent, EventSource
from datetime import datetime
event = SipEvent(
timestamp=datetime.now(),
profile_id=1,
ml_estimate=25.0,
source=EventSource.AUTO
)Daily statistics model.
from app.core.models import DailyStats
stats = DailyStats(
date=date.today(),
total_ml=1500.0,
total_cups=6.0,
total_sips=60
)Sip tracking and aggregation logic.
Main sip tracking coordinator.
from app.core.sip_logic import SipTracker
tracker = SipTracker()
# Process detection
tracker.process_detection(detection_result)
# Add manual sip
tracker.add_manual_sip(profile_id=1)
# Get daily stats
stats = tracker.get_daily_stats()Key Methods:
process_detection(detection): Process detection resultadd_manual_sip(profile_id): Add manual sip entryget_daily_stats(): Get daily statisticsget_weekly_stats(): Get weekly statistics
Aggregates detection results into sip events.
from app.core.sip_logic import SipAggregator
aggregator = SipAggregator()
result = aggregator.process_detection(detection)Converts between sips, milliliters, and cups.
from app.core.sip_logic import CupConverter
converter = CupConverter()
cups = converter.sips_to_cups(sips=10, profile_id=1)
ml = converter.estimate_sip_ml(profile_id=1)Detection engine interfaces.
Base class for detection engines.
from app.core.detection.base import DetectionEngine
class CustomDetector(DetectionEngine):
def detect(self, frame):
# Custom detection logic
return detection_resultHeuristic-based detection engine.
from app.core.detection.heuristics import AdvancedHeuristicDetector
detector = AdvancedHeuristicDetector()
result = detector.detect(frame)MediaPipe-based detection engine.
from app.core.detection.mediapipe_impl import AdvancedMediaPipeDetector
detector = AdvancedMediaPipeDetector()
result = detector.detect(frame)Data export functionality.
Export data to various formats.
from app.core.exporter import Exporter
exporter = Exporter()
# Export to CSV
exporter.export_to_csv(output_path="data.csv")
# Export to JSON
exporter.export_to_json(output_path="data.json")from app.core.sip_logic import SipTracker
from app.core.db import Database
# Initialize
tracker = SipTracker()
db = Database()
# Process detection
detection = get_detection_from_camera()
tracker.process_detection(detection)
# Get stats
stats = tracker.get_daily_stats()
print(f"Today's intake: {stats.total_ml}ml")from app.core.detection.base import DetectionEngine
from app.core.models import DetectionResult
class MyDetector(DetectionEngine):
def detect(self, frame):
# Custom detection logic
return DetectionResult(
has_sip=True,
confidence=0.85,
face_detected=True,
hand_detected=True
)from app.core.exporter import Exporter
from datetime import date, timedelta
exporter = Exporter()
# Export last 7 days
start_date = date.today() - timedelta(days=7)
end_date = date.today()
exporter.export_to_csv(
output_path="weekly_data.csv",
start_date=start_date,
end_date=end_date
)from app.core.db import Database, DatabaseError
try:
db = Database()
stats = db.get_daily_stats(date.today())
except DatabaseError as e:
print(f"Database error: {e}")from app.core.detection.base import DetectionError
try:
result = detector.detect(frame)
except DetectionError as e:
print(f"Detection error: {e}")All functions and classes use type hints for better IDE support and documentation.
from typing import Optional, List
from datetime import date
from app.core.models import SipEvent, DailyStats
def get_events(date: date) -> List[SipEvent]:
"""Get events for a specific date."""
# Implementation
passUse the logging module for application logging.
from app.core.logging import get_logger
logger = get_logger(__name__)
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")Access configuration through the settings object.
from app.core.config import settings
# Read settings
engine = settings.detection_engine
theme = settings.default_theme
# Update settings (runtime)
settings.detection_engine = "mediapipe"- Architecture: See Architecture for system design
- Examples: Check Examples for code samples
- Development: See CONTRIBUTING.md for development guidelines
API Reference complete! Use the API to extend Count-Cups functionality.