I am creating an AR-app, where you can virtually try on shoes. For now I have the location of the foot calculated using SolvePnp. Now I want to render the 3D object (.obj) file on top of the image, using the OpenCv Viz module. (Manually compiling worked)
I want to load the object as an cv::viz::Mesh object using the cv::viz::Mesh::load function, but I don't find the corresponding Python function call.
I tried
mesh = cv2.viz_Mesh()
mesh.load('shoe.obj', cv2.viz_Mesh_LOAD_OBJ)
and also
mesh = cv2.viz_Mesh_load('shoe.obj', cv2.viz_Mesh_LOAD_OBJ)
I always get the error:
AttributeError: 'cv2.viz_Mesh' object has no attribute 'load'
Using the dir() function from python I found all other methods except the load method, which is a static method (that's probably why it isn't here).
>>> dir(cv2) #contains the Viz Classes
['', ... , 'viz',... ,'viz_Mesh]
>>> dir(cv2.viz) #Stuff like Enums , Colors etc
['',...'Color_amethyst', ..., 'MOUSE_EVENT_MOUSE_DBL_CLICK'...]
>>> dir(cv2.viz_Mesh) #pyhton functions and other non static methods
['__class__', ... , 'colors','polygons']
Does someone know what the correct python call is?
Appendix: the cv2.viz_Mesh_LOAD_OBJ and cv2.viz_MESH_LOAD_OBJ represent only integers 2 ( = enums), thus
>>>print(cv2.viz_MESH_LOAD_OBJ)
>>>print(cv2.viz_Mesh_LOAD_OBJ)
2
2
I found the solution. I overlooked some methods in the dir(cv2.viz).
The correct python call would be
mesh = cv2.viz.readMesh('./shoe.ply')
It only accepts the path to .ply file, but not to .obj files (Version 4.5.5). Maybe other formats will be added in newer versions
Related
I'm trying to load a mesh from an .obj file (from ShapeNet dataset) using Trimesh, and then use the repair.fix_winding(mesh) function.
But when I load the mesh, via
trimesh.load('/path/to/file.obj') or trimesh.load_mesh('/path/to/file.obj'),
the object class returned is Scene, which is incompatible with repair.fix_winding(mesh), only Trimesh object are accepted.
How can I force it to load and return a Trimesh object or parse the Scene object to Trimesh object? Or any other way to fix winding of the triangles?
Using:
Python 3.6.5
Trimesh 3.0.14
MacOS 10.14.5
With the current trimesh version (3.9.42), you need to use trimesh.load with force='mesh':
trimesh.load('/path/to/file.obj', force='mesh')
https://trimsh.org/trimesh.html?highlight=load#trimesh.load
I have the same problem. You can access the "geometry" member of the scene, but I have no idea how the original mesh is split into multiple ones. Following.
The load_mesh has an option to be passed as aa Trimesh constructor
as explained in the documentation.
https://trimsh.org/trimesh.html?highlight=load_mesh#trimesh.load_mesh
here is an example code
mesh= t.load_mesh('./test.stl',process=False)
mesh.is_watertight
the second line ".is_watertight" is an attribute of Trimesh so you will get True if it is successfully imported as Trimesh.
How do I access the constants available as file formats when saving a Powerpoint presentation through comtypes?
In the following example 32 is used as the format but I would like to use the constants listed here) or at least find a documented list with value for each constant.
For Word there is this list that also contains the value for each constant.
import comtypes.client
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, 32)
You can access all enum names associated with the COM object you loaded via the comtypes.client.Constants() class; pass it the PowerPoint.Application COM object you created:
from comtypes.client import Constants, CreateObject
powerpoint = CreateObject("Powerpoint.Application")
pp_constants = Constants(powerpoint)
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, pp_constants.ppSaveAsPDF)
The Constants instance loads the underlying Type Library and dynamically translates attribute lookups to typelib access. It is not included in the comtypes documentation for some obscure reason, even though it was added nearly 10 years ago now.
Another option is to access the attributes on a generated module in the generated type library, as shown in the Properties with arguments (named properties) section. This would give you access to any of the constants associated with the Powerpoint IDL, including auto completion support IDEs (once generated by accessing the PowerPoint.Application object the first time around).
The module is generated automatically when you use CreateObject() if type information is exposed on the object that's being created; this is definitely the case for 'Powerpoint.Application' as you don't set an interface explicitly. Automatic interface selection only works if there is type information available.
Enumeration names are added to the generated module at the top level, so directly use those:
import comtypes.client
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
# only import the generated namespace after the com object has been created
# at least once. The generated module is cached for future runs.
from comtypes.gen import PowerPoint
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, PowerPoint.ppSaveAsPDF)
The short name of the type library can be found in a VBA Object Browser; the screenshot in Steve Rindsberg's answer shows that for the PpSaveAsFileType enum that's PowerPoint. I believe the same name is also used in the documentation for the ppSaveAsFileType enum; note the (PowerPoint) addition to the documentation title.
You can also use the GUID of the type library, plus version number, but that doesn't quite roll of the keyboard if you have to type that by hand.
You can use from comtypes.gen import PowerPoint; help(PowerPoint) to see what names have been defined if you need a reminder, or just reference the Microsoft documentation.
Either method avoids having to use magic numbers; the type library definition itself gives you the symbolic names.
If you find any code examples using win32com instead, then any use of win32com.client.constants attributes translates directly to comtypes.client.Constant(...) or comtypes.gen.<module> attributes.
I don't have access to a Windows setup to actually test any of this, I'm deducing information from reading documentation and the source code of comtypes.
Here is the list from Microsoft which contains the values of each constant:
https://learn.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype
Assuming you have a copy of PowerPoint, start it, press ALT+F11 to open the VBA editor, press F2 to open the Object Browser then search on SaveAs to get this list. Click on any constant name to see the value of the constant at the bottom of the dialog.
I'm trying to test for and fix a bug in pprint++ (edit: the correct link; original link left for posterity) which is coming up because the instancemethod type is not hashable:
In [16]: import pandas as pd
In [17]: type(pd.tslib.NaT).__repr__
Out[17]: <instancemethod __repr__ at 0x1058d2be8>
In [18]: hash(type(pd.tslib.NaT).__repr__)
...
TypeError: unhashable type: 'instancemethod'
But I'm having trouble testing for this issue because I don't know where else I can find an instancemethod in the Python 3 standard library, and I don't want my tests to depend on Pandas.
Specifically, it seems like the "normal" builtin types have "instance methods" that are implemented slightly differently:
In [19]: type(None).__repr__
Out[19]: <slot wrapper '__repr__' of 'NoneType' objects>
In [20]: hash(type(None).__repr__)
Out[20]: -9223372036583849574
So: where can I find an instancemethod in the Python 3 standard library so I can write tests against it? Or is it a special type that doesn't appear there?
(note: this only appears to affect Python 3, as the same method in Python 2 is an unbound method, which is hashable)
This type isn't used in anything that comes with Python, and there's no Python-level API to create objects of this type. However, you can do it with a direct C API call:
import ctypes
PyInstanceMethod_New = ctypes.pythonapi.PyInstanceMethod_New
PyInstanceMethod_New.argtypes = (ctypes.py_object,)
PyInstanceMethod_New.restype = ctypes.py_object
arbitrary_callable = sum
instance_method = PyInstanceMethod_New(arbitrary_callable)
The name instancemethod looks a lot like a bound method object, but it turns out it's something else entirely. It's a weird internal thing that, according to its documentation, is supposed to be the new way for C types to represent their methods, except that the standard C-level API for creating a type doesn't actually use it.
According to conversations on the Python issue tracker, this feature was requested by the developers of Cython and Pyrex. It looks like pandas.tslib.NaT is implemented in Cython, and the Cython implementation actually uses this type, where the standard C API for creating types doesn't.
Note that the situation is completely different on Python 2. On Python 2, this new type didn't exist, and instancemethod was the name of the type of method objects representing ordinary methods written in Python. In Python 3, the new type took that name, and the type of method objects for methods written in Python is now named method.
Python provides it, but basically only as part of their test suite AFAICT (no included batteries use it otherwise). You can make one for testing using the _testcapi module:
>>> import _testcapi
>>> testinstancemethod = _testcapi.instancemethod(str.__repr__)
>>> hash(testinstancemethod)
...
TypeError: unhashable type: 'instancemethod'
I'm currently looking into myHdl to see if it's worth using or not. However, I've come across a hiccup regarding the instantiation of modules. I've got two files, one that's a module and one that's the testbench. Inside the testbench, I've instantiated the module following the example they have on the website:
http://www.myhdl.org/examples/flipflops.html
The instantiation specifically is this line: dff_inst = dff(q, d, clk)
However, I get an error when I try to run the testbench:
Exception TypeError: 'isinstance() arg 2 must be a class, type, or tuple of classes and types' in <generator object _LabelGenerator at 0x7f6070b2ea50> ignored
I assume this has something to do with the fact that I have two separate files, so my guess is that python isn't finding the dff module(since it's in a separate file). I tried adding in an import dff line, but that simply gave me a 'module' object is not callable type error, which makes sense.
Looking in the documentation, they don't have a full .py file, so I'm not sure how they're linking these testbenches with the module. They specifically mention a hierarchy system and being able to instantiate other modules, but I can't seem to get it to work.
From what I understand from documentation, it looks like they're just writing the testbench and the module in the same file. However, to my understanding, it looks like they imply you can import modules, but I can't figure out how that's done. Is there just some simple thing I'm overlooking?
After experimenting a bit, it seems like I just need to use the following command: from dff import dff,
which makes a lot of sense.
I am trying to create a Mesh object in Python. I am using the python bindings which are being installed from the following web page. As far as the c++ code is concerned we can do it as follows
MeshType::Pointer mesh = MeshType::New();
I am very new to use even ITK. Have no idea how to create it. In the constructor of Mesh in the c++ documentation, it says one necessary argument which is the TPixelType. Unable to locate that as well.
Could anybody help me please with this.
Thanks
If I were you I would take a look at the Python bindings that come with ITK 4.0. You can get access to them by turning on the option WRAP_ITK_PYTHON in cmake.
Once, you compile ITK with the python bindings turned on you can create 2 mesh types out of the box:
import itk
meshType2D = itk.Mesh.D2Q.New()
meshType3D = itk.Mesh.D3Q.New()
alternatively you can explicitly instantiate your classes as follows:
import itk
meshType2D = itk.Mesh[itk.D, 2, itk.QuadEdgeMeshTraits.D2BBFF]
meshType3D = itk.Mesh[itk.D, 3, itk.QuadEdgeMeshTraits.D3BBFF]
This will give you 2 and 3 dimensional meshes of double type pixel values with default mesh traits. As far as pixel types in ITK go, these amount to the basic C++ variables types: double, float, unsigned int, etc. These basic types are wrapped in python and can be found in the ITK namespace: itk.D, itk.F, itk.UI, etc.