mirror of https://github.com/Askill/SurvBot.git
slight rework
This commit is contained in:
parent
30ffeb788f
commit
d4c5639113
|
|
@ -6,3 +6,5 @@ imgs/*
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
config copy.py
|
||||||
|
|
|
||||||
12
com.py
12
com.py
|
|
@ -18,20 +18,12 @@ def saveImage(img):
|
||||||
def notify(path):
|
def notify(path):
|
||||||
global loginDataJson
|
global loginDataJson
|
||||||
photo = open(path, "rb")
|
photo = open(path, "rb")
|
||||||
json1 = {"chat_id": loginDataJson["chat_id"]}
|
json1 = {"chat_id": config.chat_id}
|
||||||
files = {'photo': photo}
|
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():
|
def initEndpoint():
|
||||||
loadLogin()
|
|
||||||
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)
|
||||||
print("registered:", tp)
|
print("registered:", tp)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
monitor = True
|
monitor = True
|
||||||
token = ""
|
|
||||||
chat_id = ""
|
|
||||||
loginPath = "./data.json"
|
loginPath = "./data.json"
|
||||||
stream = "http://192.168.178.25:8000/stream.mjpg"
|
stream = "http://192.168.178.56:8080/video"
|
||||||
photos = "./imgs/"
|
photos = "./imgs/"
|
||||||
endpoint = "https://telegram-bot.jopa.dev"
|
endpoint = "https://telegram-bot.jopa.dev"
|
||||||
|
token = ""
|
||||||
|
chat_id = ""
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"key" : "",
|
|
||||||
"chat_id" : ""
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
from imutils.video import VideoStream
|
||||||
import argparse
|
import argparse
|
||||||
import datetime
|
import datetime
|
||||||
|
|
@ -11,46 +6,36 @@ import time
|
||||||
import cv2
|
import cv2
|
||||||
import com
|
import com
|
||||||
import config
|
import config
|
||||||
|
import traceback
|
||||||
|
import _thread
|
||||||
|
|
||||||
def compare():
|
def compare():
|
||||||
try:
|
try:
|
||||||
url = config.stream
|
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
|
min_area = 3000
|
||||||
args["video"] = url
|
max_area = 10000
|
||||||
#args["video"] = "./videos/example_02.mp4"
|
|
||||||
vs = cv2.VideoCapture(args["video"])
|
|
||||||
counter = 0
|
counter = 0
|
||||||
threashold = 18
|
threashold = 18
|
||||||
delay = .3
|
delay = .3
|
||||||
framerate = 30
|
framerate = 30
|
||||||
|
|
||||||
# initialize the first frame in the video stream
|
# initialize the first frame in the video stream
|
||||||
|
vs = cv2.VideoCapture(url)
|
||||||
firstFrame = None
|
firstFrame = None
|
||||||
|
|
||||||
# loop over the frames of the video
|
# loop over the frames of the video
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
# grab the current frame and initialize the occupied/unoccupied
|
frame = vs.read()[1]
|
||||||
# text
|
|
||||||
frame = vs.read()
|
|
||||||
frame = frame if args.get("video", None) is None else frame[1]
|
|
||||||
text = "Unoccupied"
|
text = "Unoccupied"
|
||||||
|
|
||||||
# if the frame could not be grabbed, then we have reached the end
|
# if the frame could not be grabbed, then we have reached the end of the video
|
||||||
# of the video
|
|
||||||
if frame is None:
|
if frame is None:
|
||||||
retry("frame was none")
|
retry("frame was none")
|
||||||
|
|
||||||
# resize the frame, convert it to grayscale, and blur it
|
# resize the frame, convert it to grayscale, and blur it
|
||||||
frame = imutils.resize(frame, width=500)
|
frame = imutils.resize(frame, width=500)
|
||||||
#frame = increase_brightness(frame, value=50)
|
|
||||||
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)
|
||||||
|
|
||||||
|
|
@ -59,27 +44,18 @@ def compare():
|
||||||
firstFrame = gray
|
firstFrame = gray
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# compute the absolute difference between the current frame and
|
|
||||||
# first frame
|
|
||||||
frameDelta = cv2.absdiff(gray, firstFrame)
|
frameDelta = cv2.absdiff(gray, firstFrame)
|
||||||
|
|
||||||
thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1]
|
thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1]
|
||||||
|
|
||||||
# dilate the thresholded image to fill in holes, then find contours
|
# dilate the thresholded image to fill in holes, then find contours
|
||||||
# on thresholded image
|
|
||||||
thresh = cv2.dilate(thresh, None, iterations=2)
|
thresh = cv2.dilate(thresh, None, iterations=2)
|
||||||
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
cnts = imutils.grab_contours(cnts)
|
cnts = imutils.grab_contours(cnts)
|
||||||
|
|
||||||
# loop over the contours
|
# loop over the contours
|
||||||
for c in cnts:
|
for c in cnts:
|
||||||
# if the contour is too small, ignore it
|
if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area:
|
||||||
if cv2.contourArea(c) < args["min_area"]:
|
|
||||||
continue
|
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)
|
(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)
|
||||||
|
|
@ -88,20 +64,21 @@ def compare():
|
||||||
# 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)
|
||||||
|
|
||||||
if text == "Occupied" and config.monitor:
|
if text == "Occupied" and config.monitor:
|
||||||
img = frame
|
img = frame
|
||||||
location = com.saveImage(img)
|
location = com.saveImage(img)
|
||||||
com.notify(location)
|
_thread.start_new_thread(com.notify, (location, ) )
|
||||||
print(text)
|
#com.notify(location)
|
||||||
|
|
||||||
counter+=1
|
counter+=1
|
||||||
if counter % (framerate * delay) == 0:
|
if counter % (framerate * delay) == 0:
|
||||||
firstFrame = gray
|
firstFrame = gray
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
retry(e)
|
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):
|
def retry(error):
|
||||||
print(error)
|
print(error)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue