Fix MQTT connection loop and enhance dayglo color detection

- Wrapped on_connect with a try-except block to prevent connection loop.
- Updated color thresholds for dayglo green and yellow to improve detection accuracy.
- Adjusted pixel calculation to focus on the cropped image area for better analysis.
This commit is contained in:
2024-10-20 18:33:24 +11:00
parent 9297c3cf12
commit 3b7cc953d6
+6 -5
View File
@@ -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,6 +30,7 @@ UPPER_COLOR_YELLOW = np.array([35, 255, 255])
initial_snapshot_processed = False
def on_connect(client, userdata, flags, reasonCode, properties=None):
try:
if reasonCode == 0:
print("Connected successfully to MQTT broker")
client.subscribe(MQTT_SNAPSHOT_TOPIC, qos=1)
@@ -41,6 +40,8 @@ def on_connect(client, userdata, flags, reasonCode, properties=None):
publish_rating(0)
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...")