Please help me to make this code work, I'm totally stumped

import os
import glob
import csv
import rc # Reality Capture Python API

def load_gcps(gcp_file):
“”"
Load Ground Control Points (GCPs) from a CSV file.
Assumes the CSV file has columns: image_name, GCP_name, pixel_x, pixel_y, real_x, real_y, real_z
“”"
gcps =
with open(gcp_file, ‘r’) as f:
reader = csv.reader(f)
next(reader) # Skip header
for row in reader:
image_name = row[0] # This is the image name (e.g., DSC_0001)
pixel_x = float(row[2]) # Pixel X-coordinate
pixel_y = float(row[3]) # Pixel Y-coordinate
real_x = float(row[4]) # Real-world X-coordinate
real_y = float(row[5]) # Real-world Y-coordinate
real_z = float(row[6]) # Real-world Z-coordinate
gcps.append({
“image_name”: image_name,
“pixel_x”: pixel_x,
“pixel_y”: pixel_y,
“real_x”: real_x,
“real_y”: real_y,
“real_z”: real_z
})
return gcps

def add_gcps_to_project(project, gcps):
“”"
Add Ground Control Points (GCPs) to the Reality Capture project.
“”"
for gcp in gcps:
# Find the image by name and add GCP
image = project.getImageByName(gcp[“image_name”])
if image:
# Add GCPs (image pixel coordinates and real-world coordinates)
project.addGCP(
image,
gcp[“pixel_x”], gcp[“pixel_y”], # pixel coordinates
gcp[“real_x”], gcp[“real_y”], gcp[“real_z”] # real-world coordinates
)

def set_project_units_to_mm(project):
“”"
Set the project unit to millimetres (for laboratory coordinate system).
“”"
project.setUnit(rc.Unit.Millimeter) # Set unit to millimetres

def set_reconstruction_region(project):
“”"
Set a reconstruction region for the project. This ensures the model is built within a defined region.
“”"
# Define the bounding box for the reconstruction region
xmin, ymin, zmin = -100, -100, 0 # Example min coordinates
xmax, ymax, zmax = 100, 100, 100 # Example max coordinates

# Set the reconstruction region in Reality Capture
project.setReconstructionRegion(xmin, ymin, zmin, xmax, ymax, zmax)

def process_images(gcps):
“”"
Process the images in Reality Capture, applying GCPs, aligning images, building the model, and exporting the result.
“”"
# Start a new project in Reality Capture (use the existing one)
project = rc.Project()

# Set the project unit to millimetres (laboratory units)
set_project_units_to_mm(project)

# Add the GCPs to the project
add_gcps_to_project(project, gcps)

# Align the images (Structure from Motion step)
project.alignImages()  # Align images using GCPs

# Set the reconstruction region (optional step, depending on your project)
set_reconstruction_region(project)  # Define a reconstruction region

# Build the 3D model at normal detail
project.buildModel(detail=rc.ModelDetail.Normal)  # Build at normal detail

# Add texture to the model
project.buildTexturing(textureResolution=rc.TextureResolution.Medium)  # Texture at medium resolution

# Export the model as an XYZ file (point cloud format)
output_folder = r"E:\Analogue Experiments\Topography\Flat Extrusion\Models"
output_path = os.path.join(output_folder, "model.xyz")  # Name the output file appropriately
project.export(output_path, exportType=rc.ExportType.PointCloudXYZ)  # Export as XYZ file

print(f"Processed images, built model with texture, and saved to {output_path}")

def batch_process_images(image_folder, gcp_file):
“”"
Process all images and their GCPs for the project.
“”"
# Load GCPs from the file
gcps = load_gcps(gcp_file)

# Process the images with the related GCPs
process_images(gcps)

Specify the folder where your images are located and the GCP file path

image_folder = “E:\Analogue Experiments\Topography\Flat Extrusion” # Update with your images folder path
gcp_file = “E:\Analogue Experiments\Topography\Flat Extrusion\GCPs.csv” # GCPs CSV file (ensure it’s in millimetres)

Start the batch processing

batch_process_images(image_folder, gcp_file)

Hi Volcanic_Panic,
do you need this script to have in Python? Have you checked everything I sent you in my previous post? Have you tried to use it in command line interface?

Hi otrhan, I’ve managed to make adjustments but I’m still unable to import my ground control points. RC isn’t liking the format of them and I don’t know why. I’ve included the first few lines of the .csv file and any help would be hugely appreciated. The four GCPs are the same in all images hence the repetition with different positioning.

|image_name | GCP_name | pixel_x | pixel_y | real_x | real_y | real_z|
|DSC_0001 | GCP_1 | 1613.83 | 3141.39 | 0 | 0 | 0|
|DSC_0001 | GCP_2 | 1670.65 | 909.9 | 0 | 120 | 0|
|DSC_0001 | GCP_3 | 3890.74 | 981.28 | 120 | 120 | 0|
|DSC_0001 | GCP_4 | 3828.97 | 3185.87 | 120 | 0 | 0|
|DSC_0002 | GCP_1 | 1159.26 | 693.36 | 0 | 0 | 0|
|DSC_0002 | GCP_2 | 3450.79 | 727.69 | 0 | 120 | 0|
|DSC_0002 | GCP_3 | 3452.57 | 3019.13 | 120 | 120 | 0|
|DSC_0002 | GCP_4 | 995.08 | 2982.32 | 120 | 0 | 0|
|DSC_0003 | GCP_1 | 4730.83 | 3033.84 | 0 | 0 | 0|
|DSC_0003 | GCP_2 | 2113.47 | 2978.52 | 0 | 120 | 0|
|DSC_0003 | GCP_3 | 2184.32 | 439.98 | 120 | 120 | 0|
|DSC_0003 | GCP_4 | 4657.15 | 568.11 | 120 | 0 | 0|
|DSC_0004 | GCP_1 | 1613.83 | 3141.39 | 0 | 0 | 0|
|DSC_0004 | GCP_2 | 1670.65 | 909.9 | 0 | 120 | 0|
|DSC_0004 | GCP_3 | 3890.74 | 981.28 | 120 | 120 | 0|
|DSC_0004 | GCP_4 | 3828.97 | 3185.87 | 120 | 0 | 0|
|DSC_0005 | GCP_1 | 1159.26 | 693.36 | 0 | 0 | 0|
|DSC_0005 | GCP_2 | 3450.79 | 727.69 | 0 | 120 | 0|
|DSC_0005 | GCP_3 | 3452.57 | 3019.13 | 120 | 120 | 0|
|DSC_0005 | GCP_4 | 995.08 | 2982.32 | 120 | 0 | 0|

You need to separate that file to image measurements file and to GCPs file.
Then you can use the commands -importControlPointsMeasurements and -importGroundControlPoints separately.
More about the commands can be find here: RealityCapture Help.
The GCP file will look like (txt file):
| GCP_name | real_x | real_y | real_z|
| GCP_1 | 0 | 0 | 0|

The image measurement file like (csv file):
|image path with image_name | GCP_name | pixel_x | pixel_y |
C:\Set2-a.geometry\199.jpg | GCP_1 | 540.81 |182.1

You can use comma as a separator.

Thanks. Since I have three cameras, do I need three separate file pairs?

No, it all can be in one file.