import configparser import cv2 import os import time from discord_webhook import DiscordWebhook, DiscordEmbed import logging from datetime import datetime from pycoral.adapters.common import input_size from pycoral.adapters.detect import get_objects from pycoral.utils.dataset import read_label_file from pycoral.utils.edgetpu import make_interpreter from pycoral.utils.edgetpu import run_inference def main(): last_time = datetime.now() time.sleep(5) webhook = DiscordWebhook(url=config["DEFAULT"]["url"]) labels=config["DEFAULT"]["default_labels"] model=config["DEFAULT"]["default_model"] threshold=float(config["DEFAULT"]["default_threshold"]) top_k= int(config["DEFAULT"]["top_K"]) grace_time= int(config["DEFAULT"]["grace_time"]) stream=config["DEFAULT"]["default_stream"] dump_path=config["DEFAULT"]["dump_path"] logging.warning('Loading {} with {} labels.'.format(model, labels)) interpreter = make_interpreter(model) interpreter.allocate_tensors() labels = read_label_file(labels) inference_size = input_size(interpreter) cap = cv2.VideoCapture(stream) while cap.isOpened(): ret, frame = cap.read() if not ret: break cv2_im = frame cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB) cv2_im_rgb = cv2.resize(cv2_im_rgb, inference_size) run_inference(interpreter, cv2_im_rgb.tobytes()) objs = get_objects(interpreter,threshold)[:top_k] cv2_im,alarm,message = detect_and_alarm(cv2_im, inference_size, objs, labels,threshold) #print(alarm,message) if alarm and is_time(last_time,grace_time): last_time=datetime.now() logging.warning("people alarm",last_time) #im_resize = cv2.resize(cv2_im, (640, 480)) is_success, im_buf_arr = cv2.imencode(".jpg", cv2_im) byte_im = im_buf_arr.tobytes() webhook.add_file(file=byte_im,filename='capture.png') embed = DiscordEmbed(title='Detected', description=message, color='ff2345') embed.set_image(url='attachment://capture.png') webhook.add_embed(embed) response = webhook.execute(remove_embeds=True) logging.debug(response) if config["DEFAULT"]["DUMP_VIDEO"]=="True": os.system(f"ffmpeg -i {stream} -acodec copy -vcodec copy -t {grace_time} {dump_path}record_{last_time.strftime('%d-%m-%Y_%H-%M-%S')}.mp4 ") if config["DEFAULT"]["CV_DEBUG"]=="True": cv2.imshow('frame', cv2_im) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() def detect_and_alarm (cv2_im, inference_size, objs, labels,threshold): height, width, channels = cv2_im.shape scale_x, scale_y = width / inference_size[0], height / inference_size[1] for obj in objs: if obj.id == 0 and obj.score>threshold*1.09: bbox = obj.bbox.scale(scale_x, scale_y) x0, y0 = int(bbox.xmin), int(bbox.ymin) x1, y1 = int(bbox.xmax), int(bbox.ymax) percent = int(100 * obj.score) label = '{}% {}'.format(percent, labels.get(obj.id, obj.id)) cv2_im = cv2.rectangle(cv2_im, (x0, y0), (x1, y1), (0, 255, 0), 2) cv2_im = cv2.putText(cv2_im, label, (x0, y0+30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0), 2) cv2_im = cv2.putText(cv2_im, "ALARM !!!!", (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) return cv2_im,True,f'ALARM ! PERSON detected {round(obj.score,1)*100}' return cv2_im,False,"OKAY" def is_time(last_time,threshold): delta=int((datetime.now()-last_time).total_seconds()) logging.debug(delta,last_time,threshold,delta >= threshold) return delta >= threshold if __name__ == '__main__': config = configparser.ConfigParser() configFilePath = '/home/pi/picoral_cctv/cctv.ini' config.read(configFilePath) main()