-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·130 lines (109 loc) · 3.76 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 16 19:29:41 2022
@author: david
"""
#import socket
#import threading
import numpy as np
import cv2
#import pywhatkit
import telegram
import time
import datetime
import os
import music
def getOrario():
now = str(time.strftime("%H%M")) #capire come da orario a mezanotte,se 24:00:00 o 00:00:00 o 0:00:00
ora=now[:-2]
mini=now[2:]
orario=ora+":" + mini +":00"
timeList = [orario,"04:00:00"]
mysum = datetime.timedelta()
for i in timeList:
(h, m, s) = i.split(':')
d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
mysum += d
return str(mysum)
nextOra=getOrario()
nextOra=nextOra[:-3]
oraList=[nextOra]
#get this from telegram
api_key = ''
user_id = ''
fp = open("file_configurazione.txt","r")
for riga in fp:
if riga.startswith("user_id"):
ls = riga.split("=")
user_id = ls[1][:-1]
elif riga.startswith("key_token"):
ls = riga.split("=")
api_key = ls[1][:-1]
fp.close()
try:
f = open("appo","r")
f.close()
os.remove("appo")
except:
pass
def processMessages(net,output_layers):
i=0
k=0
while True:
while True:
try:
f = open("appo","r")
f.close()
os.remove("appo")
print("preso")
break
except:
pass
trov=0
allarme = 0
fp = open("attiva.txt","r")
for f in fp:
if f == "on":
allarme = 1
fp.close()
img = cv2.imread("salvata2.png",1)
if allarme == 1:
blob=cv2.dnn.blobFromImage(img,0.00392,(416,416),(0,0,0),True,crop=False)
net.setInput(blob)
#invio a web server
outs=net.forward(output_layers) #è questa la piu lenta
#prendi outs da web server
for output in outs:
for detection in output:
#Detecting confidence in 3 steps
scores=detection[5:]
class_id=np.argmax(scores)
confidence =scores[class_id]
if confidence > 0.8 and class_id == 0: #Means if the object is detected
trov=1
#break
#if trov == 1:
#break
if trov == 1: #devo mettere 1
bot = telegram.Bot(token=api_key)
bot.send_message(chat_id=user_id, text='ATTENZIONE!!!!!!!!!!!!\nRILEVATA PERSONA')
bot.sendPhoto(chat_id=user_id, photo=open("salvata2.png","rb"))
music.sound()
#ricevo da utente messaggio di bloccare il suono
if str(time.strftime("%H%M")) in oraList:
bot = telegram.Bot(token=api_key)
bot.send_message(chat_id=user_id, text='Tutto ok,il programma è ancora in esecuzione')
oraList.remove(oraList[0])
nextOra=getOrario()
nextOra=nextOra[:-3]
oraList.append(nextOra)
print("analizzato")
net=cv2.dnn.readNet("yolov3.weights","yolov3.cfg")
classes=[]
with open("coco.names","r") as f:
read=f.readlines()
for i in range(len(read)):
classes.append(read[i].strip("\n"))
layer_names=net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
processMessages(net, output_layers)