ResearcherXman commited on
Commit
43c2435
1 Parent(s): fc43999
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -24,31 +24,6 @@ from controlnet_aux import OpenposeDetector
24
  from transformers import DPTImageProcessor, DPTForDepthEstimation
25
  import gradio as gr
26
 
27
- def get_depth_map(image):
28
- image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
29
- with torch.no_grad(), torch.autocast("cuda"):
30
- depth_map = depth_estimator(image).predicted_depth
31
-
32
- depth_map = torch.nn.functional.interpolate(
33
- depth_map.unsqueeze(1),
34
- size=(1024, 1024),
35
- mode="bicubic",
36
- align_corners=False,
37
- )
38
- depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
39
- depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
40
- depth_map = (depth_map - depth_min) / (depth_max - depth_min)
41
- image = torch.cat([depth_map] * 3, dim=1)
42
-
43
- image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
44
- image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
45
- return image
46
-
47
- def get_canny_image(image, t1=100, t2=200):
48
- image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
49
- edges = cv2.Canny(image, t1, t2)
50
- return Image.fromarray(edges, "L")
51
-
52
  # global variable
53
  MAX_SEED = np.iinfo(np.int32).max
54
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -104,6 +79,31 @@ controlnet_depth = ControlNetModel.from_pretrained(
104
  controlnet_depth_model, torch_dtype=dtype
105
  ).to(device)
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  controlnet_map = {
108
  "pose": controlnet_pose,
109
  "canny": controlnet_canny,
 
24
  from transformers import DPTImageProcessor, DPTForDepthEstimation
25
  import gradio as gr
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # global variable
28
  MAX_SEED = np.iinfo(np.int32).max
29
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
79
  controlnet_depth_model, torch_dtype=dtype
80
  ).to(device)
81
 
82
+ def get_depth_map(image):
83
+ image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
84
+ with torch.no_grad(), torch.autocast("cuda"):
85
+ depth_map = depth_estimator(image).predicted_depth
86
+
87
+ depth_map = torch.nn.functional.interpolate(
88
+ depth_map.unsqueeze(1),
89
+ size=(1024, 1024),
90
+ mode="bicubic",
91
+ align_corners=False,
92
+ )
93
+ depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
94
+ depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
95
+ depth_map = (depth_map - depth_min) / (depth_max - depth_min)
96
+ image = torch.cat([depth_map] * 3, dim=1)
97
+
98
+ image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
99
+ image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
100
+ return image
101
+
102
+ def get_canny_image(image, t1=100, t2=200):
103
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
104
+ edges = cv2.Canny(image, t1, t2)
105
+ return Image.fromarray(edges, "L")
106
+
107
  controlnet_map = {
108
  "pose": controlnet_pose,
109
  "canny": controlnet_canny,