-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgen_stats.py
More file actions
162 lines (131 loc) · 5.51 KB
/
gen_stats.py
File metadata and controls
162 lines (131 loc) · 5.51 KB
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
import datetime
import sqlite3
import json
import time
import pandas as pd
import numpy as np
import collections
REMAKE = False
HARD_START_DATE = "2019-01-01 00:00:00"
con = sqlite3.connect("darknet_server.db")
# fix numpy sends back NaT.
def strr(obj): # we can use fillna
x = str(obj)
if x == "NaT":
return "-1.0"
else:
return x
def calc_min_data():
minute_data = pd.read_sql_query("SELECT * FROM cam_detection_min order by sampled_at ASC", con)
if minute_data.empty:
start_date = HARD_START_DATE
else:
last_row = minute_data['sampled_at'].iloc[-1]
start_date = last_row
if REMAKE:
start_date = HARD_START_DATE
sql = "DELETE FROM cam_detection_min"
print(sql)
rs = con.execute(sql)
con.commit()
print(minute_data.tail(50))
print(start_date)
data = pd.read_sql_query("SELECT * FROM cam_detection WHERE created_at > '" + str(start_date) + "'", con)
# split into multi dataframe, by "cam_id"
for x, new_df in data.groupby("cam_id"):
cam_id = x
cam_name = new_df['cam_name'].iloc[-1]
num_persons = []
num_luggages = []
sampled_ats = []
# iterate all rows belongs to this "cam_id"
for index, row in new_df.iterrows():
det = json.loads((row['det']))
num_person = 0
num_luggage = 0
sampled_at = row['created_at']
for elem in det:
if elem[0] == 'person':
num_person += 1
if elem[0] in ['backpack', 'handbag', 'suitcase']:
num_luggage += 1
num_persons.append(num_person)
num_luggages.append(num_luggage)
sampled_ats.append(sampled_at)
new_df.insert(1, "num_persons", num_persons, True)
new_df.insert(1, "num_luggages", num_luggages, True)
new_df.insert(1, "sampled_at", sampled_ats, True)
new_df = new_df.set_index(pd.DatetimeIndex(new_df['sampled_at']))
new_df = new_df[['num_persons', 'num_luggages']]
down_sampled = new_df.resample('1min').mean() # resample removes
down_sampled = down_sampled.reset_index()
# ditch earlier due to partial calcualtion.
sql = "SELECT * FROM cam_detection_min WHERE cam_id=" + str(cam_id) + " AND sampled_at>='" + str(
down_sampled['sampled_at'].min()) + "';"
sql = "DELETE FROM cam_detection_min WHERE cam_id=" + str(cam_id) + " AND sampled_at>='" + str(
down_sampled['sampled_at'].min()) + "';"
print(sql)
rs = con.execute(sql)
con.commit()
# pass for now
for index, row in down_sampled.iterrows():
sql = "INSERT INTO cam_detection_min (cam_id,cam_name,num_persons,num_luggages,sampled_at) VALUES( " + str(
cam_id) + ", '" + cam_name + "', " + strr(row['num_persons']) + ", " + strr(
row['num_luggages']) + ",'" + str(row['sampled_at']) + "');"
print(sql)
rs = con.execute(sql)
con.commit()
def calc_hour_data():
hour_data = pd.read_sql_query("SELECT * FROM cam_detection_hour order by sampled_at ASC", con)
if hour_data.empty:
start_date = "2019-10-01 00:00:00"
else:
last_row = hour_data['sampled_at'].iloc[-1]
start_date = last_row
data = pd.read_sql_query("SELECT * FROM cam_detection WHERE created_at > '" + str(start_date) + "'", con)
for x, new_df in data.groupby("cam_id"):
cam_id = x
cam_name = new_df['cam_name'].iloc[-1]
num_persons = []
num_luggages = []
sampled_ats = []
# iterate all rows belongs to this "cam_id"
for index, row in new_df.iterrows():
det = json.loads((row['det']))
num_person = 0
num_luggage = 0
sampled_at = row['created_at']
for elem in det:
if elem[0] == 'person':
num_person += 1
if elem[0] in ['backpack', 'handbag', 'suitcase']:
num_luggage += 1
num_persons.append(num_person)
num_luggages.append(num_luggage)
sampled_ats.append(sampled_at)
new_df.insert(1, "num_persons", num_persons, True)
new_df.insert(1, "num_luggages", num_luggages, True)
new_df.insert(1, "sampled_at", sampled_ats, True)
new_df = new_df.set_index(pd.DatetimeIndex(new_df['sampled_at']))
new_df = new_df[['num_persons', 'num_luggages']]
down_sampled = new_df.resample('1h').sum() # resample removes
down_sampled = down_sampled.reset_index()
sql = "DELETE FROM cam_detection_hour WHERE cam_id=" + str(cam_id) + " AND sampled_at>='" + str(
down_sampled['sampled_at'].min()) + "';"
rs = con.execute(sql)
con.commit()
for index, row in down_sampled.iterrows():
sql = "INSERT INTO cam_detection_hour (cam_id,cam_name,num_persons,num_luggages,sampled_at) VALUES( " + str(
cam_id) + ", '" + cam_name + "', " + strr(row['num_persons']) + ", " + strr(
row['num_luggages']) + ",'" + str(row['sampled_at']) + "');"
rs = con.execute(sql)
con.commit()
if __name__ == "__main__":
while True:
calc_min_data()
print("LOOP done, sleep 20 sec.")
time.sleep(20)
time_now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if time_now[14:16] == '01':
print("update cam_detection_hour")
calc_hour_data()