Skip to content

Commit

Permalink
more removals of file based monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMariday committed Jun 13, 2024
1 parent 24f0667 commit 4b0c4a1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 36 deletions.
4 changes: 1 addition & 3 deletions lib/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


def debug(string):
print(string)
pass # print(string)


def error(string):
Expand Down
26 changes: 24 additions & 2 deletions lib/sfm/sfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
class SFM(Process):

def __init__(
self, directory: Path, rescale=False, interpolate=False, event_on_update=None
self,
directory: Path,
rescale=False,
interpolate=False,
event_on_update=None,
led_map_2d_queue=None,
led_map_3d_queue=None,
):
logging.debug("SFM initialising")
super().__init__()
Expand All @@ -26,8 +32,16 @@ def __init__(
self.exit_event = Event()
self.reload_event = Event()
self.event_on_update = event_on_update
self.led_map_3d_queue = led_map_3d_queue
self.led_map_2d_queue = led_map_2d_queue

self.led_map_2d_queue.put(get_all_2d_led_maps(self.directory))

logging.debug("SFM initialised")

def add_led_maps_2d(self, maps):
self.led_map_2d_queue.put(maps)

def shutdown(self):
logging.debug("SFM sending shutdown request")
self.exit_event.set()
Expand All @@ -48,10 +62,16 @@ def reload__(self):
logging.debug("SFM process reloading")
maps_2d = get_all_2d_led_maps(self.directory)
if len(maps_2d) < 2:
self.reload_event.clear()
return None

logging.debug(f"SFM process running on {len(maps_2d)} maps")

map_3d = self.process__(maps_2d, self.rescale, self.interpolate)
if map_3d is None:
self.reload_event.clear()
return None

map_3d.write_to_file(self.directory / "led_map_3d.csv")

self.event_on_update.set()
Expand Down Expand Up @@ -85,7 +105,9 @@ def process__(maps_2d, rescale=False, interpolate=False):
)

if not os.path.exists(os.path.join(temp_dir, "0", "points3D.bin")):
logging.debug("SFM process failed to run sfm process as reconstruction failed")
logging.debug(
"SFM process failed to run sfm process as reconstruction failed"
)
return None

map_3d, cams = get_map_and_cams(temp_dir)
Expand Down
26 changes: 15 additions & 11 deletions lib/visualize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
import open3d
from lib import logging
from multiprocessing import Process, Event
from lib.led_map_3d import LEDMap3D


class Renderer3D(Process):

def __init__(self, filename):
def __init__(self, led_map_3d_queue):
logging.debug("Renderer3D initialising")
super().__init__()
self.filename = filename
self._vis = None
self.exit_event = Event()
self.reload_event = Event()
self.led_map_3d_queue = led_map_3d_queue
self.point_cloud = None
logging.debug("Renderer3D initialised")

def __del__(self):
if self._vis is not None:
self._vis.destroy_window()

def get_reload_event(self):
return self.reload_event

def shutdown(self):
self.exit_event.set()

Expand All @@ -31,17 +33,19 @@ def reload(self):
def run(self):
logging.debug("Renderer3D process starting")

self.initialise_visualiser__()

self.reload_geometry__(first=True)
self._vis.update_renderer()
while not self.reload_event.wait(timeout=1):
if self.exit_event.is_set():
return

logging.debug("Renderer3D process initialised and reloaded geometry for the first time")
self.initialise_visualiser__()
self.reload_geometry__(True)

while not self.exit_event.is_set():
if self.reload_event.is_set():
logging.debug("Renderer3D process received reload event, reloading geometry")
self.reload_geometry__()
logging.debug(
"Renderer3D process received reload event, reloading geometry"
)

window_closed = not self._vis.poll_events()

Expand All @@ -56,7 +60,7 @@ def initialise_visualiser__(self):

self._vis = open3d.visualization.Visualizer()
self._vis.create_window(
window_name=f"MariMapper - {self.filename}",
window_name="MariMapper",
width=640,
height=640,
)
Expand All @@ -80,7 +84,7 @@ def initialise_visualiser__(self):
def reload_geometry__(self, first=False):
logging.debug("Renderer3D process reloading geometry")

led_map = LEDMap3D(filename=self.filename)
led_map = self.led_map_3d_queue.get()

xyz = []
normals = []
Expand Down
38 changes: 25 additions & 13 deletions marimapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,46 @@
from lib.led_map_2d import LEDMap2D
from lib.sfm.sfm import SFM
from lib.visualize_model import Renderer3D

from multiprocessing import Queue
from lib.led_map_2d import get_all_2d_led_maps

# PYCHARM DEVELOPER WARNING!
# You MUST enabled "Emulate terminal in output console" in the run configuration or
# You MUST enable "Emulate terminal in output console" in the run configuration or
# really weird stuff happens with multiprocessing!


class MariMapper:

def __init__(self, args):
self.led_backend = utils.get_backend(args.backend, args.server)
def __init__(self, cli_args):
self.led_backend = utils.get_backend(cli_args.backend, cli_args.server)
os.makedirs(args.output_dir, exist_ok=True)

self.led_map_2d_queue = Queue()
self.led_map_3d_queue = Queue()

self.reconstructor = Reconstructor(
args.device,
args.exposure,
args.threshold,
cli_args.device,
cli_args.exposure,
cli_args.threshold,
self.led_backend,
width=args.width,
height=args.height,
width=cli_args.width,
height=cli_args.height,
)

self.renderer3d = Renderer3D(Path(args.output_dir) / "led_map_3d.csv")
self.renderer3d = Renderer3D(led_map_3d_queue=self.led_map_3d_queue)
self.sfm = SFM(
Path(args.output_dir),
Path(cli_args.output_dir),
rescale=True,
interpolate=True,
event_on_update=self.renderer3d.reload_event,
event_on_update=self.renderer3d.get_reload_event(),
led_map_2d_queue=self.led_map_2d_queue,
led_map_3d_queue=self.led_map_3d_queue,
)

self.led_maps_2d = get_all_2d_led_maps(Path(cli_args.output_dir))

self.sfm.add_led_maps_2d(self.led_maps_2d)

self.sfm.start()
self.renderer3d.start()

Expand Down Expand Up @@ -124,6 +134,8 @@ def mainloop(self):
led_map_2d.write_to_file(filepath)
cprint(f"{total_leds_found}/{led_count} leds found", Col.BLUE)

self.led_maps_2d.append(led_map_2d)
self.sfm.add_led_maps_2d(self.led_maps_2d)
self.sfm.reload()


Expand All @@ -146,6 +158,6 @@ def mainloop(self):

args = parser.parse_args()

marimapper = MariMapper(args=args)
marimapper = MariMapper(cli_args=args)

marimapper.mainloop()
10 changes: 5 additions & 5 deletions test/test_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def check_dimensions(map_3d, max_error):
def test_reconstruction():
maps = get_all_2d_led_maps("test/scan")

map_3d = SFM.process(maps)
map_3d = SFM.process__(maps)

assert len(map_3d) == 21

Expand All @@ -50,7 +50,7 @@ def test_sparse_reconstruction():

maps_sparse = [maps[1], maps[3], maps[5], maps[7]]

map_3d = SFM.process(maps_sparse)
map_3d = SFM.process__(maps_sparse)

assert map_3d is not None

Expand All @@ -64,7 +64,7 @@ def test_sparse_reconstruction():
def test_2_track_reconstruction():
partial_map = get_all_2d_led_maps("test/scan")[1:3]

map_3d = SFM.process(partial_map)
map_3d = SFM.process__(partial_map)

assert map_3d is not None

Expand All @@ -76,14 +76,14 @@ def test_invalid_reconstruction_views():

invalid_maps = [maps[0], maps[4], maps[8]] # no useful overlap

map_3d = SFM.process(invalid_maps)
map_3d = SFM.process__(invalid_maps)

assert map_3d is None


def test_reconstruct_higbeam():
highbeam_map = get_all_2d_led_maps("test/MariMapper-Test-Data/highbeam")

map_3d = SFM.process(highbeam_map)
map_3d = SFM.process__(highbeam_map)

assert map_3d is not None
4 changes: 2 additions & 2 deletions test/test_remesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


def test_remesh_higbeam():
highbeam_map = get_all_2d_led_maps("test/MariMapper-Test-Data/highbeam")
highbeam_maps = get_all_2d_led_maps("test/MariMapper-Test-Data/highbeam")

map_3d = SFM.process(highbeam_map)
map_3d = SFM.process__(highbeam_maps)

mesh_high_res = remesh(map_3d, 8)

Expand Down

0 comments on commit 4b0c4a1

Please sign in to comment.