diff --git a/dayglo_detector/dayglo_detector.py b/dayglo_detector/dayglo_detector.py index 060df9f..7aba170 100644 --- a/dayglo_detector/dayglo_detector.py +++ b/dayglo_detector/dayglo_detector.py @@ -21,9 +21,7 @@ DISCOVERY_PREFIX = "homeassistant" last_rating = 0 # Default color thresholds for dayglo detection -# Fine-tune color thresholds for dayglo detection -# Fine-tune color thresholds for dayglo detection -LOWER_COLOR_GREEN = np.array([60, 100, 100]) +LOWER_COLOR_GREEN = np.array([40, 70, 70]) UPPER_COLOR_GREEN = np.array([90, 255, 255]) LOWER_COLOR_YELLOW = np.array([20, 100, 100]) UPPER_COLOR_YELLOW = np.array([35, 255, 255]) @@ -32,15 +30,18 @@ UPPER_COLOR_YELLOW = np.array([35, 255, 255]) initial_snapshot_processed = False def on_connect(client, userdata, flags, reasonCode, properties=None): - if reasonCode == 0: + try: + if reasonCode == 0: print("Connected successfully to MQTT broker") client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1) print(f"Subscribed to topic: {MQTT_SNAPSHOT_TOPIC}") publish_discovery_configurations() # Publish initial rating of 0 publish_rating(0) - else: - print(f"Failed to connect, return code {reasonCode}") + else: + print(f"Failed to connect, return code {reasonCode}") + except Exception as e: + print(f"Caught exception in on_connect: {e}") def publish_discovery_configurations(): rating_config = { @@ -108,14 +109,14 @@ def calculate_dayglo_rating(image): mask_yellow = cv2.inRange(hsv_image, LOWER_COLOR_YELLOW, UPPER_COLOR_YELLOW) mask = cv2.bitwise_or(mask_green, mask_yellow) dayglo_pixels = cv2.countNonZero(mask) - total_pixels = image.shape[0] * image.shape[1] + total_pixels = cropped_image.shape[0] * cropped_image.shape[1] rating = (dayglo_pixels / total_pixels) * 100 return rating def connect_mqtt(): while True: try: - client.connect(MQTT_BROKER, MQTT_PORT, 60) + client.connect(MQTT_BROKER, MQTT_PORT, keepalive=60) break except Exception as e: print(f"Connection failed: {e}. Retrying in 5 seconds...")