pool = multiprocessing.Pool()报错:module object has no attribute Pool

遇到moduleobjecthasnoattributePool错误?可能是由于文件名冲突或模块导入顺序问题。确保无命名冲突,正确导入multiprocessing,并检查Python版本和环境设置。
部署运行你感兴趣的模型镜像

pool = multiprocessing.Pool()报错:module object has no attribute Pool,为什么?

我很抱歉,前面提供的回答是错误的。在Python的multiprocessing模块中,确实有一个名为Pool的类。如果你在使用multiprocessing.Pool()时遇到了"module object has no attribute 'Pool'"的错误提示,可能有以下几种可能原因:

1.Python版本问题:在较旧的Python版本中,multiprocessing.Pool可能没有被支持。请确保你正在使用Python 2.7或更高版本。
2.模块导入问题:如果你在代码中执行了import multiprocessing,但是出现了"module object has no attribute 'Pool'"的错误,可能是由于该模块文件命名冲突导致的。你可以尝试重新命名你的Python文件,确保它与multiprocessing.py不冲突。
3.并发执行环境的问题:某些并发执行环境可能会限制对multiprocessing.Pool的访问权限。例如,某些Jupyter Notebook或一些特殊的执行环境可能会限制并发执行或创建进程池。你可以尝试在不同的环境中执行代码,或者检查你当前环境的限制。

如果以上解决方法都不适用,可以提供更详细的代码示例和错误日志,以便我更好地帮助你排查问题。
我的问题就是第二个,那么为什么会出现这种情况?
如果在导入multiprocessing模块时出现命名冲突,可能导致你无法访问正确的Pool类。这可能是由于以下原因之一造成的:

1.文件名冲突:如果你的Python文件命名为multiprocessing.py,与Python标准库模块名称冲突,那么在导入multiprocessing模块时,Python会首先在当前目录查找同名的Python文件。由于存在名为multiprocessing.py的文件,它会被解释为你的模块而不是Python标准库的multiprocessing模块。这样导致你无法访问multiprocessing模块中定义的正确Pool类。

解决此问题的方法是将你的Python文件重新命名,避免与标准库的模块名冲突。重命名后,你应该可以成功导入并访问正确的multiprocessing.Pool类。
例如,将你的Python文件从multiprocessing.py修改为其他名称,如my_multiprocessing.py。

2.模块导入顺序:如果在你的代码中有其他模块导入,并且其中一个模块也命名为multiprocessing,由于模块导入的顺序,在导入multiprocessing模块时可能会出现冲突。

确保在导入multiprocessing之前,没有导入与标准库模块名称相同的自定义模块。确保导入顺序正确,并且没有命名冲突的模块。
总之,避免命名冲突并保持正确的模块导入顺序是避免这种情况的关键。确保你的Python文件名不与标准库模块冲突,并且不要导入与标准库模块名称相同的自定义模块,以免出现类似的问题。

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

