|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | +from LEPAUTE import main, get_collected_data |
| 6 | +from data_access import load_data |
| 7 | +import multiprocessing |
| 8 | + |
| 9 | +async def display_all_data_window(): |
| 10 | + """Display all collected data in a single GUI window in real-time.""" |
| 11 | + window_name = "All Collected Data" |
| 12 | + cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) |
| 13 | + font = cv2.FONT_HERSHEY_SIMPLEX |
| 14 | + font_scale = 0.5 |
| 15 | + color = (0, 255, 0) # Green text |
| 16 | + thickness = 1 |
| 17 | + line_spacing = 20 |
| 18 | + |
| 19 | + while True: |
| 20 | + data = get_collected_data() |
| 21 | + height = max(100, 40 + len(data) * line_spacing * 8) |
| 22 | + img = np.zeros((height, 800, 3), dtype=np.uint8) # Black background |
| 23 | + |
| 24 | + cv2.putText(img, "Collected Data Summary", (10, 20), font, font_scale, color, thickness) |
| 25 | + |
| 26 | + if len(data) == 0: |
| 27 | + cv2.putText(img, "No data collected yet.", (10, 40), font, font_scale, color, thickness) |
| 28 | + else: |
| 29 | + for i, item in enumerate(data): |
| 30 | + y = 40 + i * line_spacing * 8 |
| 31 | + cv2.putText(img, f"Entry {i + 1}:", (10, y), font, font_scale, color, thickness) |
| 32 | + cv2.putText(img, f" Image1 shape: {len(item['image1'])}, {len(item['image1'][0])}, {len(item['image1'][0][0])}", |
| 33 | + (10, y + line_spacing), font, font_scale, color, thickness) |
| 34 | + cv2.putText(img, f" Image2 shape: {len(item['image2'])}, {len(item['image2'][0])}, {len(item['image2'][0][0])}", |
| 35 | + (10, y + 2 * line_spacing), font, font_scale, color, thickness) |
| 36 | + cv2.putText(img, f" SO(2) theta: {item['lie_params'][0][0]:.4f}", |
| 37 | + (10, y + 3 * line_spacing), font, font_scale, color, thickness) |
| 38 | + cv2.putText(img, f" SE(2) params (tx, ty): ({item['lie_params'][0][1]:.2f}, {item['lie_params'][0][2]:.2f})", |
| 39 | + (10, y + 4 * line_spacing), font, font_scale, color, thickness) |
| 40 | + cv2.putText(img, f" Model output: {[f'{x:.2f}' for x in item['output'][0][:3]]}...", |
| 41 | + (10, y + 5 * line_spacing), font, font_scale, color, thickness) |
| 42 | + cv2.putText(img, f" Loss: {item['loss']:.4f}", |
| 43 | + (10, y + 6 * line_spacing), font, font_scale, color, thickness) |
| 44 | + cv2.putText(img, f" Label: {item['label']}", |
| 45 | + (10, y + 7 * line_spacing), font, font_scale, color, thickness) |
| 46 | + |
| 47 | + cv2.imshow(window_name, img) |
| 48 | + if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to exit |
| 49 | + break |
| 50 | + await asyncio.sleep(0.1) # Update every 100ms to reduce CPU load |
| 51 | + |
| 52 | + cv2.destroyWindow(window_name) |
| 53 | + |
| 54 | +async def run_pipeline_and_access_data(display_mode: str = "json", frames_dir: str = "frames", save_json: bool = False, save_image: bool = False): |
| 55 | + print(f"Resource usage: Dynamic adjustment to maintain 10 FPS, targeting ~50% CPU ({multiprocessing.cpu_count()//2} threads initially) and ~50% GPU.") |
| 56 | + print("Monitor usage in Activity Monitor (macOS) under CPU and GPU tabs.") |
| 57 | + print(f"\nStarting the LEPAUTE pipeline in {display_mode} mode...") |
| 58 | + if save_image: |
| 59 | + print(f"Saving frames to {frames_dir}...") |
| 60 | + |
| 61 | + if display_mode in ["gui", "realtime"]: |
| 62 | + asyncio.create_task(display_all_data_window()) |
| 63 | + |
| 64 | + await main(display_mode=display_mode, frames_dir=frames_dir, unlimited=True, save_json=save_json, save_image=save_image) |
| 65 | + |
| 66 | + print("\nAccessing collected data from memory...") |
| 67 | + data = get_collected_data() |
| 68 | + |
| 69 | + print(f"Total data entries: {len(data)}") |
| 70 | + if len(data) == 0: |
| 71 | + print("No data collected. Check webcam, feature extraction, or image texture.") |
| 72 | + for i, item in enumerate(data): |
| 73 | + print(f"\nData entry {i + 1}:") |
| 74 | + print(f" Image1 shape: {len(item['image1'])}, {len(item['image1'][0])}, {len(item['image1'][0][0])}") |
| 75 | + print(f" Image2 shape: {len(item['image2'])}, {len(item['image2'][0])}, {len(item['image2'][0][0])}") |
| 76 | + print(f" SO(2) theta: {item['lie_params'][0][0]:.4f}") |
| 77 | + print(f" SE(2) params (tx, ty): ({item['lie_params'][0][1]:.2f}, {item['lie_params'][0][2]:.2f})") |
| 78 | + print(f" Model output: {item['output']}") |
| 79 | + print(f" Loss: {item['loss']}") |
| 80 | + print(f" Label: {item['label']}") |
| 81 | + |
| 82 | + if display_mode == "json" and save_json: |
| 83 | + print("\nAccessing data from file...") |
| 84 | + file_data = load_data("lepaute_data.json") |
| 85 | + print(f"Total file data entries: {len(file_data)}") |
| 86 | + if len(file_data) == 0: |
| 87 | + print("No data in file. Ensure pipeline ran successfully in json mode.") |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + frames_dir = "frames" |
| 91 | + os.makedirs(frames_dir, exist_ok=True) |
| 92 | + |
| 93 | + # Choose mode here: 'json', 'gui', or 'realtime' |
| 94 | + mode = "realtime" # Change to 'json' or 'gui' as needed |
| 95 | + save_json = False # Set to True to save JSON in json mode |
| 96 | + save_image = False # Set to True to save frames in json or gui mode |
| 97 | + |
| 98 | + print(f"Running in {mode.upper()} mode...") |
| 99 | + print(f"Save JSON: {save_json}, Save Image: {save_image}") |
| 100 | + asyncio.run(run_pipeline_and_access_data(display_mode=mode, frames_dir=frames_dir, save_json=save_json, save_image=save_image)) |
0 commit comments