-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (95 loc) · 3.74 KB
/
app.py
File metadata and controls
106 lines (95 loc) · 3.74 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
from flask import Flask, render_template, jsonify, request
import csv
from datetime import datetime
import numpy as np
app = Flask(__name__)
DATA_PATH = './data.csv'
LABELS_PATH = './coco_80_labels_list.txt'
def get_labels():
with open(LABELS_PATH, 'r') as f:
return [line.strip() for line in f if line.strip()]
def parse_date(date_str):
return datetime.strptime(date_str, '%d/%m/%Y %H:%M:%S')
def date_in_range(dt_obj, start_date, end_date):
if not start_date or not end_date:
return True
start = datetime.strptime(start_date, '%Y-%m-%d').date()
end = datetime.strptime(end_date, '%Y-%m-%d').date()
return start <= dt_obj.date() <= end
@app.route('/')
def index():
labels = get_labels()
lang = request.args.get('lang', 'es')
return render_template('index.html', labels=labels, lang=lang)
@app.route('/data')
def get_data():
selected_label = request.args.get('label')
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
lang = request.args.get('lang', 'es')
labels = get_labels()
timeline = process_timeline_data(selected_label, start_date, end_date)
histogram = process_histogram_data(selected_label, start_date, end_date)
classification = process_classification_data(labels, start_date, end_date)
return jsonify({
'timeline': timeline,
'histogram': histogram,
'classification': classification,
'lang': lang,
'labels': labels
})
def process_timeline_data(selected_label, start_date, end_date):
data = {'timestamps': [], 'objects_detected': []}
try:
with open(DATA_PATH, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
if selected_label not in row:
continue
dt_obj = parse_date(row['Timestamp'])
if not date_in_range(dt_obj, start_date, end_date):
continue
data['timestamps'].append(row['Timestamp'])
data['objects_detected'].append(int(row[selected_label]))
except Exception as e:
app.logger.error(f"Timeline error: {str(e)}")
return data
def process_histogram_data(selected_label, start_date, end_date):
hourly_counts = [0] * 24
try:
with open(DATA_PATH, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
if selected_label not in row:
continue
dt_obj = parse_date(row['Timestamp'])
if not date_in_range(dt_obj, start_date, end_date):
continue
hour = dt_obj.hour
hourly_counts[hour] += int(row[selected_label])
avg = np.mean(hourly_counts) if any(hourly_counts) else 0
except Exception as e:
app.logger.error(f"Histogram error: {str(e)}")
avg = 0
return {
'hours': list(range(24)),
'counts': hourly_counts,
'average': [avg] * 24
}
def process_classification_data(labels, start_date, end_date):
counts = {label: 0 for label in labels}
try:
with open(DATA_PATH, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
dt_obj = parse_date(row['Timestamp'])
if not date_in_range(dt_obj, start_date, end_date):
continue
for label in labels:
if label in row and row[label].isdigit():
counts[label] += int(row[label])
except Exception as e:
app.logger.error(f"Classification error: {str(e)}")
return counts
if __name__ == '__main__':
app.run(debug=True)