Browse Source

Add my old code )

master
Igor Elpin 2 years ago
parent
commit
a1d9b707d1
  1. 80
      all_models/coco_labels.txt
  2. BIN
      all_models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite
  3. 13
      cctv.ini
  4. 99
      motion4.py

80
all_models/coco_labels.txt

@ -0,0 +1,80 @@
0 person
1 bicycle
2 car
3 motorcycle
4 airplane
5 bus
6 train
7 truck
8 boat
9 traffic light
10 fire hydrant
12 stop sign
13 parking meter
14 bench
15 bird
16 cat
17 dog
18 horse
19 sheep
20 cow
21 elephant
22 bear
23 zebra
24 giraffe
26 backpack
27 umbrella
30 handbag
31 tie
32 suitcase
33 frisbee
34 skis
35 snowboard
36 sports ball
37 kite
38 baseball bat
39 baseball glove
40 skateboard
41 surfboard
42 tennis racket
43 bottle
45 wine glass
46 cup
47 fork
48 knife
49 spoon
50 bowl
51 banana
52 apple
53 sandwich
54 orange
55 broccoli
56 carrot
57 hot dog
58 pizza
59 donut
60 cake
61 chair
62 couch
63 potted plant
64 bed
66 dining table
69 toilet
71 tv
72 laptop
73 mouse
74 remote
75 keyboard
76 cell phone
77 microwave
78 oven
79 toaster
80 sink
81 refrigerator
83 book
84 clock
85 vase
86 scissors
87 teddy bear
88 hair drier
89 toothbrush

BIN
all_models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite

Binary file not shown.

13
cctv.ini

@ -0,0 +1,13 @@
[DEFAULT]
CV_DEBUG=True
DUMP_VIDEO=True
default_model = ./all_models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite
default_labels = ./all_models/coco_labels.txt
default_threshold = 0.34
top_K=3
grace_time=120
#default_stream = rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4
default_stream = rtsp://192.168.1.5:554/media/video0
#default_stream=/home/pi/11.mp4
dump_path=/home/pi/video/
url=https://discord.com/api/webhooks/123/token

99
motion4.py

@ -0,0 +1,99 @@
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()
Loading…
Cancel
Save