Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.51 KB | None | 0 0
  1. import sys
  2. sys.path.insert(0, 'include/')
  3.  
  4. import copy
  5. import numpy as np
  6. from pointCloud import PointCloud
  7. from pointCloudLine import PointCloudLine
  8. from quality import Quality
  9. from net import Net
  10. from helperFunctions import *
  11. import matplotlib.pyplot as plt
  12. from numpy import fft
  13. from scipy.signal import butter, filtfilt
  14. import open3d as o3d
  15. from trajectory import Trajectory
  16. import heapq
  17. from tqdm import tqdm
  18. import torch
  19. import torch.nn as nn
  20. import torch.nn.functional as F
  21. import torch.optim as optim
  22. from skimage import data
  23. from skimage.filters import threshold_otsu
  24. from skimage.segmentation import clear_border
  25. from skimage.measure import label, regionprops
  26. from skimage.morphology import closing, square
  27. from skimage.color import label2rgb
  28. from skimage import filters
  29. import os
  30.  
  31. FILE_NAME = "11-12-19-qual1"
  32. LOAD_PCD = False
  33. LOAD_ROI = False
  34. xOff_l = -1.6338
  35. xOff_r = -1.6338
  36.  
  37. with open("config.yaml", 'r') as ymlfile:
  38.     cfg = yaml.load(ymlfile)
  39.  
  40. net = loadClassifier()
  41.  
  42. print("Loading point cloud...")
  43.  
  44. pcd = pcdLoader(file_name = FILE_NAME, afterPIT=True, load=LOAD_PCD)
  45.  
  46. #pcd.pcd = o3d.geometry.PointCloud.remove_statistical_outlier(pcd.pcd, 1000, 1)[0]
  47. pcd.getPointCloudLines() # Construct arrays of point cloud lines
  48. img = pcd.pcd_to_image()
  49. npArr = pcd.img_to_pcd(img)
  50. pcd = PointCloud(npArr, type='NPARR')
  51. pcd.getPointCloudLines() # Construct arrays of point cloud lines
  52.  
  53. # Normalize point cloud
  54. pcdNormalized = PointCloud(pcd.normalize(), type ='NPARR')
  55. pcdNormalized.getPointCloudLines() # Construct arrays of point cloud lines
  56.  
  57. if LOAD_ROI == True:
  58.     if os.path.exists((cfg['after_file'].get("ROI") + FILE_NAME + cfg['after_file'].get("ext_npy"))) == True:
  59.         ROI = roiLoader(file_name = (cfg['after_file'].get("ROI") + FILE_NAME + cfg['after_file'].get("ext_npy")))
  60.     else:
  61.         print("Couldn't load regions of interest.")
  62. else:
  63.     ROI = getROI(pcdNormalized, net=net)
  64.  
  65. print("ROI to 2D for segmentation...")
  66.  
  67. img = pcdNormalized.pcd_to_image()
  68. roi_img = pcdNormalized.mask_image(img, ROI)
  69. roi_img = filters.median(roi_img, np.ones((3, 3)))
  70.  
  71. ROI = PointCloud(ROI, type='NPARR')
  72. ROI.setColor('b')
  73.  
  74. print("Segmenting regions of interests...")
  75. # apply threshold
  76. bw = roi_img > 0
  77.  
  78. # label image regions
  79. label_image = label(bw, connectivity=2)
  80.  
  81. regions = ROI.thresArea(label_image, thres = 1500) #4000 for 12-1-T-before
  82. masks = ROI.getMask(regions, pcdNormalized.pointCloudLine)
  83.  
  84. print("Applying mask to point cloud...")
  85. weld_toe = []
  86. pcd.unNormalize()
  87.  
  88. for mask in masks:
  89.     maskLines = ROI.maskToLines(mask)
  90.     npArr = pcdNormalized.mask_pcd(maskLines, border = cfg['model_after'].get("step_size"))
  91.     if len(npArr) != 0:
  92.         weld_toe.append(Trajectory(npArr))
  93.  
  94. weld_toe[0].unNormalize(pcd.z_max, pcd.z_min)
  95. weld_toe[0].flip()
  96. weld_toe[0].setColor('r')
  97. weld_toe[1].unNormalize(pcd.z_max, pcd.z_min)
  98. weld_toe[1].flip()
  99. weld_toe[1].setColor('g')
  100. pcd.flip()
  101. #
  102. #o3d.visualization.draw_geometries([pcd.pcd, weld_toe[0].pcd, weld_toe[1].pcd])
  103. o3d.visualization.draw_geometries([pcd.pcd, weld_toe[0].pcd]) #left
  104. o3d.visualization.draw_geometries([pcd.pcd, weld_toe[1].pcd])
  105.  
  106. pcd.getPointCloudLines()
  107. weld_toe[0].getPointCloudLines()
  108. weld_toe[1].getPointCloudLines()
  109. raw_weldtoe_left = copy.deepcopy(weld_toe[0].pointCloudLine)
  110. raw_weldtoe_right = copy.deepcopy(weld_toe[1].pointCloudLine)
  111. #o3d.visualization.draw_geometries([pcd.pcd, weld_toe[0].pcd])
  112. #o3d.visualization.draw_geometries([pcd.pcd, weld_toe[1].pcd])
  113.  
  114. # Karls rocker algorithm from here.
  115.  
  116. peenDepth_left = []
  117. peenDepth_right = []
  118. peenRadius_left = []
  119. peenRadius_right = []
  120. peenWidth_left = []
  121. peenWidth_right = []
  122. trajectpredict = []
  123. deviation_l, deviation_r = [], []
  124. line=[]
  125. perpline_left = []
  126. perpline_right = []
  127. intercept_left = []
  128. intercept_right = []
  129. for i in range(len(weld_toe[0].pointCloudLine)):
  130.  
  131.     try:
  132.         print(i)
  133.         quality_left = Quality(weld_toe[0].pointCloudLine[i], pcd.pointCloudLine[i])
  134.         quality_right = Quality(weld_toe[1].pointCloudLine[i], pcd.pointCloudLine[i])
  135.         peenWidth_left.append([quality_left.get_width_and_midpoint(),i])
  136.         peenWidth_right.append([quality_right.get_width_and_midpoint(),i])
  137.         perpline_left.append(quality_left.pline())
  138.         perpline_right.append(quality_right.pline())
  139.         intercept_left.append(quality_left.find_intercept(quality_left.pline()))
  140.         intercept_right.append(quality_right.find_intercept(quality_right.pline()))
  141.         peenDepth_left.append([quality_left.get_depth(),i])
  142.         peenDepth_right.append([quality_right.get_depth(),i])
  143.  
  144.         quality_left.plot_stuff(weld_toe[0].pointCloudLine[i])
  145.         quality_right.plot_stuff(weld_toe[1].pointCloudLine[i])
  146.  
  147.         deviation_l.append([quality_left.find_traject(xOff_l, file = FILE_NAME), i])
  148.         deviation_r.append([quality_right.find_traject(xOff_r, file = FILE_NAME), i])
  149.     except Exception as e:
  150.         pass
  151.         print(e)
  152.  
  153.  
  154. peenDepth_left = np.array(peenDepth_left)
  155. peenDepth_right = np.array(peenDepth_right)
  156. peenRadius_left = np.array(peenRadius_left)
  157. peenRadius_right = np.array(peenRadius_right)
  158. peenWidth_left = np.array(peenWidth_left)
  159. peenWidth_right = np.array(peenWidth_right)
  160. trajectpredict = np.array(trajectpredict)
  161. deviation_l = np.array(deviation_l)
  162. deviation_r = np.array(deviation_r)
  163.  
  164. fig, axs = plt.subplots(3, 1, constrained_layout=True)
  165. axs[0].set_title('Depth Left Side')
  166. axs[0].plot(peenDepth_left[:][:,1], quality_right.smooth_outliers(peenDepth_left[:][:,0],5))
  167. axs[0].hlines(0.1, 0, peenDepth_left[-1][1],colors = 'r', linestyles = 'dashed')
  168. axs[0].hlines(0.6, 0, peenDepth_left[-1][1],colors = 'r', linestyles = 'dashed')
  169. axs[0].set(ylabel='Depth (mm)')
  170. axs[0].grid()
  171.  
  172. axs[1].set_title('Width Left Side')
  173. axs[1].plot(peenWidth_left[:][:,1], quality_right.smooth_outliers(peenWidth_left[:][:,0],5))
  174. axs[1].hlines(3, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  175. axs[1].hlines(6, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  176. axs[1].set(ylabel='Width (mm)')
  177. axs[1].grid()
  178.  
  179. axs[2].set_title('Trajectory Deviation Left')
  180. axs[2].plot(deviation_l[:][:,1], quality_right.smooth_outliers(deviation_l[:][:,0],5))
  181. axs[2].hlines(-0.7121, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  182. axs[2].hlines(0.7121, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  183. axs[2].set(xlabel='y-value (mm)', ylabel='Error (mm)')
  184. axs[2].grid()
  185.  
  186. fig, axs = plt.subplots(3, 1, constrained_layout=True)
  187. axs[0].set_title('Depth Right Side')
  188. axs[0].plot(peenDepth_right[:][:,1], quality_right.smooth_outliers(peenDepth_right[:][:,0],5))
  189. axs[0].hlines(0.1, 0, peenDepth_left[-1][1],colors = 'r', linestyles = 'dashed')
  190. axs[0].hlines(0.6, 0, peenDepth_left[-1][1],colors = 'r', linestyles = 'dashed')
  191. axs[0].set(ylabel='Depth (mm)')
  192. axs[0].grid()
  193.  
  194. axs[1].set_title('Width Right Side')
  195. axs[1].plot(peenWidth_right[:][:,1], quality_right.smooth_outliers(peenWidth_right[:][:,0],5))
  196. axs[1].hlines(3, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  197. axs[1].hlines(6, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  198. axs[1].set(ylabel='Width (mm)')
  199. axs[1].grid()
  200.  
  201. axs[2].set_title('Trajectory Deviation Right')
  202. axs[2].plot(deviation_r[:][:,1], quality_right.smooth_outliers(deviation_r[:][:,0],5))
  203. axs[2].hlines(-0.7349, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  204. axs[2].hlines(0.7349, 0, peenDepth_left[-1][1],colors = 'r',linestyles = 'dashed')
  205. axs[2].set(xlabel='y-value (mm)',ylabel='Error (mm)')
  206. axs[2].grid()
  207.  
  208. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement