Add telegram bot

This commit is contained in:
Anna Sudnitsina 2019-08-10 14:14:15 +03:00
parent dcbc32d6dd
commit 27eb5bad95
2 changed files with 23 additions and 11 deletions

View File

@ -1,7 +1,15 @@
import os
import cv2 import cv2
import time import time
import pandas import pandas
from datetime import datetime from datetime import datetime
import telebot
import config
bot = telebot.TeleBot(config.TOKEN)
prev_frame = None prev_frame = None
times = [] times = []
@ -25,42 +33,42 @@ while True:
prev_frame = gray prev_frame = gray
# continue # continue
delta_frame = cv2.absdiff(prev_frame, gray) delta_frame = cv2.absdiff(prev_frame, gray)
prev_frame = gray prev_frame = gray
thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1] thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame = cv2.dilate(thresh_delta, None, iterations=2) thresh_frame = cv2.dilate(thresh_delta, None, iterations=2)
(_,cnts,_) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) (_, cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(len(cnts)) print(len(cnts))
for contour in cnts: for contour in cnts:
if cv2.contourArea(contour) < 1000: if cv2.contourArea(contour) < 1000:
continue continue
status_new, status_old = 1, status_new status_new, status_old = 1, status_new
(x, y, w, h) = cv2.boundingRect(contour) (x, y, w, h) = cv2.boundingRect(contour)
# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3) # cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3)
# status_list.append(status) # status_list.append(status)
if status_new != status_old: if status_new != status_old:
times.append(datetime.now()) # times.append(datetime.now())
cv2.imwrite('media/capture{}.png'.format(counter), frame) print("=" * 10)
file_name = config.PATH + config.FILE_NAME.format(counter)
cv2.imwrite(file_name, frame)
# send to telegram chat and rmove from disk
bot.send_photo(config.CHAT_ID, open(file_name, "rb"))
os.remove(file_name)
counter += 1 counter += 1
# cv2.imshow("capt", gray) # cv2.imshow("capt", gray)
# cv2.imshow("delta", delta_frame) # cv2.imshow("delta", delta_frame)
# cv2.imshow("threshold", thresh_frame) # cv2.imshow("threshold", thresh_frame)
faces = face_cascade.detectMultiScale(gray, faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10)
scaleFactor=1.1,
minNeighbors=10)
# for x, y, w, h in faces: # for x, y, w, h in faces:
# img = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3) # img = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
# print(type(faces)) # print(type(faces))
# cv2.imshow("face_detector", img) # cv2.imshow("face_detector", img)
cv2.imshow('cat', only_cat) cv2.imshow("cat", only_cat)
cv2.imshow("rectangle", frame) cv2.imshow("rectangle", frame)
key = cv2.waitKey(1) key = cv2.waitKey(1)
if key==ord('q'): if key == ord("q"): # quit
break break
# print(times)
# #
# for t in range(0, len(times), 2): # for t in range(0, len(times), 2):
# df = df.append({"start": times[t], "end": times[t+1]}, ignore_index=True) # df = df.append({"start": times[t], "end": times[t+1]}, ignore_index=True)

4
config.py Normal file
View File

@ -0,0 +1,4 @@
TOKEN = '0'
CHAT_ID = 0
PATH = '0'
FILE_NAME = '0'