-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.py
More file actions
211 lines (198 loc) · 7.99 KB
/
Copy pathstream.py
File metadata and controls
211 lines (198 loc) · 7.99 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
from flask import Flask, Response, jsonify
from ultralytics import YOLO
import cv2
import threading
import time
from collections import Counter
app = Flask(__name__)
model = YOLO('/home/admin/yolov8n.pt')
frame_lock = threading.Lock()
stats_lock = threading.Lock()
latest_frame = None
latest_stats = {"objects": {}, "fps": 0, "timestamp": 0}
def capture_and_infer():
global latest_frame, latest_stats
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
fps_counter = 0
fps_time = time.time()
while True:
ret, frame = cap.read()
if not ret:
continue
results = model(frame, verbose=False)
annotated = results[0].plot()
# Objekte zählen
names = model.names
boxes = results[0].boxes
counts = Counter()
confidences = {}
if boxes is not None:
for box in boxes:
cls = int(box.cls[0])
label = names[cls]
conf = float(box.conf[0])
counts[label] += 1
if label not in confidences or conf > confidences[label]:
confidences[label] = round(conf * 100, 1)
# FPS berechnen
fps_counter += 1
if time.time() - fps_time >= 1.0:
fps = fps_counter
fps_counter = 0
fps_time = time.time()
else:
fps = latest_stats["fps"]
with frame_lock:
latest_frame = annotated
with stats_lock:
latest_stats = {
"objects": {k: {"count": v, "conf": confidences[k]} for k, v in counts.items()},
"fps": fps,
"total": sum(counts.values()),
"timestamp": round(time.time(), 2)
}
def generate():
while True:
with frame_lock:
if latest_frame is None:
continue
_, buffer = cv2.imencode('.jpg', latest_frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + buffer.tobytes() + b'\r\n')
@app.route('/')
def index():
return '''<!DOCTYPE html>
<html>
<head>
<title>AI Drone Ground Station</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #0a0a0f; color: #e0e0e0; font-family: monospace; }
header {
background: #111118;
border-bottom: 1px solid #00ff8822;
padding: 12px 24px;
display: flex;
align-items: center;
gap: 12px;
}
header h1 { font-size: 16px; color: #00ff88; letter-spacing: 2px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #00ff88;
animation: pulse 1.5s infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }
.layout { display: grid; grid-template-columns: 1fr 300px; gap: 16px;
padding: 16px; height: calc(100vh - 50px); }
.video-box { background: #111118; border: 1px solid #1e1e2e;
border-radius: 8px; overflow: hidden; display: flex;
flex-direction: column; }
.video-box img { width: 100%; height: 100%; object-fit: contain; }
.sidebar { display: flex; flex-direction: column; gap: 12px; }
.card { background: #111118; border: 1px solid #1e1e2e;
border-radius: 8px; padding: 16px; }
.card h2 { font-size: 11px; color: #666; letter-spacing: 2px;
text-transform: uppercase; margin-bottom: 12px; }
.stat-row { display: flex; justify-content: space-between;
align-items: center; padding: 6px 0;
border-bottom: 1px solid #1e1e2e; }
.stat-row:last-child { border-bottom: none; }
.stat-label { color: #aaa; font-size: 13px; }
.stat-value { color: #00ff88; font-size: 14px; font-weight: bold; }
.obj-row { display: flex; justify-content: space-between;
align-items: center; padding: 8px 0;
border-bottom: 1px solid #1a1a2e; }
.obj-row:last-child { border-bottom: none; }
.obj-name { color: #e0e0e0; font-size: 13px; text-transform: capitalize; }
.obj-count { background: #00ff8822; color: #00ff88; padding: 2px 8px;
border-radius: 4px; font-size: 12px; }
.obj-conf { color: #555; font-size: 11px; }
.empty { color: #444; font-size: 12px; text-align: center; padding: 20px 0; }
.conf-bar { height: 3px; background: #1e1e2e; border-radius: 2px; margin-top: 4px; }
.conf-fill { height: 100%; background: #00ff88; border-radius: 2px;
transition: width 0.3s; }
</style>
</head>
<body>
<header>
<div class="dot"></div>
<h1>⬡ AI DRONE GROUND STATION</h1>
</header>
<div class="layout">
<div class="video-box">
<img src="/video" />
</div>
<div class="sidebar">
<div class="card">
<h2>System</h2>
<div class="stat-row">
<span class="stat-label">FPS</span>
<span class="stat-value" id="fps">—</span>
</div>
<div class="stat-row">
<span class="stat-label">Objekte gesamt</span>
<span class="stat-value" id="total">—</span>
</div>
<div class="stat-row">
<span class="stat-label">Model</span>
<span class="stat-value">YOLOv8n</span>
</div>
<div class="stat-row">
<span class="stat-label">Status</span>
<span class="stat-value" id="status">—</span>
</div>
</div>
<div class="card" style="flex:1; overflow-y:auto;">
<h2>Erkannte Objekte</h2>
<div id="objects"><div class="empty">Keine Objekte</div></div>
</div>
</div>
</div>
<script>
let lastTs = 0;
async function update() {
try {
const r = await fetch('/stats');
const d = await r.json();
document.getElementById('fps').textContent = d.fps + ' FPS';
document.getElementById('total').textContent = d.total;
document.getElementById('status').textContent = d.timestamp !== lastTs ? 'LIVE' : 'WAIT';
lastTs = d.timestamp;
const box = document.getElementById('objects');
const objs = Object.entries(d.objects);
if (objs.length === 0) {
box.innerHTML = '<div class="empty">Keine Objekte erkannt</div>';
} else {
box.innerHTML = objs.map(([name, info]) => `
<div class="obj-row">
<div>
<div class="obj-name">${name}</div>
<div class="conf-bar">
<div class="conf-fill" style="width:${info.conf}%"></div>
</div>
</div>
<div style="text-align:right">
<div class="obj-count">×${info.count}</div>
<div class="obj-conf">${info.conf}%</div>
</div>
</div>`).join('');
}
} catch(e) {}
}
setInterval(update, 500);
update();
</script>
</body>
</html>'''
@app.route('/stats')
def stats():
with stats_lock:
return jsonify(latest_stats)
@app.route('/video')
def video():
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
t = threading.Thread(target=capture_and_infer, daemon=True)
t.start()
print("Ground Station läuft auf http://10.10.10.197:5000/")
app.run(host='0.0.0.0', port=5000, threaded=True)