Brasd99 commited on
Commit
26b0060
1 Parent(s): 55226f3

Оптимизация

Browse files
Files changed (1) hide show
  1. helpers/processor.py +16 -20
helpers/processor.py CHANGED
@@ -3,6 +3,7 @@ import imageio
3
  import numpy as np
4
  import torch
5
  from typing import Any, Dict
 
6
 
7
  from detectron2.config import get_cfg
8
  from detectron2.engine.defaults import DefaultPredictor
@@ -16,8 +17,7 @@ from densepose.structures import (
16
  from densepose.vis.base import CompoundVisualizer
17
  from densepose.vis.densepose_outputs_vertex import get_texture_atlases
18
  from densepose.vis.densepose_results_textures import (
19
- DensePoseResultsVisualizerWithTexture as dp_iuv_texture,
20
- get_texture_atlas
21
  )
22
  from densepose.vis.extractor import (
23
  CompoundExtractor,
@@ -26,30 +26,32 @@ from densepose.vis.extractor import (
26
  DensePoseResultExtractor
27
  )
28
 
29
- from detectron2.data.detection_utils import read_image
30
-
31
  class TextureProcessor:
32
  def __init__(self, config, weights):
33
  self.config = self.get_config(config, weights)
34
  self.predictor = DefaultPredictor(self.config)
35
 
36
- def process_texture(self, image, output_filename):
37
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38
  output = self.execute(image)[0]
39
 
40
  if 'pred_densepose' in output:
41
  texture = self.create_iuv(output, image)
42
- imageio.imwrite(output_filename, texture)
 
 
 
 
 
43
  else:
44
- print(f'Одежда не распознана')
45
 
46
  def extract(self, person_img, model_img):
47
- texture_filename = 'texture.png'
48
- self.process_texture(model_img, texture_filename)
49
- return self.overlay_texture(texture_filename, person_img)
50
 
51
- def overlay_texture(self, texture_name, original_image):
52
- texture_atlas = get_texture_atlas(texture_name)
53
  texture_atlases_dict = get_texture_atlases(None)
54
  vis = dp_iuv_texture(
55
  cfg=self.config,
@@ -57,14 +59,11 @@ class TextureProcessor:
57
  texture_atlases_dict=texture_atlases_dict
58
  )
59
 
60
- visualizers = [vis]
61
  extractor = create_extractor(vis)
62
- extractors = [extractor]
63
 
64
- visualizer = CompoundVisualizer(visualizers)
65
- extractor = CompoundExtractor(extractors)
66
 
67
- #img = read_image(original_image, format="BGR")
68
  with torch.no_grad():
69
  outputs = self.predictor(original_image)["instances"]
70
 
@@ -86,12 +85,10 @@ class TextureProcessor:
86
  return result["pred_boxes_XYXY"][0].cpu().numpy()
87
 
88
  def interpolate_tex(self, tex):
89
- # code is adopted from https://github.com/facebookresearch/DensePose/issues/68
90
  valid_mask = np.array((tex.sum(0) != 0) * 1, dtype='uint8')
91
  radius_increase = 10
92
  kernel = np.ones((radius_increase, radius_increase), np.uint8)
93
  dilated_mask = cv2.dilate(valid_mask, kernel, iterations=1)
94
- region_to_fill = dilated_mask - valid_mask
95
  invalid_region = 1 - valid_mask
96
  actual_part_max = tex.max()
97
  actual_part_min = tex.min()
@@ -101,7 +98,6 @@ class TextureProcessor:
101
  cv2.INPAINT_TELEA).transpose((2, 0, 1))
102
  actual_part = (actual_part_uint / 255.0) * \
103
  (actual_part_max - actual_part_min) + actual_part_min
104
- # only use dilated part
105
  actual_part = actual_part * dilated_mask
106
 
107
  return actual_part
 
3
  import numpy as np
4
  import torch
5
  from typing import Any, Dict
6
+ import io
7
 
8
  from detectron2.config import get_cfg
9
  from detectron2.engine.defaults import DefaultPredictor
 
17
  from densepose.vis.base import CompoundVisualizer
18
  from densepose.vis.densepose_outputs_vertex import get_texture_atlases
19
  from densepose.vis.densepose_results_textures import (
20
+ DensePoseResultsVisualizerWithTexture as dp_iuv_texture
 
21
  )
22
  from densepose.vis.extractor import (
23
  CompoundExtractor,
 
26
  DensePoseResultExtractor
27
  )
28
 
 
 
29
  class TextureProcessor:
30
  def __init__(self, config, weights):
31
  self.config = self.get_config(config, weights)
32
  self.predictor = DefaultPredictor(self.config)
33
 
34
+ def process_texture(self, image):
35
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
36
  output = self.execute(image)[0]
37
 
38
  if 'pred_densepose' in output:
39
  texture = self.create_iuv(output, image)
40
+ atlas_texture_bytes = io.BytesIO()
41
+ imageio.imwrite(atlas_texture_bytes, texture, format='PNG')
42
+ texture_atlas_array = np.frombuffer(atlas_texture_bytes.getvalue(), dtype=np.uint8)
43
+ texture_atlas = cv2.imdecode(texture_atlas_array, cv2.IMREAD_COLOR)
44
+ texture_atlas = cv2.cvtColor(texture_atlas, cv2.COLOR_BGR2RGB)
45
+ return texture_atlas
46
  else:
47
+ raise Exception('Clothes not found')
48
 
49
  def extract(self, person_img, model_img):
50
+ texture_atlas = self.process_texture(model_img)
51
+ return self.overlay_texture(texture_atlas, person_img)
 
52
 
53
+ def overlay_texture(self, texture_atlas, original_image):
54
+ texture_atlas[:, :, :3] = texture_atlas[:, :, 2::-1]
55
  texture_atlases_dict = get_texture_atlases(None)
56
  vis = dp_iuv_texture(
57
  cfg=self.config,
 
59
  texture_atlases_dict=texture_atlases_dict
60
  )
61
 
 
62
  extractor = create_extractor(vis)
 
63
 
64
+ visualizer = CompoundVisualizer([vis])
65
+ extractor = CompoundExtractor([extractor])
66
 
 
67
  with torch.no_grad():
68
  outputs = self.predictor(original_image)["instances"]
69
 
 
85
  return result["pred_boxes_XYXY"][0].cpu().numpy()
86
 
87
  def interpolate_tex(self, tex):
 
88
  valid_mask = np.array((tex.sum(0) != 0) * 1, dtype='uint8')
89
  radius_increase = 10
90
  kernel = np.ones((radius_increase, radius_increase), np.uint8)
91
  dilated_mask = cv2.dilate(valid_mask, kernel, iterations=1)
 
92
  invalid_region = 1 - valid_mask
93
  actual_part_max = tex.max()
94
  actual_part_min = tex.min()
 
98
  cv2.INPAINT_TELEA).transpose((2, 0, 1))
99
  actual_part = (actual_part_uint / 255.0) * \
100
  (actual_part_max - actual_part_min) + actual_part_min
 
101
  actual_part = actual_part * dilated_mask
102
 
103
  return actual_part