Skip to content

Instantly share code, notes, and snippets.

@JohnMcLear
Last active October 3, 2024 15:15
Show Gist options
  • Save JohnMcLear/573f7d0d101bdfb5eb3fafb3bd5c1c70 to your computer and use it in GitHub Desktop.
Save JohnMcLear/573f7d0d101bdfb5eb3fafb3bd5c1c70 to your computer and use it in GitHub Desktop.
Logic I use to get egg count, I didn't want to make a full plugin for this and wanted to use as much debuggable CLI logic as possible so it is portable.
# prereq = imagemagick - install using apk add --update imagemagick
# place this in configuration.xml then create an automation to do each activity in order with a delay of 10 seconds or so
# I like to use switches instead of shell_command but you could use shell_command if you want.
command_line:
# get the image from the camera, we don't use the snapshot here because snapshot doesn't provide all pixels
# your camera probably has a HTTP API for retrieving a snapshot so just look it up online and modify the below url
- switch:
name: get_nesting_box_chicken_coop
command_on: 'curl "http://camera1.lan/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=coopcctv&user=username&password=password" -k --output /config/www/tmp/snapshots/nestingbox.jpg'
# crop the section of the image, you will want to adjust these values to where the eggs should be..
# See imagemagick convert docs for how this works
- switch:
name: resize_nesting_box_chicken_coop
command_on: '/usr/bin/convert /config/www/tmp/snapshots/nestingbox.jpg -crop 850x500+2900+450 /config/www/tmp/snapshots/croppednestingbox.jpg'
# send the image to an image detection API and parse the response as a string into a text file.
- switch:
name: chicken_coop_egg_post_to_roboflow
command_on: 'base64 /config/www/tmp/snapshots/croppednestingbox.jpg | curl -s -d @- "https://detect.roboflow.com/egg-detection-final/3?api_key=CVMX1lqL0pF1OOmPLN0v" | python3 -c "import sys, json; print (len(json.load(sys.stdin)[\"predictions\"]))" > /config/www/tmp/snapshots/eggcount.txt'
# once the final step is run you have a text file that includes a count of eggs..
# Again, this is useful for debugging so don't hate me.
# Read this file using the below sensor
- sensor:
name: Egg Count from Text File
command: "cat /config/www/tmp/snapshots/eggcount.txt"
# If errors occur, make sure configuration file is encoded as UTF-8
unit_of_measurement: "eggs"
value_template: "{{ value | round(1) | int(0) -2 }}"
# note the -2 should be adjusted to however many fake eggs you have :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment