added docs and silght refactor

This commit is contained in:
Askill 2020-06-03 11:29:24 +02:00
parent d4c5639113
commit 38d3cb3694
11 changed files with 44 additions and 30 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ __pycache__/
.vscode/ .vscode/
config copy.py config copy.py
app/imgs/

View File

@ -6,20 +6,28 @@ It can be enabled/disabled via chat commands ("/start", "/stop")
- video feed - video feed
- server - server
- telegram bot - telegram bot
- tls endpoint - tls endpoint (optional)
#### Quick Start
#### Start - insert bot-token + chat-key into config.py
- rename img to imgs
- rename data1.json to data.json
- insert token + key into data.json
- change config.py to your requirements - change config.py to your requirements
- pip install -r requirements.txt - pip install -r requirements.txt
- python app.py - python app.py
#### Docker #### Docker Deployment
docker build -t survbot . docker build -t survbot .
docker run -d --rm -p 80:5003 survbot docker run -d --rm -p 80:5003 survbot
#### Result: #### Result:
![](1.png) ![](images/1.png)
#### How it works
The image is scaled down and blured, then the difference to an earlier frame is calculated.
The resulting frame is binarized. Then all contours are marked if they contain a number of changed pixels between the upper and lower bound specified in config.py.
Those contours are then marked in the original image, which is then saved in the filesystem and sent to the specified telegram bot.
![](images/2.png)

View File

View File

@ -4,25 +4,18 @@ import config
import time import time
import cv2 import cv2
login = config.loginPath
loginDataJson = {}
config.token
config.chat_id
def saveImage(img): def saveImage(img):
name = '{}'.format(int(time.time())) name = '{}'.format(time.time()).split(".")[0]
path = config.photos + name + '.png' path = config.photos + name + '.png'
cv2.imwrite(path, img) cv2.imwrite(path, img)
return path return path
def notify(path): def notify(path):
global loginDataJson
photo = open(path, "rb") photo = open(path, "rb")
json1 = {"chat_id": config.chat_id} json1 = {"chat_id": config.chat_id}
files = {'photo': photo} files = {'photo': photo}
print(requests.post("https://api.telegram.org/bot" + config.token + "/sendPhoto", json1, files=files)) print(requests.post("https://api.telegram.org/bot" + config.token + "/sendPhoto", json1, files=files))
def initEndpoint(): def initEndpoint():
tp = "http://api.telegram.org/bot" + config.token + "/setWebHook?url=" + config.endpoint tp = "http://api.telegram.org/bot" + config.token + "/setWebHook?url=" + config.endpoint
requests.get(tp) requests.get(tp)

14
app/config.py Normal file
View File

@ -0,0 +1,14 @@
monitor = True
stream = "http://192.168.178.56:8080/video"
photos = "./imgs/"
min_area = 3000
max_area = 10000
threashold = 18
delay = .3
framerate = 30
endpoint = ""
token = ""
chat_id = ""

View File

@ -9,17 +9,19 @@ import config
import traceback import traceback
import _thread import _thread
def compare(): def compare():
try: try:
url = config.stream url = config.stream
min_area = 3000 min_area = config.min_area
max_area = 10000 max_area = config.max_area
counter = 0 counter = 0
threashold = 18 threashold = config.threashold
delay = .3 delay = config.delay
framerate = 30 framerate = config.framerate
# initialize the first frame in the video stream # initialize the first frame in the video stream
vs = cv2.VideoCapture(url) vs = cv2.VideoCapture(url)
@ -39,6 +41,9 @@ def compare():
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (31, 31), 0) gray = cv2.GaussianBlur(gray, (31, 31), 0)
cv2.imshow("frame", frame)
cv2.imshow("gray", gray)
# if the first frame is None, initialize it # if the first frame is None, initialize it
if firstFrame is None: if firstFrame is None:
firstFrame = gray firstFrame = gray
@ -69,8 +74,8 @@ def compare():
img = frame img = frame
location = com.saveImage(img) location = com.saveImage(img)
_thread.start_new_thread(com.notify, (location, ) ) _thread.start_new_thread(com.notify, (location, ) )
#com.notify(location)
# get new image to compare to every so often to avoid changes over time ie. sun
counter+=1 counter+=1
if counter % (framerate * delay) == 0: if counter % (framerate * delay) == 0:
firstFrame = gray firstFrame = gray

View File

@ -1,8 +0,0 @@
monitor = True
loginPath = "./data.json"
stream = "http://192.168.178.56:8080/video"
photos = "./imgs/"
endpoint = "https://telegram-bot.jopa.dev"
token = ""
chat_id = ""

View File

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 133 KiB

BIN
images/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB