-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
3D_Build.py
271 lines (209 loc) · 8.11 KB
/
3D_Build.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import copy
import json
import os
import shutil
import cv2
import time
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import Image
import import_ipynb
from rotate_translate import node
from parent_child_tree import createtree
from multipleoperation import *
from subprocess import run
import open3d as o3d
import time
from pyntcloud import PyntCloud
# Below Code generates 2 Contours for a given shape. One 'Outer Contour' and other 'Inner Contour' for a 2 shape .
#
# In[ ]:
print("Please enter the image view file with extension")
s=input()
image=cv2.imread(s)
imgray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,0,cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
hierarchy,contours = zip(*sorted(zip(hierarchy[0],contours),key = lambda x: cv2.contourArea(x[1]),reverse=True))
print ("no, of contours = " + str(len(contours)-1))
cv2.drawContours(image, contours, 1, (0, 255, 0), 2)
cv2.drawContours(image, contours, 2, (0, 0 , 255), 2)
cv2.imshow('IMAGE', image)
cv2.imshow('Image Gray', imgray)
cv2.waitKey(0)
# In[ ]:
print("Please enter the image view file with extension")
s=input()
image=cv2.imread(s)
imgray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray,127,255,0,cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE)
hierarchy,contours = zip(*sorted(zip(hierarchy[0],contours),key = lambda x: cv2.contourArea(x[1]),reverse=True))
# Below Function 'detect()' will determine the shape of a given 2D image on the basis of number of edges in the given Shape .
# Allowed Shapes for a 2d Image are : Triangle , Square , Rectangle , Pentagon , Hexagon , Circle
#
# In[ ]:
def detect(c):
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * perimeter , True)
if len(approx) == 3:
shape = "triangle"
elif len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
ar = w / float(h)
if (ar >= 0.999 and ar <= 1.001):
shape = "square"
else:
shape = "rectangle"
elif len(approx) == 5:
shape = "pentagon"
elif len(approx) == 6:
shape = "hexagon"
else:
shape = "circle"
return shape
# In[ ]:
d=detect(contours[1])
print(d)
# Below 'Dimensioning()' function stores the image temprory amd gives Image imformation like its 'Shape' and 'Ratio'
# In[ ]:
def Dimensioning(userId,view,image):
os.makedirs('static/temp', exist_ok=True)
img = cv2.imread(image,cv2.IMREAD_UNCHANGED)
ratio = 0
shape = "unidentified"
try:
w,h,_=img.shape
drawSize = int(h/300)
imgrey = cv2.cvtColor(img.copy(),cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgrey,127,255,cv2.THRESH_BINARY_INV)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
hierarchy,contours = zip(*sorted(zip(hierarchy[0],contours),key = lambda x: cv2.contourArea(x[1]),reverse=True))
for i,c in enumerate(contours):
if(hierarchy[i][3] != -1 or (hierarchy[i][3] == -1 and hierarchy[i][2] == -1) ):
M = cv2.moments(c)
if(M["m00"] !=0):
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
box = np.int0(box)
x,y,w,h = cv2.boundingRect(c)
shape, cylinder_type = detect(c)
if(shape == "unidentified"):
continue
if(shape=="triangle" or shape=="pentagon" or shape=="hexagon"):
img = cv2.drawContours(img, [box], 0, (0,0, 255), drawSize)
if(shape=="circle"):
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0, 0, 255),drawSize)
cv2.line(img, (x,y), (x+w,y), (0,255, 0), 2)
ratio = 1/w
else:
cv2.line(img, tuple(box[0]), tuple(box[1]), (0,255, 0), 2)
ratio = 1.0/rect[1][1]
break
folder = 'static/temp/' + userId
os.makedirs(folder, exist_ok=True)
try:
os.remove(folder + '/' + view + '.jpg')
except: pass
path_file = (folder + '/' + view + '.jpg')
small = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
cv2.imwrite(path_file,small)
ratio = str(ratio)
data={'image': path_file,'shape': shape,'ratio': ratio}
display(Image(filename = path_file))
return data
except:
print(view + "image not found")
# In[ ]:
def Convert(userId, front_image, side_image, top_image, fratio, sratio, tratio):
start = time.time()
os.makedirs('static/'+userId, exist_ok=True)
img_front = cv2.imread(front_image,cv2.IMREAD_UNCHANGED)
img_side = cv2.imread(side_image,cv2.IMREAD_UNCHANGED)
img_top = cv2.imread(top_image,cv2.IMREAD_UNCHANGED)
fratio = float(fratio)
sratio = float(sratio)
tratio = float(tratio)
filePath = "static/temp/"+userId
try:
shutil.rmtree(filePath)
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))
primitive = []
object_front = valid_contours(img_front,"front",fratio)
re_arrange(object_front,"front")
object_side = valid_contours(img_side,"side",sratio)
re_arrange(object_side,"side")
object_top = valid_contours(img_top,"top",tratio)
re_arrange(object_top,"top")
minApprox = 0.05
primitive = combining(object_front,object_side,object_top,minApprox)
final = []
for set in primitive:
for shape in set:
final.append(shape[0])
try:
os.remove('static/' + userId + "/" + userId + '.scad')
except: pass
path_file = ('static/' + userId + "/" + userId + '.scad')
if(len(final) == 0):
path_file = 'static/error.txt'
f = open(path_file, "w")
f.write("Cannot determine the 3d geometry, check your files again!")
f.close()
createtree(final,path_file)
end = time.time()
print("Total time taken to convert:",end-start)
return path_file
# In[ ]:
userId="1"
front_image = "TestBench/"+userId+"/front.jpg"
side_image = "TestBench/"+userId+"/side.jpg"
top_image = "TestBench/"+userId+"/top.jpg"
front_image
# In[ ]:
d1 = Dimensioning(userId,"front",front_image)
d1
# In[ ]:
d2 = Dimensioning(userId,"side",side_image)
d2
# In[ ]:
d3 = Dimensioning(userId,"top",top_image)
d3
# In[ ]:
fratio = float(d1["ratio"]) * 2
sratio = float(d2["ratio"]) * 2
tratio = float(d3["ratio"]) * 2
# Convert Function joins the multiple 2D images with their respective Ratio to insert them in a tree to form a 3D model from it .
# If the given shape consist of more than one shape (complex object in Image object) Than we will sub divide it to individual shapes and store them in tree as Parent and Child .
# In[ ]:
Convert(userId,front_image, side_image, top_image,fratio, sratio, tratio)
# Point Cloud Representation of the generated Scad File
#
# In[ ]:
print("please wait generating 3D view...")
start = time.time()
scad_filename = "static/" + userId + '/' + userId + ".1.scad"
stl_filename = "static/" + userId + '/' + userId + ".stl"
pcd_filename = "static/" + userId + '/' + userId + ".pcd"
if(os.path.isfile(scad_filename)):
run("openscad.exe -o " + stl_filename + " " + scad_filename)
if(os.path.isfile(stl_filename)):
print("stl file generated at " + stl_filename)
mesh = o3d.io.read_triangle_mesh(stl_filename)
pointcloud = mesh.sample_points_poisson_disk(100000)
o3d.io.write_point_cloud(pcd_filename, pointcloud)
model = PyntCloud.from_file(pcd_filename)
print("pcd file generated at " + pcd_filename)
model.plot()
end = time.time()
print("Total time taken to convert from stl to point cloud:",end-start)
else:
print("file not found")
else:
print("scad file not found")