marta-marta commited on
Commit
e0d1700
·
1 Parent(s): 4661dbe

Added to incorporate a function to export the STL files directly from the user generated inputs

Browse files
Files changed (1) hide show
  1. app.py +156 -3
app.py CHANGED
@@ -5,6 +5,9 @@ import matplotlib.pyplot as plt
5
  from huggingface_hub import from_pretrained_keras
6
  import streamlit as st
7
  from elasticity import elasticity
 
 
 
8
 
9
  # Needed in requirements.txt for importing to use in the transformers model
10
  import tensorflow
@@ -344,19 +347,128 @@ for column in range(latent_dimensionality):
344
  new_column = np.linspace(latent_point_1[column], latent_point_2[column], num_interp)
345
  latent_matrix.append(new_column)
346
  latent_matrix = np.array(latent_matrix).T # Transposes the matrix so that each row can be easily indexed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  ########################################################################################################################
348
  # Plotting the Interpolation in 2D Using Chosen Points
349
- if st.button("Generate Linear Interpolation"):
 
350
  linear_interp_latent = np.linspace(latent_point_1, latent_point_2, num_interp)
351
-
352
  linear_predicted_interps = []
353
  figure_2 = np.zeros((28, 28 * num_interp))
 
 
354
  for i in range(num_interp):
355
  generated_image = decoder_model_boxes.predict(np.array([linear_interp_latent[i]]))[0]
356
  figure_2[0:28, i * 28:(i + 1) * 28, ] = generated_image[:, :, -1]
357
  linear_predicted_interps.append(generated_image[:, :, -1])
358
 
359
  st.image(figure_2, width=600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
360
  ########################################################################################################################
361
  # Provide User Options
362
  st.header("Option 2: Perform a Mesh Interpolation")
@@ -404,7 +516,7 @@ latent_point_4 = encoder_model_boxes.predict(number_4_expand)[0]
404
  latent_dimensionality = len(latent_point_1) # define the dimensionality of the latent space
405
  ########################################################################################################################
406
  # Plot a Mesh Gridded Interpolation
407
- if st.button("Generate Mesh Interpolation"):
408
  latent_matrix_2 = [] # This will contain the latent points of the interpolation
409
  for column in range(latent_dimensionality):
410
  new_column = np.linspace(latent_point_3[column], latent_point_4[column], num_interp)
@@ -429,3 +541,44 @@ if st.button("Generate Mesh Interpolation"):
429
  mesh_predicted_interps.append(generated_image[:, :, -1])
430
 
431
  st.image(figure_3, width=600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from huggingface_hub import from_pretrained_keras
6
  import streamlit as st
7
  from elasticity import elasticity
8
+ import io
9
+ from voxel_to_SDF_to_STL import voxel_to_sdf, sdf_to_stl, single_body_check
10
+ from PIL import Image
11
 
12
  # Needed in requirements.txt for importing to use in the transformers model
13
  import tensorflow
 
347
  new_column = np.linspace(latent_point_1[column], latent_point_2[column], num_interp)
348
  latent_matrix.append(new_column)
349
  latent_matrix = np.array(latent_matrix).T # Transposes the matrix so that each row can be easily indexed
350
+
351
+
352
+ ########################################################################################################################
353
+ # Create a gif from an interpolation
354
+ def interpolate_gif(decoder, latent_endpoint_1, latent_endpoint_2, n=100):
355
+ # z = np.stack([latent_endpoint_1 + (latent_endpoint_2 - latent_endpoint_1) * t for t in np.linspace(0, 1, n)])
356
+
357
+ # interpolate_list = decoder.predict(z)
358
+ # interpolate_list = (interpolate_list * 255).astype(np.uint8)
359
+
360
+ # images_list = [Image.fromarray(img.reshape(28, 28)).resize((256, 256)) for img in interpolate_list]
361
+ # images_list = images_list + images_list[::-1] # loop back beginning
362
+ predicted_interps = []
363
+ interp_latent = np.linspace(latent_endpoint_1, latent_endpoint_2, n)
364
+
365
+ figure = np.zeros((28, 28 * n))
366
+ for i in range(n):
367
+ generated_image = decoder.predict(np.array([interp_latent[i]]))[0]
368
+ figure[0:28, i * 28:(i + 1) * 28, ] = generated_image[:, :, -1]
369
+ predicted_interps.append(generated_image[:, :, -1])
370
+
371
+
372
+ # Regular Save for GIF
373
+ # images_list[0].save(f'{filename}.gif',save_all=True,append_images=images_list[1:],loop=1)
374
+
375
+ images_list = [Image.fromarray(img.reshape(28, 28)).resize((256, 256)) for img in predicted_interps]
376
+ images_list = images_list + images_list[::-1] # loop back beginning
377
+
378
+ # Create a BytesIO object to hold the GIF data
379
+ gif_bytes = io.BytesIO()
380
+ images_list[0].save(
381
+ gif_bytes,
382
+ format='GIF',
383
+ save_all=True,
384
+ append_images=images_list[1:],
385
+ loop=0, duration=100) # Set loop to 0 for infinite looping
386
+
387
+ # Reset the BytesIO object to the beginning
388
+ gif_bytes.seek(0)
389
+
390
+ st.video(gif_bytes)
391
+ # return gif_bytes
392
+
393
+
394
+ ########################################################################################################################
395
+ # Create an STL file from an interpolation
396
+ def convert_to_2_5d_sdf(interpolation, voxel_threshold, pixel_thickness):
397
+ # Thresholding determines the distance from the SDF that is used, the threshold provided is a divisor
398
+
399
+ # 1. Convert the interpolation into a 3D structure
400
+ interpolation_3d = [interpolation] * pixel_thickness
401
+
402
+ # 2. Convert the voxels into an SDF
403
+ sdf = voxel_to_sdf(interpolation_3d, voxel_threshold)
404
+ return sdf
405
+
406
+ def convert_sdf_to_stl(sdf, threshold_divisor):
407
+ # 3. Check if the SDF is a single body, then convert into an STL
408
+ if single_body_check(sdf, threshold_divisor):
409
+ # Thresholding determines the distance from the SDF that is used, the thresdhold provided is a divisor
410
+ stl = sdf_to_stl(sdf, threshold_divisor)
411
+ return stl
412
+
413
+
414
  ########################################################################################################################
415
  # Plotting the Interpolation in 2D Using Chosen Points
416
+ if st.checkbox("Generate Linear Interpolation"):
417
+ # Generate the set of latent points in the interpolation
418
  linear_interp_latent = np.linspace(latent_point_1, latent_point_2, num_interp)
 
419
  linear_predicted_interps = []
420
  figure_2 = np.zeros((28, 28 * num_interp))
421
+
422
+ # Predict the image for each latent point
423
  for i in range(num_interp):
424
  generated_image = decoder_model_boxes.predict(np.array([linear_interp_latent[i]]))[0]
425
  figure_2[0:28, i * 28:(i + 1) * 28, ] = generated_image[:, :, -1]
426
  linear_predicted_interps.append(generated_image[:, :, -1])
427
 
428
  st.image(figure_2, width=600)
429
+
430
+ # Code to display a gif
431
+ # interpolate_gif(decoder_model_boxes, latent_point_1, latent_point_2)
432
+
433
+ # Code for generating the STL file
434
+ st.subheader("Create an STL file from the extruded image!")
435
+
436
+ if st.checkbox("Select to begin model generation"):
437
+ # Creating an STL file of the linear interpolation
438
+
439
+ pixel_thickness_input = st.number_input("(1) Select a pixel thickness for the 3D model: ", min_value=1, value=28)
440
+ # Set the image threshold for binarization
441
+ voxel_threshold_input = st.slider("(2) Select a value to threshold the image (Recommend <= 0.1) "
442
+ "Higher values will result in less defined shapes: ",
443
+ min_value=0.0001, max_value=0.999, value=0.1, key='voxel_threshold')
444
+
445
+ # Create the SDF File
446
+ linear_sdf = convert_to_2_5d_sdf(figure_2, voxel_threshold_input, pixel_thickness_input)
447
+
448
+ # Set the threshold for the Mesh
449
+ threshold_divisor_input = st.slider("(3) Choose a threshold divisor for the SDF: ", min_value=0.0,
450
+ max_value=5.0,
451
+ value=3.0, key="divisor_threshold")
452
+
453
+ linear_sdf_min = np.min(linear_sdf)
454
+ linear_sdf_max = np.max(linear_sdf)
455
+ st.info("Lower SDF Thresholds will result in smoother, but less accurate shapes. Higher thresholds will result in more "
456
+ "rugged shapes, but they are more accurate. Suggested value for threshold is less than: " +
457
+ str((linear_sdf_max - abs(linear_sdf_min)) / 2))
458
+
459
+ if st.checkbox("Generate STL Model"):
460
+ # Generate the STL File
461
+ linear_stl = convert_sdf_to_stl(linear_sdf, threshold_divisor=threshold_divisor_input)
462
+
463
+ # Download the STL File
464
+ with open(linear_stl, 'rb') as file:
465
+ st.download_button(
466
+ label='Download STL',
467
+ data=file,
468
+ file_name='linear_interpolation.stl',
469
+ key='stl-download'
470
+ )
471
+
472
  ########################################################################################################################
473
  # Provide User Options
474
  st.header("Option 2: Perform a Mesh Interpolation")
 
516
  latent_dimensionality = len(latent_point_1) # define the dimensionality of the latent space
517
  ########################################################################################################################
518
  # Plot a Mesh Gridded Interpolation
519
+ if st.checkbox("Generate Mesh Interpolation"):
520
  latent_matrix_2 = [] # This will contain the latent points of the interpolation
521
  for column in range(latent_dimensionality):
522
  new_column = np.linspace(latent_point_3[column], latent_point_4[column], num_interp)
 
541
  mesh_predicted_interps.append(generated_image[:, :, -1])
542
 
543
  st.image(figure_3, width=600)
544
+
545
+ # Code for generating the STL file
546
+ st.subheader("Create an STL file from the extruded image!")
547
+
548
+ if st.checkbox("Select to begin model generation"):
549
+ # Creating an STL file of the linear interpolation
550
+
551
+ mesh_pixel_thickness_input = st.number_input("(1) Select a pixel thickness for the 3D model: ", min_value=1,
552
+ value=28)
553
+ # Set the image threshold for binarization
554
+ mesh_voxel_threshold_input = st.slider("(2) Select a value to threshold the image (Recommend <= 0.1) "
555
+ "Higher values will result in less defined shapes: ",
556
+ min_value=0.0001, max_value=0.999, value=0.1, key='voxel_threshold')
557
+
558
+ # Create the SDF File
559
+ mesh_sdf = convert_to_2_5d_sdf(figure_3, mesh_voxel_threshold_input, mesh_pixel_thickness_input)
560
+
561
+ # Set the threshold for the Mesh
562
+ mesh_threshold_divisor_input = st.slider("(3) Choose a threshold divisor for the SDF: ", min_value=0.0,
563
+ max_value=5.0,
564
+ value=3.0, key="divisor_threshold")
565
+
566
+ mesh_sdf_min = np.min(mesh_sdf)
567
+ mesh_sdf_max = np.max(mesh_sdf)
568
+ st.info(
569
+ "Lower SDF Thresholds will result in smoother, but less accurate shapes. Higher thresholds will result in more "
570
+ "rugged shapes, but they are more accurate. Suggested value for threshold is less than: " +
571
+ str((mesh_sdf_max - abs(mesh_sdf_min)) / 2))
572
+
573
+ if st.checkbox("Generate STL Model"):
574
+ # Generate the STL File
575
+ linear_stl = convert_sdf_to_stl(mesh_sdf, threshold_divisor=mesh_threshold_divisor_input)
576
+
577
+ # Download the STL File
578
+ with open(linear_stl, 'rb') as file:
579
+ st.download_button(
580
+ label='Download STL',
581
+ data=file,
582
+ file_name='interpolation.stl',
583
+ key='stl-download'
584
+ )