|
- # -*- coding: UTF-8 -*-
- '''
- 读取单个点云文件,进行量化测试,然后gpcc编解码,并计算bpp,d1,d2的值
- '''
-
- import os, time
- import numpy as np
- # from pc_error import pc_error
- import subprocess
- import pandas as pd
- import json
- from pyntcloud import PyntCloud
- import open3d
-
- pd.set_option('display.max_columns',1000) #防止打印的内容过多,会出现省略号
-
- def number_in_line(line):
- wordlist = line.split(' ')
- for _, item in enumerate(wordlist):
- try:
- number = float(item)
- except ValueError:
- continue
-
- return number
-
- def pc_error(infile1, infile2, res, normal=False, show=False):
- # Symmetric Metrics. D1 mse, D1 hausdorff.
- headers1 = ["mse1 (p2point)", "mse1,PSNR (p2point)",
- "h. 1(p2point)", "h.,PSNR 1(p2point)" ]
-
- headers2 = ["mse2 (p2point)", "mse2,PSNR (p2point)",
- "h. 2(p2point)", "h.,PSNR 2(p2point)" ]
-
- headersF = ["mseF (p2point)", "mseF,PSNR (p2point)",
- "h. (p2point)", "h.,PSNR (p2point)" ]
-
- haders_p2plane = ["mse1 (p2plane)", "mse1,PSNR (p2plane)",
- "mse2 (p2plane)", "mse2,PSNR (p2plane)",
- "mseF (p2plane)", "mseF,PSNR (p2plane)"]
-
- headers = headers1 + headers2 + headersF
-
- command = str('./pc_error_d' +
- ' -a '+infile1+
- ' -b '+infile2+
- # ' -n '+infile1+
- ' --hausdorff=1 '+
- ' --resolution='+str(res-1))
-
- if normal:
- headers += haders_p2plane
- command = str(command + ' -n ' + infile1)
-
- results = {}
-
- start = time.time()
- subp=subprocess.Popen(command,
- shell=True, stdout=subprocess.PIPE)
-
- c=subp.stdout.readline()
- while c:
- line = c.decode(encoding='utf-8')# python3.
- if show:
- print(line)
- for _, key in enumerate(headers):
- if line.find(key) != -1:
- value = number_in_line(line)
- results[key] = value
-
- c=subp.stdout.readline()
- # print('===== measure PCC quality using `pc_error` version 0.13.4', round(time.time() - start, 4))
-
- return pd.DataFrame([results])
-
-
- def gpcc_encode(filedir, bin_dir, rec_file, mode='octree',show=False): # trisoup octree
- """Compress point cloud losslessly using MPEG G-PCCv12.
- You can download and install TMC13 from
- https://github.com/MPEGGroup/mpeg-pcc-tmc13
- """
- if mode == 'octree':
- subp=subprocess.Popen('/userhome/PCGCv2/PCGCv2-8.3/PCGCv2-master/tmc3'+
- ' --mode=0' +
- ' --trisoupNodeSizeLog2=0' +
- ' --mergeDuplicatedPoints=0' +
- ' --neighbourAvailBoundaryLog2=8' +
- ' --intra_pred_max_node_size_log2=6' +
- ' --srcResolution=0' +
- ' --outputResolution=0' +
- ' --positionQuantizationScale=1' +
- ' --inferredDirectCodingMode=1' +
- ' --maxNumQtBtBeforeOt=4' +
- ' --minQtbtSizeLog2=0' +
- ' --planarEnabled=1' +
- ' --planarModeIdcmUse=0' +
- ' --convertPlyColourspace=1' +
- ' --transformType=2' +
- ' --numberOfNearestNeighborsInPrediction=3' +
- ' --levelOfDetailCount=12' +
- ' --lodDecimator=0' +
-
- ' --adaptivePredictionThreshold=64' +
- ' --qp=51' +
- ' --qpChromaOffset=0' +
- ' --bitdepth=8' +
- ' --attribute=color'+
- ' --reconstructedDataPath='+rec_file +
- ' --uncompressedDataPath='+filedir +
- ' --compressedStreamPath='+bin_dir,
- shell=True, stdout=subprocess.PIPE)
-
- else:
- subp=subprocess.Popen('./tmc3'+
- ' --mode=0' + #encode
- ' --positionQuantizationScale=1' +
- ' --trisoupNodeSizeLog2=2' +
- ' --neighbourAvailBoundaryLog2=8' +
- ' --intra_pred_max_node_size_log2=6' +
- ' --inferredDirectCodingMode=0' +
- ' --maxNumQtBtBeforeOt=4' +
- ' --uncompressedDataPath='+filedir +
- ' --compressedStreamPath='+bin_dir,
- shell=True, stdout=subprocess.PIPE)
- c=subp.stdout.readline()
- while c:
- if show: print(c)
- c=subp.stdout.readline()
-
- return
-
- def gpcc_decode(bin_dir, rec_dir, show=False):
- subp=subprocess.Popen('/userhome/PCGCv2/PCGCv2-8.3/PCGCv2-master/tmc3'+
- ' --mode=1'+
- ' --outputResolution=0'+
- ' --convertPlyColourspace=1'+
- ' --compressedStreamPath='+bin_dir+
- ' --reconstructedDataPath='+rec_dir,
- shell=True, stdout=subprocess.PIPE)
- c=subp.stdout.readline()
- while c:
- if show: print(c)
- c=subp.stdout.readline()
-
- return
- def get_min_value(martix):
- '''
- 得到矩阵中每一列最大的值
- '''
- res_list=[]
- # print("martix=",martix)
- for j in range(len(martix[0])):
- one_list=[]
- for i in range(len(martix)):
- one_list.append(martix[i][j])
- res_list.append(min(one_list))
- return np.array(res_list)
-
- def tupleToStr(tuple):
- '''
- Converts a tuple of N size into a string, where each element is separated by a space.
- Arguments:
- - tuple: tuple to be converted into string
- Returns:
- - string with the tuple values
- '''
- tuple_string = ""
- for i in range(0, len(tuple)):
- if i == (len(tuple)-1):
- tuple_string += str(tuple[i])
- else:
- tuple_string += str(tuple[i]) + " "
-
- return tuple_string
-
- def saveO3dPly(filepath, o3d_pcd, normals = None):
- '''
- For testing in the Main.py file
- Save list of points (possibly with attributes such as color) into a .PLY formated file
- Arguments:
- - o3d_pcd: list of points and their normals
- - normals: 法向量
- '''
- num_points = len(o3d_pcd.points)
- print("num_points:",num_points)
- # print("o3d_pcd.normals[0]:",o3d_pcd.normals[0])
- with open(filepath, "w") as the_file:
- header_lines = ["ply", "format ascii 1.0"]
- header_lines.append("element vertex " + str(num_points))
- header_lines.append("property float x")
- header_lines.append("property float y")
- header_lines.append("property float z")
-
- # if point have normals
- if normals == True:
- header_lines.append("property float nx")
- header_lines.append("property float ny")
- header_lines.append("property float nz")
-
- header_lines.append("end_header")
-
- for i in range(0, len(header_lines)):
- the_file.write(header_lines[i] + "\n")
-
- for i in range(0, num_points):
- points = np.float32(o3d_pcd.points[i])
- if normals:
- normal = np.float32(o3d_pcd.normals[i])
- points = np.concatenate((points, normal), axis=0)
- the_file.write(tupleToStr(points) + "\n")
-
- if __name__ == '__main__':
- PC_file = '/userhome/pcc_attr_folding/pcc_attr_folding-master/gpcc/octree-predlift/lossless-geom-lossy-attrs/redandblack_vox10_1550/r01/redandblack_vox10_1550.ply'
- outfile = PC_file + '.bin'
- print(outfile)
- rec_file = outfile+'.ply'
- gpcc_encode(PC_file, outfile, rec_file, mode='octree',show=True) #用gpcc工具转成bin
- bits = os.path.getsize(outfile)*8 #字节*8
- rec_filename = outfile+'.decoded.ply'
- print(rec_filename)
- gpcc_decode(outfile, rec_filename,show=True) #从bin回到ply
- # bpp = round((bits/2048),3)
- # print("bpp:",bpp)
|