Почему я получил ошибку памяти в spyder при преобразовании pointcloud_.txt в pcd? - PullRequest
0 голосов
/ 10 июля 2019

Я запускаю этот код с https://github.com/intel-isl/Open3D-PointNet2-Semantic3D/blob/master/preprocess.py, Я использую Spyder (Python 3.6), CUDA 9.0 на Windows, но оригинальный код используется на Ubuntu

Я сделал некоторые изменения, но яполучил ошибку

[txt->pts]
txt: D:\Open3D-PointNet2-Semantic3D-master\dataset\semantic_raw\sg27_station1_intensity_rgb.txt
pts: D:\Open3D-PointNet2-Semantic3D-master\dataset\semantic_raw\sg27_station1_intensity_rgb.pts
Traceback (most recent call last):

  File "<ipython-input-2-dc44e02ef908>", line 1, in <module>
    runfile('D:/Open3D-PointNet2-Semantic3D-master/preprocess.py', wdir='D:/Open3D-PointNet2-Semantic3D-master')

  File "C:\Users\SAGHO13\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\SAGHO13\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Open3D-PointNet2-Semantic3D-master/preprocess.py", line 78, in <module>
    point_cloud_txt_to_pcd(raw_dir, file_prefix)

  File "D:/Open3D-PointNet2-Semantic3D-master/preprocess.py", line 52, in point_cloud_txt_to_pcd
    prepend_line(pts_file, str(wc(txt_file)))

  File "D:/Open3D-PointNet2-Semantic3D-master/preprocess.py", line 20, in prepend_line
    content = f.read()

MemoryError

Это модифицированный код:

import os
import argparse
import subprocess
import shutil
import open3d
import numpy as np
from dataset.semantic_dataset import all_file_prefixes


def wc(file_name):  
    out = subprocess.Popen(
        ["wc", "-l", file_name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell= True
    ).communicate()[0]

    return out.partition(b" ")[0]


def prepend_line(file_name, line):
    with open(file_name, "r+") as f:
        content = f.read()
        f.seek(0, 0)
        f.write(line.rstrip("\r\n") + "\n" + content)



def point_cloud_txt_to_pcd(raw_dir, file_prefix):
    # File names
    txt_file = os.path.join(raw_dir, file_prefix + ".txt")
    pts_file = os.path.join(raw_dir, file_prefix + ".pts")
    pcd_file = os.path.join(raw_dir, file_prefix + ".pcd")

    # Skip if already done
    if os.path.isfile(pcd_file):
        print("pcd {} exists, skipped".format(pcd_file))
        return

    # .txt to .pts
    # We could just prepend the line count, however, there are some intensity value
    # which are non-integers.
    print("[txt->pts]")
    print("txt: {}".format(txt_file))
    print("pts: {}".format(pts_file))

    with open(txt_file, "r") as txt_f, open(pts_file, "w") as pts_f:
        for line in txt_f:
            # x, y, z, i, r, g, b
            tokens = line.split()
            tokens[3] = str(int(float(tokens[3])))
            line = " ".join(tokens)
            pts_f.write(line + "\n")

    prepend_line(pts_file, str(wc(txt_file)))
    print(" fin loop ")
    # .pts -> .pcd
    print("[pts->pcd]")
    print("pts: {}".format(pts_file))

    print("pcd: {}".format(pcd_file))

    point_cloud = open3d.read_point_cloud(pts_file)

    open3d.write_point_cloud(pcd_file, point_cloud)
    os.remove(pts_file)
    pcd = open3d.read_point_cloud(pcd_file)
    open3d.draw_geometries([pcd])

if __name__ == "__main__":
    # By default
    # raw data: "dataset/semantic_raw"
    parser = argparse.ArgumentParser()
    flags = parser.parse_args()
    parser.add_argument("--pcd_path", default="", type=str)
    current_dir = os.path.dirname(os.path.realpath(__file__))
    dataset_dir = os.path.join(current_dir, "dataset")
    raw_dir = os.path.join(dataset_dir, "semantic_raw")

    for file_prefix in all_file_prefixes:
        point_cloud_txt_to_pcd(raw_dir, file_prefix)
...