-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp3.py
More file actions
721 lines (596 loc) · 23.1 KB
/
app3.py
File metadata and controls
721 lines (596 loc) · 23.1 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
import sqlite3
from flask import Flask, render_template, request, redirect, session, jsonify, flash, url_for, send_from_directory
from flask import g
import bcrypt
import os
from werkzeug.utils import secure_filename
import tensorflow as tf
import warnings
from aed import vggish_input
from aed import vggish_params
from aed import vggish_postprocess
from aed import vggish_slim
from utils.console import Console
from utils.common import decode_wav_bytes, read_wav_bytes
import numpy as np
import joblib
import math
from pathlib import Path
import librosa
import soundfile as sf
# 初始化应用
app = Flask(__name__)
app.secret_key = 'your_secure_secret_key_here'
app.config['UPLOAD_FOLDER'] = './uploads'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# 禁用警告
warnings.filterwarnings("ignore", category=UserWarning)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
tf.compat.v1.disable_eager_execution()
# 加载VGGish模型和预训练的权重
tf.compat.v1.reset_default_graph()
sess = tf.compat.v1.Session()
vggish_slim.define_vggish_slim(training=False)
vggish_slim.load_vggish_slim_checkpoint(sess, "vggish_model.ckpt")
# 设置音频特征提取参数
params = vggish_params.EXAMPLE_HOP_SECONDS
input_tensor = sess.graph.get_tensor_by_name(vggish_params.INPUT_TENSOR_NAME)
output_tensor = sess.graph.get_tensor_by_name(vggish_params.OUTPUT_TENSOR_NAME)
def IsViolent(audio_file):
"""
使用GMM模型预测音频文件是否为暴力事件,返回暴力和非暴力事件的概率。
"""
# 读取音频文件并进行特征提取
examples_batch = vggish_input.wavfile_to_examples(audio_file)
features_tensor = sess.run(output_tensor, feed_dict={input_tensor: examples_batch})
# 后处理提取的特征
pproc = vggish_postprocess.Postprocessor("vggish_pca_params.npz")
postprocessed_batch = pproc.postprocess(features_tensor)
# 提取处理后的特征数据
data = postprocessed_batch
# 加载两个GMM模型(暴力和非暴力事件的模型)
gmm1 = joblib.load(os.path.join('Model/fulei.model')) # 暴力事件的GMM模型
gmm2 = joblib.load(os.path.join('Model/zhenglei.model')) # 非暴力事件的GMM模型
# 使用GMM模型计算得分
score_violent = gmm1.score(data) # 暴力事件的得分
score_non_violent = gmm2.score(data) # 非暴力事件的得分
# 计算softmax以归一化两者的得分为概率
max_val = max(score_violent, score_non_violent)
prob_violent = (math.exp(score_violent - max_val)) / (
math.exp(score_violent - max_val) + math.exp(score_non_violent - max_val))
prob_non_violent = (math.exp(score_non_violent - max_val)) / (
math.exp(score_violent - max_val) + math.exp(score_non_violent - max_val))
# 返回暴力和非暴力事件的概率
return prob_violent, prob_non_violent
@app.route('/audio/upload')
def upload_page():
"""上传页面"""
return render_template('upload.html')
@app.route('/upload_audio', methods=['POST'])
def handle_upload():
"""处理音频上传"""
try:
if 'audio_file' not in request.files:
return jsonify({'status': 'error', 'message': '未选择文件'}), 400
file = request.files['audio_file']
if not file or file.filename == '':
return jsonify({'status': 'error', 'message': '无效文件'}), 400
# 验证文件格式
filename = secure_filename(file.filename)
if not filename.lower().endswith(('.wav', '.mp3')):
return jsonify({'status': 'error', 'message': '仅支持WAV/MP3格式'}), 400
# 保存文件
save_path = Path(app.config['UPLOAD_FOLDER']) / filename
file.save(save_path)
# 使用 IsViolent 函数分析音频
violent_prob, non_violent_prob = IsViolent(str(save_path))
return jsonify({
'status': 'success',
'probability': round(violent_prob, 4),
'non_violence_probability': round(non_violent_prob, 4),
'audio_url': f'/audio/files/{filename}'
})
except Exception as e:
app.logger.error(f'文件处理错误: {str(e)}')
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/audio/files/<path:filename>')
def serve_audio(filename):
"""提供音频文件"""
ext = Path(filename).suffix.lower()
mimetype = 'audio/wav' if ext == '.wav' else 'audio/mpeg' if ext == '.mp3' else 'application/octet-stream'
return send_from_directory(
app.config['UPLOAD_FOLDER'],
filename,
mimetype=mimetype
)
# 初始化数据库
# 初始化数据库
def init_db():
conn = sqlite3.connect('violence_events.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL
)''')
c.execute('''CREATE TABLE IF NOT EXISTS mac (
mac_address TEXT PRIMARY KEY
)''')
c.execute('''CREATE TABLE IF NOT EXISTS mac_user (
mac_address TEXT,
device_name TEXT,
user_id INTEGER,
FOREIGN KEY (mac_address) REFERENCES mac(mac_address),
FOREIGN KEY (user_id) REFERENCES user(id)
)''')
c.execute('''CREATE TABLE IF NOT EXISTS events (
mac_address TEXT,
event_datetime TEXT,
device_name TEXT,
PRIMARY KEY (mac_address, event_datetime)
)''')
c.execute("INSERT OR IGNORE INTO user (username, password, role) VALUES ('admin', 'root', 'admin')")
conn.commit()
conn.close()
# 获取数据库连接
def get_db_connection():
if 'db' not in g:
g.db = sqlite3.connect('violence_events.db')
g.db.row_factory = sqlite3.Row
return g.db
# 关闭数据库连接
@app.teardown_appcontext
def close_db_connection(exception):
db = g.pop('db', None)
if db is not None:
db.close()
# 登录路由
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
conn = sqlite3.connect('violence_events.db')
c = conn.cursor()
c.execute("SELECT id, username, role FROM user WHERE username =? AND password =?", (username, password))
user = c.fetchone()
conn.close()
if user:
session['user_id'] = user[0]
session['username'] = user[1]
session['role'] = user[2]
return redirect('/dashboard')
return render_template('login.html', error='Invalid username or password')
return render_template('login.html')
# 注册路由
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password') # 直接使用明文密码
confirm_password = request.form.get('confirm_password')
if password != confirm_password:
return render_template('register.html', error='Passwords do not match')
conn = sqlite3.connect('violence_events.db')
c = conn.cursor()
try:
# 直接插入明文密码(危险操作!)
c.execute("INSERT INTO user (username, password, role) VALUES (?, ?, 'user')",
(username, password)) # 移除了哈希
conn.commit()
conn.close()
return redirect('/login')
except sqlite3.IntegrityError:
conn.close()
return render_template('register.html', error='Username already exists')
return render_template('register.html')
# 仪表盘
@app.route('/dashboard')
def dashboard():
if 'user_id' not in session:
return redirect('/login')
return render_template('dashboard.html')
@app.route('/audio/uploads/<path:filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename) # 确保路径正确
# 添加CORS头解决跨域问题
@app.after_request
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
return response
# 设备绑定路由
@app.route('/device/bind', methods=['GET', 'POST'])
def device_bind():
if 'username' not in session:
return "请先登录", 401
if request.method == 'POST':
mac_address = request.form['mac_address'].strip()
device_name = request.form.get('device_name', '默认设备名').strip()
conn = get_db_connection()
cursor = conn.cursor()
# 检查MAC是否存在
cursor.execute('SELECT COUNT(*) FROM mac WHERE mac_address =?', (mac_address,))
if cursor.fetchone()[0] == 0:
flash("错误:MAC地址不存在", "error")
conn.close()
return redirect(url_for('device_bind'))
# 检查是否已被当前用户绑定
cursor.execute('''
SELECT COUNT(*)
FROM mac_user
WHERE mac_address =?
AND user_id =?
''', (mac_address, session['user_id']))
if cursor.fetchone()[0] > 0:
flash("错误:该设备已绑定到您的账户", "error")
conn.close()
return redirect(url_for('device_bind'))
# 检查是否被其他用户绑定
cursor.execute('''
SELECT COUNT(*)
FROM mac_user
WHERE mac_address =?
AND user_id !=?
''', (mac_address, session['user_id']))
if cursor.fetchone()[0] > 0:
flash("错误:该设备已被其他用户绑定", "error")
conn.close()
return redirect(url_for('device_bind'))
# 执行绑定
try:
cursor.execute('''
INSERT INTO mac_user (mac_address, device_name, user_id)
VALUES (?, ?, ?)
''', (mac_address, device_name, session['user_id']))
conn.commit()
flash("设备绑定成功", "success")
except sqlite3.IntegrityError:
conn.rollback()
flash("绑定失败,数据完整性错误", "error")
finally:
conn.close()
return redirect(url_for('device_manage'))
return render_template('device_bind.html')
# 管理员查看所有设备路由
@app.route('/device/all')
def device_all():
if 'username' not in session or session['role'] != 'admin':
return "权限不足", 403
conn = get_db_connection()
cursor = conn.cursor()
# 查询mac表所有设备,以及绑定情况
cursor.execute('''
SELECT
m.mac_address,
mu.device_name,
mu.user_id,
u.username
FROM mac m
LEFT JOIN mac_user mu ON m.mac_address = mu.mac_address
LEFT JOIN user u ON mu.user_id = u.id
''')
devices = [dict(row) for row in cursor.fetchall()]
conn.close()
return render_template('device_all.html', devices=devices)
@app.route('/events/all')
def events_all():
if 'username' not in session:
return "请先登录", 401
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT id, role FROM user WHERE username =?', (session['username'],))
user_info = cursor.fetchone()
if not user_info:
return "用户不存在", 404
user_id = user_info['id']
role = user_info['role']
# 基础查询语句
base_query = '''
SELECT
e.event_datetime,
e.mac_address,
mu.device_name
'''
join_clause = 'FROM events e JOIN mac_user mu ON e.mac_address = mu.mac_address '
where_clause = []
params = []
if role == 'admin':
base_query += ', u.username '
join_clause += 'JOIN user u ON mu.user_id = u.id ' # 通过 mac_user 关联用户表
else:
where_clause.append("mu.user_id =?") # 使用 mac_user 的 user_id 过滤
params.append(user_id)
# 处理筛选条件(保持原逻辑)
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
device_mac = request.args.get('device_mac')
if start_date:
where_clause.append("e.event_datetime >=?")
params.append(f"{start_date} 00:00:00")
if end_date:
where_clause.append("e.event_datetime <=?")
params.append(f"{end_date} 23:59:59")
if device_mac:
where_clause.append("e.mac_address =?")
params.append(device_mac)
# 组合查询语句
final_query = base_query + join_clause
if where_clause:
final_query += "WHERE " + " AND ".join(where_clause)
# 执行查询
cursor.execute(final_query, params)
records = [dict(row) for row in cursor.fetchall()]
conn.close()
return render_template('events_all.html', records=records, role=role)
# 设备管理
@app.route('/device/manage')
def device_manage():
if 'username' not in session:
return "请先登录", 401
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('SELECT id, role FROM user WHERE username =?', (session['username'],))
user_info = cursor.fetchone()
if not user_info:
return "用户不存在", 404
user_id = user_info['id']
role = user_info['role']
if role == 'admin':
cursor.execute('''
SELECT
mac.mac_address,
mac_user.device_name,
user.username AS username
FROM mac
JOIN mac_user ON mac.mac_address = mac_user.mac_address
JOIN user ON mac_user.user_id = user.id
''')
else:
cursor.execute('''
SELECT
mac.mac_address,
mac_user.device_name
FROM mac
JOIN mac_user ON mac.mac_address = mac_user.mac_address
WHERE mac_user.user_id =?
''', (user_id,))
devices = [dict(row) for row in cursor.fetchall()]
conn.close()
return render_template('device_manage.html', devices=devices, role=role)
@app.route('/device/unbind', methods=['POST'])
def device_unbind():
if 'username' not in session:
return jsonify({"status": "error", "message": "请先登录"}), 401
mac_address = request.form.get('mac_address')
conn = get_db_connection()
cursor = conn.cursor()
user_id = session.get('user_id')
role = session.get('role')
try:
if role == 'admin':
# 管理员解绑任意设备
cursor.execute('DELETE FROM mac_user WHERE mac_address = ?', (mac_address,))
else:
# 普通用户解绑自己的设备
cursor.execute('''
DELETE FROM mac_user
WHERE mac_address = ? AND user_id = ?
''', (mac_address, user_id))
conn.commit()
return jsonify({"status": "success", "message": "解绑成功"})
except Exception as e:
conn.rollback()
return jsonify({"status": "error", "message": f"解绑失败:{str(e)}"}), 500
finally:
conn.close()
@app.route('/device/update_name')
def update_device_name():
if 'username' not in session:
return "请先登录", 401
mac_address = request.args.get('mac_address')
new_name = request.args.get('new_name')
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute('SELECT id, role FROM user WHERE username =?', (session['username'],))
user_info = cursor.fetchone()
if not user_info:
conn.close()
return "用户不存在", 404
user_id = user_info['id']
role = user_info['role']
if role == 'admin':
cursor.execute('UPDATE mac_user SET device_name =? WHERE mac_address =?', (new_name, mac_address))
else:
cursor.execute('UPDATE mac_user SET device_name =? WHERE mac_address =? AND user_id =?',
(new_name, mac_address, user_id))
conn.commit()
return 'success'
except sqlite3.Error as e:
conn.rollback() # 出错时回滚事务
return f"设备名称更新失败,数据库错误:{str(e)}", 500
finally:
conn.close()
# 数据可视化页面
@app.route('/data_visualization')
def data_visualization():
if 'user_id' not in session:
return redirect('/login')
return render_template('data_visualization.html')
# 获取折线图数据
@app.route('/api/line_chart_data', methods=['GET'])
def get_line_chart_data():
if 'user_id' not in session:
return redirect('/login')
try:
conn = sqlite3.connect('violence_events.db')
cursor = conn.cursor()
if session['role'] == 'admin':
cursor.execute('''
SELECT
e.mac_address,
SUBSTR(e.event_datetime, 1, 10) AS event_date,
COUNT(*) AS event_count,
mu.device_name
FROM events e
JOIN mac_user mu ON e.mac_address = mu.mac_address
GROUP BY e.mac_address, event_date
ORDER BY event_date
''')
else:
user_id = session['user_id']
cursor.execute('''
SELECT
e.mac_address,
SUBSTR(e.event_datetime, 1, 10) AS event_date,
COUNT(*) AS event_count,
mu.device_name
FROM events e
JOIN mac_user mu ON e.mac_address = mu.mac_address
WHERE mu.user_id = ?
GROUP BY e.mac_address, event_date
ORDER BY event_date
''', (user_id,))
data = cursor.fetchall()
conn.close()
formatted_data = {}
for mac_address, date, count, device_name in data:
if date not in formatted_data:
formatted_data[date] = {}
formatted_data[date][mac_address] = {
'event_count': count,
'device_name': device_name,
'device_id': mac_address # 明确返回设备ID(mac_address)
}
return jsonify(formatted_data), 200
except Exception as e:
return jsonify({'message': f'服务器内部错误:{str(e)}'}), 500
# 获取设备列表
@app.route('/api/devices')
def get_devices():
if 'user_id' not in session:
return jsonify({"status": "error", "message": "未登录"}), 401
conn = get_db_connection()
cursor = conn.cursor()
user_id = session['user_id']
role = session['role']
if role == 'admin':
cursor.execute('''
SELECT
mu.mac_address AS device_id,
mu.device_name
FROM mac_user mu
''')
else:
cursor.execute('''
SELECT
mu.mac_address AS device_id,
mu.device_name
FROM mac_user mu
WHERE mu.user_id = ?
''', (user_id,))
devices = [dict(row) for row in cursor.fetchall()]
conn.close()
return jsonify(devices)
# 获取热力图数据
@app.route('/api/heatmap_data')
def get_heatmap_data():
device_id = request.args.get('device_id')
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT
strftime('%m', event_datetime) AS month,
strftime('%d', event_datetime) AS day,
COUNT(*) AS event_count
FROM events
WHERE mac_address =?
GROUP BY month, day
''', (device_id,))
records = [dict(row) for row in cursor.fetchall()]
conn.close()
return jsonify(records)
@app.route('/admin/audio_logs')
def admin_audio_logs():
# 验证管理员权限
if 'role' not in session or session['role'] != 'admin':
abort(403)
# 定义日志根目录
log_root = "/root/bullyingdectection/logs"
# 遍历所有音频文件
audio_files = []
for root, dirs, files in os.walk(log_root):
for file in files:
if file.lower().endswith('.wav'):
full_path = os.path.join(root, file)
# 获取相对路径(用于生成URL)
relative_path = os.path.relpath(full_path, start=log_root)
# 获取文件信息
stat = os.stat(full_path)
audio_files.append({
"name": file,
"path": relative_path,
"size": f"{stat.st_size // 1024} KB",
"modified": datetime.fromtimestamp(stat.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
})
# 按修改时间倒序排序
audio_files.sort(key=lambda x: x["modified"], reverse=True)
return render_template('admin_audio_logs.html', files=audio_files)
# 错误处理
@app.errorhandler(403)
def handle_403(e):
return render_template('error.html', message="仅管理员可访问此页面"), 403
@app.route('/admin/audio/<path:path>')
def admin_serve_audio(path):
# 再次验证管理员权限
if 'role' not in session or session['role'] != 'admin':
abort(403)
# 防止路径穿越攻击
if '..' in path or path.startswith('/'):
abort(403)
# 构造完整路径
full_path = os.path.join("/root/bullyingdectection/logs", path)
# 检查文件是否存在
if not os.path.isfile(full_path):
abort(404)
return send_file(full_path, mimetype='audio/wav')
@app.route('/personal_center', methods=['GET', 'POST'])
def personal_center():
if 'username' not in session:
return "请先登录", 401
if request.method == 'POST':
new_username = request.form.get('new_username')
new_password = request.form.get('new_password')
confirm_password = request.form.get('confirm_password')
conn = get_db_connection()
cursor = conn.cursor()
if new_username:
try:
cursor.execute('UPDATE user SET username =? WHERE id =?', (new_username, session['user_id']))
session['username'] = new_username
except sqlite3.Error as e:
conn.rollback()
flash(f"修改用户名失败,数据库错误:{str(e)}", "error")
if new_password and new_password == confirm_password:
try:
cursor.execute('UPDATE user SET password =? WHERE id =?', (new_password, session['user_id']))
flash("密码修改成功", "success")
except sqlite3.Error as e:
conn.rollback()
flash(f"修改密码失败,数据库错误:{str(e)}", "error")
elif new_password and new_password != confirm_password:
flash("两次输入的密码不一致", "error")
conn.commit()
conn.close()
return render_template('personal_center.html')
# 注销
@app.route('/logout')
def logout():
session.pop('user_id', None)
session.pop('role', None)
return redirect('/login')
if __name__ == '__main__':
init_db() # 确保数据库初始化
app.run(host='0.0.0.0', port=5000, debug=True)