diff --git a/app.py b/app.py new file mode 100644 index 0000000..844a147 --- /dev/null +++ b/app.py @@ -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) \ No newline at end of file diff --git a/mon.py b/com.py similarity index 57% rename from mon.py rename to com.py index ef6e822..1cdbae2 100644 --- a/mon.py +++ b/com.py @@ -1,25 +1,17 @@ import requests import json -import telebot -import numpy as np +import config +import time import cv2 -import time - -login = "./data.json" +login = config.loginPath loginDataJson = {} - -photos = "./imgs/" -url = "http://192.168.178.25:8000/stream.mjpg" - -def checkCam(): - cap = cv2.VideoCapture(url) - r, img = cap.read() - return img +config.token +config.chat_id def saveImage(img): - name = '{0:010x}'.format(int(time.time() * 256))[:10] - path = photos + name + '.png' + name = '{}'.format(int(time.time())) + path = config.photos + name + '.png' cv2.imwrite(path, img) return path @@ -35,6 +27,14 @@ def loadLogin(): with open(login, 'r', encoding='utf-8') as f: loginData = f.read() loginDataJson = json.loads(loginData) + config.token = loginDataJson["key"] + config.chat_id = loginDataJson["chat_id"] + +def initEndpoint(): + loadLogin() + tp = "http://api.telegram.org/bot" + config.token + "/setWebHook?url=" + config.endpoint + requests.get(tp) + print("registered:", tp) + -loadLogin() diff --git a/config.py b/config.py new file mode 100644 index 0000000..dc41c87 --- /dev/null +++ b/config.py @@ -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" \ No newline at end of file diff --git a/motion_detector.py b/motion_detector.py index 784f89e..7d04fd4 100644 --- a/motion_detector.py +++ b/motion_detector.py @@ -9,8 +9,8 @@ import datetime import imutils import time import cv2 -import mon - +import com +import config def increase_brightness(img, value=30): @@ -26,7 +26,8 @@ def increase_brightness(img, value=30): return img -def compare(url): +def compare(): + url = config.stream # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() 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, # and update the text - print(cv2.contourArea(c)) - (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "Occupied" @@ -102,26 +101,26 @@ def compare(url): # draw the text and timestamp on the frame cv2.putText(frame, "Room Status: {}".format(text), (10, 20), 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"), - (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1) - - if text == "Occupied": + if text == "Occupied" and config.monitor: img = frame - location = mon.saveImage(img) - mon.notify(location) + location = com.saveImage(img) + com.notify(location) + print(text) + key = cv2.waitKey(1) & 0xFF counter+=1 if counter % (framerate * delay) == 0: firstFrame = gray - except: + + except Exception as e: + print(e) # cleanup the camera and close any open windows - vs.stop() if args.get("video", None) is None else vs.release() - print("error") - time.sleep(10) + #vs.stop() if args.get("video", None) is None else vs.release() + + -compare("http://192.168.178.25:8000/stream.mjpg") \ No newline at end of file