File size: 2,509 Bytes
5b3c62d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35db0ae
 
 
 
 
5b3c62d
 
 
 
 
 
 
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
import { exec } from "child_process"

/**
 * Interface for the options for the `runColmap` function.
 * See: https://colmap.github.io/cli.html
 */
export interface ColmapOptions {
  /**
   * The command to run (one of the available commands in COLMAP).
   */
  command: string;

  /**
   * The path to your project, which must contain a folder "images" with all the images.
   */
  projectPath?: string;

  /**
   * The workspace path for the project (used in some commands).
   */
  workspacePath?: string;

  /**
   * The image path for the project (used in some commands).
   */
  imagePath?: string;

  /**
   * The database path for the project (used in some commands).
   */
  databasePath?: string;

  /**
   * The output path for the project (used in some commands).
   */
  outputPath?: string;

  /**
   * Additional parameters that should be passed to the command.
   */
  parameters?: { [key: string]: any };

  /**
   * If set, forces the program to use CPU-based feature extraction and matching.
   */
  useCPU?: boolean;
}

/**
 * Run COLMAP commands from NodeJS programmatically.
 *
 * @param {ColmapOptions} options A set of configurations to be passed to the application.
 */
export const runColmap = (options: ColmapOptions): Promise<string> => {
  return new Promise((resolve, reject) => {
    let command = `colmap ${options.command}`;

    if (options.projectPath) command += ` --project_path ${options.projectPath}`;
    if (options.workspacePath) command += ` --workspace_path ${options.workspacePath}`;
    if (options.imagePath) command += ` --image_path ${options.imagePath}`;
    if (options.databasePath) command += ` --database_path ${options.databasePath}`;
    if (options.outputPath) command += ` --output_path ${options.outputPath}`;

    // Loop through additional parameters (if any) and add them to the command.
    if (options.parameters) {
      for (let key of Object.keys(options.parameters)) {
        command += ` --${key} ${options.parameters[key]}`;
      }
    }

    if (options.useCPU) command += ` --SiftExtraction.use_gpu 0 --SiftMatching.use_gpu 0`;

    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.error('Error running colmap command:', command);
        console.error('Error message:', error.message);
        console.error('Error stack:', error.stack);
        console.log("stdout:", stdout);
        console.log("stderr:", stderr);
        reject(stderr);
      } else {
        resolve(stdout);
      }
    });
  });
};