forked from TUM-HN/iTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheatmap.py
More file actions
554 lines (449 loc) · 21.2 KB
/
Copy pathheatmap.py
File metadata and controls
554 lines (449 loc) · 21.2 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
import matplotlib
matplotlib.use('Agg')
import numpy as np
from flask import Flask, request, send_file, jsonify
from flask_cors import CORS
import cv2
import tempfile
import os
import json
import subprocess
import time
from datetime import datetime
import threading
import socket
from zeroconf import ServiceInfo, Zeroconf
import uuid
import shutil
import sys
import argparse
import glob
# Configuration
OUTPUT_DIR = os.path.expanduser("~/Desktop/Heatmap")
# Global state
app = Flask(__name__)
CORS(app)
def find_video_file(folder_path):
"""Find the first video file in the folder"""
video_extensions = ['*.mp4', '*.avi', '*.mov', '*.mkv', '*.flv', '*.wmv']
for ext in video_extensions:
video_files = glob.glob(os.path.join(folder_path, ext))
if video_files:
print(f"Found video file: {video_files[0]}")
return video_files[0]
print(f"No video files found in {folder_path}")
return None
def reduce_video_quality(input_path, max_width=1280, max_height=720, crf=28):
try:
cap = cv2.VideoCapture(input_path)
w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
cap.release()
scale = min(min(max_width / w, max_height / h), 1.0)
new_w, new_h = int(w * scale) & ~1, int(h * scale) & ~1
if scale >= 0.95:
return input_path, 1.0, 1.0
reduced_path = input_path.replace('.mp4', '_reduced.mp4')
# Check if input has audio and preserve it
probe_cmd = ['ffprobe', '-v', 'quiet', '-select_streams', 'a', '-show_entries', 'stream=codec_type', '-of', 'csv=p=0', input_path]
has_audio = False
try:
result = subprocess.run(probe_cmd, capture_output=True, text=True)
has_audio = 'audio' in result.stdout
except:
pass
if has_audio:
cmd = ['ffmpeg', '-i', input_path, '-vf', f'scale={new_w}:{new_h}',
'-c:v', 'libx264', '-c:a', 'aac', '-preset', 'ultrafast', '-crf', str(crf), '-y', reduced_path]
else:
cmd = ['ffmpeg', '-i', input_path, '-vf', f'scale={new_w}:{new_h}',
'-c:v', 'libx264', '-preset', 'ultrafast', '-crf', str(crf), '-an', '-y', reduced_path]
if subprocess.run(cmd, capture_output=True).returncode == 0:
return reduced_path, new_w / w, new_h / h
return input_path, 1.0, 1.0
except:
return input_path, 1.0, 1.0
def create_heatmap_overlay(brightness_grid, video_width, video_height, base_sigma=40, base_resolution=1920):
if np.sum(brightness_grid) == 0:
return None
resolution_scale = video_width / base_resolution
scaled_sigma = max(base_sigma * resolution_scale, 5.0)
blurred = cv2.GaussianBlur(brightness_grid.astype(np.float32), (0, 0), scaled_sigma)
if np.max(blurred) > 0:
blurred = (blurred / np.max(blurred) * 255).astype(np.uint8)
return cv2.applyColorMap(blurred, cv2.COLORMAP_INFERNO)
def generate_filename(tracking_data, suffix=""):
timestamp = tracking_data.get('timestamp', datetime.now().strftime("%Y%m%d_%H%M%S"))
user_name = tracking_data.get('user_name', 'unknown_user').replace(' ', '_')
tracking_type = tracking_data.get('tracking_type', 'unknown')
if tracking_data.get('video_name'):
video_name = os.path.splitext(tracking_data['video_name'])[0].replace(' ', '_')
base_name = f"{user_name}_{video_name}_{tracking_type}_{timestamp}"
else:
base_name = f"{user_name}_{tracking_type}_{timestamp}"
return f"{base_name}{suffix}"
def save_tracking_data(tracking_data, filename_base):
try:
os.makedirs(OUTPUT_DIR, exist_ok=True)
json_path = os.path.join(OUTPUT_DIR, f"{filename_base}_data.json")
with open(json_path, 'w') as f:
json.dump(tracking_data, f, indent=2)
return json_path
except Exception as e:
print(f"Error saving tracking data: {e}")
return None
def generate_averaged_heatmap(video_path, all_click_data, output_folder=None):
"""Generate averaged heatmap by reusing existing generate_heatmap function"""
if output_folder is None:
output_folder = OUTPUT_DIR
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Create fake tracking_data that mimics the expected format
tracking_data = {
'click_data': all_click_data,
'user_name': 'averaged',
'tracking_type': 'heatmap',
'timestamp': timestamp
}
# Use existing generate_heatmap function
temp_output = generate_heatmap(video_path, tracking_data)
if temp_output and os.path.exists(temp_output):
# Move to final location with timestamped name
final_video_path = os.path.join(output_folder, f"averaged_heatmap_{timestamp}.mp4")
shutil.move(temp_output, final_video_path)
# Load data from original JSON files
folder_path = os.path.dirname(video_path)
participants = []
for json_file in glob.glob(os.path.join(folder_path, "*.json")):
try:
with open(json_file, 'r') as f:
data = json.load(f)
if 'user_name' in data and 'precision_score' in data and 'click_data' in data:
participants.append({
"user_name": data['user_name'],
"click_count": len(data['click_data']),
"precision_score": data['precision_score']
})
except Exception as e:
print(f"Error processing {json_file}: {e}")
summary_data = {
"participant_count": len(participants),
"video_name": os.path.basename(video_path),
"participants": participants,
"generation_timestamp": timestamp,
"processing_type": "averaged_heatmap"
}
summary_path = os.path.join(output_folder, f"averaged_heatmap_{timestamp}.json")
with open(summary_path, 'w') as f:
json.dump(summary_data, f, indent=2)
print(f"Averaged heatmap generated: {final_video_path}")
return final_video_path
return None
def process_folder(folder_path):
"""Process a folder containing JSON files and video to generate averaged heatmap"""
if not os.path.exists(folder_path):
print(f"Error: Folder {folder_path} does not exist")
return None
# Load all JSON files
all_click_data = load_json_files(folder_path)
if not all_click_data:
print("No valid click data found in JSON files")
return None
# Find video file
video_path = find_video_file(folder_path)
if not video_path:
print("No video file found in folder")
return None
# Generate averaged heatmap
output_path = generate_averaged_heatmap(video_path, all_click_data, OUTPUT_DIR)
if output_path:
print(f"Successfully generated averaged heatmap: {output_path}")
return output_path
else:
print("Failed to generate averaged heatmap")
return None
def generate_heatmap(video_path, tracking_data):
try:
reduced_path, scale_x, scale_y = reduce_video_quality(video_path)
filename_base = generate_filename(tracking_data)
os.makedirs(OUTPUT_DIR, exist_ok=True)
output_path = os.path.join(OUTPUT_DIR, f"{filename_base}_heatmap.mp4")
save_tracking_data(tracking_data, filename_base)
cap = cv2.VideoCapture(reduced_path)
if not cap.isOpened():
return None
fps = cap.get(cv2.CAP_PROP_FPS)
w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Create temporary video without audio for processing
temp_video_path = output_path.replace('.mp4', '_temp.mp4')
out = cv2.VideoWriter(temp_video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
fade_duration = int(fps * 0.3)
click_data = tracking_data.get('click_data', [])
brightness_per_frame = np.zeros((frame_count, h, w), dtype=np.float32)
for click in click_data:
x, y = int(float(click["x"]) * w), int(float(click["y"]) * h)
x, y = min(max(x, 0), w - 1), min(max(y, 0), h - 1)
start_frame = max(0, int((click["timestamp"] * fps) - fade_duration))
end_frame = min(start_frame + fade_duration * 2, frame_count)
frame_range = np.arange(start_frame, end_frame)
fade_in = frame_range < start_frame + fade_duration
fade_out = frame_range >= end_frame - fade_duration
brightness = np.ones_like(frame_range, dtype=np.float32)
brightness[fade_in] = (frame_range[fade_in] - start_frame) / fade_duration
brightness[fade_out] = (end_frame - frame_range[fade_out]) / fade_duration
brightness_per_frame[frame_range, y, x] += brightness
max_brightness = np.max(brightness_per_frame)
if max_brightness > 1.0:
brightness_per_frame = np.sqrt(brightness_per_frame / max_brightness)
batch_size = 50 if w * h < 1000000 else 25
for i in range(0, frame_count, batch_size):
batch_end = min(i + batch_size, frame_count)
progress = int((i / frame_count) * 100)
print(f"Video generation: {progress}%")
for j in range(i, batch_end):
ret, frame = cap.read()
if not ret: break
darkened = cv2.addWeighted(frame, 0.5, np.zeros_like(frame), 0.5, 0)
heatmap = create_heatmap_overlay(brightness_per_frame[j], w, h)
if heatmap is not None:
result = cv2.addWeighted(darkened, 1.0, heatmap, 0.8, 0)
else:
result = darkened
out.write(result)
# Get the last frame for final heatmap overlay
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count - 1)
ret, last_frame = cap.read()
if not ret:
# If we can't get the last frame, reset to beginning and read through
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
for _ in range(frame_count):
ret, last_frame = cap.read()
if not ret:
last_frame = np.zeros((h, w, 3), dtype=np.uint8)
break
# Add final heatmap frame with extended duration
final_grid = np.zeros((h, w), dtype=np.float32)
for click in click_data:
x, y = int(float(click["x"]) * w), int(float(click["y"]) * h)
if 0 <= x < w and 0 <= y < h:
final_grid[y, x] += 1
if np.sum(final_grid) > 0:
if np.max(final_grid) > 1.0:
final_grid = np.sqrt(final_grid / np.max(final_grid))
final_heatmap = create_heatmap_overlay(final_grid, w, h)
if final_heatmap is not None:
# Darken the last frame and overlay the heatmap
darkened_last = cv2.addWeighted(last_frame, 0.5, np.zeros_like(last_frame), 0.5, 0)
final_frame = cv2.addWeighted(darkened_last, 1.0, final_heatmap, 0.8, 0)
out.write(final_frame)
cap.release()
out.release()
# Check if original video has audio and merge it
probe_cmd = ['ffprobe', '-v', 'quiet', '-select_streams', 'a', '-show_entries', 'stream=codec_type', '-of', 'csv=p=0', reduced_path]
has_audio = False
try:
result = subprocess.run(probe_cmd, capture_output=True, text=True)
has_audio = 'audio' in result.stdout
except:
pass
if has_audio:
# Get duration of temp video to ensure audio sync
duration_cmd = ['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'csv=p=0', temp_video_path]
try:
duration_result = subprocess.run(duration_cmd, capture_output=True, text=True)
temp_duration = float(duration_result.stdout.strip())
# Merge video with audio
merge_cmd = [
'ffmpeg', '-i', temp_video_path, '-i', reduced_path,
'-c:v', 'libx264', '-c:a', 'aac', '-map', '0:v:0', '-map', '1:a:0',
'-t', str(temp_duration),
'-y', output_path
]
result = subprocess.run(merge_cmd, capture_output=True)
if result.returncode == 0:
os.unlink(temp_video_path)
print("Audio merged successfully")
else:
print(f"Failed to merge audio: {result.stderr.decode() if result.stderr else 'Unknown error'}")
shutil.move(temp_video_path, output_path)
except Exception as e:
print(f"Error during audio merge: {e}")
shutil.move(temp_video_path, output_path)
else:
print("No audio found in original video")
shutil.move(temp_video_path, output_path)
if reduced_path != video_path:
try:
os.unlink(reduced_path)
except:
pass
print("Heatmap generation completed")
return output_path
except Exception as e:
print(f"Error generating heatmap: {e}")
return None
def find_free_port():
"""Find a random free port"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('', 0))
s.listen(1)
port = s.getsockname()[1]
return port
def get_local_ip():
"""Get local IP address, trying multiple interfaces"""
try:
# Try connecting to a remote address to get local IP
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
return ip
except:
try:
# Fallback: get hostname IP
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
if ip.startswith("127."):
# If localhost, try to get actual IP
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("1.1.1.1", 80))
ip = s.getsockname()[0]
return ip
except:
return "127.0.0.1"
def register_service(port):
try:
zeroconf = Zeroconf()
local_ip = get_local_ip()
hostname = socket.gethostname()
unique_id = str(uuid.uuid4())[:8]
service_name = f"Vision Pro Server {unique_id}"
service_type = "_visionpro._tcp.local."
info = ServiceInfo(
service_type,
f"{service_name}.{service_type}",
addresses=[socket.inet_aton(local_ip)],
port=port,
properties={
'description': 'Apple Vision Pro Heatmap Generation Server',
'hostname': hostname,
'unique_id': unique_id
}
)
zeroconf.register_service(info)
print(f"Service registered: {service_name} at {local_ip}:{port}")
return zeroconf, info
except Exception as e:
print(f"Failed to register service: {e}")
return None, None
current_recording_process = None
current_recording_filepath = None
@app.route('/start_recording', methods=['POST'])
def start_recording():
global current_recording_process, current_recording_filepath
if current_recording_process:
current_recording_process.terminate()
current_recording_process.wait()
current_recording_process = None
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
current_recording_filepath = os.path.join(tempfile.gettempdir(), f"temp_recording_{timestamp}.mp4")
audio_filepath = os.path.join(tempfile.gettempdir(), f"temp_audio_{timestamp}.wav")
video_filepath = os.path.join(tempfile.gettempdir(), f"temp_video_{timestamp}.mp4")
def record():
global current_recording_process
# Start SoX audio recording
audio_cmd = ['sox', '-d', audio_filepath]
audio_process = subprocess.Popen(audio_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# FFmpeg video recording
cmd = ['ffmpeg', '-f', 'avfoundation', '-i', '1', '-r', '20',
'-vf', 'crop=iw:ih*0.865:0:ih*0.085,scale=1280:720',
'-vcodec', 'libx264', '-preset', 'veryfast', '-crf', '25',
'-pix_fmt', 'yuv420p', '-y', video_filepath]
current_recording_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait for video recording to finish
current_recording_process.wait()
# Stop audio recording
audio_process.terminate()
audio_process.wait()
# Merge audio and video
merge_cmd = [
'ffmpeg', '-i', video_filepath, '-i', audio_filepath,
'-c:v', 'copy', '-c:a', 'aac', '-shortest', '-y', current_recording_filepath
]
subprocess.run(merge_cmd, capture_output=True)
# Clean up temp files
try:
os.unlink(video_filepath)
os.unlink(audio_filepath)
except:
pass
threading.Thread(target=record).start()
return jsonify({"status": "success", "message": "Recording with SoX audio started"})
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
@app.route('/stop_recording', methods=['POST'])
def stop_recording():
global current_recording_process, current_recording_filepath
try:
data = request.get_json()
tracking_data = data.get('tracking_data', {})
if current_recording_process:
current_recording_process.terminate()
current_recording_process.wait()
current_recording_process = None
time.sleep(2)
if current_recording_filepath and os.path.exists(current_recording_filepath):
heatmap_path = generate_heatmap(current_recording_filepath, tracking_data)
os.unlink(current_recording_filepath)
if heatmap_path:
return send_file(heatmap_path, mimetype='video/mp4', download_name='heatmap.mp4')
else:
return jsonify({"status": "error", "message": "Failed to generate heatmap"}), 500
else:
return jsonify({"status": "error", "message": "Recording file not found"}), 500
else:
return jsonify({"status": "error", "message": "No active recording"}), 400
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
@app.route('/generate_heatmap', methods=['POST'])
def generate_heatmap_endpoint():
try:
video_file = request.files['video']
tracking_data = json.loads(request.form.get('tracking_data'))
temp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
video_file.save(temp_input.name)
heatmap_path = generate_heatmap(temp_input.name, tracking_data)
os.unlink(temp_input.name)
if heatmap_path:
return send_file(heatmap_path, mimetype='video/mp4', download_name='heatmap.mp4')
else:
return jsonify({"status": "error", "message": "Failed to generate heatmap"}), 500
except Exception as e:
return jsonify({"status": "error", "message": str(e)}), 500
def main():
parser = argparse.ArgumentParser(description='Vision Pro Heatmap Server')
parser.add_argument('--folder', '-f', type=str, help='Folder path containing JSON files and video to process')
parser.add_argument('--server', '-s', action='store_true', help='Start the Flask server (default behavior)')
parser.add_argument('--port', '-p', type=int, help='Port to run server on (default: random free port)')
args = parser.parse_args()
if args.folder:
# Process folder mode
result = process_folder(args.folder)
if result:
sys.exit(0)
else:
print("Processing failed")
sys.exit(1)
else:
# Server mode
port = args.port if args.port else find_free_port()
local_ip = get_local_ip()
zeroconf, service_info = register_service(port)
try:
print(f"Server starting on {local_ip}:{port}")
app.run(host='0.0.0.0', port=port, debug=True)
finally:
if zeroconf and service_info:
zeroconf.unregister_service(service_info)
zeroconf.close()
if __name__ == "__main__":
main()