Spaces:
Runtime error
Runtime error
Commit
·
7098dbe
1
Parent(s):
7a4e452
Refactor image processing and saving logic
Browse files
app.py
CHANGED
|
@@ -82,17 +82,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
def on_submit(image):
|
| 83 |
original_image = image.copy()
|
| 84 |
|
|
|
|
| 85 |
h, w = image.shape[:2]
|
| 86 |
|
| 87 |
-
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
| 88 |
-
# image = transform({'image': image})['image']
|
| 89 |
-
# image = torch.from_numpy(image).unsqueeze(0).to(DEVICE)
|
| 90 |
-
|
| 91 |
image = np.asarray(image, dtype=np.float32) / 255.0
|
| 92 |
image = torch.from_numpy(image.transpose((2, 0, 1)))
|
| 93 |
image = Normalize(mean=[0.485, 0.456, 0.406], std=[
|
| 94 |
0.229, 0.224, 0.225])(image)
|
| 95 |
-
# image = torch.from_numpy(image).unsqueeze(0)
|
| 96 |
with torch.no_grad():
|
| 97 |
image = torch.autograd.Variable(image.unsqueeze(0))
|
| 98 |
print("== Processing image")
|
|
@@ -106,19 +102,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 106 |
# Convert the PyTorch tensor to a NumPy array and squeeze
|
| 107 |
pred_depth = pred_depth.cpu().numpy().squeeze()
|
| 108 |
|
| 109 |
-
# Convert to uint8 if necessary for the colormap
|
| 110 |
-
pred_output_depth = pred_depth.astype(np.uint8)
|
| 111 |
-
|
| 112 |
-
# Apply color map
|
| 113 |
-
output_image = cv2.applyColorMap(
|
| 114 |
-
pred_output_depth, cv2.COLORMAP_INFERNO)[:, :, ::-1]
|
| 115 |
-
|
| 116 |
# Continue with your file saving operations
|
| 117 |
tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 118 |
# cv2.imwrite(tmp.name, output_image)
|
| 119 |
plt.imsave(tmp.name, pred_depth, cmap='jet')
|
| 120 |
|
| 121 |
-
return [(original_image,
|
| 122 |
|
| 123 |
submit.click(on_submit, inputs=[input_image], outputs=[
|
| 124 |
depth_image_slider, raw_file])
|
|
|
|
| 82 |
def on_submit(image):
|
| 83 |
original_image = image.copy()
|
| 84 |
|
| 85 |
+
# This is for resizing the image to 518x518
|
| 86 |
h, w = image.shape[:2]
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
image = np.asarray(image, dtype=np.float32) / 255.0
|
| 89 |
image = torch.from_numpy(image.transpose((2, 0, 1)))
|
| 90 |
image = Normalize(mean=[0.485, 0.456, 0.406], std=[
|
| 91 |
0.229, 0.224, 0.225])(image)
|
|
|
|
| 92 |
with torch.no_grad():
|
| 93 |
image = torch.autograd.Variable(image.unsqueeze(0))
|
| 94 |
print("== Processing image")
|
|
|
|
| 102 |
# Convert the PyTorch tensor to a NumPy array and squeeze
|
| 103 |
pred_depth = pred_depth.cpu().numpy().squeeze()
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Continue with your file saving operations
|
| 106 |
tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 107 |
# cv2.imwrite(tmp.name, output_image)
|
| 108 |
plt.imsave(tmp.name, pred_depth, cmap='jet')
|
| 109 |
|
| 110 |
+
return [(original_image, tmp.name), tmp.name]
|
| 111 |
|
| 112 |
submit.click(on_submit, inputs=[input_image], outputs=[
|
| 113 |
depth_image_slider, raw_file])
|