|
1 | 1 |
|
2 | 2 | import logging
|
| 3 | +from multiprocessing import Pool |
3 | 4 |
|
4 | 5 | import cv2
|
5 | 6 | import numpy as np
|
|
14 | 15 | def compute_depthmaps(data, graph, reconstruction):
|
15 | 16 | """Compute and refine depthmaps for all shots."""
|
16 | 17 | logger.info('Computing neighbors')
|
17 |
| - neighbors = {} |
| 18 | + processes = data.config.get('processes', 1) |
18 | 19 | num_neighbors = data.config['depthmap_num_neighbors']
|
| 20 | + |
| 21 | + neighbors = {} |
19 | 22 | for shot in reconstruction.shots.values():
|
20 | 23 | neighbors[shot.id] = find_neighboring_images(
|
21 | 24 | shot, graph, reconstruction, num_neighbors)
|
22 | 25 |
|
| 26 | + arguments = [] |
23 | 27 | for shot in reconstruction.shots.values():
|
24 | 28 | if len(neighbors[shot.id]) <= 1:
|
25 | 29 | continue
|
26 |
| - logger.info("Computing depthmap for image {}".format(shot.id)) |
27 |
| - compute_depthmap(data, graph, reconstruction, neighbors, shot) |
| 30 | + arguments.append((data, graph, reconstruction, neighbors, shot)) |
| 31 | + parallel_run(compute_depthmap, arguments, processes) |
28 | 32 |
|
| 33 | + arguments = [] |
29 | 34 | for shot in reconstruction.shots.values():
|
30 | 35 | if len(neighbors[shot.id]) <= 1:
|
31 | 36 | continue
|
32 |
| - logger.info("Cleaning depthmap for image {}".format(shot.id)) |
33 |
| - clean_depthmap(data, graph, reconstruction, neighbors, shot) |
| 37 | + arguments.append((data, graph, reconstruction, neighbors, shot)) |
| 38 | + parallel_run(clean_depthmap, arguments, processes) |
34 | 39 |
|
35 | 40 | merge_depthmaps(data, graph, reconstruction, neighbors)
|
36 | 41 |
|
37 | 42 |
|
38 |
| -def compute_depthmap(data, graph, reconstruction, neighbors, shot): |
| 43 | +def parallel_run(function, arguments, num_processes): |
| 44 | + """Run function for all arguments using multiple processes.""" |
| 45 | + num_processes = min(num_processes, len(arguments)) |
| 46 | + if num_processes == 1: |
| 47 | + return [function(arg) for arg in arguments] |
| 48 | + else: |
| 49 | + p = Pool(num_processes) |
| 50 | + return p.map(function, arguments) |
| 51 | + |
| 52 | + |
| 53 | +def compute_depthmap(arguments): |
39 | 54 | """Compute depthmap for a single shot."""
|
| 55 | + data, graph, reconstruction, neighbors, shot = arguments |
| 56 | + |
40 | 57 | if data.raw_depthmap_exists(shot.id):
|
| 58 | + logger.info("Using precomputed raw depthmap {}".format(shot.id)) |
41 | 59 | return
|
| 60 | + logger.info("Computing depthmap for image {}".format(shot.id)) |
42 | 61 |
|
43 | 62 | min_depth, max_depth = compute_depth_range(graph, reconstruction, shot)
|
44 | 63 |
|
@@ -75,9 +94,14 @@ def compute_depthmap(data, graph, reconstruction, neighbors, shot):
|
75 | 94 | plt.show()
|
76 | 95 |
|
77 | 96 |
|
78 |
| -def clean_depthmap(data, graph, reconstruction, neighbors, shot): |
| 97 | +def clean_depthmap(arguments): |
| 98 | + """Clean depthmap by checking consistency with neighbors.""" |
| 99 | + data, graph, reconstruction, neighbors, shot = arguments |
| 100 | + |
79 | 101 | if data.clean_depthmap_exists(shot.id):
|
| 102 | + logger.info("Using precomputed clean depthmap {}".format(shot.id)) |
80 | 103 | return
|
| 104 | + logger.info("Cleaning depthmap for image {}".format(shot.id)) |
81 | 105 |
|
82 | 106 | dc = csfm.DepthmapCleaner()
|
83 | 107 | dc.set_same_depth_threshold(data.config['depthmap_same_depth_threshold'])
|
@@ -108,6 +132,9 @@ def clean_depthmap(data, graph, reconstruction, neighbors, shot):
|
108 | 132 |
|
109 | 133 |
|
110 | 134 | def merge_depthmaps(data, graph, reconstruction, neighbors):
|
| 135 | + """Merge depthmaps into a single point cloud.""" |
| 136 | + logger.info("Merging depthmaps") |
| 137 | + |
111 | 138 | # Load clean depthmaps.
|
112 | 139 | depths, planes = {}, {}
|
113 | 140 | for sid in neighbors:
|
|
0 commit comments