From d4c56391139eedf5318658393c06e026f2ecfb4f Mon Sep 17 00:00:00 2001 From: Askill Date: Sun, 24 May 2020 20:48:40 +0200 Subject: [PATCH] slight rework --- .gitignore | 2 ++ com.py | 12 ++-------- config.py | 8 +++---- data1.json | 4 ---- motion_detector.py | 55 ++++++++++++++-------------------------------- 5 files changed, 24 insertions(+), 57 deletions(-) delete mode 100644 data1.json diff --git a/.gitignore b/.gitignore index 6067b5d..ad078c6 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ imgs/* __pycache__/ .vscode/ + +config copy.py diff --git a/com.py b/com.py index 1cdbae2..37d1067 100644 --- a/com.py +++ b/com.py @@ -18,20 +18,12 @@ def saveImage(img): def notify(path): global loginDataJson photo = open(path, "rb") - json1 = {"chat_id": loginDataJson["chat_id"]} + json1 = {"chat_id": config.chat_id} files = {'photo': photo} - print(requests.post("https://api.telegram.org/bot" + loginDataJson["key"] + "/sendPhoto", json1, files=files)) + print(requests.post("https://api.telegram.org/bot" + config.token + "/sendPhoto", json1, files=files)) -def loadLogin(): - global loginDataJson - 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) diff --git a/config.py b/config.py index dc41c87..46ef04d 100644 --- a/config.py +++ b/config.py @@ -1,8 +1,8 @@ monitor = True -token = "" -chat_id = "" loginPath = "./data.json" -stream = "http://192.168.178.25:8000/stream.mjpg" +stream = "http://192.168.178.56:8080/video" photos = "./imgs/" -endpoint = "https://telegram-bot.jopa.dev" \ No newline at end of file +endpoint = "https://telegram-bot.jopa.dev" +token = "" +chat_id = "" \ No newline at end of file diff --git a/data1.json b/data1.json deleted file mode 100644 index 6ef315e..0000000 --- a/data1.json +++ /dev/null @@ -1,4 +0,0 @@ -{ -"key" : "", -"chat_id" : "" -} \ No newline at end of file diff --git a/motion_detector.py b/motion_detector.py index 728c0ea..4f2123e 100644 --- a/motion_detector.py +++ b/motion_detector.py @@ -1,8 +1,3 @@ -# USAGE -# python motion_detector.py -# python motion_detector.py --video videos/example_01.mp4 - -# import the necessary packages from imutils.video import VideoStream import argparse import datetime @@ -11,46 +6,36 @@ import time import cv2 import com import config +import traceback +import _thread def compare(): try: 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") - ap.add_argument("-amin", "--min-area", type=int, default=3000, help="minimum area size") - ap.add_argument("-amax", "--max-area", type=int, default=10000, help="minimum area size") - args = vars(ap.parse_args()) - # if the video argument is None, then we are reading from webcam - args["video"] = url - #args["video"] = "./videos/example_02.mp4" - vs = cv2.VideoCapture(args["video"]) + min_area = 3000 + max_area = 10000 + counter = 0 threashold = 18 delay = .3 framerate = 30 - + # initialize the first frame in the video stream + vs = cv2.VideoCapture(url) firstFrame = None - # loop over the frames of the video while True: - - # grab the current frame and initialize the occupied/unoccupied - # text - frame = vs.read() - frame = frame if args.get("video", None) is None else frame[1] + + frame = vs.read()[1] text = "Unoccupied" - # if the frame could not be grabbed, then we have reached the end - # of the video + # if the frame could not be grabbed, then we have reached the end of the video if frame is None: retry("frame was none") # resize the frame, convert it to grayscale, and blur it frame = imutils.resize(frame, width=500) - #frame = increase_brightness(frame, value=50) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (31, 31), 0) @@ -59,27 +44,18 @@ def compare(): firstFrame = gray continue - # compute the absolute difference between the current frame and - # first frame frameDelta = cv2.absdiff(gray, firstFrame) - thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1] # dilate the thresholded image to fill in holes, then find contours - # on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # loop over the contours for c in cnts: - # if the contour is too small, ignore it - if cv2.contourArea(c) < args["min_area"]: + if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area: continue - if cv2.contourArea(c) > args["max_area"]: - continue - # compute the bounding box for the contour, draw it on the frame, - # and update the text (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) @@ -88,20 +64,21 @@ def compare(): # 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) + if text == "Occupied" and config.monitor: img = frame location = com.saveImage(img) - com.notify(location) - print(text) + _thread.start_new_thread(com.notify, (location, ) ) + #com.notify(location) counter+=1 if counter % (framerate * delay) == 0: firstFrame = gray except Exception as e: + traceback.print_exc() retry(e) - # cleanup the camera and close any open windows - vs.stop() if args.get("video", None) is None else vs.release() + def retry(error): print(error)