g2opy


Nameg2opy JSON
Version 0.0.9 PyPI version JSON
download
home_pagehttps://github.com/ubicoders/g2opy
Summaryg2o for python
upload_time2024-05-12 04:40:03
maintainerNone
docs_urlNone
authorubicoders
requires_python==3.9
licenseNone
keywords g2o slam ba icp optimization python binding
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ![Static Badge](https://img.shields.io/badge/python-3.9-blue)


# g2o Graph Optimization Lib for Ubuntu Linux

If you are on Ubuntu and/or its docker contianer,


```
pip install g2opy
```

is all you need to get started.


This g2opy module is built based on
1. https://github.com/uoip/g2opy
2. https://github.com/RainerKuemmerle/g2o

## Intellisense Enhanced


![sample1](https://raw.githubusercontent.com/ubicoders/g2opy/main/pics/intel0.png)


![sample2](https://raw.githubusercontent.com/ubicoders/g2opy/main/pics/intel1.png)


## License
MIT license. Currently, additional work is the build configuration on top of uoip/g2opy.

## Examples

To run the examples, install numpy if applicable.

```
pip install numpy
```

Bundle Adjustmend Example

```python
# https://github.com/RainerKuemmerle/g2o/blob/master/g2o/examples/sba/sba_demo.cpp

import numpy as np
import g2opy as g2o
np.set_printoptions(precision=3)
np.set_printoptions(suppress=True)
from collections import defaultdict

def showCameraPoses(optim, idx, N):
    for i in range(idx, idx + N):
        T = optim.vertex(i).estimate().matrix()
        trans = T[:3, 3]
        print('camera pose at ', i, ': ', trans)

def calcSSE(optim, idx, wPts):
    sse = 0
    N = wPts.shape[0]
    for i in range(idx, idx + N):
        guessedWpt = optim.vertex(i)
        error = guessedWpt.estimate() - wPts[i-idx]
        sse += np.sum(error ** 2)
    return sse

def showWpts(optim, idx, N):
    for i in range(idx, idx + N):
        T = optim.vertex(i).estimate()
        #T = np.round(T, 0)
        print('guessed wPt at ', i-idx, ': ', T)

def main():
    optimizer = g2o.SparseOptimizer()
    solver = g2o.BlockSolverSE3(g2o.LinearSolverCSparseSE3())
    solver = g2o.OptimizationAlgorithmLevenberg(solver)
    optimizer.set_algorithm(solver)

    f, p = 200, 256
    baseline = 0.3
    K = np.array([[f, 0, p],
                  [0, f, p],
                  [0, 0, 1]])

    wPts = np.array([
        [0, 0, 10],
        [-1, 3, 30],
        [2, 2, 37.2],
    ])

    num_pose = 10
    for i in range(0, int(num_pose/2)):
        pose = g2o.Isometry3d(np.identity(3), [(i-2)*10, 0, 0])
        v_se3 = g2o.VertexSCam()
        v_se3.set_cam(f, f, p, p, baseline)
        v_se3.set_id(i)
        v_se3.set_estimate(pose)
        if i < 2:
            v_se3.set_fixed(True)
        v_se3.set_all() # sets transfrom, projection, dR (angle)
        optimizer.add_vertex(v_se3)

    for i in range(int(num_pose/2), num_pose):
        pose = g2o.Isometry3d(np.identity(3), [0, (i - int(num_pose/2)- 2) * 10, 0])
        v_se3 = g2o.VertexSCam()
        v_se3.set_cam(f, f, p, p, baseline)
        v_se3.set_id(i)
        v_se3.set_estimate(pose)
        v_se3.set_fixed(False)
        v_se3.set_all() # sets transfrom, projection, dR (angle)
        optimizer.add_vertex(v_se3)

    point_id = 0

    for i, wpt in enumerate(wPts):
        guessed_wPt = g2o.VertexSBAPointXYZ()
        guessed_wPt.set_id(num_pose + point_id)
        guessed_wPt.set_marginalized(True)
        guessed_wPt.set_estimate(wpt + np.random.randn(3) )
        optimizer.add_vertex(guessed_wPt)

        for j in range(num_pose):
            z = optimizer.vertex(j).map_point(wpt)
            if i > 1:
                z +=  np.random.randn(3)
            edge = g2o.Edge_XYZ_VSC()
            edge.set_vertex(0, guessed_wPt)
            edge.set_vertex(1, optimizer.vertex(j))
            edge.set_measurement(z)
            edge.set_information(np.identity(3))
            optimizer.add_edge(edge)

        point_id += 1

    sse0 = calcSSE(optimizer, num_pose, wPts)
    print('\nRMSE (inliers only):')
    print('before optimization:', np.sqrt(sse0 / wPts.shape[0]))
    showCameraPoses(optimizer, 0, num_pose)
    showWpts(optimizer, num_pose, 3)


    print('Performing full BA:')
    optimizer.initialize_optimization()
    optimizer.set_verbose(False)
    optimizer.optimize(100)

    sse1 = calcSSE(optimizer, num_pose, wPts)

    print('\nRMSE (inliers only):')
    print('after  optimization:', np.sqrt(sse1 / wPts.shape[0]))
    showCameraPoses(optimizer, 0, num_pose)
    showWpts(optimizer, num_pose, 3)


if __name__ == '__main__':
    main()
```






            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ubicoders/g2opy",
    "name": "g2opy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "==3.9",
    "maintainer_email": null,
    "keywords": "g2o, SLAM, BA, ICP, optimization, python, binding",
    "author": "ubicoders",
    "author_email": "info@ubicoders.com",
    "download_url": "https://files.pythonhosted.org/packages/c6/f2/078fdb5558944402181e683750fddb4d0c4e7b23cb4f1286131944ffb964/g2opy-0.0.9.tar.gz",
    "platform": null,
    "description": "![Static Badge](https://img.shields.io/badge/python-3.9-blue)\n\n\n# g2o Graph Optimization Lib for Ubuntu Linux\n\nIf you are on Ubuntu and/or its docker contianer,\n\n\n```\npip install g2opy\n```\n\nis all you need to get started.\n\n\nThis g2opy module is built based on\n1. https://github.com/uoip/g2opy\n2. https://github.com/RainerKuemmerle/g2o\n\n## Intellisense Enhanced\n\n\n![sample1](https://raw.githubusercontent.com/ubicoders/g2opy/main/pics/intel0.png)\n\n\n![sample2](https://raw.githubusercontent.com/ubicoders/g2opy/main/pics/intel1.png)\n\n\n## License\nMIT license. Currently, additional work is the build configuration on top of uoip/g2opy.\n\n## Examples\n\nTo run the examples, install numpy if applicable.\n\n```\npip install numpy\n```\n\nBundle Adjustmend Example\n\n```python\n# https://github.com/RainerKuemmerle/g2o/blob/master/g2o/examples/sba/sba_demo.cpp\n\nimport numpy as np\nimport g2opy as g2o\nnp.set_printoptions(precision=3)\nnp.set_printoptions(suppress=True)\nfrom collections import defaultdict\n\ndef showCameraPoses(optim, idx, N):\n    for i in range(idx, idx + N):\n        T = optim.vertex(i).estimate().matrix()\n        trans = T[:3, 3]\n        print('camera pose at ', i, ': ', trans)\n\ndef calcSSE(optim, idx, wPts):\n    sse = 0\n    N = wPts.shape[0]\n    for i in range(idx, idx + N):\n        guessedWpt = optim.vertex(i)\n        error = guessedWpt.estimate() - wPts[i-idx]\n        sse += np.sum(error ** 2)\n    return sse\n\ndef showWpts(optim, idx, N):\n    for i in range(idx, idx + N):\n        T = optim.vertex(i).estimate()\n        #T = np.round(T, 0)\n        print('guessed wPt at ', i-idx, ': ', T)\n\ndef main():\n    optimizer = g2o.SparseOptimizer()\n    solver = g2o.BlockSolverSE3(g2o.LinearSolverCSparseSE3())\n    solver = g2o.OptimizationAlgorithmLevenberg(solver)\n    optimizer.set_algorithm(solver)\n\n    f, p = 200, 256\n    baseline = 0.3\n    K = np.array([[f, 0, p],\n                  [0, f, p],\n                  [0, 0, 1]])\n\n    wPts = np.array([\n        [0, 0, 10],\n        [-1, 3, 30],\n        [2, 2, 37.2],\n    ])\n\n    num_pose = 10\n    for i in range(0, int(num_pose/2)):\n        pose = g2o.Isometry3d(np.identity(3), [(i-2)*10, 0, 0])\n        v_se3 = g2o.VertexSCam()\n        v_se3.set_cam(f, f, p, p, baseline)\n        v_se3.set_id(i)\n        v_se3.set_estimate(pose)\n        if i < 2:\n            v_se3.set_fixed(True)\n        v_se3.set_all() # sets transfrom, projection, dR (angle)\n        optimizer.add_vertex(v_se3)\n\n    for i in range(int(num_pose/2), num_pose):\n        pose = g2o.Isometry3d(np.identity(3), [0, (i - int(num_pose/2)- 2) * 10, 0])\n        v_se3 = g2o.VertexSCam()\n        v_se3.set_cam(f, f, p, p, baseline)\n        v_se3.set_id(i)\n        v_se3.set_estimate(pose)\n        v_se3.set_fixed(False)\n        v_se3.set_all() # sets transfrom, projection, dR (angle)\n        optimizer.add_vertex(v_se3)\n\n    point_id = 0\n\n    for i, wpt in enumerate(wPts):\n        guessed_wPt = g2o.VertexSBAPointXYZ()\n        guessed_wPt.set_id(num_pose + point_id)\n        guessed_wPt.set_marginalized(True)\n        guessed_wPt.set_estimate(wpt + np.random.randn(3) )\n        optimizer.add_vertex(guessed_wPt)\n\n        for j in range(num_pose):\n            z = optimizer.vertex(j).map_point(wpt)\n            if i > 1:\n                z +=  np.random.randn(3)\n            edge = g2o.Edge_XYZ_VSC()\n            edge.set_vertex(0, guessed_wPt)\n            edge.set_vertex(1, optimizer.vertex(j))\n            edge.set_measurement(z)\n            edge.set_information(np.identity(3))\n            optimizer.add_edge(edge)\n\n        point_id += 1\n\n    sse0 = calcSSE(optimizer, num_pose, wPts)\n    print('\\nRMSE (inliers only):')\n    print('before optimization:', np.sqrt(sse0 / wPts.shape[0]))\n    showCameraPoses(optimizer, 0, num_pose)\n    showWpts(optimizer, num_pose, 3)\n\n\n    print('Performing full BA:')\n    optimizer.initialize_optimization()\n    optimizer.set_verbose(False)\n    optimizer.optimize(100)\n\n    sse1 = calcSSE(optimizer, num_pose, wPts)\n\n    print('\\nRMSE (inliers only):')\n    print('after  optimization:', np.sqrt(sse1 / wPts.shape[0]))\n    showCameraPoses(optimizer, 0, num_pose)\n    showWpts(optimizer, num_pose, 3)\n\n\nif __name__ == '__main__':\n    main()\n```\n\n\n\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "g2o for python",
    "version": "0.0.9",
    "project_urls": {
        "Homepage": "https://github.com/ubicoders/g2opy"
    },
    "split_keywords": [
        "g2o",
        " slam",
        " ba",
        " icp",
        " optimization",
        " python",
        " binding"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c6f2078fdb5558944402181e683750fddb4d0c4e7b23cb4f1286131944ffb964",
                "md5": "3cdd9122fb5b62f58dcf334145d75ff0",
                "sha256": "4e705bb12570e569f05c6a5dc1d1df08917bb2e074e2bbf888615ec8813456cd"
            },
            "downloads": -1,
            "filename": "g2opy-0.0.9.tar.gz",
            "has_sig": false,
            "md5_digest": "3cdd9122fb5b62f58dcf334145d75ff0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "==3.9",
            "size": 1417828,
            "upload_time": "2024-05-12T04:40:03",
            "upload_time_iso_8601": "2024-05-12T04:40:03.443464Z",
            "url": "https://files.pythonhosted.org/packages/c6/f2/078fdb5558944402181e683750fddb4d0c4e7b23cb4f1286131944ffb964/g2opy-0.0.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-12 04:40:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ubicoders",
    "github_project": "g2opy",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "appveyor": true,
    "lcname": "g2opy"
}
        
Elapsed time: 0.22309s