def getMaterialNameFromObject(obj, thresholdRelArea=0.2): nodePath = obj.find('**/model*') # Get the list of materials areas, _, _, textures = getColorAttributesFromModel(nodePath) # Get the most dominant material based on threshold on relative surface area materialDescriptions = [] for area, texture in zip(areas, textures): if texture is None or not area >= thresholdRelArea: continue # Remove any digits texture = texture.translate({ord(c):'' for c in "1234567890"}) # Remove trailing underscores texture = texture.rstrip("_") # NOTE: handle many variations of textile and wood in SUNCG texture names if "textile" in texture: texture = "textile" if "wood" in texture: texture = "wood" if texture in MATERIAL_TABLE["materials"]: textureName = MATERIAL_TABLE["materials"][texture] materialDescriptions.append(textureName) else: logger.debug('Unsupported texture basename ' 'for material semantics: %s' % (texture)) # Remove duplicates (if any) materialDescriptions = list(set(materialDescriptions)) return materialDescriptions
def getColorsFromObject(obj, mode='advanced', thresholdRelArea=0.2): nodePath = obj.find('**/model*') # Get the list of materials areas, colors, transparencies, _ = getColorAttributesFromModel( nodePath) if mode == 'basic': table = MATERIAL_COLOR_TABLE["BasicColorTable"] elif mode == 'advanced': table = MATERIAL_COLOR_TABLE["AdvancedColorTable"] elif mode == 'xkcd': table = MATERIAL_COLOR_TABLE["XkcdColorTable"] else: raise Exception('Unsupported color mode: %s' % (mode)) # Get the most dominant colors based on threshold on relative surface # area colorDescriptions = [] for area, color, _ in zip(areas, colors, transparencies): if not area >= thresholdRelArea: continue # TODO: compare color in HSV or HSL domain instead of RGB? # hsvColor = colorsys.rgb_to_hsv(*color) # Find nearest color minDistance = np.Inf bestColorName = None for colorName, refColor in iteritems(table): dist = np.linalg.norm(np.array(refColor) / 255.0 - np.array(color), ord=2) if dist < minDistance: minDistance = dist bestColorName = colorName colorDescriptions.append(bestColorName) # Remove duplicates (if any) colorDescriptions = list(set(colorDescriptions)) return colorDescriptions