This commit is contained in:
Askill 2019-11-16 15:46:38 +01:00
parent 3f3b2b2ec5
commit fe3f9f177b
4 changed files with 77 additions and 32 deletions

38
app.py Normal file
View File

@ -0,0 +1,38 @@
from flask import Flask, request
import requests
import motion_detector as md
import com
from telegram.ext import Updater, CommandHandler
import _thread
import config
import telebot
app = Flask(__name__)
bot = telebot.TeleBot(config.token)
@bot.message_handler(commands=['start'])
def start(message):
config.monitor = True
print("start")
@bot.message_handler(commands=['stop'])
def stop(message):
config.monitor = False
print("stop")
# Process webhook calls
@app.route("/", methods=['POST'])
def webhook():
if request.headers.get('content-type') == 'application/json':
json_string = request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
return "", 403
if __name__ == '__main__':
com.initEndpoint()
_thread.start_new_thread(md.compare, () )
app.run(host='0.0.0.0', port=5003, threaded=True, debug=False)

View File

@ -1,25 +1,17 @@
import requests import requests
import json import json
import telebot import config
import numpy as np import time
import cv2 import cv2
import time login = config.loginPath
login = "./data.json"
loginDataJson = {} loginDataJson = {}
config.token
photos = "./imgs/" config.chat_id
url = "http://192.168.178.25:8000/stream.mjpg"
def checkCam():
cap = cv2.VideoCapture(url)
r, img = cap.read()
return img
def saveImage(img): def saveImage(img):
name = '{0:010x}'.format(int(time.time() * 256))[:10] name = '{}'.format(int(time.time()))
path = photos + name + '.png' path = config.photos + name + '.png'
cv2.imwrite(path, img) cv2.imwrite(path, img)
return path return path
@ -35,6 +27,14 @@ def loadLogin():
with open(login, 'r', encoding='utf-8') as f: with open(login, 'r', encoding='utf-8') as f:
loginData = f.read() loginData = f.read()
loginDataJson = json.loads(loginData) loginDataJson = json.loads(loginData)
config.token = loginDataJson["key"]
config.chat_id = loginDataJson["chat_id"]
def initEndpoint():
loadLogin() loadLogin()
tp = "http://api.telegram.org/bot" + config.token + "/setWebHook?url=" + config.endpoint
requests.get(tp)
print("registered:", tp)

8
config.py Normal file
View File

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

View File

@ -9,8 +9,8 @@ import datetime
import imutils import imutils
import time import time
import cv2 import cv2
import mon import com
import config
def increase_brightness(img, value=30): def increase_brightness(img, value=30):
@ -26,7 +26,8 @@ def increase_brightness(img, value=30):
return img return img
def compare(url): def compare():
url = config.stream
# construct the argument parser and parse the arguments # construct the argument parser and parse the arguments
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file") ap.add_argument("-v", "--video", help="path to the video file")
@ -93,8 +94,6 @@ def compare(url):
# compute the bounding box for the contour, draw it on the frame, # compute the bounding box for the contour, draw it on the frame,
# and update the text # and update the text
print(cv2.contourArea(c))
(x, y, w, h) = cv2.boundingRect(c) (x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Occupied" text = "Occupied"
@ -102,26 +101,26 @@ def compare(url):
# draw the text and timestamp on the frame # draw the text and timestamp on the frame
cv2.putText(frame, "Room Status: {}".format(text), (10, 20), cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), if text == "Occupied" and config.monitor:
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
if text == "Occupied":
img = frame img = frame
location = mon.saveImage(img) location = com.saveImage(img)
mon.notify(location) com.notify(location)
print(text)
key = cv2.waitKey(1) & 0xFF key = cv2.waitKey(1) & 0xFF
counter+=1 counter+=1
if counter % (framerate * delay) == 0: if counter % (framerate * delay) == 0:
firstFrame = gray firstFrame = gray
except:
except Exception as e:
print(e)
# cleanup the camera and close any open windows # cleanup the camera and close any open windows
vs.stop() if args.get("video", None) is None else vs.release() #vs.stop() if args.get("video", None) is None else vs.release()
print("error")
time.sleep(10)
compare("http://192.168.178.25:8000/stream.mjpg")