-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.py
163 lines (139 loc) · 6.86 KB
/
notifier.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import cx_Oracle
from datetime import datetime, timedelta
import time
import threading
import os
import cv2
import requests
import base64
import json
import logging
PREVIOUS_ENCOUNTER_CLEAR_TIME = 5 # in minutes
# logging.basicConfig(filename='model.log', format='%(asctime)s %(levelname)s:%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG, encoding='utf-8')
logging.basicConfig(
filename = 'model.log',
# encoding = 'utf-8',
format='%(asctime)s,%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
level=logging.DEBUG)
class Database:
def __init__(self, username, password, host, port, service_name):
while True:
try:
dsn = cx_Oracle.makedsn(host, port, service_name=service_name)
self.connection = cx_Oracle.connect(username, password, dsn)
print('[INFO] Connected to the database')
logging.info("Connected to the database")
break
except Exception as e:
print("[ERROR] Failed to connect to the database. Retrying in 5 seconds...")
logging.error("Failed to connect to the database. Retrying in 5 seconds...")
time.sleep(5)
def get_unique_encounters(self, time_period_minutes):
query = f"""
SELECT e1.NAME, e1.CONFIDENCE, e1.TIMESTAMP, e1.IMAGE, e1.CAMERASOCKETURL, e1.LOCATION, e1.CAMERA_ID
FROM encounters e1
JOIN (
SELECT NAME, MAX(TIMESTAMP) AS max_timestamp
FROM encounters
WHERE TO_DATE(TIMESTAMP, 'DD-MON-YYYY HH24:MI:SS') >= :start_time
GROUP BY NAME
) e2 ON e1.NAME = e2.NAME AND e1.TIMESTAMP = e2.max_timestamp
"""
start_time = datetime.now() - timedelta(minutes=time_period_minutes)
with self.connection.cursor() as cursor:
cursor.execute(query, start_time=start_time)
results = cursor.fetchall()
return results
def get_other_persons(self, image)->list:
query = f"""
SELECT NAME FROM encounters WHERE IMAGE = :image
"""
with self.connection.cursor() as cursor:
cursor.execute(query, image=image)
results = cursor.fetchall()
return results
def get_person_details(self, name, image):
query = f"""
SELECT NAME, CONFIDENCE, TIMESTAMP, IMAGE, CAMERASOCKETURL, LOCATION, CAMERA_ID FROM encounters WHERE NAME = :name AND IMAGE = :image
"""
with self.connection.cursor() as cursor:
cursor.execute(query, name=name, image=image)
results = cursor.fetchall()
return results
def clear_encounters():
global previous_encounters
while True:
time.sleep(PREVIOUS_ENCOUNTER_CLEAR_TIME * 60)
print("[INFO] Clearing previous encounters")
logging.info("Clearing previous encounters")
previous_encounters.clear()
def send_encounters(encounters_list):
try:
images = []
for encounter in encounters_list.keys():
other_persons = db.get_other_persons(encounter)
# print("Other criminals in the image:", other_persons)
for other_person in other_persons:
if other_person[0] not in previous_encounters.keys():
previous_encounters[other_person[0]] = [encounter, [encounters_list[encounter]['criminals'][0]["camera_id"]]]
encounters_list[encounter]['criminals'].append(db.get_person_details(other_person[0], encounter))
else:
if encounters_list[encounter]['criminals'][0]["camera_id"] not in previous_encounters[other_person[0]][1]:
previous_encounters[other_person[0]][1].append(encounters_list[encounter]['criminals'][0]["camera_id"])
encounters_list[encounter]['criminals'].append(db.get_person_details(other_person[0], encounter))
image = cv2.imread('encounters/' + encounter + '.jpg')
image_data = cv2.imencode('.jpg', image)[1].tobytes()
images.append(base64.b64encode(image_data).decode('utf-8'))
for i, encounter in enumerate(encounters_list):
encounters_list[encounter]['image'] = images[i]
response = requests.post(server_url + "/notify", json=encounters_list)
if response.status_code == 200:
print("[INFO] Encounters sent successfully")
logging.info("Encounters sent successfully")
print(response.text)
else:
print("[ERROR] Failed to send encounters")
logging.error("Failed to send encounters")
except Exception as e:
print('[ERROR] Exception occurred: ', e)
if __name__ == "__main__":
server_details = json.load(open("config.json", "r"))['notify_server']
server_ip, server_port = server_details["ip"], server_details["port"]
server_url = f"http://{server_ip}:{server_port}"
previous_encounters = dict()
db = Database(username="aimlb4",
password="vishalsai",
host="localhost",
port="1521",
service_name="xe")
time_period_minutes = 5
encounter_clearer_thread = threading.Thread(target=clear_encounters)
encounter_clearer_thread.daemon = True
encounter_clearer_thread.start()
size = len(os.listdir("encounters"))
column_names = ["name", "confidence", "timestamp", "image", "camerasocketurl", "location", "camera_id"]
while True:
new_size = len(os.listdir("encounters"))
if new_size != size:
size = new_size
encounters = db.get_unique_encounters(time_period_minutes)
encounter_list = dict()
for encounter in encounters:
encounter_details = dict(zip(column_names, encounter))
if encounter_details["name"] not in previous_encounters.keys():
previous_encounters[encounter_details["name"]] = [encounter_details["image"], [encounter_details["camera_id"]]]
temp = dict()
temp['criminals'] = [encounter_details]
encounter_list[encounter_details['image']] = temp
else:
if encounter_details["camera_id"] not in previous_encounters[encounter_details["name"]][1]:
previous_encounters[encounter_details["name"]][1].append(encounter_details["camera_id"])
temp = dict()
temp['criminals'] = [encounter_details]
encounter_list[encounter_details['image']] = temp
if len(encounter_list) > 0:
print("Encounters:", encounter_list)
print("Previous encounters:", previous_encounters)
send_encounters(encounter_list)
time.sleep(1)