#ldtui文件夹中有两个.py 1.__init__.py """ .. module:: ldtui :synopsis: Main tools UI. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import sys import imp import os import logging from functools import partial from ldtui import qtutils import ldt logger = logging.getLogger(__name__) class LDTWindow(QMainWindow): '''Main Tools UI Window. Loads the plugInfo.plugin_object.plugin_layout QWidget from all loaded plugins as tabs''' def __init__(self, plugins): super(LDTWindow, self).__init__() self.setWindowTitle("Look Dev Tool Set") self.setGeometry(0, 0, 650, 600) layout = QtWidgets.QGridLayout() self.setLayout(layout) tabwidget = QtWidgets.QTabWidget() tabwidget.setTabBar(qtutils.HTabWidget(width=150, height=50)) tabwidget.setTabPosition(QtWidgets.QTabWidget.West) # Stylesheet fix for Katana # With default colors, the tab text is almost the # same as the tab background stylesheet = """ QTabBar::tab:unselected {background: #222222;} QTabWidget>QWidget>QWidget{background: #222222;} QTabBar::tab:selected {background: #303030;} QTabWidget>QWidget>QWidget{background: #303030;} """ tabwidget.setStyleSheet(stylesheet) layout.addWidget(tabwidget, 0, 0) plugins_ui = {} plugins_buttons = {} for pluginInfo in plugins.getAllPlugins(): tabwidget.addTab( pluginInfo.plugin_object.plugin_layout, pluginInfo.name) self.setCentralWidget(tabwidget) 2.qtutils.py """ .. module:: qtutils :synopsis: small qt utilies and custom widgets. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import logging logger = logging.getLogger(__name__) def get_folder_path(): """Gets a folder path from the user""" file_dialog = QtWidgets.QFileDialog() file_dialog.setFileMode(QtWidgets.QFileDialog.Directory) if file_dialog.exec_(): path = str(file_dialog.selectedFiles()[0]) return path else: return None class HTabWidget(QtWidgets.QTabBar): ''' QPaint event to draw the QTabWidget titles horizontally ''' def __init__(self, *args, **kwargs): self.tabSize = QtCore.QSize(kwargs.pop('width'), kwargs.pop('height')) super(HTabWidget, self).__init__(*args, **kwargs) def paintEvent(self, event): painter = QtWidgets.QStylePainter(self) option = QtWidgets.QStyleOptionTab() for index in range(self.count()): self.initStyleOption(option, index) tabRect = self.tabRect(index) tabRect.moveLeft(10) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option) painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index)) def tabSizeHint(self, index): return self.tabSize #ldtutils文件夹中有一个文件 1.__init__.py """ .. module:: ldtutils :synopsis: general non dcc specific utils. .. moduleauthor:: Ezequiel Mastrasso """ from ldtcommon import CONFIG_MATERIALS_JSON from ldtcommon import TEXTURE_CHANNEL_MATCHING_RATIO from ldtcommon import TEXTURE_MATCHING_RATIO from fuzzywuzzy import fuzz import subprocess import multiprocessing import sys import json import logging import os import random import lucidity logger = logging.getLogger(__name__) def create_commands(texture_mapping): default_command = "maya -batch -file someMayaFile.mb -command " commands = [] for key, value in texture_mapping: dir, filename = os.path.split(key) filename, ext = os.path.splitext(filename) filename = os.path.join(dir, filename + "WithTexture." + ext) commands.append("".format(default_command, "abc_file", key, "texture", value, "file -save ", filename)) return commands def launch_function(func, args): commands = create_commands(args) launch_multiprocess(launch_subprocess, commands) def launch_subprocess(command): try: subprocess_output = subprocess.check_output(command) logger.info("Subprocess launched\t{out}\nrunning the command\t{command}", out=subprocess_output, command=command) except subprocess.CalledProcessError as e: logger.exception( "Error while trying to launch subprocess: {}".format(e.output)) raise return command def launch_multiprocess(function, args): """ :param function: Function to be called inside the multiprocess -- Takes only 1 arg :param args: list/tuple of arguments for above function :return: """ multiprocessing.freeze_support() pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()/2)) logger.debug("Pool created") try: pool_process = pool.map_async(function, (args)) except Exception as e: msg = "Error while trying to launch process in pool: {}".format(e) logger.exception(msg) raise finally: pool.close() pool.join() try: logger.info("output from the process: {pool_out}", out=pool_process.successful()) return 0 except AssertionError: logger.exception("Couldn't communicate with the process poll created; \ No output from processes fetched") return 1 def map_textures_to_alembic(texture_mapping): launch_subprocess(command) def load_json(file_path): """ Load a json an returns a dict. Args: file_path (str): Json file path to open. """ with open(file_path) as handle: dictdump = json.loads(handle.read()) return dictdump def save_json(file_path, data): """ Dump a dict into a json file. Args: file_path (str): Json file path to save. data (dict): Data to save into the json file. """ # TODO (eze) pass def get_random_color(seed): """ Return a random color using a seed. Used by all material creating, and viewport color functions that do not use textures, to have a common color accross dccs Args: seed (str): Returns: tuple, R,G,B colors. """ random.seed(seed + "_r") color_red = random.uniform(0, 1) random.seed(seed + "_g") color_green = random.uniform(0, 1) random.seed(seed + "_b") color_blue = random.uniform(0, 1) return color_red, color_green, color_blue def create_directoy(path): """ Create a folder. Args: path (str): Directory path to create. """ os.mkdir(path) logger.info("Directory created: %s" % path) def is_directory(path): """ Check if the given path exists, and is a directory. Args: path (str): Directory to check. """ if os.path.exists(path) and os.path.isdir(path): return True else: return False def get_files_in_folder(path, recursive=False, pattern=None): """ Search files in a folder. Args: path (str): Path to search. Kwards: recursive (bool): Search files recursively in folder. pattern (str): pattern to match, for ie '.exr'. Returns: array. File list """ logger.info("Searching for files in: %s" % path) logger.info("Searching options: Recursive %s, pattern: %s" % (recursive, pattern)) file_list = [] for path, subdirs, files in os.walk(path): for file in files: # skip .mayaswatchs stuff if ".maya" not in file: if pattern: if pattern in file: file_list.append(os.path.join(path, file)) logger.debug( "File with pattern found, added to the list: %s" % file) else: file_list.append(os.path.join(path, file)) logger.debug("File added to the list: %s" % file) if not recursive: break return file_list def string_matching_ratio(stringA, stringB): """ Compare two strings and returns a fuzzy string matching ratio. In general ratio, partial_ratio, token_sort_ratio and token_set_ratio did not give different results given that we are comparin a single. TODO: Try bitap algorithm for fuzzy matching, partial substring matching might be better for our cases. Different channels fuzzy ratio comparission ('baseColor','diffusecolor') = 67 ('base','diffusecolor') = 25 ('specular','specularColor') = 76 ('specular','specularcolor') = 76 ('specular_color', 'specular_bump') = 67 ('coat_color', 'coat_ior') = 78 ('secondary_specular_color', 'secondary_specular_ior') = 91 ('subsurface_weight', 'subsurface_Color') = 67 ('emission', 'emission_weight') = 70 Same channel diferent naming ratio comparission ('diffuse_weight','diffuseGain') = 64 Args: stringA (str): string to compare against. stringB (str): string to compare. Returns: int. Ratio, from 0 to 100 according to fuzzy matching. """ return fuzz.token_set_ratio(stringA, stringB) def get_config_materials(): """ Gets the CONFIG_MATERIALS_JSON as a dict Returns: dict. CONFIG_MATERIALS_JSON """ return load_json(CONFIG_MATERIALS_JSON) 你看看
08-22
ldtcommon代码: """ .. module:: ldtcommon :synopsis: Constants and templates non dcc specific used in other packages. .. moduleauthor:: Ezequiel Mastrasso """ import lucidity import logging import os logger = logging.getLogger(__name__) LOOKDEVTOOLS_FOLDER = os.environ['LOOKDEVTOOLS'] #: Attributes for tagging meshes for surfacing and texture-to-mesh matching ATTR_SURFACING_PROJECT = "surfacing_project" ATTR_SURFACING_OBJECT = "surfacing_object" #: Attribute for tagging Materials. Values: name of assigned project or object ATTR_MATERIAL = "surfacing_material" #: Attribute for tagging material assignmnents. Values: 'project' or 'object'] ATTR_MATERIAL_ASSIGN = "surfacing_assign" #: Attribute for tagging viewport material. Values: 'color' or 'pattern'] ATTR_MATERIAL_VP = "surfacing_vp" #: Global string matching ratios to compare strings against lucidity parsed files. #: Notice that the ratio constant should be high enough, TEXTURE_MATCHING_RATIO = 90 TEXTURE_CHANNEL_MATCHING_RATIO = 90 #: Texture file template, ANCHOR RIGHT TEXTURE_FILE_PATTERN = '{surfacing_project}_{surfacing_object}_{channel}_{colorspace}.{udim}.{extension}' #: Default shader node to use DEFAULT_SHADER = 'PxrSurface' #: Materials json Config, contains texture name mappings to shader plugs CONFIG_MATERIALS_JSON = os.path.join( os.environ['LOOKDEVTOOLS'], 'python', 'ldtconfig', 'materials.json') def texture_file_template(custom_pattern=None): """ Get a lucidity Template object using a custom template. Kwargs: custom_pattern (str): Custom lucidity file pattern. Returns: lucity.Template object with the custom partern """ logger.info('Loading lucidity with:\n %s' % custom_pattern) texture_file_template = lucidity.Template( 'textureset_element', custom_pattern, anchor=lucidity.Template.ANCHOR_END # TODO (Eze) Add STRICT? ) return texture_file_template ldtmaya代码: """ .. module:: ldtmaya :synopsis: general maya functions. .. moduleauthor:: Ezequiel Mastrasso """ import ldtcommon import ldtutils from ldtcommon import ATTR_SURFACING_PROJECT from ldtcommon import ATTR_SURFACING_OBJECT from ldtcommon import ATTR_MATERIAL from ldtcommon import ATTR_MATERIAL_ASSIGN from ldtcommon import ATTR_MATERIAL_VP from ldtui import qtutils from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import os import sys import traceback import random import logging import pymel.core as pm import maya.mel as mel import maya.cmds as mc logger = logging.getLogger(__name__) def surfacingInit(): """ Initialize the scene for surfacing projects. Creates the surfacing root, an empty surfacing project and object, and runs the validation to create and connect the partition Returns: bool. Valid scene. """ root = create_surfacing_root() if not root.members: surfacing_project = create_surfacing_project("defaultProject") create_surfacing_object(surfacing_project, "defaultObject") validate_surfacing() def create_surfacing_root_node(): """Create projects root node""" surfacing_root = pm.createNode( "objectSet", name="surfacing_root" ) surfacing_root.setAttr( "surfacing_root", "", force=True ) return surfacing_root def create_surfacing_root(): """Create projects root if it doesnt exist.""" if not get_surfacing_root(): surfacing_root = get_surfacing_root() return surfacing_root else: return get_surfacing_root() def create_surfacing_project(name=None): """ Creates a surfacing project. Kwargs: name (str): surfacing project name """ if not name: name = "project" surfacing_project = pm.createNode( "objectSet", name=name ) surfacing_project.setAttr( ATTR_SURFACING_PROJECT, "", force=True ) create_surfacing_object(surfacing_project) get_surfacing_root().add(surfacing_project) update_surfacing_partition() return surfacing_project def create_surfacing_object(project, name=None): """ Creates a surfacing Object under a given project. Args: project (PyNode): surfacing project Kwargs: name (str): surfacing object name """ if not name: name = "object" surfacing_set = pm.createNode( "objectSet", name=name ) surfacing_set.setAttr( ATTR_SURFACING_OBJECT, "", force=True ) project.add(surfacing_set) return surfacing_set def get_surfacing_root(): """ Get the project root node. Returns: PyNode. Surfacing root node Raises: Exception. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr("surfacing_root") ] if len(objSetLs) == 0: logger.info( "surfacing_root node found, creating one" ) return create_surfacing_root_node() elif len(objSetLs) > 1: raise Exception( "More than 1 surfacing_root node found, clean up your scene" ) return objSetLs[0] def get_surfacing_projects(): """ Get all surfacing Projects under the root. Returns: list. surfacing projects PyNodes list. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr(ATTR_SURFACING_PROJECT) ] return objSetLs def get_surfacing_project_by_name(name=None): """ Get surfacing Project by name. Kwargs: name (str): surfacing project name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_project attr value. """ projects_list = get_surfacing_projects() for each in projects_list: if name == each.name(): return each return None def get_surfacing_object_by_name(name=None): """ Get surfacing Object by name. Kwargs: name (str): surfacing object name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_object attr value. """ projects_list = get_surfacing_projects() for prj in projects_list: objs = get_surfacing_objects(prj) for obj in objs: if name == obj.name(): return obj return None def delete_surfacing_project(project): """ Delete a surfacing_project, and its members. Args: project (PyNode): surfacing project. """ if is_surfacing_project(project): pm.delete(project.members()) def get_surfacing_objects(project): """ Get all surfacing Objects under the given surfacing project Args: project (PyNode): surfacing project """ if is_surfacing_project(project): return project.members() else: return [] def is_surfacing_project(project): """ Check if the node is a surfacing project Args: project (PyNode): surfacing project Returns: bool. True if it is. """ if project.hasAttr(ATTR_SURFACING_PROJECT): return True else: return False def is_surfacing_object(surfacing_object): """ Check if node is surfacing Object Args: surfacing_object (PyNode): surfacing_object """ if surfacing_object.hasAttr(ATTR_SURFACING_OBJECT): return True else: return False def remove_surfacing_invalid_members(): """ Pops all not-allowd member types from surfacing projects and objects. Only Allowed types: objectSets (surfacing_projects) inside the surfacing projects root objectSets (surfacing_object) inside surfacing projects transforms (that have a mesh) inside surfacing_object """ project_root = get_surfacing_root() for project in project_root.members(): if ( not project.type() == "objectSet" ): # TODO (eze) add check for attr project_root.removeMembers([project]) for project in get_surfacing_projects(): for object in get_surfacing_objects( project ): # TODO (eze) add check for attr if not object.type() == "objectSet": project.removeMembers([object]) else: for member in object.members(): if not member.type() == "transform": logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) elif not member.listRelatives( type="mesh" ): logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) def get_mesh_transforms(object_list): # TODO move to common """ Get all the mesh shapes transforms. Includes all descendants in hierarchy. Args: object_list (list): PyNode list of nodes. """ shapes_in_hierarchy = pm.listRelatives( object_list, allDescendents=True, path=True, f=True, type="mesh", ) shapes_transforms = pm.listRelatives( shapes_in_hierarchy, p=True, path=True, f=True ) return shapes_transforms def add_member(surfacing_object, transform): # TODO move to common """ Add transform to surfacing Object Args: surfacing_object (PyNode): surfacing object transform (PyNode): transform node """ pm.sets(surfacing_object, transform, fe=True) def add_mesh_transforms_to_surfacing_object( surfacing_object, object_list ): """ Add all mesh shape transforms -and descendants- from the list to a surfacing Object. Args: surfacing_object (PyNode): surfacing object object_list (list): object list """ pm.select() if is_surfacing_object(surfacing_object): for item in object_list: # Disconnect the objects from other Surf proj and obj for c in item.instObjGroups.listConnections(c=True, p=True): if is_surfacing_object(c[1].node()) or is_surfacing_project(c[1].node()): logger.info( "disconnecting from Surf project or obj: %s" % c[1].node() ) pm.disconnectAttr("%s"%c[0], "%s"%c[1]) for transform in get_mesh_transforms(item): pm.select(transform) add_member(surfacing_object, transform) def update_surfacing_partition(): """Recreate the partition node, and reconnects to all the surfacing objects objectSets.""" partitions = [ item for item in pm.ls(type="partition") if item.hasAttr("surfacing_partition") ] for each in partitions: logger.info( "disconnecting existing partition: %s" % each ) each.sets.disconnect() pm.delete(each) logger.info("deleted partition") surfacing_partition = pm.createNode( "partition", name="surfacing_partition" ) logger.info( "partition created: %s" % surfacing_partition ) surfacing_partition.setAttr( "surfacing_partition", "", force=True ) for project in get_surfacing_projects(): for object in get_surfacing_objects(project): pm.connectAttr( "%s.partition" % object, surfacing_partition.sets, na=True, ) logger.info( "partition connected: %s " % object ) def remove_invalid_characters(): """Remove not allowed characters from surfacing projects and names like '_'.""" project_root = get_surfacing_root() surfacing_projects = get_surfacing_projects() #invalid_character = '_' invalid_character = '*' for project in surfacing_projects: if invalid_character in project.name(): project.rename(project.name().replace(invalid_character, '')) logger.info( 'Invalid character removed from surfacing_project,' 'new name: %s' % project) for surfacing_object in get_surfacing_objects(project): if invalid_character in surfacing_object.name(): surfacing_object.rename( surfacing_object.name().replace(invalid_character, '')) logger.info( 'Invalid characters removed from surfacing_object,' 'new name: %s' % surfacing_object) def validate_surfacing(): """ Validate the scene. Removes invalidad characters and members, updates the partition, and mesh attributes. """ remove_invalid_characters() remove_surfacing_invalid_members() update_surfacing_partition() update_surfacing_attributes() def export_alembic(geo_list, file_path): """ Export alembic file from the object list. Args: geo_list (list): list of geometry to export file_path (str): export file path """ if geo_list and file_path: roots = " -root |" + " -root |".join( [str(x) for x in geo_list] ) cmd = ( r'-frameRange 0 0 -uvWrite -dataFormat ogawa ' r'-userAttrPrefix surfacing' + roots + ' -file ' + (file_path) ) logger.info("AbcExport: %s" % cmd) mc.AbcExport(j=cmd) logger.info( "Succesful Alembic export to: %s" % file_path ) def merge_surfacing_object_meshes(surfacing_object): """ Merge all the meshs assigned to a surfacing Object. Args: surfacing_object (PyNode): surfacing object Raises: BaseException. Could not merge member meshes. """ try: members = surfacing_object.members() logger.info("Merging members: %s" % members) geo_name = "%s_geo" % str(surfacing_object) if len(members) > 1: geo = pm.polyUnite(*members, n=geo_name) return geo[0] else: logger.info( "single object found, skipping merge: %s" % members[0] ) members[0].rename(geo_name) pm.parent(members[0], world=True) return members[0] except BaseException: logger.error( "Could not merge members of: %s" % surfacing_object ) return False def export_surfacing_project(project, subdiv_level=0, single_export=True, folder_path=False): """ Export surfacing Project to Alembic. Args: project (PyNode): surfacing project Kwargs: single_export (bool): is single export folder_path (str): Export folder path """ current_file = pm.sceneName() if single_export: save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() project_geo_list = [] if ldtutils.is_directory(folder_path) and is_surfacing_project(project): for each in get_surfacing_objects(project): merged_geo = merge_surfacing_object_meshes(each) if merged_geo: project_geo_list.append(merged_geo) if project_geo_list: if subdiv_level: for geo in project_geo_list: logger.info( "subdivision level: %s" % subdiv_level ) logger.info( "subdividing merged members: %s" % geo ) # -mth 0 -sdt 2 -ovb 1 -ofb 3 -ofc 0 -ost 0 -ocr 0 -dv 3 # -bnr 1 -c 1 -kb 1 -ksb 1 -khe 0 -kt 1 -kmb 1 -suv 1 # -peh 0 -sl 1 -dpe 1 -ps 0.1 -ro 1 -ch 1 pm.polySmooth( geo, mth=0, sdt=2, ovb=1, dv=subdiv_level ) export_file_path = os.path.join( folder_path, str(project) + ".abc" ) export_alembic(project_geo_list, export_file_path) export_surfacing_object_dir = os.path.join( folder_path, str(project) ) ldtutils.create_directoy(export_surfacing_object_dir) for geo in project_geo_list: export_root = " -root |" + geo export_surfacing_object_path = os.path.join( export_surfacing_object_dir + "/" + geo + ".abc" ) export_alembic( [geo], export_surfacing_object_path ) if single_export: pm.openFile(current_file, force=True) def export_all_surfacing_projects(folder_path=None, subdiv_level=0): """ Export all surfacing Projects. Kwargs: folder_path (str): folder to export files. """ save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() current_file = pm.sceneName() for project in get_surfacing_projects(): export_surfacing_project( project, subdiv_level, single_export=False, folder_path=folder_path ) pm.openFile(current_file, force=True) return True def save_unsaved_scene_(): # TODO move to common """Check the scene state, if modified, will ask the user to save it.""" if unsaved_scene(): if save_scene_dialog(): pm.saveFile(force=True) else: raise ValueError("Unsaved changes") def update_surfacing_attributes(): """ Create attributes on meshes of what surfacing object they are assigned to. Adds the attributes to all the shapes transforms assigned to surfacing objects. This will be used later for quick shader/material creation and assignment. """ for project in get_surfacing_projects(): project.setAttr(ATTR_SURFACING_PROJECT, project) logger.info( "Updating attributes for project: %s" % project ) for surfacing_object_set in get_surfacing_objects(project): logger.info( "\tUpdating attributes for object texture set: %s" % surfacing_object_set ) surfacing_object_set.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set ) members = surfacing_object_set.members() logger.info( "\t\tUpdating attr for meshes: %s" % members ) for member in members: member.setAttr( ATTR_SURFACING_PROJECT, project.name(), force=True, ) member.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set.name(), force=True, ) def set_wifreframe_color_black(): """Set the wireframe color to black in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(1) def set_wifreframe_color_none(): """Remove the wireframe color in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(0) def set_wireframe_colors_per_project(): """ Set the wireframe color per surfacing project. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() for project in projects: random.seed(project) wire_color = random.randint(1, 31) for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(wire_color) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_wireframe_colors_per_object(): """ Set the wireframe color per surfacing object. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() print projects for project in projects: for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(1) mesh_shape.overrideColorRGB.set( ldtutils.get_random_color(surfacingObject) ) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_materials_per_object(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: for obj in get_surfacing_objects(project): shader, shading_group = create_shader( type=shader_type) pm.select(obj) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(obj) ) else: shader.color.set( ldtutils.get_random_color(obj) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'obj', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), obj.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def set_materials_per_project(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: shader, shading_group = create_shader( type=shader_type) pm.select(project) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(project) ) else: shader.color.set( ldtutils.get_random_color(project) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'project', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), project.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def delete_materials(): """delete all material networks that have surfacing attributes""" all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL): to_delete.append(shading_group) pm.delete(to_delete) def delete_materials_viewport(type=None): """ delete all material networks that have surfacing attributes. Kwargs: type (str): type of vp material to delete, usually 'color', or 'pattern' """ all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL_VP): to_delete.append(shading_group) pm.delete(to_delete) def unsaved_scene(): """Check for unsaved changes.""" import maya.cmds as cmds return cmds.file(q=True, modified=True) def save_scene_dialog(): """ Ask the user to go ahead save or cancel the operation. Returns: bool. True is Ok clicked, false otherwise. """ msg = QtWidgets.QMessageBox() msg.setIcon(QtWidgets.QMessageBox.Information) msg.setText("Your scene has unsaved changes") msg.setInformativeText("") msg.setWindowTitle("Warning") msg.setDetailedText( "This tool will do undoable changes. It requires you to save your scene, and reopen it after its finished" ) msg.setStandardButtons( QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel ) retval = msg.exec_() if retval == QtWidgets.QMessageBox.Ok: return True else: return False def create_file_node(name=None): """ Create a file node, and its 2dPlacement Node. Kwargs: name (str): file node name Returns: PyNode. Image file node """ file_node = pm.shadingNode( 'file', name=name, asTexture=True, isColorManaged=True) placement_name = '%s_place2dfile_nodeture' % name placement_node = pm.shadingNode( 'place2dTexture', name=placement_name, asUtility=True) file_node.filterType.set(0) pm.connectAttr(placement_node.outUV, file_node.uvCoord) pm.connectAttr(placement_node.outUvFilterSize, file_node.uvFilterSize) pm.connectAttr(placement_node.coverage, file_node.coverage) pm.connectAttr(placement_node.mirrorU, file_node.mirrorU) pm.connectAttr(placement_node.mirrorV, file_node.mirrorV) pm.connectAttr(placement_node.noiseUV, file_node.noiseUV) pm.connectAttr(placement_node.offset, file_node.offset) pm.connectAttr(placement_node.repeatUV, file_node.repeatUV) pm.connectAttr(placement_node.rotateFrame, file_node.rotateFrame) pm.connectAttr(placement_node.rotateUV, file_node.rotateUV) pm.connectAttr(placement_node.stagger, file_node.stagger) pm.connectAttr(placement_node.translateFrame, file_node.translateFrame) pm.connectAttr(placement_node.wrapU, file_node.wrapU) pm.connectAttr(placement_node.wrapV, file_node.wrapV) return file_node def create_shader(type='PxrSurface'): """ Create shaders and shading groups. Kwargs: type (str): type of material shader to create, for ie 'blinn' tag (str): tag to set in ATTR_MATERIAL, usually the surfacing project or surfacing object Returns: tuple. PyNode shader, and PyNode shading_group """ shader, shading_group = pm.createSurfaceShader(type) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), '', force=True) return shader, shading_group def import_surfacing_textures(): """ TODO WE DONT REALLY NEED THIS DONT WE TODO THIS IS A WORKING HARDCODED IMPORT, IMPLEMENT CORRECTLY Import textures to surfacing objects or projects. Kwargs: parsed_files (list): list of lucidity parsed files with 'filepath' key key (str): surfacing attr to use for import, surfacing project or surfacing object shaders (list): a list of shaders, where the keys match the parsed files key to use for import """ import pymel.core as pm from ldtcommon import TEXTURE_FILE_PATTERN import ldtmaya import ldtutils import ldttextures textures_folder = '/run/media/ezequielm/misc/wrk/current/cabinPixar/textures' texture_list = ldtutils.get_files_in_folder( textures_folder, recursive=True, pattern='.tex') texture_finder = ldttextures.TextureFinder( TEXTURE_FILE_PATTERN, texture_list) texture_finder.get_channel_plug(texture_list[0]) for surfPrj in ldtmaya.get_surfacing_projects(): for surfObj in ldtmaya.get_surfacing_objects(surfPrj): # Find texture files with a matching surfacing_project, get udim paths texture_files = texture_finder.find_key_values( surfacing_project=surfPrj, merge_udims=True) if texture_files: print surfObj # create and assign the material to each surfacing_object pm.select(surfObj) meshes = pm.ls(sl=True) shader, shading_group = ldtmaya.create_shader() shader.rename('%s_%s' % (surfObj, 'material')) pm.sets(shading_group, forceElement=meshes) pm.select(None) for texture_file in texture_files: # get the shader plug input, to connect the texture to shader_plug = texture_finder.get_channel_plug(texture_file) if shader_plug: shader_plug = pm.PyNode( '%s.%s' % (shader.name(), shader_plug)) # create image nodes file_node = ldtmaya.create_file_node( '%s_%s' % (surfObj, shader_plug)) # paths come with 'udim', replac this with the prman <UDIM> file_node.fileTextureName.set( texture_file.replace('udim', '<UDIM>')) pm.setAttr('%s.%s' % (file_node, 'uvTilingMode'), 3) pm.setAttr('%s.%s' % (file_node, 'alphaIsLuminance'), 1) # TODO Query the shader plug, connect RGB or single channel # TODO if a normal or bump, create the inbetween node # if shader_plug == "normal" or shader_plug == "bump" print "plug %s -->%s" % (texture_file, shader_plug) if len(shader_plug.elements()) == 4: if "bump" in shader_plug.name(): bump_node = pm.shadingNode( 'bump2d', asTexture=True) bump_node.rename('%s_%s' % (surfObj, 'bump')) file_node.outAlpha.connect(bump_node.bumpValue) bump_node.outNormal.connect(shader_plug) else: file_node.outColor.connect(shader_plug) else: file_node.outAlpha.connect(shader_plug) ldtui代码:init_.py: """ .. module:: ldtui :synopsis: Main tools UI. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import sys import imp import os import logging from functools import partial from ldtui import qtutils import ldt logger = logging.getLogger(__name__) class LDTWindow(QMainWindow): '''Main Tools UI Window. Loads the plugInfo.plugin_object.plugin_layout QWidget from all loaded plugins as tabs''' def __init__(self, plugins): super(LDTWindow, self).__init__() self.setWindowTitle("Look Dev Tool Set") self.setGeometry(0, 0, 650, 600) layout = QtWidgets.QGridLayout() self.setLayout(layout) tabwidget = QtWidgets.QTabWidget() tabwidget.setTabBar(qtutils.HTabWidget(width=150, height=50)) tabwidget.setTabPosition(QtWidgets.QTabWidget.West) # Stylesheet fix for Katana # With default colors, the tab text is almost the # same as the tab background stylesheet = """ QTabBar::tab:unselected {background: #222222;} QTabWidget>QWidget>QWidget{background: #222222;} QTabBar::tab:selected {background: #303030;} QTabWidget>QWidget>QWidget{background: #303030;} """ tabwidget.setStyleSheet(stylesheet) layout.addWidget(tabwidget, 0, 0) plugins_ui = {} plugins_buttons = {} for pluginInfo in plugins.getAllPlugins(): tabwidget.addTab( pluginInfo.plugin_object.plugin_layout, pluginInfo.name) self.setCentralWidget(tabwidget) qtutils.py: """ .. module:: qtutils :synopsis: small qt utilies and custom widgets. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import logging logger = logging.getLogger(__name__) def get_folder_path(): """Gets a folder path from the user""" file_dialog = QtWidgets.QFileDialog() file_dialog.setFileMode(QtWidgets.QFileDialog.Directory) if file_dialog.exec_(): path = str(file_dialog.selectedFiles()[0]) return path else: return None class HTabWidget(QtWidgets.QTabBar): ''' QPaint event to draw the QTabWidget titles horizontally ''' def __init__(self, *args, **kwargs): self.tabSize = QtCore.QSize(kwargs.pop('width'), kwargs.pop('height')) super(HTabWidget, self).__init__(*args, **kwargs) def paintEvent(self, event): painter = QtWidgets.QStylePainter(self) option = QtWidgets.QStyleOptionTab() for index in range(self.count()): self.initStyleOption(option, index) tabRect = self.tabRect(index) tabRect.moveLeft(10) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option) painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index)) def tabSizeHint(self, index): return self.tabSize ldtutils代码: """ .. module:: ldtutils :synopsis: general non dcc specific utils. .. moduleauthor:: Ezequiel Mastrasso """ from ldtcommon import CONFIG_MATERIALS_JSON from ldtcommon import TEXTURE_CHANNEL_MATCHING_RATIO from ldtcommon import TEXTURE_MATCHING_RATIO from fuzzywuzzy import fuzz import subprocess import multiprocessing import sys import json import logging import os import random import lucidity logger = logging.getLogger(__name__) def create_commands(texture_mapping): default_command = "maya -batch -file someMayaFile.mb -command " commands = [] for key, value in texture_mapping: dir, filename = os.path.split(key) filename, ext = os.path.splitext(filename) filename = os.path.join(dir, filename + "WithTexture." + ext) commands.append("".format(default_command, "abc_file", key, "texture", value, "file -save ", filename)) return commands def launch_function(func, args): commands = create_commands(args) launch_multiprocess(launch_subprocess, commands) def launch_subprocess(command): try: subprocess_output = subprocess.check_output(command) logger.info("Subprocess launched\t{out}\nrunning the command\t{command}", out=subprocess_output, command=command) except subprocess.CalledProcessError as e: logger.exception( "Error while trying to launch subprocess: {}".format(e.output)) raise return command def launch_multiprocess(function, args): """ :param function: Function to be called inside the multiprocess -- Takes only 1 arg :param args: list/tuple of arguments for above function :return: """ multiprocessing.freeze_support() pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()/2)) logger.debug("Pool created") try: pool_process = pool.map_async(function, (args)) except Exception as e: msg = "Error while trying to launch process in pool: {}".format(e) logger.exception(msg) raise finally: pool.close() pool.join() try: logger.info("output from the process: {pool_out}", out=pool_process.successful()) return 0 except AssertionError: logger.exception("Couldn't communicate with the process poll created; \ No output from processes fetched") return 1 def map_textures_to_alembic(texture_mapping): launch_subprocess(command) def load_json(file_path): """ Load a json an returns a dict. Args: file_path (str): Json file path to open. """ with open(file_path) as handle: dictdump = json.loads(handle.read()) return dictdump def save_json(file_path, data): """ Dump a dict into a json file. Args: file_path (str): Json file path to save. data (dict): Data to save into the json file. """ # TODO (eze) pass def get_random_color(seed): """ Return a random color using a seed. Used by all material creating, and viewport color functions that do not use textures, to have a common color accross dccs Args: seed (str): Returns: tuple, R,G,B colors. """ random.seed(seed + "_r") color_red = random.uniform(0, 1) random.seed(seed + "_g") color_green = random.uniform(0, 1) random.seed(seed + "_b") color_blue = random.uniform(0, 1) return color_red, color_green, color_blue def create_directoy(path): """ Create a folder. Args: path (str): Directory path to create. """ os.mkdir(path) logger.info("Directory created: %s" % path) def is_directory(path): """ Check if the given path exists, and is a directory. Args: path (str): Directory to check. """ if os.path.exists(path) and os.path.isdir(path): return True else: return False def get_files_in_folder(path, recursive=False, pattern=None): """ Search files in a folder. Args: path (str): Path to search. Kwards: recursive (bool): Search files recursively in folder. pattern (str): pattern to match, for ie '.exr'. Returns: array. File list """ logger.info("Searching for files in: %s" % path) logger.info("Searching options: Recursive %s, pattern: %s" % (recursive, pattern)) file_list = [] for path, subdirs, files in os.walk(path): for file in files: # skip .mayaswatchs stuff if ".maya" not in file: if pattern: if pattern in file: file_list.append(os.path.join(path, file)) logger.debug( "File with pattern found, added to the list: %s" % file) else: file_list.append(os.path.join(path, file)) logger.debug("File added to the list: %s" % file) if not recursive: break return file_list def string_matching_ratio(stringA, stringB): """ Compare two strings and returns a fuzzy string matching ratio. In general ratio, partial_ratio, token_sort_ratio and token_set_ratio did not give different results given that we are comparin a single. TODO: Try bitap algorithm for fuzzy matching, partial substring matching might be better for our cases. Different channels fuzzy ratio comparission ('baseColor','diffusecolor') = 67 ('base','diffusecolor') = 25 ('specular','specularColor') = 76 ('specular','specularcolor') = 76 ('specular_color', 'specular_bump') = 67 ('coat_color', 'coat_ior') = 78 ('secondary_specular_color', 'secondary_specular_ior') = 91 ('subsurface_weight', 'subsurface_Color') = 67 ('emission', 'emission_weight') = 70 Same channel diferent naming ratio comparission ('diffuse_weight','diffuseGain') = 64 Args: stringA (str): string to compare against. stringB (str): string to compare. Returns: int. Ratio, from 0 to 100 according to fuzzy matching. """ return fuzz.token_set_ratio(stringA, stringB) def get_config_materials(): """ Gets the CONFIG_MATERIALS_JSON as a dict Returns: dict. CONFIG_MATERIALS_JSON """ return load_json(CONFIG_MATERIALS_JSON)
08-21
============================================================ 超大规模泊松圆盘采样生成器 (区域: 100x100mm, 间距: 0.026mm) ============================================================ 理论最大点数: 17081368 使用分布式采样生成随机点 (4个进程)... multiprocessing.pool.RemoteTraceback: """ Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\pool.py", line 125, in worker result = (True, func(*args, **kwds)) ^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\pool.py", line 48, in mapstar return list(map(*args)) ^^^^^^^^^^^^^^^^ File "D:\pycharm\New Test\FBe01.py", line 559, in distributed_sampling sampler = PoissonDiskSampler( ^^^^^^^^^^^^^^^^^^^ File "D:\pycharm\New Test\FBe01.py", line 347, in __init__ self.grid = HierarchicalGrid(width, height, min_distance) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pycharm\New Test\FBe01.py", line 46, in __init__ self.create_levels() File "D:\pycharm\New Test\FBe01.py", line 54, in create_levels 'cell_size': self.base极cell_size ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'HierarchicalGrid' object has no attribute 'base极cell_size' """ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\pycharm\New Test\FBe01.py", line 754, in <module> main() File "D:\pycharm\New Test\FBe01.py", line 676, in main results = pool.map(distributed_sampling, tasks) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\pool.py", line 367, in map return self._map_async(func, iterable, mapstar, chunksize).get() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\pool.py", line 774, in get raise self._value AttributeError: 'HierarchicalGrid' object has no attribute 'base极cell_size'
07-02
import math import os import struct from argparse import ArgumentParser import av import numpy as np import open3d as o3d import rosbag import yaml from sensor_msgs import point_cloud2 import subprocess from protoc.octopudata_localizationinfo_pb2 import LocalizationInfoFrame, LocalizationInfo from protoc.octopudata_trackedobject_pb2 import TrackedObjectFrame, Object, TrackedObject from protoc.octopusdata_controlcommand_pb2 import CommandFrame, ControlCommand from protoc.octopusdata_gnss_pb2 import GnssPoint, GnssPoints from protoc.octopusdata_plantrajectory_pb2 import Trajectory, TrajectoryPoint, PlanTrajectory from protoc.octopusdata_predictionobstacles_pb2 import PerceptionObstacle, \ Obstacle, PredictionTrajectory, PathPoint, PredictionObstacles from protoc.octopusdata_routingpath_pb2 import RoutingPath, Path, Point, RoutingFrames from protoc.octopusdata_vehicleinfo_pb2 import VehicleFrame, VehicleInfo av.logging.set_level(av.logging.PANIC) codec_ctx = av.codec.Codec('hevc','r') h265_code = codec_ctx.create() class Pose: def __init__(self, q0, q1, q2, q3, x, y, z): self.q0 = q0 self.q1 = q1 self.q2 = q2 self.q3 = q3 self.x = x self.y = y self.z = z def get_ts(secs, nsecs): return int(secs * 1000 + nsecs / 1000000) def get_modulo(x, y, z): return math.sqrt(x * x + y * y + z * z) def yaw_from_quaternions(w, x, y, z): a = 2 * (w * z + x * y) b = 1 - 2 * (y * y + z * z) return math.atan2(a, b) def pose_has_nan(p): return math.isnan(p.x) or math.isnan(p.y) or math.isnan(p.z) or \ math.isnan(p.q0) or math.isnan(p.q1) or math.isnan(p.q2) or \ math.isnan(p.q3) def get_t(q0, q1, q2, q3, x, y, z): aa = 1 - 2 * (q2 * q2 + q3 * q3) ab = 2 * (q1 * q2 - q0 * q3) ac = 2 * (q1 * q3 + q0 * q2) ba = 2 * (q1 * q2 + q0 * q3) bb = 1 - 2 * (q1 * q1 + q3 * q3) bc = 2 * (q2 * q3 - q0 * q1) ca = 2 * (q1 * q3 - q0 * q2) cb = 2 * (q2 * q3 + q0 * q1) cc = 1 - 2 * (q1 * q1 + q2 * q2) t = np.mat([[aa, ab, ac, x], [ba, bb, bc, y], [ca, cb, cc, z], [0, 0, 0, 1]]) return t def get_label(perception_typ, subtype): if perception_typ == 3: return 'Pedestrian' elif perception_typ == 4: return 'Bi_Tricycle' elif perception_typ == 5: if subtype == 5: return 'Truck' elif subtype == 6: return 'Bus' else: return 'Car' else: return 'unknow' def main(args): for file in os.listdir(args.input): if file.endswith('.bag'): bag_path = os.path.join(args.input, file) bag = rosbag.Bag(bag_path, "r") output_dir = os.getenv('output_dir') if not os.path.exists(os.path.join(output_dir, 'innoPtClound_A4')): os.makedirs(os.path.join(output_dir, 'innoPtClound_A4')) if not os.path.exists(os.path.join(output_dir, 'innoPtClound_B2')): os.makedirs(os.path.join(output_dir, 'innoPtClound_B2')) if not os.path.exists(os.path.join(output_dir, 'radar_track_array_0')): os.makedirs(os.path.join(output_dir, 'radar_track_array_0')) if not os.path.exists(os.path.join(output_dir, 'radar_track_array_3')): os.makedirs(os.path.join(output_dir, 'radar_track_array_3')) if not os.path.exists(os.path.join(output_dir, 'mdc_camera_instance_74')): os.makedirs(os.path.join(output_dir, 'mdc_camera_instance_74')) if not os.path.exists(os.path.join(output_dir, 'mdc_camera_instance_73')): os.makedirs(os.path.join(output_dir, 'mdc_camera_instance_73')) if not os.path.exists(os.path.join(output_dir, 'mdc_camera_instance_72')): os.makedirs(os.path.join(output_dir, 'mdc_camera_instance_72')) if not os.path.exists(os.path.join(output_dir, 'mdc_camera_instance_71')): os.makedirs(os.path.join(output_dir, 'mdc_camera_instance_71')) routes = [] controls = [] plans = [] preds = [] gnss = [] vehs = [] locs = [] objs = [] ego_pose = None has_camera_71 = False has_camera_72 = False has_camera_73 = False has_camera_74 = False has_lidar_A4 = False has_lidar_B2 = False has_radar_0 = False has_radar_3 = False lidar_num = 0 image_num = 0 radar_num = 0 for topic, msg, t in bag.read_messages(): time_stamp = int(t.to_sec() * 1000) # 以 rosbag 时间戳(t)为基准,转换为 13 位时间戳 if topic == '/innoPtClound_A4': ### 图达通 时间辍应该是13位数字,图达通雷达8位,华为96线6位 # file_path = os.path.join(output_dir, 'innoPtClound_A4', '{}.pcd'.format(int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000))) file_path = os.path.join(output_dir, 'innoPtClound_A4', '{}.pcd'.format(time_stamp)) # print(file_path) # 提取点云数据,包括 x, y, z points = list(point_cloud2.read_points(msg, field_names=["x", "y", "z", "intensity"], skip_nans=True)) if points: # 转换为 numpy 数组,添加 intensity, ring, timestamp 字段 np_points = np.array(points) # (N, 3), 包含 x, y, z # 转换为 Open3D 格式点云 pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(np_points[:, :3]) # x, y, z pcd.colors = o3d.utility.Vector3dVector(np.tile(np_points[:, 3:4] / np_points[:, 3:4].max(), (1, 3))) # 用 intensity 作为灰度颜色 o3d.io.write_point_cloud(file_path, pcd) lidar_num += 1 has_lidar_A4 = True elif topic == '/innoPtClound_B2': ### 图达通 # file_path = os.path.join(output_dir, 'innoPtClound_B2', '{}.pcd'.format(int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000))) file_path = os.path.join(output_dir, 'innoPtClound_B2', '{}.pcd'.format(time_stamp)) # print(file_path) # 提取点云数据,包括 x, y, z points = list(point_cloud2.read_points(msg, field_names=["x", "y", "z", "intensity"], skip_nans=True)) if points: # 转换为 numpy 数组,添加 intensity, ring, timestamp 字段 np_points = np.array(points) # (N, 3), 包含 x, y, z # 转换为 Open3D 格式点云 pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(np_points[:, :3]) # x, y, z pcd.colors = o3d.utility.Vector3dVector(np.tile(np_points[:, 3:4] / np_points[:, 3:4].max(), (1, 3))) # 用 intensity 作为灰度颜色 o3d.io.write_point_cloud(file_path, pcd) lidar_num += 1 has_lidar_B2 = True elif topic == 'mdc_camera_instance_74': ### 相机 时间辍应该是13位数字 # time_stamp = int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000) file_path = os.path.join(output_dir, 'mdc_camera_instance_74', '{}.jpg'.format(time_stamp)) packet = av.packet.Packet(msg.data) try: out = h265_code.decode(packet) for frame in out: if frame.format.name != 'rgb24': frame = frame.reformat(format='rgb24') img = frame.to_image() img.save(file_path) image_num += 1 has_camera_74 = True except Exception as e: print("{} frame can not trans to jpg".format(time_stamp), e) elif topic == 'mdc_camera_instance_73': ### 相机 时间辍应该是13位数字 # time_stamp = int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000) file_path = os.path.join(output_dir, 'mdc_camera_instance_73', '{}.jpg'.format(time_stamp)) packet = av.packet.Packet(msg.data) try: out = h265_code.decode(packet) for frame in out: if frame.format.name != 'rgb24': frame = frame.reformat(format='rgb24') img = frame.to_image() img.save(file_path) image_num += 1 has_camera_73 = True except Exception as e: print("{} frame can not trans to jpg".format(time_stamp), e) elif topic == 'mdc_camera_instance_72': ### 相机 # time_stamp = int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000) file_path = os.path.join(output_dir, 'mdc_camera_instance_72', '{}.jpg'.format(time_stamp)) packet = av.packet.Packet(msg.data) try: out = h265_code.decode(packet) for frame in out: if frame.format.name != 'rgb24': frame = frame.reformat(format='rgb24') img = frame.to_image() img.save(file_path) image_num += 1 has_camera_72 = True except Exception as e: print("{} frame can not trans to jpg".format(time_stamp), e) elif topic == 'mdc_camera_instance_71': ### 相机 # time_stamp = int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000) file_path = os.path.join(output_dir, 'mdc_camera_instance_71', '{}.jpg'.format(time_stamp)) packet = av.packet.Packet(msg.data) try: out = h265_code.decode(packet) for frame in out: if frame.format.name != 'rgb24': frame = frame.reformat(format='rgb24') img = frame.to_image() img.save(file_path) image_num += 1 has_camera_71 = True except Exception as e: print("{} frame can not trans to jpg".format(time_stamp), e) elif topic == '/radar_track_array_0': ### 大陆408 时间辍应该是13位数字 # file_path = os.path.join(output_dir, 'radar_track_array_0', '{}.pcd'.format(int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000))) file_path = os.path.join(output_dir, 'radar_track_array_0', '{}.pcd'.format(time_stamp)) points = [] for track in msg.trackList: x, y, z = track.x, track.y, track.z vx, vy, ax, ay = track.vx, track.vy, track.ax, track.ay rcs, snr, yawRate = track.rcs, track.snr, track.yawRate obj_id, trackType, lifetime = track.id, track.trackType, track.lifetime # 过滤无效点 if np.isnan(x) or np.isnan(y) or np.isnan(z): continue points.append([x, y, z, vx, vy, ax, ay, rcs, snr, yawRate, obj_id, trackType, lifetime]) if not points: print("没有有效点云数据") continue points = np.array(points) # **写入 PCD 文件** with open(file_path, 'w') as f: # **写入 PCD 头部** f.write("# .PCD v0.7 - Point Cloud Data file format\n") f.write("VERSION 0.7\n") f.write(f"FIELDS x y z vx vy ax ay rcs snr yawRate id trackType lifetime\n") f.write("SIZE 4 4 4 4 4 4 4 4 4 4 4 4 4\n") f.write("TYPE F F F F F F F F F F I I I\n") # F = float, I = int f.write("COUNT 1 1 1 1 1 1 1 1 1 1 1 1 1\n") f.write(f"WIDTH {points.shape[0]}\n") f.write("HEIGHT 1\n") f.write("VIEWPOINT 0 0 0 1 0 0 0\n") f.write(f"POINTS {points.shape[0]}\n") f.write("DATA ascii\n") # **写入点云数据** np.savetxt(f, points, fmt="%.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %d %d %d") radar_num += 1 has_radar_0 = True elif topic == '/radar_track_array_3': ### 大陆408 # file_path = os.path.join(output_dir, 'radar_track_array_3', '{}.pcd'.format(int(msg.header.stamp.secs * 1000 + # msg.header.stamp.nsecs / 1000000))) file_path = os.path.join(output_dir, 'radar_track_array_3', '{}.pcd'.format(time_stamp)) points = [] for track in msg.trackList: x, y, z = track.x, track.y, track.z vx, vy, ax, ay = track.vx, track.vy, track.ax, track.ay rcs, snr, yawRate = track.rcs, track.snr, track.yawRate obj_id, trackType, lifetime = track.id, track.trackType, track.lifetime # 过滤无效点 if np.isnan(x) or np.isnan(y) or np.isnan(z): continue points.append([x, y, z, vx, vy, ax, ay, rcs, snr, yawRate, obj_id, trackType, lifetime]) if not points: print("没有有效点云数据") continue points = np.array(points) # **写入 PCD 文件** with open(file_path, 'w') as f: # **写入 PCD 头部** f.write("# .PCD v0.7 - Point Cloud Data file format\n") f.write("VERSION 0.7\n") f.write(f"FIELDS x y z vx vy ax ay rcs snr yawRate id trackType lifetime\n") f.write("SIZE 4 4 4 4 4 4 4 4 4 4 4 4 4\n") f.write("TYPE F F F F F F F F F F I I I\n") # F = float, I = int f.write("COUNT 1 1 1 1 1 1 1 1 1 1 1 1 1\n") f.write(f"WIDTH {points.shape[0]}\n") f.write("HEIGHT 1\n") f.write("VIEWPOINT 0 0 0 1 0 0 0\n") f.write(f"POINTS {points.shape[0]}\n") f.write("DATA ascii\n") # **写入点云数据** np.savetxt(f, points, fmt="%.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %.6f %d %d %d") radar_num += 1 has_radar_3 = True elif topic == '/routing/routing_response_viz': rv = RoutingPath() rv.timestamp = int(t.secs * 1000 + t.nsecs / 1000000) rv.stamp_secs = t.secs rv.stamp_nsecs = t.nsecs mark_list = list() for mark in msg.markers: path_pb = Path() path_pb.id = mark.id point_list = [] for point in mark.points: point_pb = Point() point_pb.x = point.x point_pb.y = point.y point_pb.z = point.z point_list.append(point_pb) path_pb.path_point.extend(point_list) mark_list.append(path_pb) rv.routing_path_info.extend(mark_list) routes.append(rv) elif topic == '/holo/ControlCommand': cf = CommandFrame() cf.timestamp = int(t.secs * 1000 + t.nsecs / 1000000) cf.stamp_secs = t.secs cf.stamp_nsecs = t.nsecs cf.acceleration = msg.acceleration cf.front_wheel_angle = msg.front_wheel_angle cf.gear = msg.gear controls.append(cf) elif topic == '/planning/trajectory': tj = Trajectory() tj.timestamp = int(t.secs * 1000 + t.nsecs / 1000000) tj.stamp_secs = t.secs tj.stamp_nsecs = t.nsecs p_list = [] for point in msg.trajectory_points: p = TrajectoryPoint() p.x = point.path_point.point.x p.y = point.path_point.point.y p.z = point.path_point.point.z p.theta = point.path_point.theta p.kappa = point.path_point.kappa p.v = point.v p.a = point.a p.relative_time = point.relative_time p_list.append(p) tj.trajectory_points.extend(p_list) plans.append(tj) elif topic == '/prediction/prediction_obstacles': tr_pb = PerceptionObstacle() tr_pb.timestamp = int(msg.header.stamp.secs * 1000 + msg.header.stamp.nsecs / 1000000) tr_pb.stamp_secs = msg.header.stamp.secs tr_pb.stamp_nsecs = msg.header.stamp.nsecs obj_list = list() for obj in msg.prediction_obstacle: ob_pb = Obstacle() ob_pb.obstacle_timestamp = int(obj.timestamp * 1000) ob_pb.id = obj.perception_obstacle.id ob_pb.x = obj.perception_obstacle.position.x ob_pb.y = obj.perception_obstacle.position.y ob_pb.z = obj.perception_obstacle.position.z traj_pbs = [] for traj in obj.trajectory: traj_pb = PredictionTrajectory() points_pbs = [] for trajectory_point in traj.trajectory_points: point_pb = PathPoint() point_pb.x = trajectory_point.path_point.point.x point_pb.y = trajectory_point.path_point.point.y point_pb.z = trajectory_point.path_point.point.z point_pb.theta = trajectory_point.path_point.theta point_pb.kappa = trajectory_point.path_point.kappa point_pb.lane_id = trajectory_point.path_point.lane_id point_pb.v = trajectory_point.v point_pb.a = trajectory_point.a point_pb.relative_time = trajectory_point.relative_time points_pbs.append(point_pb) traj_pb.path_point.extend(points_pbs) traj_pbs.append(traj_pb) ob_pb.prediction_trajectory.extend(traj_pbs) obj_list.append(ob_pb) tr_pb.obstacle_info.extend(obj_list) preds.append(tr_pb) elif topic == '/inspvax': pb_loc_gnss = GnssPoint() pb_loc_gnss.stamp_secs = msg.header.stamp.secs # 1 pb_loc_gnss.stamp_nsecs = msg.header.stamp.nsecs # 2 pb_loc_gnss.timestamp = get_ts(msg.header.stamp.secs, msg.header.stamp.nsecs) pb_loc_gnss.latitude = msg.latitude # 3 pb_loc_gnss.longitude = msg.longitude # 4 pb_loc_gnss.elevation = msg.altitude gnss.append(pb_loc_gnss) elif topic == '/holo/VehicleInfoMagotan': veh_pb = VehicleFrame() veh_pb.stamp_secs = msg.timestamp.secs # 1 veh_pb.stamp_nsecs = msg.timestamp.nsecs # 2 veh_pb.timestamp = get_ts(veh_pb.stamp_secs, veh_pb.stamp_nsecs) veh_pb.gear_value = msg.gear # 4 veh_pb.vehicle_speed = msg.vehicle_speed * 3.6 # 5 veh_pb.steering_angle = msg.steering_angle # 6 veh_pb.longitude_acc = msg.longitude_acc veh_pb.lateral_acc = msg.lateral_acc veh_pb.turn_left_light = msg.turn_left_light veh_pb.turn_right_light = msg.turn_right_light veh_pb.brake = msg.brake_torque veh_pb.autonomy_status = 0 vehs.append(veh_pb) elif topic == '/localization/localization_info': lo_pb = LocalizationInfoFrame() lo_pb.timestamp = get_ts(msg.header.stamp.secs, msg.header.stamp.nsecs) lo_pb.stamp_secs = msg.header.stamp.secs lo_pb.stamp_nsecs = msg.header.stamp.nsecs lo_pb.pose_position_x = msg.pose.position.x lo_pb.pose_position_y = msg.pose.position.y lo_pb.pose_position_z = msg.pose.position.z lo_pb.pose_orientation_x = msg.pose.orientation.x lo_pb.pose_orientation_y = msg.pose.orientation.y lo_pb.pose_orientation_z = msg.pose.orientation.z lo_pb.pose_orientation_w = msg.pose.orientation.w lo_pb.pose_orientation_yaw = \ yaw_from_quaternions(msg.pose.orientation.w, msg.pose.orientation.x, msg.pose.orientation.y, msg.pose.orientation.z) lo_pb.velocity_linear = get_modulo(msg.pose.linear_velocity.x, msg.pose.linear_velocity.y, msg.pose.linear_velocity.z) lo_pb.velocity_angular = get_modulo(msg.pose.angular_velocity.x, msg.pose.angular_velocity.y, msg.pose.angular_velocity.z) lo_pb.acceleration_linear = get_modulo(msg.pose.linear_acceleration_vrf.x, msg.pose.linear_acceleration_vrf.y, msg.pose.linear_acceleration_vrf.z) lo_pb.acceleration_angular = get_modulo(msg.pose.angular_velocity_vrf.x, msg.pose.angular_velocity_vrf.y, msg.pose.angular_velocity_vrf.z) locs.append(lo_pb) ego_pose = Pose(msg.pose.orientation.w, msg.pose.orientation.x, msg.pose.orientation.y, msg.pose.orientation.z, msg.pose.position.x, msg.pose.position.y, msg.pose.position.z) elif topic == '/perception/perception_obstacles': if ego_pose is None or pose_has_nan(ego_pose): continue tr_pb = TrackedObjectFrame() tr_pb.timestamp = get_ts(msg.header.stamp.secs, msg.header.stamp.nsecs) tr_pb.stamp_secs = msg.header.stamp.secs tr_pb.stamp_nsecs = msg.header.stamp.nsecs obj_list = list() for object in msg.perception_obstacle: ob_pb = Object() ob_pb.id = object.id ob_pb.label = get_label(object.type, object.sub_type) ob_pb.pose_position_x = object.position.x ob_pb.pose_position_y = object.position.y ob_pb.pose_position_z = object.position.z ob_pb.pose_orientation_x = 0 ob_pb.pose_orientation_y = 0 ob_pb.pose_orientation_z = math.sin(object.theta / 2) ob_pb.pose_orientation_w = math.cos(object.theta / 2) ob_pb.pose_orientation_yaw = object.theta ob_pb.dimensions_x = object.length ob_pb.dimensions_y = object.width ob_pb.dimensions_z = object.height ob_pb.speed_vector_linear_x = object.velocity.x ob_pb.speed_vector_linear_y = object.velocity.y ob_pb.speed_vector_linear_z = object.velocity.z world_obj = np.transpose(np.array([[object.position.x, object.position.y, object.position.z, 1]])) world_ego_t = get_t(ego_pose.q0, ego_pose.q1, ego_pose.q2, ego_pose.q3, ego_pose.x, ego_pose.y, ego_pose.z) try: world_ego_invt = np.linalg.pinv(world_ego_t) except Exception as err: print('pinv failed:', world_ego_t) raise err vehicle_obj = world_ego_invt * world_obj ob_pb.relative_position_x = vehicle_obj[0] ob_pb.relative_position_y = vehicle_obj[1] ob_pb.relative_position_z = vehicle_obj[2] obj_list.append(ob_pb) tr_pb.objects.extend(obj_list) objs.append(tr_pb) print(f"lidar_num : {lidar_num}") print(f"image_num : {image_num}") print(f"radar_num : {radar_num}") folders = [] if len(routes) > 0: os.makedirs(os.path.join(output_dir, 'routing_routing_response_viz')) folders.append({'folder': 'routing_routing_response_viz', 'sensor_type': 'routing_path'}) route_out = RoutingFrames() route_out.routing_frame.extend(routes) with open(os.path.join(output_dir, 'routing_routing_response_viz', 'route.pb'), "wb") as c: c.write(route_out.SerializeToString()) if len(controls) > 0: os.makedirs(os.path.join(output_dir, 'holo_ControlCommand')) folders.append({'folder': 'holo_ControlCommand', 'sensor_type': 'control'}) ctl_cmd_pb_out = ControlCommand() ctl_cmd_pb_out.command_frame.extend(controls) with open(os.path.join(output_dir, 'holo_ControlCommand', 'control.pb'), "wb") as c: c.write(ctl_cmd_pb_out.SerializeToString()) if len(plans) > 0: os.makedirs(os.path.join(output_dir, 'planning_trajectory')) folders.append({'folder': 'planning_trajectory', 'sensor_type': 'planning_trajectory'}) plan_traj_pb_out = PlanTrajectory() plan_traj_pb_out.trajectory_info.extend(plans) with open(os.path.join(output_dir, 'planning_trajectory', 'planning.pb'), "wb") as p: p.write(plan_traj_pb_out.SerializeToString()) if len(preds) > 0: os.makedirs(os.path.join(output_dir, 'prediction_prediction_obstacles')) folders.append({'folder': 'prediction_prediction_obstacles', 'sensor_type': 'predicted_objects'}) pred_obstacles_pb_out = PredictionObstacles() pred_obstacles_pb_out.perception_obstacle.extend(preds) with open(os.path.join(output_dir, 'prediction_prediction_obstacles', 'predicted.pb'), "wb") as p: p.write(pred_obstacles_pb_out.SerializeToString()) if len(gnss) > 0: os.makedirs(os.path.join(output_dir, 'inspvax')) folders.append({'folder': 'inspvax', 'sensor_type': 'gnss'}) gn_pb_out = GnssPoints() gn_pb_out.gnss_points.extend(gnss) with open(os.path.join(output_dir, 'inspvax', 'gnss.pb'), "wb") as g: g.write(gn_pb_out.SerializeToString()) if len(vehs) > 0: os.makedirs(os.path.join(output_dir, 'holo_VehicleInfoMagotan')) folders.append({'folder': 'holo_VehicleInfoMagotan', 'sensor_type': 'vehicle'}) veh_pb_out = VehicleInfo() veh_pb_out.vehicle_info.extend(vehs) with open(os.path.join(output_dir, 'holo_VehicleInfoMagotan', 'vehicle.pb'), "wb") as v: v.write(veh_pb_out.SerializeToString()) if len(locs) > 0: os.makedirs(os.path.join(output_dir, 'localization_localization_info')) folders.append({'folder': 'localization_localization_info', 'sensor_type': 'ego_tf'}) lo_pb_out = LocalizationInfo() lo_pb_out.localization_info.extend(locs) with open(os.path.join(output_dir, 'localization_localization_info', 'ego_tf.pb'), "wb") as lo: lo.write(lo_pb_out.SerializeToString()) if len(objs) > 0: os.makedirs(os.path.join(output_dir, 'perception_perception_obstacles')) folders.append({'folder': 'perception_perception_obstacles', 'sensor_type': 'object_array_vision'}) tr_pb_out = TrackedObject() tr_pb_out.tracked_object.extend(objs) with open(os.path.join(output_dir, 'perception_perception_obstacles', 'object_array_vision.pb'), "wb") as tr: tr.write(tr_pb_out.SerializeToString()) if has_camera_74: folders.append({'folder': 'mdc_camera_instance_74', 'sensor_type': 'camera'}) if has_camera_73: folders.append({'folder': 'mdc_camera_instance_73', 'sensor_type': 'camera'}) if has_camera_72: folders.append({'folder': 'mdc_camera_instance_72', 'sensor_type': 'camera'}) if has_camera_71: folders.append({'folder': 'mdc_camera_instance_71', 'sensor_type': 'camera'}) if has_lidar_A4: if args.calibration_id: folders.append({'folder': 'innoPtClound_A4', 'sensor_type': 'lidar', 'calibration_item_id': args.calibration_id}) else: folders.append({'folder': 'innoPtClound_A4', 'sensor_type': 'lidar'}) if has_lidar_B2: if args.calibration_id: folders.append({'folder': 'innoPtClound_B2', 'sensor_type': 'lidar', 'calibration_item_id': args.calibration_id}) else: folders.append({'folder': 'innoPtClound_B2', 'sensor_type': 'lidar'}) if has_radar_0: folders.append({'folder': 'radar_track_array_0', 'sensor_type': 'radar'}) if has_radar_3: folders.append({'folder': 'radar_track_array_3', 'sensor_type': 'radar'}) collect_yaml = {'folders': folders} with open(os.path.join(output_dir, "opendata_to_platform.yaml"), 'w', encoding='utf-8') as collect_file: yaml.safe_dump(collect_yaml, collect_file) with open(os.path.join(os.getenv('output_dir'), '_SUCCESS'), 'w') as f: f.write("") os.system('chmod -R a+r ${output_dir}/*') if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('-i', '--input', help="input bag path", default=os.getenv('rosbag_path')) parser.add_argument('-o', '--output', default=os.getenv('output_dir'), help="result output directory, default to ./bags/") parser.add_argument('-ci', '--calibration_id', type=int) params = parser.parse_args() main(params)
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值