📜 ⬆️ ⬇️

Neuroculture part 2: about the bot that posts photos

image

The simplest working solution for reporting real-time events in a chicken coop. And a little more chatter about why you need to take on tasks and learning new things, even if you do not have enough knowledge.

Articles about neurocooler
Spoiler header
  1. Intro to learning about neural networks
  2. Iron, software and config for monitoring chickens
  3. A bot that posts events from the life of chickens - without a neural network
  4. Dataset markup
  5. A working model for the recognition of chickens in the hen house
  6. The result - a working bot that recognizes chickens in the hen house



')

Mom, messenger, bot, chickens


First, a little about the Python script from the previous article . The script has undergone changes, for example, we abandoned the contours completely, corrected some bugs related to the movement counter, which was ignored, and related to the time of the aplode. But all these things are simple and easily corrected if desired.


Earlier, I mentioned how I run this script. I did it with the help of perpetual ssh-sessions, but here’s the problem: I need to leave and take a laptop with me, for this time I can’t support an ssh-session. The obvious solution is to make the demon in the background, where it will work independently.


It turned out that simply sending the process to the background is not enough, when the session dies, the process also dies. In order to make a full-fledged daemon, the process must be untied from the terminal-player. There are several ways to do this, and here is a selection of methods and additional information.


The second problem was the virtual environment. I needed a python from a virtual environment to run the script. The solution turned out to be incredibly simple; it is enough just to set the path to the virtual environment when calling. As a result, to turn my process into a daemon, it was enough to run it like this:


/home/sshuser/.virtualenvs/cv/bin/python3 /home/sshuser/chickencoop/pi_surveillance_mod2.py --conf /home/sshuser/chickencoop/conf.json </dev/null &>/dev/null & 

Now, about the bot that spam my mom with photos of hens from a chicken coop in real time. There are no analytics and recognitions there yet, and the bot will just post photos on motion detection. And there is still a crookedly hanging camera, and solid tails are observed instead of chicken heads.


Bot inconspicuous and simple. Upon the file creation event on the server, its extension and home directory are checked, and if successful, the Python script is called, which makes a request to the messenger API (upload a photo to the channel) and writes logs to a plain text file (you can parse the response and add it to the database, but no need).


To make such a bot in Telegram, you need:


  1. Create your channel
  2. Create a bot in telegrams using BotFather (this is also a bot in the telegraph), you will need a bot token in order to post messages and photos.
  3. Make a bot script and demonize it.

With the first two points everything is simple, the third is also simple to boring given the fact that the telegram has a well-documented API and you need only one method.


To track the file creation event, I use a bash script, the event listens to inotify-tools. The send script is not called immediately, but after a 10-second wait. This is done because the file is loaded for some time after creation, and given the excellent speed of the Internet in the chicken coop, it has been empirically established that 10 seconds is the most.


 #!/bin/sh DIR="/your/directory" inotifywait -m -r -e create --format '%w%f' "$DIR" | while read f do PARDIR="$(dirname $f)" PARFOLD="$(basename $PARDIR)" FBASENAME="$(basename $f)" FILEEXT="${FBASENAME##*.}" EXT="jpg" FOLD="resized" if [ $FILEEXT = $EXT ]; then if [ $PARFOLD = $FOLD ]; then # sleep til file is uploaded sleep 10 python3 /your/directory/bot_request.py --params /your/directory/params_public.json -f $f fi fi done 

bot_request.py:


 import requests import json import datetime import argparse import warnings # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--params", required=True, help="params json. chat, token") ap.add_argument("-f", "--file", required=True, help="file path") args = vars(ap.parse_args()) # filter warnings, load the configuration and check if we are going to use server warnings.filterwarnings("ignore") params = json.load(open(args["params"])) # set url and post data payload = {"chat_id": params["chat"], "caption": "motion detected {ts}".format(ts=datetime.datetime.now().strftime("%A-%d-%B-%Y-%I:%M:%S%p"))} url = "https://api.telegram.org/bot" + params["token"] + "/sendPhoto" files = {'photo': open(args["file"], "rb")} # post request request = requests.post(url, files=files, data=payload) # log the output with open("/path/to/your/botlogs/{dt}-{chat}.txt".format(dt=datetime.datetime.now().strftime("%d-%B-%Y"), chat=params["type"]), "a") as logfile: logfile.write("\n" + request.text) 

