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__/
|
||||
|
||||
.vscode/
|
||||
|
||||
config copy.py
|
||||
|
|
|
|||
12
com.py
12
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)
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
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
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue