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labels = cardetector.dls.vocab
|
| 6 |
-
def predict(img):
|
| 7 |
-
img = PILImage.create(img)
|
| 8 |
-
brand, index, probs = cardetector.predict(img)
|
| 9 |
-
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 10 |
gr.Interface(
|
| 11 |
-
fn
|
| 12 |
-
inputs
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
title = "Car Detector",
|
| 15 |
description = "A classfier to detect car brands from an image. Created using the car connection picture dataset as a demo for hugging face and gradio.",
|
| 16 |
|
| 17 |
-
).launch()
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import random
|
| 4 |
+
import open3d as o3d
|
| 5 |
+
import numpy as np
|
| 6 |
+
from matplotlib import pyplot as plt
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def as_mesh(points):
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
pcd = o3d.geometry.PointCloud()
|
| 17 |
+
|
| 18 |
+
pcd.points = o3d.utility.Vector3dVector(points[0])
|
| 19 |
+
pcd.estimate_normals()
|
| 20 |
+
pcd.orient_normals_consistent_tangent_plane(100)
|
| 21 |
+
show_mesh((pcd))
|
| 22 |
+
alpha = 0.04
|
| 23 |
+
print(f"alpha={alpha:.3f}")
|
| 24 |
+
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha)
|
| 25 |
+
mesh.compute_vertex_normals()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
voxel_size = max(mesh.get_max_bound() - mesh.get_min_bound()) / 32
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
mesh = mesh.simplify_vertex_clustering(voxel_size=voxel_size,contraction=o3d.geometry.SimplificationContraction.Average)
|
| 32 |
+
|
| 33 |
+
points = np.asarray(mesh.vertices)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def ddim_step(x_t, noise, abar_t, abar_t1, bbar_t, bbar_t1, eta, sig,m, clamp=True):
|
| 38 |
+
sig = ((bbar_t1/bbar_t).sqrt() * (1-abar_t/abar_t1).sqrt()) * eta
|
| 39 |
+
x_0_hat = ((x_t-(1-abar_t).sqrt()*noise) / abar_t.sqrt())
|
| 40 |
+
if clamp: x_0_hat = x_0_hat.clamp(-1,1)
|
| 41 |
+
if bbar_t1<=sig**2+0.01: sig=0. # set to zero if very small or NaN
|
| 42 |
+
x_t = abar_t1.sqrt()*x_0_hat + (bbar_t1-sig**2).sqrt()*noise
|
| 43 |
+
if x_t.isnan().any():
|
| 44 |
+
|
| 45 |
+
x_t = torch.ones(x_t.shape)
|
| 46 |
+
|
| 47 |
+
x__0_hat = x_t*m
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
x_t += sig * torch.randn(x_t.shape).to(x_t)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
return x__0_hat,x_t
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def cond_sample(c, f, model, sz, steps, eta=1.):
|
| 60 |
+
ts = torch.linspace(1-1/steps,0,steps)
|
| 61 |
+
x_t = torch.randn(sz)
|
| 62 |
+
c = x_t.new_full((sz[0],), c, dtype=torch.int32)
|
| 63 |
+
preds = []
|
| 64 |
+
for i,t in enumerate(progress_bar(ts)):
|
| 65 |
+
t = t[None]
|
| 66 |
+
abar_t = abar(t)
|
| 67 |
+
noise = model((x_t, t, c))
|
| 68 |
+
abar_t1 = abar(t-1/steps) if t>=1/steps else torch.tensor(1)
|
| 69 |
+
x_0_hat,x_t = f(x_t, noise, abar_t, abar_t1, 1-abar_t, 1-abar_t1, eta, 1-((i+1)/100),m)
|
| 70 |
+
preds.append(x_0_hat.float().cpu())
|
| 71 |
+
return preds
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def load_mod():
|
| 77 |
+
model = CondUNetModel(8, in_channels=1, out_channels=1, nfs=(64,128,256,512), num_layers=30, attn_chans = 64)
|
| 78 |
+
model.load_state_dict(torch.load('working/model.pt'))
|
| 79 |
+
|
| 80 |
+
model
|
| 81 |
+
model.eval()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def normalize(v):
|
| 85 |
+
norm = np.linalg.norm(v)
|
| 86 |
+
|
| 87 |
+
if norm == 0:
|
| 88 |
+
return v
|
| 89 |
+
|
| 90 |
+
return v / norm
|
| 91 |
+
def diff3D(prompt):
|
| 92 |
+
|
| 93 |
+
base_path = os.path.join(os.getcwd(), '3D-Data')
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
if not os.path.exists(os.path.join(base_path, prompt)):
|
| 97 |
+
return "False Prompt"
|
| 98 |
+
folder_path = os.path.join(base_path, prompt)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
models = os.listdir(folder_path)
|
| 103 |
+
random_model = random.choice(models)
|
| 104 |
+
model_path = os.path.join(folder_path, random_model)
|
| 105 |
+
mesh = o3d.io.read_triangle_mesh(model_path)
|
| 106 |
+
mesh.compute_vertex_normals()
|
| 107 |
+
pcda = mesh.sample_points_uniformly(number_of_points=512*4)
|
| 108 |
+
pcda = mesh.sample_points_poisson_disk(number_of_points=256*4, pcl=pcda)
|
| 109 |
+
points = normalize(np.asarray(pcda.points))
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
pcd = o3d.geometry.PointCloud()
|
| 113 |
+
|
| 114 |
+
pcd.points = o3d.utility.Vector3dVector(points)
|
| 115 |
+
pcd.estimate_normals()
|
| 116 |
+
pcd.orient_normals_consistent_tangent_plane(100)
|
| 117 |
+
alpha = 0.03
|
| 118 |
+
if prompt == 'Vase' or prompt == 'Table':
|
| 119 |
+
alpha = 0.015
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha)
|
| 123 |
+
mesh.compute_vertex_normals()
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
voxel_size = max(mesh.get_max_bound() - mesh.get_min_bound()) /64
|
| 127 |
+
mesh = mesh.simplify_vertex_clustering(voxel_size=voxel_size, contraction=o3d.geometry.SimplificationContraction.Average)
|
| 128 |
+
mesh = o3d.geometry.TriangleMesh.compute_triangle_normals(mesh)
|
| 129 |
+
|
| 130 |
+
output_path = os.path.join(base_path, 'out.stl')
|
| 131 |
+
o3d.io.write_triangle_mesh(output_path, mesh)
|
| 132 |
+
points = np.asarray(mesh.vertices)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
return os.path.join(base_path, 'out.stl')
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
import gradio as gr
|
| 147 |
+
import os
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
gr.Interface(
|
| 154 |
+
fn=diff3D,
|
| 155 |
+
inputs=gr.Dropdown(
|
| 156 |
+
["Cone","Cube","Cylinder","Pencil","Sphere","Table","Vase"], info="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nisl eget ultricies aliquam, nunc nisl aliquet nunc, eget aliquam nisl nunc vel nisl."
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
,
|
| 160 |
+
outputs=gr.Model3D(
|
| 161 |
+
clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
|
| 162 |
title = "Car Detector",
|
| 163 |
description = "A classfier to detect car brands from an image. Created using the car connection picture dataset as a demo for hugging face and gradio.",
|
| 164 |
|
| 165 |
+
).launch()
|
| 166 |
+
|