params_public.json:


 { "chat": "@your_chat", "token": "your_tocken", "type": "public" } 

That is, in fact, everything, the most simple and working solution is ready.


image

Honestly and truthfully: why do I often find myself stupid / stupid

A bit of chatter. Someone thinks nagging - go ahead and do whatever you want. The main message of this part of the article is that if someone criticizes you, without offering in return at least a direction in which to look for an answer, do not be discouraged. No, you can make prototypes and solve problems, you can get experience without knowing everything and everyone. Man cannot know everything. I do not believe that there are people who are not mistaken, and by whom knowledge descends in the form of revelations from heaven.


image I don’t know who they are whispering to them there ...

I will briefly tell you what I think about this. It should be added that I am simply sharing my opinion, not imposing on anyone, not urging anyone to take it at face value.


To begin, perhaps, with the fact that I am not a developer. Maybe I'll be to them someday, who knows? Due to the fact that I am not a developer, I mostly use programming languages ​​as a tool for prototyping and solving problems for which there is no need to implement a robust system that goes through many iterations of development, support, fouling with any state tracking tool, reports and analytics and etc. In general, I rarely have to think about architecture. The simplest example: you need to clean a particular dataset. It's easier to do it in python, do it once and throw the code in the trash.


Therefore, when I write code, I always really don't want to put it in public. Because I know that I did something wrong, not according to the canon. And, most importantly, I always know that there will be criticism. There is at least one person who knows how to do it, but he will not propose a solution, will not give any key to understanding, he will simply say something from the category “You have everything wrong, everything does not work, you are a worthless person (and I D'Artagnan, but I will not say why, and I will not offer anything in return for your sloppy written article, because I do not want). ”


From my code, which actually got into a fight, it is possible to select only the stored procedures of the database and the system for sending analytical reports to the company by krone. It was written in php, and there was some sort of architecture.


Now, when the proud java of the falcon closed the article, php primates, and I among them, scratched their low sloping forehead (at this moment the article was also closed), I will continue.


It is necessary to read documentation, articles, books, but this will not help without solving real problems. And if you solve every problem according to the canon, you will never even make a working prototype. Seriously. Never.


Do not forget that all complex systems began with a simple. And when they introduced something else, they knew WHY they entered it. If you are trying to study a system that has already acquired such innovations so that complexity goes off scale, you often don’t know why it contains certain phenomena, processes, or functionality. These are black boxes, and at times their study resembles cramming worse than humanitarian subjects. And all because you do not know why. You do not see the logic.


If you are told that it is you who are stupid, you do not understand what's what, you have not understood this elementary region, everything is clear there (seers and prophets, as I call them), do not worry. You are not a prophet, you can’t know what people got into the head, who wrote this system, what crutches (a departure from the canons in the name of a working functional) they put in there and why. If there is also no documentation (adequate documentation, and not three words about the fact that there is such a method at best), as is often the case, then it is impossible to understand anything without going through buckets of slops that the prophets will pour on you in response for each of your questions (instead of useful information). I do not know why, but for some reason in IT it is a rule with rare exceptions.


The neurocooler is an example of a system that is born as a prototype and will never become a complete system and a complete product. This is just a hobby, learning on a real task, which has no monetized application. It will not scale.


I do not know Python. I just use it in this case, recognizing in passing. Similarly, I can’t write bash scripts. But if I take seriously the comments that I don’t need to write code, I don’t need to do anything like that, I will never learn anything.



Source: https://habr.com/ru/post/328940/


All Articles