├── .gitignore ├── README.md ├── README └── Logo.png ├── license ├── CHOSUNTRUCK_LICENSE ├── PYUSERINPUT_LICENSE.txt ├── TENSORBOX_LICENSE.txt └── TENSORFLOW_LICENSE.txt ├── linux ├── Makefile ├── src │ ├── IPM.cc │ ├── IPM.h │ ├── ets2_self_driving.h │ ├── getScreen_linux.cc │ ├── getScreen_linux.h │ ├── linefinder.cc │ ├── main2.cc │ ├── uinput.c │ └── uinput.h └── tensorbox │ ├── Car_detection.py │ ├── data │ └── inception_v1.ckpt │ ├── license │ ├── TENSORBOX_LICENSE.txt │ └── TENSORFLOW_LICENSE.txt │ ├── output │ └── overfeat_rezoom_2017_02_09_13.28 │ │ ├── checkpoint │ │ ├── hypes.json │ │ ├── rpc-save.ckpt-100000.val_overlap0.5.txt │ │ ├── save.ckpt-100000.data-00000-of-00001 │ │ ├── save.ckpt-100000.gt_val.json │ │ ├── save.ckpt-100000.index │ │ └── save.ckpt-100000.val.json │ ├── pymouse │ ├── __init__.py │ ├── base.py │ ├── java_.py │ ├── mac.py │ ├── mir.py │ ├── wayland.py │ ├── windows.py │ └── x11.py │ ├── train.py │ └── utils │ ├── Makefile │ ├── __init__.py │ ├── annolist │ ├── AnnoList_pb2.py │ ├── AnnotationLib.py │ ├── LICENSE_FOR_THIS_FOLDER │ ├── MatPlotter.py │ ├── PalLib.py │ ├── __init__.py │ ├── doRPC.py │ ├── ma_utils.py │ └── plotSimple.py │ ├── data_utils.py │ ├── googlenet_load.py │ ├── hungarian │ ├── hungarian.cc │ ├── hungarian.cpp │ └── hungarian.hpp │ ├── rect.py │ ├── slim_nets │ ├── __init__.py │ ├── inception_v1.py │ ├── resnet_utils.py │ └── resnet_v1.py │ ├── stitch_rects.cpp │ ├── stitch_rects.hpp │ ├── stitch_wrapper.py │ ├── stitch_wrapper.pyx │ └── train_utils.py └── windows └── src ├── IPM.cpp ├── IPM.h ├── ets2_self_driving.h ├── guassian_filter.cpp ├── hwnd2mat.cpp ├── linefinder.cpp └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/c,c++,cuda,opencv,visualstudio,windows 2 | 3 | ### C ### 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Libraries 15 | *.lib 16 | *.a 17 | *.la 18 | *.lo 19 | 20 | # Shared objects (inc. Windows DLLs) 21 | *.dll 22 | *.so 23 | *.so.* 24 | *.dylib 25 | 26 | # Executables 27 | *.exe 28 | *.out 29 | *.app 30 | *.i*86 31 | *.x86_64 32 | *.hex 33 | 34 | # Debug files 35 | *.dSYM/ 36 | *.su 37 | 38 | 39 | ### C++ ### 40 | # Compiled Object files 41 | *.slo 42 | *.lo 43 | *.o 44 | *.obj 45 | 46 | # Precompiled Headers 47 | *.gch 48 | *.pch 49 | 50 | # Compiled Dynamic libraries 51 | *.so 52 | *.dylib 53 | *.dll 54 | 55 | # Fortran module files 56 | *.mod 57 | *.smod 58 | 59 | # Compiled Static libraries 60 | *.lai 61 | *.la 62 | *.a 63 | *.lib 64 | 65 | # Executables 66 | *.exe 67 | *.out 68 | *.app 69 | 70 | 71 | ### CUDA ### 72 | *.i 73 | *.ii 74 | *.gpu 75 | *.ptx 76 | *.cubin 77 | *.fatbin 78 | 79 | 80 | ### OpenCV ### 81 | #OpenCV for Mac and Linux 82 | #build and release folders 83 | */CMakeFiles 84 | */CMakeCache.txt 85 | */cmake_install.cmake 86 | .DS_Store 87 | 88 | 89 | ### VisualStudio ### 90 | ## Ignore Visual Studio temporary files, build results, and 91 | ## files generated by popular Visual Studio add-ons. 92 | 93 | # User-specific files 94 | *.suo 95 | *.user 96 | *.userosscache 97 | *.sln.docstates 98 | 99 | # User-specific files (MonoDevelop/Xamarin Studio) 100 | *.userprefs 101 | 102 | # Build results 103 | [Dd]ebug/ 104 | [Dd]ebugPublic/ 105 | [Rr]elease/ 106 | [Rr]eleases/ 107 | x64/ 108 | x86/ 109 | bld/ 110 | [Bb]in/ 111 | [Oo]bj/ 112 | [Ll]og/ 113 | 114 | # Visual Studio 2015 cache/options directory 115 | .vs/ 116 | # Uncomment if you have tasks that create the project's static files in wwwroot 117 | #wwwroot/ 118 | 119 | # MSTest test Results 120 | [Tt]est[Rr]esult*/ 121 | [Bb]uild[Ll]og.* 122 | 123 | # NUNIT 124 | *.VisualState.xml 125 | TestResult.xml 126 | 127 | # Build Results of an ATL Project 128 | [Dd]ebugPS/ 129 | [Rr]eleasePS/ 130 | dlldata.c 131 | 132 | # DNX 133 | project.lock.json 134 | project.fragment.lock.json 135 | artifacts/ 136 | 137 | *_i.c 138 | *_p.c 139 | *_i.h 140 | *.ilk 141 | *.meta 142 | *.obj 143 | *.pch 144 | *.pdb 145 | *.pgc 146 | *.pgd 147 | *.rsp 148 | *.sbr 149 | *.tlb 150 | *.tli 151 | *.tlh 152 | *.tmp 153 | *.tmp_proj 154 | *.log 155 | *.vspscc 156 | *.vssscc 157 | .builds 158 | *.pidb 159 | *.svclog 160 | *.scc 161 | 162 | # Chutzpah Test files 163 | _Chutzpah* 164 | 165 | # Visual C++ cache files 166 | ipch/ 167 | *.aps 168 | *.ncb 169 | *.opendb 170 | *.opensdf 171 | *.sdf 172 | *.cachefile 173 | *.VC.db 174 | *.VC.VC.opendb 175 | 176 | # Visual Studio profiler 177 | *.psess 178 | *.vsp 179 | *.vspx 180 | *.sap 181 | 182 | # TFS 2012 Local Workspace 183 | $tf/ 184 | 185 | # Guidance Automation Toolkit 186 | *.gpState 187 | 188 | # ReSharper is a .NET coding add-in 189 | _ReSharper*/ 190 | *.[Rr]e[Ss]harper 191 | *.DotSettings.user 192 | 193 | # JustCode is a .NET coding add-in 194 | .JustCode 195 | 196 | # TeamCity is a build add-in 197 | _TeamCity* 198 | 199 | # DotCover is a Code Coverage Tool 200 | *.dotCover 201 | 202 | # NCrunch 203 | _NCrunch_* 204 | .*crunch*.local.xml 205 | nCrunchTemp_* 206 | 207 | # MightyMoose 208 | *.mm.* 209 | AutoTest.Net/ 210 | 211 | # Web workbench (sass) 212 | .sass-cache/ 213 | 214 | # Installshield output folder 215 | [Ee]xpress/ 216 | 217 | # DocProject is a documentation generator add-in 218 | DocProject/buildhelp/ 219 | DocProject/Help/*.HxT 220 | DocProject/Help/*.HxC 221 | DocProject/Help/*.hhc 222 | DocProject/Help/*.hhk 223 | DocProject/Help/*.hhp 224 | DocProject/Help/Html2 225 | DocProject/Help/html 226 | 227 | # Click-Once directory 228 | publish/ 229 | 230 | # Publish Web Output 231 | *.[Pp]ublish.xml 232 | *.azurePubxml 233 | # TODO: Comment the next line if you want to checkin your web deploy settings 234 | # but database connection strings (with potential passwords) will be unencrypted 235 | *.pubxml 236 | *.publishproj 237 | 238 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 239 | # checkin your Azure Web App publish settings, but sensitive information contained 240 | # in these scripts will be unencrypted 241 | PublishScripts/ 242 | 243 | # NuGet Packages 244 | *.nupkg 245 | # The packages folder can be ignored because of Package Restore 246 | **/packages/* 247 | # except build/, which is used as an MSBuild target. 248 | !**/packages/build/ 249 | # Uncomment if necessary however generally it will be regenerated when needed 250 | #!**/packages/repositories.config 251 | # NuGet v3's project.json files produces more ignoreable files 252 | *.nuget.props 253 | *.nuget.targets 254 | 255 | # Microsoft Azure Build Output 256 | csx/ 257 | *.build.csdef 258 | 259 | # Microsoft Azure Emulator 260 | ecf/ 261 | rcf/ 262 | 263 | # Windows Store app package directories and files 264 | AppPackages/ 265 | BundleArtifacts/ 266 | Package.StoreAssociation.xml 267 | _pkginfo.txt 268 | 269 | # Visual Studio cache files 270 | # files ending in .cache can be ignored 271 | *.[Cc]ache 272 | # but keep track of directories ending in .cache 273 | !*.[Cc]ache/ 274 | 275 | # Others 276 | ClientBin/ 277 | ~$* 278 | *~ 279 | *.dbmdl 280 | *.dbproj.schemaview 281 | *.pfx 282 | *.publishsettings 283 | node_modules/ 284 | orleans.codegen.cs 285 | 286 | # Since there are multiple workflows, uncomment next line to ignore bower_components 287 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 288 | #bower_components/ 289 | 290 | # RIA/Silverlight projects 291 | Generated_Code/ 292 | 293 | # Backup & report files from converting an old project file 294 | # to a newer Visual Studio version. Backup files are not needed, 295 | # because we have git ;-) 296 | _UpgradeReport_Files/ 297 | Backup*/ 298 | UpgradeLog*.XML 299 | UpgradeLog*.htm 300 | 301 | # SQL Server files 302 | *.mdf 303 | *.ldf 304 | 305 | # Business Intelligence projects 306 | *.rdl.data 307 | *.bim.layout 308 | *.bim_*.settings 309 | 310 | # Microsoft Fakes 311 | FakesAssemblies/ 312 | 313 | # GhostDoc plugin setting file 314 | *.GhostDoc.xml 315 | 316 | # Node.js Tools for Visual Studio 317 | .ntvs_analysis.dat 318 | 319 | # Visual Studio 6 build log 320 | *.plg 321 | 322 | # Visual Studio 6 workspace options file 323 | *.opt 324 | 325 | # Visual Studio LightSwitch build output 326 | **/*.HTMLClient/GeneratedArtifacts 327 | **/*.DesktopClient/GeneratedArtifacts 328 | **/*.DesktopClient/ModelManifest.xml 329 | **/*.Server/GeneratedArtifacts 330 | **/*.Server/ModelManifest.xml 331 | _Pvt_Extensions 332 | 333 | # Paket dependency manager 334 | .paket/paket.exe 335 | paket-files/ 336 | 337 | # FAKE - F# Make 338 | .fake/ 339 | 340 | # JetBrains Rider 341 | .idea/ 342 | *.sln.iml 343 | 344 | 345 | ### Windows ### 346 | # Windows image file caches 347 | Thumbs.db 348 | ehthumbs.db 349 | 350 | # Folder config file 351 | Desktop.ini 352 | 353 | # Recycle Bin used on file shares 354 | $RECYCLE.BIN/ 355 | 356 | # Windows Installer files 357 | *.cab 358 | *.msi 359 | *.msm 360 | *.msp 361 | 362 | # Windows shortcuts 363 | *.lnk 364 | 365 | # Generated files 366 | ChosunTruck 367 | 368 | # Build files 369 | build/ 370 | 371 | # pyc files 372 | *.pyc 373 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ChosunTruck 2 | 3 | ## Introduction 4 | ChosunTruck is an autonomous driving solution for [Euro Truck Simulator 2](https://eurotrucksimulator2.com/). 5 | Recently, autonomous driving technology has become a big issue and as a result we have been studying technology that incorporates this. 6 | It is being developed in a simulated environment called Euro Truck Simulator 2 to allow us to study it using vehicles. 7 | We chose Euro Truck Simulator 2 because this simulator provides a good test environment that is similar to the real road. 8 | 9 | ## Features 10 | * You can drive a vehicle without handling it yourself. 11 | * You can understand the principles of autonomous driving. 12 | * (Experimental/Linux only) You can detect where other vehicles are. 13 | 14 | ## How To Run It 15 | ### Windows 16 | 17 | #### Dependencies 18 | - OS: Windows 7, 10 (64bit) 19 | 20 | - IDE: Visual Studio 2013, 2015 21 | 22 | - OpenCV version: >= 3.1 23 | 24 | - [Cuda Toolkit 7.5](https://developer.nvidia.com/cuda-75-downloads-archive) (Note: Do an ADVANCED INSTALLATION. ONLY install the Toolkit + Integration to Visual Studio. Do NOT install the drivers + other stuff it would normally give you. Once installed, your project properties should look like this: https://i.imgur.com/e7IRtjy.png) 25 | 26 | - If you have a problem during installation, look at our [Windows Installation wiki page](https://github.com/bethesirius/ChosunTruck/wiki/Windows-Installation) 27 | 28 | #### Required to allow input to work in Windows: 29 | - **Go to C:\Users\YOURUSERNAME\Documents\Euro Truck Simulator 2\profiles and edit controls.sii from** 30 | ``` 31 | config_lines[0]: "device keyboard `di8.keyboard`" 32 | config_lines[1]: "device mouse `fusion.mouse`" 33 | ``` 34 | to 35 | ``` 36 | config_lines[0]: "device keyboard `sys.keyboard`" 37 | config_lines[1]: "device mouse `sys.mouse`" 38 | ``` 39 | (thanks Komat!) 40 | - **While you are in controls.sii, make sure your sensitivity is set to:** 41 | ``` 42 | config_lines[33]: "constant c_rsteersens 0.775000" 43 | config_lines[34]: "constant c_asteersens 4.650000" 44 | ``` 45 | #### Then: 46 | - Set controls.sii to read-only 47 | - Open the visual studio project and build it. 48 | - Run ETS2 in windowed mode and set resolution to 1024 * 768.(It will work properly with 1920 * 1080 screen resolution and 1024 * 768 window mode ETS2.) 49 | 50 | ### Linux 51 | #### Dependencies 52 | - OS: Ubuntu 16.04 LTS 53 | 54 | - [OpenCV version: >= 3.1](http://embedonix.com/articles/image-processing/installing-opencv-3-1-0-on-ubuntu/) 55 | 56 | - (Optional) Tensorflow version: >= 0.12.1 57 | 58 | ### Build the source code with the following command (inside the linux directory). 59 | ``` 60 | make 61 | ``` 62 | ### If you want the car detection function then: 63 | ```` 64 | make Drive 65 | ```` 66 | #### Then: 67 | - Run ETS2 in windowed mode and set its resolution to 1024 * 768. (It will work properly with 1920 * 1080 screen resolution and 1024 * 768 windowed mode ETS2) 68 | - It cannot find the ETS2 window automatically. Move the ETS2 window to the right-down corner to fix this. 69 | - In ETS2 Options, set controls to 'Keyboard + Mouse Steering', 'left click' to acclerate, and 'right click' to brake. 70 | - Go to a highway and set the truck's speed to 40~60km/h. (I recommend you turn on cruise mode to set the speed easily) 71 | - Run this program! 72 | 73 | #### To enable car detection mode, add -D or --Car_Detection. 74 | ``` 75 | ./ChosunTruck [-D|--Car_Detection] 76 | ``` 77 | ## Troubleshooting 78 | See [Our wiki page](https://github.com/bethesirius/ChosunTruck/wiki/Troubleshooting). 79 | 80 | If you have some problems running this project, reference the demo video below. Or, [open a issue to contact our team](https://github.com/bethesirius/ChosunTruck/issues). 81 | 82 | ## Demo Video 83 | Lane Detection (Youtube link) 84 | 85 | [![youtube link](http://img.youtube.com/vi/vF7J_uC045Q/0.jpg)](http://www.youtube.com/watch?v=vF7J_uC045Q) 86 | [![youtube link](http://img.youtube.com/vi/qb99czlIklA/0.jpg)](http://www.youtube.com/watch?v=qb99czlIklA) 87 | 88 | Lane Detection + Vehicle Detection (Youtube link) 89 | 90 | [![youtube link](http://img.youtube.com/vi/w6H2eGEvzvw/0.jpg)](http://www.youtube.com/watch?v=w6H2eGEvzvw) 91 | 92 | ## Todo 93 | * For better detection performance, Change the Tensorbox to YOLO2. 94 | * The information from in-game screen have Restrictions. Read ETS2 process memory to collect more driving environment data. 95 | 96 | ## Founders 97 | - Chiwan Song, chi3236@gmail.com 98 | 99 | - JaeCheol Sim, simjaecheol@naver.com 100 | 101 | - Seongjoon Chu, hs4393@gmail.com 102 | 103 | ## Contributors 104 | - [zappybiby](https://github.com/zappybiby) 105 | 106 | ## How To Contribute 107 | Anyone who is interested in this project is welcome! Just fork it and pull requests! 108 | 109 | ## License 110 | ChosunTruck, Euro Truck Simulator 2 auto driving solution 111 | Copyright (C) 2017 chi3236, bethesirius, uoyssim 112 | 113 | This program is free software: you can redistribute it and/or modify 114 | it under the terms of the GNU General Public License as published by 115 | the Free Software Foundation, either version 3 of the License, or 116 | (at your option) any later version. 117 | 118 | This program is distributed in the hope that it will be useful, 119 | but WITHOUT ANY WARRANTY; without even the implied warranty of 120 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 121 | GNU General Public License for more details. 122 | 123 | You should have received a copy of the GNU General Public License 124 | along with this program. If not, see . 125 | -------------------------------------------------------------------------------- /README/Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/README/Logo.png -------------------------------------------------------------------------------- /license/TENSORBOX_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 "The Contributors" 2 | 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /license/TENSORFLOW_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2015 The TensorFlow Authors. All rights reserved. 2 | 3 | Apache License 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "[]" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright 2015, The TensorFlow Authors. 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | -------------------------------------------------------------------------------- /linux/Makefile: -------------------------------------------------------------------------------- 1 | OUT_O_DIR = build 2 | TENSORBOX_UTILS_DIR = tensorbox/utils 3 | 4 | OBJS = $(OUT_O_DIR)/getScreen_linux.o $(OUT_O_DIR)/main2.o $(OUT_O_DIR)/IPM.o $(OUT_O_DIR)/linefinder.o $(OUT_O_DIR)/uinput.o 5 | 6 | CC = gcc 7 | CFLAGS = -std=c11 -Wall -O3 -march=native 8 | CPP = g++ 9 | CPPFLAGS = `pkg-config opencv --cflags --libs` -std=c++11 -lX11 -Wall -fopenmp -O3 -march=native 10 | 11 | TARGET = ChosunTruck 12 | 13 | $(TARGET) : $(OBJS) 14 | $(CPP) $(OBJS) $(CPPFLAGS) -o $@ 15 | 16 | $(OUT_O_DIR)/main2.o : src/main2.cc 17 | mkdir -p $(@D) 18 | $(CPP) -c $< $(CPPFLAGS) -o $@ 19 | $(OUT_O_DIR)/getScreen_linux.o : src/getScreen_linux.cc 20 | mkdir -p $(@D) 21 | $(CPP) -c $< $(CPPFLAGS) -o $@ 22 | $(OUT_O_DIR)/IPM.o : src/IPM.cc 23 | mkdir -p $(@D) 24 | $(CPP) -c $< $(CPPFLAGS) -o $@ 25 | $(OUT_O_DIR)/linefinder.o : src/linefinder.cc 26 | mkdir -p $(@D) 27 | $(CPP) -c $< $(CPPFLAGS) -o $@ 28 | $(OUT_O_DIR)/uinput.o : src/uinput.c 29 | mkdir -p $(@D) 30 | $(CC) -c $< $(CFLAGS) -o $@ 31 | 32 | clean : 33 | rm -f $(OBJS) ./$(TARGET) 34 | 35 | .PHONY: Drive 36 | 37 | Drive: 38 | pip install runcython 39 | makecython++ $(TENSORBOX_UTILS_DIR)/stitch_wrapper.pyx "" "$(TENSORBOX_UTILS_DIR)/stitch_rects.cpp $(TENSORBOX_UTILS_DIR)/hungarian/hungarian.cpp" 40 | 41 | hungarian: $(TENSORBOX_UTILS_DIR)/hungarian/hungarian.so 42 | 43 | $(TENSORBOX_UTILS_DIR)/hungarian/hungarian.so: 44 | cd $(TENSORBOX_UTILS_DIR)/hungarian && \ 45 | TF_INC=$$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())') && \ 46 | if [ `uname` == Darwin ];\ 47 | then g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I -D_GLIBCXX_USE_CXX11_ABI=0$$TF_INC;\ 48 | else g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I $$TF_INC; fi 49 | 50 | 51 | -------------------------------------------------------------------------------- /linux/src/IPM.cc: -------------------------------------------------------------------------------- 1 | #include "IPM.h" 2 | 3 | using namespace cv; 4 | using namespace std; 5 | 6 | // Public 7 | IPM::IPM( const cv::Size& _origSize, const cv::Size& _dstSize, const std::vector& _origPoints, const std::vector& _dstPoints ) 8 | : m_origSize(_origSize), m_dstSize(_dstSize), m_origPoints(_origPoints), m_dstPoints(_dstPoints) 9 | { 10 | assert( m_origPoints.size() == 4 && m_dstPoints.size() == 4 && "Orig. points and Dst. points must vectors of 4 points" ); 11 | m_H = getPerspectiveTransform( m_origPoints, m_dstPoints ); 12 | m_H_inv = m_H.inv(); 13 | 14 | createMaps(); 15 | } 16 | 17 | void IPM::setIPM( const cv::Size& _origSize, const cv::Size& _dstSize, const std::vector& _origPoints, const std::vector& _dstPoints ) 18 | { 19 | m_origSize = _origSize; 20 | m_dstSize = _dstSize; 21 | m_origPoints = _origPoints; 22 | m_dstPoints = _dstPoints; 23 | assert( m_origPoints.size() == 4 && m_dstPoints.size() == 4 && "Orig. points and Dst. points must vectors of 4 points" ); 24 | m_H = getPerspectiveTransform( m_origPoints, m_dstPoints ); 25 | m_H_inv = m_H.inv(); 26 | 27 | createMaps(); 28 | } 29 | void IPM::drawPoints( const std::vector& _points, cv::Mat& _img ) const 30 | { 31 | assert(_points.size() == 4); 32 | 33 | line(_img, Point(static_cast(_points[0].x), static_cast(_points[0].y)), Point(static_cast(_points[3].x), static_cast(_points[3].y)), CV_RGB( 205,205,0), 2); 34 | line(_img, Point(static_cast(_points[2].x), static_cast(_points[2].y)), Point(static_cast(_points[3].x), static_cast(_points[3].y)), CV_RGB( 205,205,0), 2); 35 | line(_img, Point(static_cast(_points[0].x), static_cast(_points[0].y)), Point(static_cast(_points[1].x), static_cast(_points[1].y)), CV_RGB( 205,205,0), 2); 36 | line(_img, Point(static_cast(_points[2].x), static_cast(_points[2].y)), Point(static_cast(_points[1].x), static_cast(_points[1].y)), CV_RGB( 205,205,0), 2); 37 | for(size_t i=0; i<_points.size(); i++) 38 | { 39 | circle(_img, Point(static_cast(_points[i].x), static_cast(_points[i].y)), 2, CV_RGB(238,238,0), -1); 40 | circle(_img, Point(static_cast(_points[i].x), static_cast(_points[i].y)), 5, CV_RGB(255,255,255), 2); 41 | } 42 | } 43 | void IPM::getPoints(vector& _origPts, vector& _ipmPts) 44 | { 45 | _origPts = m_origPoints; 46 | _ipmPts = m_dstPoints; 47 | } 48 | void IPM::applyHomography(const Mat& _inputImg, Mat& _dstImg, int _borderMode) 49 | { 50 | // Generate IPM image from src 51 | remap(_inputImg, _dstImg, m_mapX, m_mapY, INTER_LINEAR, _borderMode);//, BORDER_CONSTANT, Scalar(0,0,0,0)); 52 | } 53 | void IPM::applyHomographyInv(const Mat& _inputImg, Mat& _dstImg, int _borderMode) 54 | { 55 | // Generate IPM image from src 56 | remap(_inputImg, _dstImg, m_mapX, m_mapY, INTER_LINEAR, _borderMode);//, BORDER_CONSTANT, Scalar(0,0,0,0)); 57 | } 58 | Point2d IPM::applyHomography( const Point2d& _point ) 59 | { 60 | return applyHomography( _point, m_H ); 61 | } 62 | Point2d IPM::applyHomographyInv( const Point2d& _point ) 63 | { 64 | return applyHomography( _point, m_H_inv ); 65 | } 66 | Point2d IPM::applyHomography( const Point2d& _point, const Mat& _H ) 67 | { 68 | Point2d ret = Point2d( -1, -1 ); 69 | 70 | const double u = _H.at(0,0) * _point.x + _H.at(0,1) * _point.y + _H.at(0,2); 71 | const double v = _H.at(1,0) * _point.x + _H.at(1,1) * _point.y + _H.at(1,2); 72 | const double s = _H.at(2,0) * _point.x + _H.at(2,1) * _point.y + _H.at(2,2); 73 | if ( s != 0 ) 74 | { 75 | ret.x = ( u / s ); 76 | ret.y = ( v / s ); 77 | } 78 | return ret; 79 | } 80 | Point3d IPM::applyHomography( const Point3d& _point ) 81 | { 82 | return applyHomography( _point, m_H ); 83 | } 84 | Point3d IPM::applyHomographyInv( const Point3d& _point ) 85 | { 86 | return applyHomography( _point, m_H_inv ); 87 | } 88 | Point3d IPM::applyHomography( const Point3d& _point, const cv::Mat& _H ) 89 | { 90 | Point3d ret = Point3d( -1, -1, 1 ); 91 | 92 | const double u = _H.at(0,0) * _point.x + _H.at(0,1) * _point.y + _H.at(0,2) * _point.z; 93 | const double v = _H.at(1,0) * _point.x + _H.at(1,1) * _point.y + _H.at(1,2) * _point.z; 94 | const double s = _H.at(2,0) * _point.x + _H.at(2,1) * _point.y + _H.at(2,2) * _point.z; 95 | if ( s != 0 ) 96 | { 97 | ret.x = ( u / s ); 98 | ret.y = ( v / s ); 99 | } 100 | else 101 | ret.z = 0; 102 | return ret; 103 | } 104 | 105 | // Private 106 | void IPM::createMaps() 107 | { 108 | // Create remap images 109 | m_mapX.create(m_dstSize, CV_32F); 110 | m_mapY.create(m_dstSize, CV_32F); 111 | //#pragma omp parallel for schedule(dynamic) 112 | for( int j = 0; j < m_dstSize.height; ++j ) 113 | { 114 | float* ptRowX = m_mapX.ptr(j); 115 | float* ptRowY = m_mapY.ptr(j); 116 | //#pragma omp parallel for schedule(dynamic) 117 | for( int i = 0; i < m_dstSize.width; ++i ) 118 | { 119 | Point2f pt = applyHomography( Point2f( static_cast(i), static_cast(j) ), m_H_inv ); 120 | ptRowX[i] = pt.x; 121 | ptRowY[i] = pt.y; 122 | } 123 | } 124 | 125 | m_invMapX.create(m_origSize, CV_32F); 126 | m_invMapY.create(m_origSize, CV_32F); 127 | 128 | //#pragma omp parallel for schedule(dynamic) 129 | for( int j = 0; j < m_origSize.height; ++j ) 130 | { 131 | float* ptRowX = m_invMapX.ptr(j); 132 | float* ptRowY = m_invMapY.ptr(j); 133 | //#pragma omp parallel for schedule(dynamic) 134 | for( int i = 0; i < m_origSize.width; ++i ) 135 | { 136 | Point2f pt = applyHomography( Point2f( static_cast(i), static_cast(j) ), m_H ); 137 | ptRowX[i] = pt.x; 138 | ptRowY[i] = pt.y; 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /linux/src/IPM.h: -------------------------------------------------------------------------------- 1 | #ifndef __IPM_H__ 2 | #define __IPM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | class IPM 12 | { 13 | public: 14 | IPM( const cv::Size& _origSize, const cv::Size& _dstSize, 15 | const std::vector& _origPoints, const std::vector& _dstPoints ); 16 | 17 | IPM() {} 18 | // Apply IPM on points 19 | cv::Point2d applyHomography(const cv::Point2d& _point, const cv::Mat& _H); 20 | cv::Point3d applyHomography( const cv::Point3d& _point, const cv::Mat& _H); 21 | cv::Point2d applyHomography(const cv::Point2d& _point); 22 | cv::Point3d applyHomography( const cv::Point3d& _point); 23 | cv::Point2d applyHomographyInv(const cv::Point2d& _point); 24 | cv::Point3d applyHomographyInv( const cv::Point3d& _point); 25 | void applyHomography( const cv::Mat& _origBGR, cv::Mat& _ipmBGR, int borderMode = cv::BORDER_CONSTANT); 26 | void applyHomographyInv( const cv::Mat& _ipmBGR, cv::Mat& _origBGR, int borderMode = cv::BORDER_CONSTANT); 27 | 28 | // Getters 29 | cv::Mat getH() const { return m_H; } 30 | cv::Mat getHinv() const { return m_H_inv; } 31 | void getPoints(std::vector& _origPts, std::vector& _ipmPts); 32 | 33 | // Draw 34 | void drawPoints( const std::vector& _points, cv::Mat& _img ) const; 35 | 36 | void setIPM( const cv::Size& _origSize, const cv::Size& _dstSize, 37 | const std::vector& _origPoints, const std::vector& _dstPoints ); 38 | private: 39 | void createMaps(); 40 | 41 | // Sizes 42 | cv::Size m_origSize; 43 | cv::Size m_dstSize; 44 | 45 | // Points 46 | std::vector m_origPoints; 47 | std::vector m_dstPoints; 48 | 49 | // Homography 50 | cv::Mat m_H; 51 | cv::Mat m_H_inv; 52 | 53 | // Maps 54 | cv::Mat m_mapX, m_mapY; 55 | cv::Mat m_invMapX, m_invMapY; 56 | }; 57 | 58 | #endif /*__IPM_H__*/ 59 | -------------------------------------------------------------------------------- /linux/src/ets2_self_driving.h: -------------------------------------------------------------------------------- 1 | #ifndef __ets_self_driving_h__ 2 | #define __ets_self_driving_h__ 3 | 4 | #include 5 | #include 6 | //#include 7 | #include 8 | #include 9 | #include 10 | #include 11 | //#include 12 | //#include 13 | //#include 14 | //#include 15 | #include 16 | #include 17 | #include 18 | 19 | #define PI 3.1415926 20 | 21 | 22 | using namespace cv; 23 | using namespace std; 24 | 25 | class LineFinder{ 26 | 27 | private: 28 | cv::Mat image; // 원 영상 29 | std::vector lines; // 선을 감지하기 위한 마지막 점을 포함한 벡터 30 | double deltaRho; 31 | double deltaTheta; // 누산기 해상도 파라미터 32 | int minVote; // 선을 고려하기 전에 받아야 하는 최소 투표 개수 33 | double minLength; // 선에 대한 최소 길이 34 | double maxGap; // 선에 따른 최대 허용 간격 35 | 36 | public: 37 | LineFinder() : deltaRho(1), deltaTheta(PI / 180), minVote(50), minLength(50), maxGap(10) {} 38 | // 기본 누적 해상도는 1각도 1화소 39 | // 간격이 없고 최소 길이도 없음 40 | void setAccResolution(double dRho, double dTheta); 41 | void setMinVote(int minv); 42 | void setLineLengthAndGap(double length, double gap); 43 | std::vector findLines(cv::Mat& binary); 44 | void drawDetectedLines(cv::Mat &image, cv::Scalar color = cv::Scalar(112, 112, 0)); 45 | }; 46 | //Mat hwnd2mat(HWND hwnd); 47 | void cudaf(); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /linux/src/getScreen_linux.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void ImageFromDisplay(std::vector& Pixels, int& Width, int& Height, int& BitsPerPixel) 8 | { 9 | Display* display = XOpenDisplay(nullptr); 10 | Window root = DefaultRootWindow(display); 11 | 12 | XWindowAttributes attributes = {0}; 13 | XGetWindowAttributes(display, root, &attributes); 14 | 15 | Width = attributes.width; 16 | Height = attributes.height; 17 | 18 | XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap); 19 | BitsPerPixel = img->bits_per_pixel; 20 | Pixels.resize(Width * Height * 4); 21 | 22 | memcpy(&Pixels[0], img->data, Pixels.size()); 23 | 24 | XDestroyImage(img); 25 | XCloseDisplay(display); 26 | } 27 | -------------------------------------------------------------------------------- /linux/src/getScreen_linux.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void ImageFromDisplay(std::vector& Pixels, int& Width, int& Height, int& BitsPerPixel); 8 | -------------------------------------------------------------------------------- /linux/src/linefinder.cc: -------------------------------------------------------------------------------- 1 | #include "ets2_self_driving.h" 2 | #include 3 | #include 4 | //#include 5 | #include 6 | #include 7 | #include 8 | #include 9 | //#include 10 | //#include 11 | //#include 12 | #include 13 | #include 14 | 15 | #define PI 3.1415926 16 | 17 | cv::Point prev_point; 18 | 19 | using namespace cv; 20 | using namespace std; 21 | 22 | // 해당 세터 메소드들 23 | // 누적기에 해상도 설정 24 | void LineFinder::setAccResolution(double dRho, double dTheta) { 25 | deltaRho = dRho; 26 | deltaTheta = dTheta; 27 | } 28 | 29 | // 투표 최소 개수 설정 30 | void LineFinder::setMinVote(int minv) { 31 | minVote = minv; 32 | } 33 | 34 | // 선 길이와 간격 설정 35 | void LineFinder::setLineLengthAndGap(double length, double gap) { 36 | minLength = length; 37 | maxGap = gap; 38 | } 39 | 40 | // 허프 선 세그먼트 감지를 수행하는 메소드 41 | // 확률적 허프 변환 적용 42 | std::vector LineFinder::findLines(cv::Mat& binary) { 43 | UMat gpuBinary = binary.getUMat(ACCESS_RW); 44 | lines.clear(); 45 | cv::HoughLinesP(gpuBinary, lines, deltaRho, deltaTheta, minVote, minLength, maxGap); 46 | return lines; 47 | } // cv::Vec4i 벡터를 반환하고, 감지된 각 세그먼트의 시작과 마지막 점 좌표를 포함. 48 | 49 | // 위 메소드에서 감지한 선을 다음 메소드를 사용해서 그림 50 | // 영상에서 감지된 선을 그리기 51 | void LineFinder::drawDetectedLines(cv::Mat &image, cv::Scalar color) { 52 | 53 | UMat gpuImage = image.getUMat(ACCESS_RW); 54 | // 선 그리기 55 | std::vector::const_iterator it2 = lines.begin(); 56 | cv::Point endPoint; 57 | 58 | while (it2 != lines.end()) { 59 | cv::Point startPoint((*it2)[0], (*it2)[1]); 60 | endPoint = cv::Point((*it2)[2], (*it2)[3]); 61 | cv::line(gpuImage, startPoint, endPoint, color, 3); 62 | ++it2; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /linux/src/main2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "ets2_self_driving.h" 24 | #include "IPM.h" 25 | #include "getScreen_linux.h" 26 | #include "uinput.c" 27 | 28 | #define PI 3.1415926 29 | 30 | using namespace cv; 31 | using namespace std; 32 | 33 | void input(int, int, int); 34 | string exec(const char* cmd); 35 | int counter = 0; 36 | 37 | int main(int argc, char** argv) { 38 | if(-1 == setUinput()) { 39 | return -1; 40 | } 41 | bool detection = false; 42 | bool python_on = false; 43 | int width = 1024, height = 768; 44 | double IPM_BOTTOM_RIGHT = width+400; 45 | double IPM_BOTTOM_LEFT = -400; 46 | double IPM_RIGHT = width/2+100; 47 | double IPM_LEFT = width/2-100; 48 | int IPM_diff = 0; 49 | if(argc == 2){ 50 | if(strcmp(argv[1], "--Car_Detection")){ 51 | detection = true; 52 | cout<<"Car Detection Enabled"<(shmat(shmid, NULL, 0))) == (char *) -1) 90 | { 91 | printf("Error attaching shared memory id"); 92 | exit(1); 93 | } 94 | 95 | int curve; 96 | key = 123464; 97 | if ((curve = shmget(key, 4, IPC_CREAT | 0666)) < 0) 98 | { 99 | printf("Error getting shared memory curve"); 100 | exit(1); 101 | } 102 | // Attached shared memory 103 | if ((shared_curve = static_cast(shmat(curve, NULL, 0))) == (char *) -1) 104 | { 105 | printf("Error attaching shared memory curve"); 106 | exit(1); 107 | } 108 | } 109 | } 110 | int move_mouse_pixel = 0; 111 | while (true) { 112 | auto begin = chrono::high_resolution_clock::now(); 113 | // ETS2 114 | Mat image, sendImg, outputImg; 115 | int Width = 1024; 116 | int Height = 768; 117 | int Bpp = 0; 118 | std::vector Pixels; 119 | 120 | ImageFromDisplay(Pixels, Width, Height, Bpp); 121 | 122 | Mat img = Mat(Height, Width, Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]); //Mat(Size(Height, Width), Bpp > 24 ? CV_8UC4 : CV_8UC3, &Pixels[0]); 123 | cv::Rect myROI(896, 312, 1024, 768); 124 | image = img(myROI); 125 | 126 | //------------------------ 127 | //cv::cvtColor(image, sendImg, CV_YUV2RGBA_NV21); 128 | cv::cvtColor(image, sendImg, CV_RGBA2RGB); 129 | //printf("%d\n", sendImg.dataend - sendImg.datastart); 130 | 131 | if(detection == true) { 132 | memcpy(shared_memory, sendImg.datastart, sendImg.dataend - sendImg.datastart); 133 | } 134 | // 135 | 136 | // Mat to GpuMat 137 | //cuda::GpuMat imageGPU; 138 | //imageGPU.upload(image); 139 | 140 | //medianBlur(image, image, 3); 141 | //cv::cuda::bilateralFilter(imageGPU, imageGPU, ); 142 | 143 | IPM ipm; 144 | vector origPoints; 145 | vector dstPoints; 146 | // The 4-points at the input image 147 | 148 | origPoints.push_back(Point2f(IPM_BOTTOM_LEFT, (height-50))); 149 | origPoints.push_back(Point2f(IPM_BOTTOM_RIGHT, height-50)); 150 | origPoints.push_back(Point2f(IPM_RIGHT, height/2+30)); 151 | origPoints.push_back(Point2f(IPM_LEFT, height/2+30)); 152 | 153 | // The 4-points correspondences in the destination image 154 | 155 | dstPoints.push_back(Point2f(0, height)); 156 | dstPoints.push_back(Point2f(width, height)); 157 | dstPoints.push_back(Point2f(width, 0)); 158 | dstPoints.push_back(Point2f(0, 0)); 159 | 160 | // IPM object 161 | ipm.setIPM(Size(width, height), Size(width, height), origPoints, dstPoints); 162 | 163 | // Process 164 | //clock_t begin = clock(); 165 | ipm.applyHomography(image, outputImg); 166 | //clock_t end = clock(); 167 | //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; 168 | //printf("%.2f (ms)\r", 1000 * elapsed_secs); 169 | //ipm.drawPoints(origPoints, image); 170 | 171 | //Mat row = outputImg.row[0]; 172 | cv::UMat gray; 173 | cv::UMat blur; 174 | cv::UMat sobel; 175 | cv::Mat contours; 176 | LineFinder ld; // 인스턴스 생성 177 | 178 | cv::resize(outputImg, outputImg, cv::Size(320, 240)); 179 | cv::cvtColor(outputImg, gray, COLOR_RGB2GRAY); 180 | cv::blur(gray, blur, cv::Size(10, 10)); 181 | cv::Sobel(blur, sobel, blur.depth(), 1, 0, 3, 0.5, 127); 182 | cv::threshold(sobel, contours, 145, 255, CV_THRESH_BINARY); 183 | //Thinning(contours, contours.rows, contours.cols); 184 | //cv::Canny(gray, contours, 125, 350); 185 | 186 | 187 | // 확률적 허프변환 파라미터 설정하기 188 | 189 | ld.setLineLengthAndGap(20, 120); 190 | ld.setMinVote(55); 191 | 192 | std::vector li = ld.findLines(contours); 193 | ld.drawDetectedLines(contours); 194 | 195 | //////////////////////////////////////////// 196 | // 자율 주행 197 | 198 | int bottom_center = 160; 199 | int sum_centerline = 0; 200 | int count_centerline = 0; 201 | int first_centerline = 0; 202 | int last_centerline = 0; 203 | double avr_center_to_left = 0; 204 | double avr_center_to_right = 0; 205 | 206 | //#pragma omp parallel for 207 | for(int i=240; i>30; i--){ 208 | double center_to_right = -1; 209 | double center_to_left = -1; 210 | 211 | for (int j=0;j<150;j++) { 212 | if (contours.at(i, bottom_center+j) == 112 && center_to_right == -1) { 213 | center_to_right = j; 214 | } 215 | if (contours.at(i, bottom_center-j) == 112 && center_to_left == -1) { 216 | center_to_left = j; 217 | } 218 | } 219 | if(center_to_left!=-1 && center_to_right!=-1){ 220 | int centerline = (center_to_right - center_to_left +2*bottom_center)/2; 221 | if (first_centerline == 0 ) { 222 | first_centerline = centerline; 223 | } 224 | //cv::circle(outputImg, Point(centerline, i), 1, Scalar(30, 255, 30) , 3); 225 | //cv::circle(outputImg, Point(centerline + center_to_right+20, i), 1, Scalar(255, 30, 30) , 3); 226 | //cv::circle(outputImg, Point(centerline - center_to_left+10, i), 1, Scalar(255, 30, 30) , 3); 227 | sum_centerline += centerline; 228 | avr_center_to_left = (avr_center_to_left * count_centerline + center_to_left)/count_centerline+1; 229 | avr_center_to_right = (avr_center_to_right * count_centerline + center_to_right)/count_centerline+1; 230 | last_centerline = centerline; 231 | count_centerline++; 232 | } else { 233 | } 234 | } 235 | 236 | // 컨트롤러 입력 237 | int diff = 0; 238 | if (count_centerline!=0) { 239 | diff = sum_centerline/count_centerline - bottom_center; 240 | int degree = atan2 (last_centerline - first_centerline, count_centerline) * 180 / PI; 241 | //diff = (90 - degree); 242 | 243 | move_mouse_pixel = 0 - counter + diff; 244 | cout << "Steer: "<< move_mouse_pixel << "px "; 245 | //goDirection(move_mouse_pixel); 246 | if(move_mouse_pixel == 0){ 247 | if(IPM_diff > 0){ 248 | IPM_RIGHT -= 1; 249 | IPM_LEFT -= 1; 250 | IPM_diff -= 1; 251 | } 252 | else if(IPM_diff < 0){ 253 | IPM_RIGHT += 1; 254 | IPM_LEFT += 1; 255 | IPM_diff += 1; 256 | } 257 | else{ 258 | IPM_RIGHT = width/2+100; 259 | IPM_LEFT = width/2-100; 260 | IPM_diff = 0; 261 | } 262 | } 263 | else{ 264 | if (IPM_diff >= -30 && IPM_diff <= 30){ 265 | IPM_RIGHT += move_mouse_pixel; 266 | IPM_LEFT += move_mouse_pixel; 267 | if(move_mouse_pixel > 0){ 268 | IPM_diff++; 269 | } 270 | else{ 271 | IPM_diff--; 272 | } 273 | } 274 | } 275 | moveMouse(move_mouse_pixel); 276 | int road_curve = (int)((sum_centerline/count_centerline-bottom_center)); 277 | 278 | if(detection == true) { 279 | memcpy(shared_curve, &road_curve, sizeof(int)); 280 | } 281 | 282 | counter = diff;/* 283 | 284 | 285 | if (abs(move_mouse_pixel)< 5) { 286 | goDirection(0 - counter/3); 287 | counter -= counter/3; 288 | } else if (abs(move_mouse_pixel) < 4) { 289 | goDirection(0 - counter/2); 290 | counter -= counter/2; 291 | } else if (abs (move_mouse_pixel) < 2) { 292 | goDirection(0 - counter); 293 | counter = 0; 294 | } else { 295 | goDirection(move_mouse_pixel); 296 | counter += move_mouse_pixel; 297 | } 298 | */ 299 | 300 | } else {} 301 | 302 | 303 | //////////////////////////////////////////// 304 | 305 | //cv::cvtColor(contours, contours, COLOR_GRAY2RGB); 306 | imshow("Road", outputImg); 307 | imshow("Lines", contours); 308 | moveWindow("Lines", 200, 400); 309 | moveWindow("Road", 530, 400); 310 | waitKey(1); 311 | 312 | auto end = chrono::high_resolution_clock::now(); 313 | auto dur = end - begin; 314 | auto ms = std::chrono::duration_cast(dur).count(); 315 | ms++; 316 | //cout << 1000 / ms << "fps avr:" << 1000 / (sum / (++i)) << endl; 317 | cout << 1000 / ms << "fps" << endl; 318 | 319 | } 320 | return 0; 321 | } 322 | 323 | 324 | 325 | 326 | std::string exec(const char* cmd) { 327 | std::array buffer; 328 | std::string result; 329 | std::shared_ptr pipe(popen(cmd, "r"), pclose); 330 | if (!pipe) 331 | throw std::runtime_error("popen() failed!"); 332 | while (!feof(pipe.get())) { 333 | if (fgets(buffer.data(), 128, pipe.get()) != NULL) 334 | result += buffer.data(); 335 | } 336 | return result; 337 | } 338 | -------------------------------------------------------------------------------- /linux/src/uinput.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "uinput.h" 3 | 4 | void setEventAndWrite(__u16 type, __u16 code, __s32 value) 5 | { 6 | ev.type=type; 7 | ev.code=code; 8 | ev.value=value; 9 | if(write(fd, &ev, sizeof(struct input_event)) < 0) 10 | die("error: write"); 11 | } 12 | 13 | int setUinput() { 14 | fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); 15 | if(fd < 0) { 16 | fd=open("/dev/input/uinput",O_WRONLY|O_NONBLOCK); 17 | if(fd<0){ 18 | die("error: Can't open an uinput file. see #17.(https://github.com/bethesirius/ChosunTruck/issues/17)"); 19 | return -1; 20 | } 21 | } 22 | if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0) { 23 | die("error: ioctl"); 24 | return -1; 25 | } 26 | if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0) { 27 | die("error: ioctl"); 28 | return -1; 29 | } 30 | if(ioctl(fd, UI_SET_KEYBIT, KEY_TAB) < 0) { 31 | die("error: ioctl"); 32 | return -1; 33 | } 34 | if(ioctl(fd, UI_SET_KEYBIT, KEY_ENTER) < 0) { 35 | die("error: ioctl"); 36 | return -1; 37 | } 38 | if(ioctl(fd, UI_SET_KEYBIT, KEY_LEFTSHIFT) < 0) { 39 | die("error: ioctl"); 40 | return -1; 41 | } 42 | if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0) { 43 | die("error: ioctl"); 44 | return -1; 45 | } 46 | if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0) { 47 | die("error: ioctl"); 48 | return -1; 49 | } 50 | if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0) { 51 | die("error: ioctl"); 52 | return -1; 53 | } 54 | 55 | memset(&uidev, 0, sizeof(uidev)); 56 | snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-virtualMouse"); 57 | uidev.id.bustype = BUS_USB; 58 | uidev.id.vendor = 0x1; 59 | uidev.id.product = 0x1; 60 | uidev.id.version = 1; 61 | 62 | if(write(fd, &uidev, sizeof(uidev)) < 0) { 63 | die("error: write"); 64 | return -1; 65 | } 66 | if(ioctl(fd, UI_DEV_CREATE) < 0) { 67 | die("error: ioctl"); 68 | return -1; 69 | } 70 | 71 | memset(&ev, 0, sizeof(struct input_event)); 72 | setEventAndWrite(EV_REL,REL_X,1); 73 | setEventAndWrite(EV_REL,REL_Y,1); 74 | setEventAndWrite(EV_SYN,0,0); 75 | return 0; 76 | } 77 | 78 | void moveMouse(int x) { 79 | int dx=x; 80 | setEventAndWrite(EV_REL,REL_X,dx); 81 | setEventAndWrite(EV_SYN,0,0); 82 | } 83 | 84 | 85 | -------------------------------------------------------------------------------- /linux/src/uinput.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define die(str, args...) do { \ 14 | perror(str); \ 15 | exit(EXIT_FAILURE); \ 16 | } while(0) 17 | 18 | static int fd;//uinput fd 19 | static struct uinput_user_dev uidev; 20 | static struct input_event ev; 21 | 22 | int setUinput(); 23 | void moveMouse(int); 24 | 25 | -------------------------------------------------------------------------------- /linux/tensorbox/Car_detection.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import os 3 | import json 4 | import subprocess 5 | import sysv_ipc 6 | import struct 7 | 8 | from scipy.misc import imread, imresize 9 | from scipy import misc 10 | 11 | from train import build_forward 12 | from utils.annolist import AnnotationLib as al 13 | from utils.train_utils import add_rectangles, rescale_boxes 14 | from pymouse import PyMouse 15 | 16 | import cv2 17 | import argparse 18 | import time 19 | import numpy as np 20 | 21 | def get_image_dir(args): 22 | weights_iteration = int(args.weights.split('-')[-1]) 23 | expname = '_' + args.expname if args.expname else '' 24 | image_dir = '%s/images_%s_%d%s' % (os.path.dirname(args.weights), os.path.basename(args.test_boxes)[:-5], weights_iteration, expname) 25 | return image_dir 26 | 27 | def get_results(args, H): 28 | tf.reset_default_graph() 29 | x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) 30 | if H['use_rezoom']: 31 | pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) 32 | grid_area = H['grid_height'] * H['grid_width'] 33 | pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) 34 | if H['reregress']: 35 | pred_boxes = pred_boxes + pred_boxes_deltas 36 | else: 37 | pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) 38 | saver = tf.train.Saver() 39 | with tf.Session() as sess: 40 | sess.run(tf.initialize_all_variables()) 41 | saver.restore(sess, args.weights) 42 | 43 | pred_annolist = al.AnnoList() 44 | 45 | data_dir = os.path.dirname(args.test_boxes) 46 | image_dir = get_image_dir(args) 47 | subprocess.call('mkdir -p %s' % image_dir, shell=True) 48 | 49 | memory = sysv_ipc.SharedMemory(123463) 50 | memory2 = sysv_ipc.SharedMemory(123464) 51 | size = 768, 1024, 3 52 | 53 | pedal = PyMouse() 54 | pedal.press(1) 55 | road_center = 320 56 | while True: 57 | cv2.waitKey(1) 58 | frameCount = bytearray(memory.read()) 59 | curve = bytearray(memory2.read()) 60 | curve = str(struct.unpack('i',curve)[0]) 61 | m = np.array(frameCount, dtype=np.uint8) 62 | orig_img = m.reshape(size) 63 | 64 | img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') 65 | feed = {x_in: img} 66 | (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) 67 | pred_anno = al.Annotation() 68 | 69 | new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, 70 | use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) 71 | flag = 0 72 | road_center = 320 + int(curve) 73 | print(road_center) 74 | for rect in rects: 75 | print(rect.x1, rect.x2, rect.y2) 76 | if (rect.x1 < road_center and rect.x2 > road_center and rect.y2 > 200) and (rect.x2 - rect.x1 > 30): 77 | flag = 1 78 | 79 | if flag is 1: 80 | pedal.press(2) 81 | print("break!") 82 | else: 83 | pedal.release(2) 84 | pedal.press(1) 85 | print("acceleration!") 86 | 87 | pred_anno.rects = rects 88 | pred_anno.imagePath = os.path.abspath(data_dir) 89 | pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) 90 | pred_annolist.append(pred_anno) 91 | 92 | cv2.imshow('.jpg', new_img) 93 | 94 | return none; 95 | 96 | def main(): 97 | parser = argparse.ArgumentParser() 98 | parser.add_argument('--weights', default='tensorbox/output/overfeat_rezoom_2017_02_09_13.28/save.ckpt-100000') 99 | parser.add_argument('--expname', default='') 100 | parser.add_argument('--test_boxes', default='default') 101 | parser.add_argument('--gpu', default=0) 102 | parser.add_argument('--logdir', default='output') 103 | parser.add_argument('--iou_threshold', default=0.5, type=float) 104 | parser.add_argument('--tau', default=0.25, type=float) 105 | parser.add_argument('--min_conf', default=0.2, type=float) 106 | parser.add_argument('--show_suppressed', default=True, type=bool) 107 | args = parser.parse_args() 108 | os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) 109 | hypes_file = '%s/hypes.json' % os.path.dirname(args.weights) 110 | with open(hypes_file, 'r') as f: 111 | H = json.load(f) 112 | expname = args.expname + '_' if args.expname else '' 113 | pred_boxes = '%s.%s%s' % (args.weights, expname, os.path.basename(args.test_boxes)) 114 | 115 | get_results(args, H) 116 | 117 | if __name__ == '__main__': 118 | main() 119 | -------------------------------------------------------------------------------- /linux/tensorbox/data/inception_v1.ckpt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/linux/tensorbox/data/inception_v1.ckpt -------------------------------------------------------------------------------- /linux/tensorbox/license/TENSORBOX_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 "The Contributors" 2 | 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /linux/tensorbox/license/TENSORFLOW_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2015 The TensorFlow Authors. All rights reserved. 2 | 3 | Apache License 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "[]" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright 2015, The TensorFlow Authors. 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | -------------------------------------------------------------------------------- /linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "save.ckpt-100000" 2 | all_model_checkpoint_paths: "save.ckpt-100000" 3 | -------------------------------------------------------------------------------- /linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/hypes.json: -------------------------------------------------------------------------------- 1 | { 2 | "deconv": false, 3 | "lstm_size": 500, 4 | "later_feat_channels": 832, 5 | "slim_basename": "InceptionV1", 6 | "image_width": 640, 7 | "rezoom_h_coords": [ 8 | -0.25, 9 | 0.25 10 | ], 11 | "grid_width": 20, 12 | "exp_name": "overfeat_rezoom", 13 | "use_lstm": false, 14 | "region_size": 32, 15 | "num_lstm_layers": 2, 16 | "focus_size": 1.8, 17 | "avg_pool_size": 5, 18 | "early_feat_channels": 256, 19 | "grid_height": 15, 20 | "use_rezoom": true, 21 | "slim_top_lname": "Mixed_5b", 22 | "rezoom_w_coords": [ 23 | -0.25, 24 | 0.25 25 | ], 26 | "rezoom_change_loss": "center", 27 | "batch_size": 1, 28 | "reregress": true, 29 | "data": { 30 | "test_idl": "./data/truckdata/val.json", 31 | "truncate_data": false, 32 | "train_idl": "./data/truckdata/train.json" 33 | }, 34 | "num_classes": 2, 35 | "logging": { 36 | "save_iter": 10000, 37 | "display_iter": 50 38 | }, 39 | "rnn_len": 1, 40 | "solver": { 41 | "opt": "RMS", 42 | "rnd_seed": 1, 43 | "epsilon": 1e-05, 44 | "learning_rate": 0.001, 45 | "use_jitter": false, 46 | "hungarian_iou": 0.25, 47 | "weights": "", 48 | "learning_rate_step": 33000, 49 | "gpu": 0, 50 | "head_weights": [ 51 | 1.0, 52 | 0.1 53 | ] 54 | }, 55 | "clip_norm": 1.0, 56 | "image_height": 480, 57 | "save_dir": "output/overfeat_rezoom_2017_02_09_13.28", 58 | "slim_attention_lname": "Mixed_3b", 59 | "slim_ckpt": "inception_v1.ckpt", 60 | "biggest_box_px": 10000 61 | } -------------------------------------------------------------------------------- /linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/save.ckpt-100000.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/save.ckpt-100000.data-00000-of-00001 -------------------------------------------------------------------------------- /linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/save.ckpt-100000.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/linux/tensorbox/output/overfeat_rezoom_2017_02_09_13.28/save.ckpt-100000.index -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/__init__.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | """ 17 | The goal of PyMouse is to have a cross-platform way to control the mouse. 18 | PyMouse should work on Windows, Mac and any Unix that has xlib. 19 | 20 | PyMouse is a part of PyUserInput, along with PyKeyboard, for more information 21 | about this project, see: 22 | http://github.com/SavinaRoja/PyUserInput 23 | 24 | PyMouse was originally developed by Pepijn de Vos. For the original repository, 25 | see: 26 | https://github.com/pepijndevos/PyMouse 27 | """ 28 | 29 | import sys 30 | 31 | if sys.platform.startswith('java'): 32 | from .java_ import PyMouse 33 | 34 | elif sys.platform == 'darwin': 35 | from .mac import PyMouse, PyMouseEvent 36 | 37 | elif sys.platform == 'win32': 38 | from .windows import PyMouse, PyMouseEvent 39 | 40 | else: 41 | from .x11 import PyMouse, PyMouseEvent 42 | 43 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/base.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | """ 17 | The goal of PyMouse is to have a cross-platform way to control the mouse. 18 | PyMouse should work on Windows, Mac and any Unix that has xlib. 19 | 20 | As the base file, this provides a rough operational model along with the 21 | framework to be extended by each platform. 22 | """ 23 | 24 | from threading import Thread 25 | 26 | 27 | class ScrollSupportError(Exception): 28 | pass 29 | 30 | 31 | class PyMouseMeta(object): 32 | 33 | def press(self, x, y, button=1): 34 | """ 35 | Press the mouse on a given x, y and button. 36 | Button is defined as 1 = left, 2 = right, 3 = middle. 37 | """ 38 | 39 | raise NotImplementedError 40 | 41 | def release(self, x, y, button=1): 42 | """ 43 | Release the mouse on a given x, y and button. 44 | Button is defined as 1 = left, 2 = right, 3 = middle. 45 | """ 46 | 47 | raise NotImplementedError 48 | 49 | def click(self, x, y, button=1, n=1): 50 | """ 51 | Click a mouse button n times on a given x, y. 52 | Button is defined as 1 = left, 2 = right, 3 = middle. 53 | """ 54 | 55 | for i in range(n): 56 | self.press(x, y, button) 57 | self.release(x, y, button) 58 | 59 | def scroll(self, vertical=None, horizontal=None, depth=None): 60 | """ 61 | Generates mouse scrolling events in up to three dimensions: vertical, 62 | horizontal, and depth (Mac-only). Values for these arguments may be 63 | positive or negative numbers (float or int). Refer to the following: 64 | Vertical: + Up, - Down 65 | Horizontal: + Right, - Left 66 | Depth: + Rise (out of display), - Dive (towards display) 67 | 68 | Dynamic scrolling, which is used Windows and Mac platforms, is not 69 | implemented at this time due to an inability to test Mac code. The 70 | events generated by this code will thus be discrete units of scrolling 71 | "lines". The user is advised to take care at all times with scrolling 72 | automation as scrolling event consumption is relatively un-standardized. 73 | 74 | Float values will be coerced to integers. 75 | """ 76 | 77 | raise NotImplementedError 78 | 79 | def move(self, x, y): 80 | """Move the mouse to a given x and y""" 81 | 82 | raise NotImplementedError 83 | 84 | def drag(self, x, y): 85 | """Drag the mouse to a given x and y. 86 | A Drag is a Move where the mouse key is held down.""" 87 | 88 | raise NotImplementedError 89 | 90 | def position(self): 91 | """ 92 | Get the current mouse position in pixels. 93 | Returns a tuple of 2 integers 94 | """ 95 | 96 | raise NotImplementedError 97 | 98 | def screen_size(self): 99 | """ 100 | Get the current screen size in pixels. 101 | Returns a tuple of 2 integers 102 | """ 103 | 104 | raise NotImplementedError 105 | 106 | 107 | class PyMouseEventMeta(Thread): 108 | def __init__(self, capture=False, capture_move=False): 109 | Thread.__init__(self) 110 | self.daemon = True 111 | self.capture = capture 112 | self.capture_move = capture_move 113 | self.state = True 114 | 115 | def stop(self): 116 | self.state = False 117 | 118 | def click(self, x, y, button, press): 119 | """Subclass this method with your click event handler""" 120 | pass 121 | 122 | def move(self, x, y): 123 | """Subclass this method with your move event handler""" 124 | pass 125 | 126 | def scroll(self, x, y, vertical, horizontal): 127 | """ 128 | Subclass this method with your scroll event handler 129 | Vertical: + Up, - Down 130 | Horizontal: + Right, - Left 131 | """ 132 | pass 133 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/java_.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | from java.awt import Robot, Toolkit 17 | from java.awt.event import InputEvent 18 | from java.awt.MouseInfo import getPointerInfo 19 | from .base import PyMouseMeta 20 | 21 | r = Robot() 22 | 23 | class PyMouse(PyMouseMeta): 24 | def press(self, x, y, button = 1): 25 | button_list = [None, InputEvent.BUTTON1_MASK, InputEvent.BUTTON3_MASK, InputEvent.BUTTON2_MASK] 26 | self.move(x, y) 27 | r.mousePress(button_list[button]) 28 | 29 | def release(self, x, y, button = 1): 30 | button_list = [None, InputEvent.BUTTON1_MASK, InputEvent.BUTTON3_MASK, InputEvent.BUTTON2_MASK] 31 | self.move(x, y) 32 | r.mouseRelease(button_list[button]) 33 | 34 | def move(self, x, y): 35 | r.mouseMove(x, y) 36 | 37 | def position(self): 38 | loc = getPointerInfo().getLocation() 39 | return loc.getX, loc.getY 40 | 41 | def screen_size(self): 42 | dim = Toolkit.getDefaultToolkit().getScreenSize() 43 | return dim.getWidth(), dim.getHeight() 44 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/mac.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | import Quartz 17 | from AppKit import NSEvent, NSScreen 18 | from .base import PyMouseMeta, PyMouseEventMeta 19 | 20 | pressID = [None, Quartz.kCGEventLeftMouseDown, 21 | Quartz.kCGEventRightMouseDown, Quartz.kCGEventOtherMouseDown] 22 | releaseID = [None, Quartz.kCGEventLeftMouseUp, 23 | Quartz.kCGEventRightMouseUp, Quartz.kCGEventOtherMouseUp] 24 | 25 | 26 | class PyMouse(PyMouseMeta): 27 | 28 | def press(self, x, y, button=1): 29 | event = Quartz.CGEventCreateMouseEvent(None, 30 | pressID[button], 31 | (x, y), 32 | button - 1) 33 | Quartz.CGEventPost(Quartz.kCGHIDEventTap, event) 34 | 35 | def release(self, x, y, button=1): 36 | event = Quartz.CGEventCreateMouseEvent(None, 37 | releaseID[button], 38 | (x, y), 39 | button - 1) 40 | Quartz.CGEventPost(Quartz.kCGHIDEventTap, event) 41 | 42 | def move(self, x, y): 43 | move = Quartz.CGEventCreateMouseEvent(None, Quartz.kCGEventMouseMoved, (x, y), 0) 44 | Quartz.CGEventPost(Quartz.kCGHIDEventTap, move) 45 | 46 | def drag(self, x, y): 47 | drag = Quartz.CGEventCreateMouseEvent(None, Quartz.kCGEventLeftMouseDragged, (x, y), 0) 48 | Quartz.CGEventPost(Quartz.kCGHIDEventTap, drag) 49 | 50 | def position(self): 51 | loc = NSEvent.mouseLocation() 52 | return loc.x, Quartz.CGDisplayPixelsHigh(0) - loc.y 53 | 54 | def screen_size(self): 55 | return NSScreen.mainScreen().frame().size.width, NSScreen.mainScreen().frame().size.height 56 | 57 | def scroll(self, vertical=None, horizontal=None, depth=None): 58 | #Local submethod for generating Mac scroll events in one axis at a time 59 | def scroll_event(y_move=0, x_move=0, z_move=0, n=1): 60 | for _ in range(abs(n)): 61 | scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent( 62 | None, # No source 63 | Quartz.kCGScrollEventUnitLine, # Unit of measurement is lines 64 | 3, # Number of wheels(dimensions) 65 | y_move, 66 | x_move, 67 | z_move) 68 | Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent) 69 | 70 | #Execute vertical then horizontal then depth scrolling events 71 | if vertical is not None: 72 | vertical = int(vertical) 73 | if vertical == 0: # Do nothing with 0 distance 74 | pass 75 | elif vertical > 0: # Scroll up if positive 76 | scroll_event(y_move=1, n=vertical) 77 | else: # Scroll down if negative 78 | scroll_event(y_move=-1, n=abs(vertical)) 79 | if horizontal is not None: 80 | horizontal = int(horizontal) 81 | if horizontal == 0: # Do nothing with 0 distance 82 | pass 83 | elif horizontal > 0: # Scroll right if positive 84 | scroll_event(x_move=1, n=horizontal) 85 | else: # Scroll left if negative 86 | scroll_event(x_move=-1, n=abs(horizontal)) 87 | if depth is not None: 88 | depth = int(depth) 89 | if depth == 0: # Do nothing with 0 distance 90 | pass 91 | elif vertical > 0: # Scroll "out" if positive 92 | scroll_event(z_move=1, n=depth) 93 | else: # Scroll "in" if negative 94 | scroll_event(z_move=-1, n=abs(depth)) 95 | 96 | 97 | class PyMouseEvent(PyMouseEventMeta): 98 | def run(self): 99 | tap = Quartz.CGEventTapCreate( 100 | Quartz.kCGSessionEventTap, 101 | Quartz.kCGHeadInsertEventTap, 102 | Quartz.kCGEventTapOptionDefault, 103 | Quartz.CGEventMaskBit(Quartz.kCGEventMouseMoved) | 104 | Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseDown) | 105 | Quartz.CGEventMaskBit(Quartz.kCGEventLeftMouseUp) | 106 | Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseDown) | 107 | Quartz.CGEventMaskBit(Quartz.kCGEventRightMouseUp) | 108 | Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseDown) | 109 | Quartz.CGEventMaskBit(Quartz.kCGEventOtherMouseUp), 110 | self.handler, 111 | None) 112 | 113 | loopsource = Quartz.CFMachPortCreateRunLoopSource(None, tap, 0) 114 | loop = Quartz.CFRunLoopGetCurrent() 115 | Quartz.CFRunLoopAddSource(loop, loopsource, Quartz.kCFRunLoopDefaultMode) 116 | Quartz.CGEventTapEnable(tap, True) 117 | 118 | while self.state: 119 | Quartz.CFRunLoopRunInMode(Quartz.kCFRunLoopDefaultMode, 5, False) 120 | 121 | def handler(self, proxy, type, event, refcon): 122 | (x, y) = Quartz.CGEventGetLocation(event) 123 | if type in pressID: 124 | self.click(x, y, pressID.index(type), True) 125 | elif type in releaseID: 126 | self.click(x, y, releaseID.index(type), False) 127 | else: 128 | self.move(x, y) 129 | 130 | if self.capture: 131 | Quartz.CGEventSetType(event, Quartz.kCGEventNull) 132 | 133 | return event 134 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/mir.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/wayland.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/windows.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | from ctypes import * 17 | import win32api 18 | import win32con 19 | from .base import PyMouseMeta, PyMouseEventMeta, ScrollSupportError 20 | import pythoncom 21 | from time import sleep 22 | 23 | class POINT(Structure): 24 | _fields_ = [("x", c_ulong), 25 | ("y", c_ulong)] 26 | 27 | class PyMouse(PyMouseMeta): 28 | """MOUSEEVENTF_(button and action) constants 29 | are defined at win32con, buttonAction is that value""" 30 | 31 | def press(self, x, y, button=1): 32 | buttonAction = 2 ** ((2 * button) - 1) 33 | self.move(x, y) 34 | win32api.mouse_event(buttonAction, x, y) 35 | 36 | def release(self, x, y, button=1): 37 | buttonAction = 2 ** ((2 * button)) 38 | self.move(x, y) 39 | win32api.mouse_event(buttonAction, x, y) 40 | 41 | def scroll(self, vertical=None, horizontal=None, depth=None): 42 | 43 | #Windows supports only vertical and horizontal scrolling 44 | if depth is not None: 45 | raise ScrollSupportError('PyMouse cannot support depth-scrolling \ 46 | in Windows. This feature is only available on Mac.') 47 | 48 | #Execute vertical then horizontal scrolling events 49 | if vertical is not None: 50 | vertical = int(vertical) 51 | if vertical == 0: # Do nothing with 0 distance 52 | pass 53 | elif vertical > 0: # Scroll up if positive 54 | for _ in range(vertical): 55 | win32api.mouse_event(0x0800, 0, 0, 120, 0) 56 | else: # Scroll down if negative 57 | for _ in range(abs(vertical)): 58 | win32api.mouse_event(0x0800, 0, 0, -120, 0) 59 | if horizontal is not None: 60 | horizontal = int(horizontal) 61 | if horizontal == 0: # Do nothing with 0 distance 62 | pass 63 | elif horizontal > 0: # Scroll right if positive 64 | for _ in range(horizontal): 65 | win32api.mouse_event(0x01000, 0, 0, 120, 0) 66 | else: # Scroll left if negative 67 | for _ in range(abs(horizontal)): 68 | win32api.mouse_event(0x01000, 0, 0, -120, 0) 69 | 70 | def move(self, x, y): 71 | windll.user32.SetCursorPos(x, y) 72 | 73 | def drag(self, x, y): 74 | self.press(*self.position()) 75 | #self.move(x, y) 76 | self.release(x, y) 77 | 78 | def position(self): 79 | pt = POINT() 80 | windll.user32.GetCursorPos(byref(pt)) 81 | return pt.x, pt.y 82 | 83 | def screen_size(self): 84 | if windll.user32.GetSystemMetrics(80) == 1: 85 | width = windll.user32.GetSystemMetrics(0) 86 | height = windll.user32.GetSystemMetrics(1) 87 | else: 88 | width = windll.user32.GetSystemMetrics(78) 89 | height = windll.user32.GetSystemMetrics(79) 90 | return width, height 91 | 92 | class PyMouseEvent(PyMouseEventMeta): 93 | def __init__(self, capture=False, capture_move=False): 94 | import pyHook 95 | 96 | PyMouseEventMeta.__init__(self, capture=capture, capture_move=capture_move) 97 | self.hm = pyHook.HookManager() 98 | 99 | def run(self): 100 | self.hm.MouseAll = self._action 101 | self.hm.HookMouse() 102 | while self.state: 103 | sleep(0.01) 104 | pythoncom.PumpWaitingMessages() 105 | 106 | def stop(self): 107 | self.hm.UnhookMouse() 108 | self.state = False 109 | 110 | def _action(self, event): 111 | import pyHook 112 | x, y = event.Position 113 | 114 | if event.Message == pyHook.HookConstants.WM_MOUSEMOVE: 115 | self.move(x,y) 116 | return not self.capture_move 117 | 118 | elif event.Message == pyHook.HookConstants.WM_LBUTTONDOWN: 119 | self.click(x, y, 1, True) 120 | elif event.Message == pyHook.HookConstants.WM_LBUTTONUP: 121 | self.click(x, y, 1, False) 122 | elif event.Message == pyHook.HookConstants.WM_RBUTTONDOWN: 123 | self.click(x, y, 2, True) 124 | elif event.Message == pyHook.HookConstants.WM_RBUTTONUP: 125 | self.click(x, y, 2, False) 126 | elif event.Message == pyHook.HookConstants.WM_MBUTTONDOWN: 127 | self.click(x, y, 3, True) 128 | elif event.Message == pyHook.HookConstants.WM_MBUTTONUP: 129 | self.click(x, y, 3, False) 130 | 131 | elif event.Message == pyHook.HookConstants.WM_MOUSEWHEEL: 132 | # event.Wheel is -1 when scrolling down, 1 when scrolling up 133 | self.scroll(x, y, event.Wheel, 0) 134 | 135 | return not self.capture 136 | -------------------------------------------------------------------------------- /linux/tensorbox/pymouse/x11.py: -------------------------------------------------------------------------------- 1 | #Copyright 2013 Paul Barton 2 | # 3 | #This program is free software: you can redistribute it and/or modify 4 | #it under the terms of the GNU General Public License as published by 5 | #the Free Software Foundation, either version 3 of the License, or 6 | #(at your option) any later version. 7 | # 8 | #This program is distributed in the hope that it will be useful, 9 | #but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | #GNU General Public License for more details. 12 | # 13 | #You should have received a copy of the GNU General Public License 14 | #along with this program. If not, see . 15 | 16 | from Xlib.display import Display 17 | from Xlib import X 18 | from Xlib.ext.xtest import fake_input 19 | from Xlib.ext import record 20 | from Xlib.protocol import rq 21 | 22 | from .base import PyMouseMeta, PyMouseEventMeta, ScrollSupportError 23 | 24 | 25 | class X11Error(Exception): 26 | """An error that is thrown at the end of a code block managed by a 27 | :func:`display_manager` if an *X11* error occurred. 28 | """ 29 | pass 30 | 31 | 32 | def display_manager(display): 33 | """Traps *X* errors and raises an :class:``X11Error`` at the end if any 34 | error occurred. 35 | 36 | This handler also ensures that the :class:`Xlib.display.Display` being 37 | managed is sync'd. 38 | 39 | :param Xlib.display.Display display: The *X* display. 40 | 41 | :return: the display 42 | :rtype: Xlib.display.Display 43 | """ 44 | from contextlib import contextmanager 45 | 46 | @contextmanager 47 | def manager(): 48 | errors = [] 49 | 50 | def handler(*args): 51 | errors.append(args) 52 | 53 | old_handler = display.set_error_handler(handler) 54 | yield display 55 | display.sync() 56 | display.set_error_handler(old_handler) 57 | if errors: 58 | raise X11Error(errors) 59 | 60 | return manager() 61 | 62 | 63 | def translate_button_code(button): 64 | # In X11, the button numbers are: 65 | # leftclick=1, middleclick=2, rightclick=3 66 | # For the purposes of the cross-platform interface of PyMouse, we 67 | # invert the button number values of the right and middle buttons 68 | if button in [1, 2, 3]: 69 | return (None, 1, 3, 2)[button] 70 | else: 71 | return button 72 | 73 | def button_code_to_scroll_direction(button): 74 | # scrollup=4, scrolldown=5, scrollleft=6, scrollright=7 75 | return { 76 | 4: (1, 0), 77 | 5: (-1, 0), 78 | 6: (0, 1), 79 | 7: (0, -1), 80 | }[button] 81 | 82 | 83 | class PyMouse(PyMouseMeta): 84 | def __init__(self, display=None): 85 | PyMouseMeta.__init__(self) 86 | self.display = Display(display) 87 | self.display2 = Display(display) 88 | 89 | def press(self, button=1): 90 | #self.move(x, y) 91 | 92 | with display_manager(self.display) as d: 93 | fake_input(d, X.ButtonPress, translate_button_code(button)) 94 | 95 | def release(self, button=1): 96 | #self.move(x, y) 97 | 98 | with display_manager(self.display) as d: 99 | fake_input(d, X.ButtonRelease, translate_button_code(button)) 100 | 101 | def scroll(self, vertical=None, horizontal=None, depth=None): 102 | #Xlib supports only vertical and horizontal scrolling 103 | if depth is not None: 104 | raise ScrollSupportError('PyMouse cannot support depth-scrolling \ 105 | in X11. This feature is only available on Mac.') 106 | 107 | #Execute vertical then horizontal scrolling events 108 | if vertical is not None: 109 | vertical = int(vertical) 110 | if vertical == 0: # Do nothing with 0 distance 111 | pass 112 | elif vertical > 0: # Scroll up if positive 113 | self.click(*self.position(), button=4, n=vertical) 114 | else: # Scroll down if negative 115 | self.click(*self.position(), button=5, n=abs(vertical)) 116 | if horizontal is not None: 117 | horizontal = int(horizontal) 118 | if horizontal == 0: # Do nothing with 0 distance 119 | pass 120 | elif horizontal > 0: # Scroll right if positive 121 | self.click(*self.position(), button=7, n=horizontal) 122 | else: # Scroll left if negative 123 | self.click(*self.position(), button=6, n=abs(horizontal)) 124 | 125 | def move(self, x, y): 126 | if (x, y) != self.position(): 127 | with display_manager(self.display) as d: 128 | fake_input(d, X.MotionNotify, x=x, y=y) 129 | 130 | def drag(self, x, y): 131 | with display_manager(self.display) as d: 132 | fake_input(d, X.ButtonPress, 1) 133 | fake_input(d, X.MotionNotify, x=x, y=y) 134 | fake_input(d, X.ButtonRelease, 1) 135 | 136 | def position(self): 137 | coord = self.display.screen().root.query_pointer()._data 138 | return coord["root_x"], coord["root_y"] 139 | 140 | def screen_size(self): 141 | width = self.display.screen().width_in_pixels 142 | height = self.display.screen().height_in_pixels 143 | return width, height 144 | 145 | 146 | class PyMouseEvent(PyMouseEventMeta): 147 | def __init__(self, capture=False, capture_move=False, display=None): 148 | PyMouseEventMeta.__init__(self, 149 | capture=capture, 150 | capture_move=capture_move) 151 | self.display = Display(display) 152 | self.display2 = Display(display) 153 | self.ctx = self.display2.record_create_context( 154 | 0, 155 | [record.AllClients], 156 | [{ 157 | 'core_requests': (0, 0), 158 | 'core_replies': (0, 0), 159 | 'ext_requests': (0, 0, 0, 0), 160 | 'ext_replies': (0, 0, 0, 0), 161 | 'delivered_events': (0, 0), 162 | 'device_events': (X.ButtonPressMask, X.ButtonReleaseMask), 163 | 'errors': (0, 0), 164 | 'client_started': False, 165 | 'client_died': False, 166 | }]) 167 | 168 | def run(self): 169 | try: 170 | if self.capture and self.capture_move: 171 | capturing = X.ButtonPressMask | X.ButtonReleaseMask | X.PointerMotionMask 172 | elif self.capture: 173 | capturing = X.ButtonPressMask | X.ButtonReleaseMask 174 | elif self.capture_move: 175 | capturing = X.PointerMotionMask 176 | else: 177 | capturing = False 178 | 179 | if capturing: 180 | self.display2.screen().root.grab_pointer(True, 181 | capturing, 182 | X.GrabModeAsync, 183 | X.GrabModeAsync, 184 | 0, 0, X.CurrentTime) 185 | self.display.screen().root.grab_pointer(True, 186 | capturing, 187 | X.GrabModeAsync, 188 | X.GrabModeAsync, 189 | 0, 0, X.CurrentTime) 190 | 191 | self.display2.record_enable_context(self.ctx, self.handler) 192 | self.display2.record_free_context(self.ctx) 193 | except KeyboardInterrupt: 194 | self.stop() 195 | 196 | def stop(self): 197 | self.state = False 198 | with display_manager(self.display) as d: 199 | d.ungrab_pointer(X.CurrentTime) 200 | d.record_disable_context(self.ctx) 201 | with display_manager(self.display2) as d: 202 | d.ungrab_pointer(X.CurrentTime) 203 | d.record_disable_context(self.ctx) 204 | 205 | def handler(self, reply): 206 | data = reply.data 207 | while len(data): 208 | event, data = rq.EventField(None).parse_binary_value(data, self.display.display, None, None) 209 | 210 | if event.detail in [4, 5, 6, 7]: 211 | if event.type == X.ButtonPress: 212 | self.scroll(event.root_x, event.root_y, *button_code_to_scroll_direction(event.detail)) 213 | elif event.type == X.ButtonPress: 214 | self.click(event.root_x, event.root_y, translate_button_code(event.detail), True) 215 | elif event.type == X.ButtonRelease: 216 | self.click(event.root_x, event.root_y, translate_button_code(event.detail), False) 217 | else: 218 | self.move(event.root_x, event.root_y) 219 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/bash 2 | 3 | .PHONY: all 4 | all: 5 | pip install runcython 6 | makecython++ stitch_wrapper.pyx "" "stitch_rects.cpp ./hungarian/hungarian.cpp" 7 | 8 | hungarian: hungarian/hungarian.so 9 | 10 | hungarian/hungarian.so: 11 | cd hungarian && \ 12 | TF_INC=$$(python -c 'import tensorflow as tf; print(tf.sysconfig.get_include())') && \ 13 | if [ `uname` == Darwin ];\ 14 | then g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I -D_GLIBCXX_USE_CXX11_ABI=0$$TF_INC;\ 15 | else g++ -std=c++11 -shared hungarian.cc -o hungarian.so -fPIC -I $$TF_INC; fi 16 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | from distutils.version import LooseVersion 4 | 5 | TENSORFLOW_VERSION = LooseVersion(tf.__version__) 6 | 7 | def tf_concat(axis, values, **kwargs): 8 | if TENSORFLOW_VERSION >= LooseVersion('1.0'): 9 | return tf.concat(values, axis, **kwargs) 10 | else: 11 | return tf.concat(axis, values, **kwargs) 12 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/LICENSE_FOR_THIS_FOLDER: -------------------------------------------------------------------------------- 1 | MPII Human Pose Dataset, Version 1.0 2 | Copyright 2015 Max Planck Institute for Informatics 3 | Licensed under the Simplified BSD License 4 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/MatPlotter.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import string 4 | import matplotlib 5 | matplotlib.use('Agg') 6 | from pylab import * 7 | import numpy as np 8 | 9 | class MatPlotter: 10 | fontsize=15 11 | color=0 12 | colors=["r-", "b-", "k-", "c-", "m-", "y-"] 13 | colors+=[x + "-" for x in colors] 14 | colors+=["g-", "g--"] 15 | curFigure=[] 16 | legendNames=[] 17 | fontsizeLegend=14 18 | legendPlace='lower right' 19 | legendborderpad = None 20 | legendlabelsep = None 21 | 22 | 23 | def __init__(self, fontsize=15): 24 | # self.newFigure() 25 | self.fontsize=fontsize 26 | self.fontsizeLegend=fontsize - 1 27 | pass 28 | 29 | def formatLegend(self, newFontSize = 14, newPlace = 'lower right', borderpad = None, labelsep = None): 30 | self.fontsizeLegend=newFontSize 31 | self.legendPlace=newPlace 32 | self.legendborderpad = borderpad 33 | self.legendlabelsep = labelsep 34 | 35 | def newFigure(self, plotTitle="", fsize=rcParams['figure.figsize']): 36 | return self.newRPCFigure(plotTitle, fsize) 37 | 38 | def newRPCFigure(self, plotTitle="", fsize=rcParams['figure.figsize']): 39 | curFigure = figure(figsize=fsize) 40 | self.title = title(plotTitle, fontsize=self.fontsize) 41 | #subplots_adjust(left=0.085, right=0.975, top=0.975, bottom=0.085) 42 | subplots_adjust(right=0.975, top=0.975) 43 | 44 | #axis('equal') 45 | axis([0, 1, 0, 1]) 46 | xticklocs, xticklabels = xticks(arange(0, 1.01, 0.1)) 47 | setp(xticklabels, size=self.fontsize) 48 | yticklocs, yticklabels = yticks(arange(0, 1.01, 0.1)) 49 | setp(yticklabels, size=self.fontsize) 50 | self.xlabel = xlabel("1-precision") 51 | self.xlabel.set_size(self.fontsize+2) 52 | self.ylabel = ylabel("recall") 53 | self.ylabel.set_size(self.fontsize+4) 54 | grid() 55 | hold(True) 56 | 57 | def newFPPIFigure(self, plotTitle="", fsize=rcParams['figure.figsize']): 58 | curFigure = figure(figsize=fsize) 59 | self.title = title(plotTitle, fontsize=self.fontsize) 60 | subplots_adjust(left=0.085, right=0.975, top=0.975, bottom=0.085) 61 | 62 | #axis('equal') 63 | axis([0, 100, 0, 1]) 64 | xticklocs, xticklabels = xticks(arange(0, 100.01, 0.5)) 65 | setp(xticklabels, size=self.fontsize) 66 | yticklocs, yticklabels = yticks(arange(0, 1.01, 0.1)) 67 | setp(yticklabels, size=self.fontsize) 68 | self.xlabel = xlabel("false positives per image") 69 | self.xlabel.set_size(self.fontsize+2) 70 | self.ylabel = ylabel("recall") 71 | self.ylabel.set_size(self.fontsize+4) 72 | grid() 73 | hold(True) 74 | 75 | 76 | def newFreqFigure(self, plotTitle="", maxX = 10, maxY = 10,fsize=rcParams['figure.figsize']): 77 | curFigure = figure(figsize=fsize) 78 | self.title = title(plotTitle, fontsize=self.fontsize) 79 | subplots_adjust(left=0.085, right=0.975, top=0.975, bottom=0.1) 80 | #axis('equal') 81 | 82 | axis([0, maxX, 0, maxY]) 83 | xticklocs, xticklabels = xticks(arange(0, maxX + 0.01, maxX * 1.0/ 10)) 84 | setp(xticklabels, size=self.fontsize) 85 | yticklocs, yticklabels = yticks(arange(0, maxY + 0.01, maxY * 1.0/ 10)) 86 | setp(yticklabels, size=self.fontsize) 87 | self.xlabel = xlabel("False positive / ground truth rect") 88 | self.xlabel.set_size(self.fontsize+2) 89 | self.ylabel = ylabel("True positives / ground truth rect") 90 | self.ylabel.set_size(self.fontsize+4) 91 | grid() 92 | hold(True) 93 | 94 | def newFPPWFigure(self, plotTitle="", fsize=rcParams['figure.figsize']): 95 | curFigure = figure(figsize=fsize) 96 | self.title = title(plotTitle, fontsize=self.fontsize) 97 | subplots_adjust(left=0.085, right=0.975, top=0.975, bottom=0.085) 98 | 99 | self.xlabel = xlabel("false positive per windows (FPPW)") 100 | self.xlabel.set_size(self.fontsize+2) 101 | self.ylabel = ylabel("miss rate") 102 | self.ylabel.set_size(self.fontsize+4) 103 | 104 | grid() 105 | hold(True) 106 | 107 | def newLogFPPIFigure(self, plotTitle="", fsize=rcParams['figure.figsize']): 108 | curFigure = figure(figsize=fsize) 109 | self.title = title(plotTitle, fontsize=self.fontsize) 110 | subplots_adjust(left=0.085, right=0.975, top=0.975, bottom=0.1) 111 | 112 | #axis('equal') 113 | 114 | self.xlabel = xlabel("false positives per image") 115 | self.xlabel.set_size(self.fontsize+2) 116 | self.ylabel = ylabel("miss rate") 117 | self.ylabel.set_size(self.fontsize+4) 118 | grid() 119 | hold(True) 120 | 121 | def loadRPCData(self, fname): 122 | self.filename = fname 123 | self.prec=[] 124 | self.rec=[] 125 | self.score=[] 126 | self.fppi=[] 127 | file = open(fname) 128 | 129 | precScores = [] 130 | for i in range(1,10,1): 131 | precScores.append(100 - i * 10) 132 | 133 | fppiScores=[] 134 | for i in range(0, 500, 5): 135 | fppiScores.append(i * 1.0 / 100.0) 136 | 137 | 138 | 139 | precinfo = [] 140 | fppiinfo = [] 141 | eerinfo = [] 142 | logAvInfo = [] 143 | 144 | logAvMR= [] 145 | self.lamr = 0; 146 | self.eer = None; 147 | firstLine = True 148 | leadingZeroCount = 0 149 | 150 | for line in file.readlines(): 151 | vals = line.split() 152 | #vals=line.split(" ") 153 | #for val in vals: 154 | # if val=="": 155 | # vals.remove(val) 156 | self.prec.append(1-float(vals[0])) 157 | self.rec.append(float(vals[1])) 158 | self.score.append(float(vals[2])) 159 | 160 | if(len(vals)>3): 161 | self.fppi.append(float(vals[3])) 162 | if firstLine and not float(vals[3]) == 0: 163 | firstLine = False 164 | 165 | lamrcount = 1 166 | self.lamr = 1 - float(vals[1]) 167 | 168 | lowest_fppi = math.ceil( math.log(float(vals[3]))/ math.log(10) * 10 ) 169 | print "lowest_fppi: ",lowest_fppi; 170 | 171 | # MA: temporarily commented out 172 | # for i in range(lowest_fppi, 1, 1): 173 | # logAvMR.append(10** (i * 1.0 / 10)) 174 | 175 | #self.score.append(float(vals[2][:-1])) 176 | #print 1-self.prec[-1], self.rec[-1], self.score[-1] 177 | if (len(self.prec)>1): 178 | diff = (1-self.prec[-1]-self.rec[-1]) * (1-self.prec[-2]-self.rec[-2]) 179 | if ( diff <0): 180 | eerinfo.append( "EER between: %.03f and %.03f\tScore:%f" % (self.rec[-1], self.rec[-2], self.score[-1])) 181 | self.eer = (self.rec[-1]+self.rec[-2]) * 0.5 182 | if ( diff == 0 and 1-self.prec[-1]-self.rec[-1]==0): 183 | eerinfo.append( "EER: %.03f\tScore:%f" % (self.rec[-1], self.score[-1])) 184 | self.eer = self.rec[-1] 185 | 186 | #Remove already passed precision 187 | if (len(precScores) > 0 and (float(vals[0])) < precScores[0] / 100.0): 188 | precinfo.append("%d percent precision score: %f, recall: %.03f" % (precScores[0], float(vals[2]), float(vals[1]))) 189 | while(len(precScores) > 0 and precScores[0]/100.0 > float(vals[0])): 190 | precScores.pop(0) 191 | 192 | #Remove already passed precision 193 | if(len(vals) > 3): 194 | if (len(fppiScores) > 0 and (float(vals[3])) > fppiScores[0]): 195 | fppiinfo.append("%f fppi score: %f, recall: %.03f" % (fppiScores[0], float(vals[2]), float(vals[1]))) 196 | while(len(fppiScores) > 0 and fppiScores[0] < float(vals[3])): 197 | fppiScores.pop(0) 198 | 199 | if (len(logAvMR) > 0 and (float(vals[3])) > logAvMR[0]): 200 | while(len(logAvMR) > 0 and logAvMR[0] < float(vals[3])): 201 | logAvInfo.append("%f fppi, miss rate: %.03f, score: %f" % (logAvMR[0], 1-float(vals[1]), float(vals[2])) ) 202 | self.lamr += 1-float(vals[1]) 203 | lamrcount += 1 204 | logAvMR.pop(0) 205 | 206 | lastMR = 1-float(vals[1]) 207 | 208 | 209 | if(len(vals)>3): 210 | for i in logAvMR: 211 | logAvInfo.append("%f fppi, miss rate: %.03f, extended" % (i, lastMR) ) 212 | self.lamr += lastMR 213 | lamrcount += 1 214 | 215 | for i in precinfo: 216 | print i; 217 | print; 218 | for i in fppiinfo: 219 | print i; 220 | print 221 | for i in eerinfo: 222 | print i; 223 | print 224 | print "Recall at first false positive: %.03f" % self.rec[0] 225 | if(len(vals)>3): 226 | print 227 | for i in logAvInfo: 228 | print i; 229 | self.lamr = self.lamr * 1.0 / lamrcount 230 | print "Log average miss rate in [10^%.01f, 10^0]: %.03f" % (lowest_fppi / 10.0, self.lamr ) 231 | 232 | 233 | 234 | print; print 235 | file.close() 236 | 237 | def loadFreqData(self, fname): 238 | self.filename = fname 239 | self.prec=[] 240 | self.rec=[] 241 | self.score=[] 242 | file = open(fname) 243 | 244 | for line in file.readlines(): 245 | vals = line.split() 246 | 247 | self.prec.append(float(vals[0])) 248 | self.rec.append(float(vals[1])) 249 | self.score.append(float(vals[2])) 250 | 251 | file.close() 252 | 253 | def loadFPPWData(self, fname): 254 | self.loadFreqData(fname) 255 | 256 | def finishPlot(self, axlimits = [0,1.0,0,1.0]): 257 | # MA: 258 | #self.legend = legend(self.legendNames, self.legendPlace, pad = self.legendborderpad, labelsep = self.legendlabelsep) 259 | self.legend = legend(self.legendNames, self.legendPlace) 260 | 261 | lstrings = self.legend.get_texts() 262 | setp(lstrings, fontsize=self.fontsizeLegend) 263 | #line= plot( [1 - axlimits[0], 0], [axlimits[3], 1 - axlimits[3] ] , 'k') 264 | line= plot( [1, 0], [0, 1] , 'k') 265 | 266 | def finishFreqPlot(self): 267 | self.legend = legend(self.legendNames, self.legendPlace, pad = self.legendborderpad, labelsep = self.legendlabelsep) 268 | lstrings = self.legend.get_texts() 269 | setp(lstrings, fontsize=self.fontsizeLegend) 270 | 271 | 272 | def show(self, plotEER = True, axlimits = [0,1.0,0,1.0]): 273 | if (plotEER): 274 | self.finishPlot(axlimits) 275 | axis(axlimits) 276 | else: 277 | self.finishFreqPlot() 278 | 279 | show() 280 | 281 | def saveCurrentFigure(self, plotEER, filename, axlimits = [0,1.0,0,1.0]): 282 | if (plotEER): 283 | self.finishPlot(axlimits) 284 | axis(axlimits) 285 | else: 286 | self.finishFreqPlot() 287 | 288 | print "Saving: " + filename 289 | savefig(filename) 290 | 291 | def plotRFP(self, numImages, fname, line="r-"): 292 | print 'NOT YET IMPLEMENTED' 293 | 294 | def plotRPC(self, fname, descr="line", style="-1", axlimits = [0,1.0,0,1.0], linewidth = 2, dashstyle = [], addEER = False ): 295 | self.loadRPCData(fname) 296 | 297 | #axis(axlimits); 298 | if (style=="-1"): 299 | if dashstyle != []: 300 | line = plot(self.prec, self.rec, self.colors[self.color], dashes = dashstyle) 301 | else: 302 | line = plot(self.prec, self.rec, self.colors[self.color]) 303 | self.color=self.color+1 304 | self.color=self.color % len(self.colors) 305 | else: 306 | if dashstyle != []: 307 | line = plot(self.prec, self.rec, style, dashes = dashstyle) 308 | else: 309 | line = plot(self.prec, self.rec, style) 310 | 311 | axis(axlimits) 312 | 313 | if addEER and self.eer != None: 314 | descr += " (%.01f%%)" % (self.eer * 100) 315 | 316 | setp(line, 'linewidth', linewidth) 317 | self.legendNames= self.legendNames+[descr] 318 | 319 | def plotFPPI(self, fname, descr="line", style="-1", axlimits = [0,2,0,1], linewidth = 2, dashstyle = []): 320 | self.loadRPCData(fname) 321 | 322 | if (style=="-1"): 323 | if dashstyle != []: 324 | line = plot(self.fppi, self.rec, self.colors[self.color], dashes = dashstyle) 325 | else: 326 | line = plot(self.fppi, self.rec, self.colors[self.color]) 327 | self.color=self.color+1 328 | self.color=self.color % len(self.colors) 329 | else: 330 | if dashstyle != []: 331 | line = plot(self.fppi, self.rec, style, dashes = dashstyle) 332 | else: 333 | line = plot(self.fppi, self.rec, style) 334 | 335 | axis(axlimits); 336 | 337 | setp(line, 'linewidth', linewidth) 338 | self.legendNames= self.legendNames+[descr] 339 | 340 | 341 | def plotFreq(self, fname, descr="line", style="-1", linewidth = 2, dashstyle = []): 342 | self.loadFreqData(fname) 343 | if (style=="-1"): 344 | if dashstyle != []: 345 | line = plot(self.prec, self.rec, self.colors[self.color], dashes = dashstyle) 346 | else: 347 | line = plot(self.prec, self.rec, self.colors[self.color]) 348 | self.color=self.color+1 349 | self.color=self.color % len(self.colors) 350 | else: 351 | if dashstyle != []: 352 | line = plot(self.prec, self.rec, style, dashes = dashstyle) 353 | else: 354 | line = plot(self.prec, self.rec, style) 355 | 356 | 357 | setp(line, 'linewidth', linewidth) 358 | self.legendNames= self.legendNames+[descr] 359 | 360 | def plotFPPW(self, fname, descr="line", style="-1", axlimits = [5e-6, 1e0, 1e-2, 0.5], linewidth = 2, dashstyle = []): 361 | self.loadFPPWData(fname) 362 | if (style=="-1"): 363 | if dashstyle != []: 364 | line = loglog(self.prec, self.rec, self.colors[self.color], dashes = dashstyle) 365 | else: 366 | line = loglog(self.prec, self.rec, self.colors[self.color]) 367 | self.color=self.color+1 368 | self.color=self.color % len(self.colors) 369 | else: 370 | if dashstyle != []: 371 | line = loglog(self.prec, self.rec, style, dashes = dashstyle) 372 | else: 373 | line = loglog(self.prec, self.rec, style) 374 | 375 | xticklocs, xticklabels = xticks([1e-5, 1e-4,1e-3, 1e-2, 1e-1, 1e0]) 376 | setp(xticklabels, size=self.fontsize) 377 | yticklocs, yticklabels = yticks(array([0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5]), 378 | ("0.01", "0.02", "0.03", "0.04", "0.05", "0.06", "0.07", "0.08","0.09", "0.1", "0.2", "0.3", "0.4", "0.5")) 379 | setp(yticklabels, size=self.fontsize) 380 | 381 | axis(axlimits) 382 | 383 | gca().yaxis.grid(True, 'minor') 384 | setp(line, 'linewidth', linewidth) 385 | 386 | self.legendNames= self.legendNames+[descr] 387 | 388 | def plotLogFPPI(self, fname, descr="line", style="-1", axlimits = [5e-3, 1e1, 1e-1, 1], linewidth = 2, dashstyle = [], addlamr = False): 389 | self.loadRPCData(fname) 390 | if (style=="-1"): 391 | if dashstyle != []: 392 | line = loglog(self.fppi, [1 - x for x in self.rec], self.colors[self.color], dashes = dashstyle) 393 | else: 394 | line = loglog(self.fppi, [1 - x for x in self.rec], self.colors[self.color]) 395 | 396 | self.color=(self.color+1) % len(self.colors) 397 | else: 398 | if dashstyle != []: 399 | line = loglog(self.fppi, [1 - x for x in self.rec], style, dashes = dashstyle) 400 | else: 401 | line = loglog(self.fppi, [1 - x for x in self.rec], style) 402 | 403 | gca().yaxis.grid(True, 'minor') 404 | 405 | m = min(self.fppi) 406 | lax = axlimits[0] 407 | for i in self.fppi: 408 | if(i != m): 409 | lax = math.floor(log(i)/math.log(10)) 410 | leftlabel = math.pow(10, lax) 411 | break 412 | 413 | m = max(self.fppi) 414 | rightlabel = math.pow(10, math.ceil(log(m)/math.log(10))) + 0.01 415 | 416 | k = leftlabel 417 | ticks = [k] 418 | while k < rightlabel: 419 | k = k * 10 420 | ticks.append(k) 421 | 422 | xticklocs, xticklabels = xticks(ticks) 423 | setp(xticklabels, size=self.fontsize) 424 | yticklocs, yticklabels = yticks(arange(0.1, 1.01, 0.1), ("0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0")) 425 | setp(yticklabels, size=self.fontsize) 426 | 427 | axlimits[0] = lax 428 | axis(axlimits) 429 | 430 | setp(line, 'linewidth', linewidth) 431 | 432 | if addlamr: 433 | descr += " (%.01f%%)" % (self.lamr * 100) 434 | 435 | self.legendNames= self.legendNames+[descr] 436 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/PalLib.py: -------------------------------------------------------------------------------- 1 | import sys 2 | #import AnnoList_pb2 3 | import AnnotationLib; 4 | 5 | from ma_utils import is_number; 6 | 7 | def loadPal(filename): 8 | _annolist = AnnoList_pb2.AnnoList(); 9 | 10 | f = open(filename, "rb"); 11 | _annolist.ParseFromString(f.read()); 12 | f.close(); 13 | 14 | return _annolist; 15 | 16 | def savePal(filename, _annolist): 17 | f = open(filename, "wb"); 18 | f.write(_annolist.SerializeToString()); 19 | f.close(); 20 | 21 | def al2pal(annotations): 22 | _annolist = AnnoList_pb2.AnnoList(); 23 | 24 | #assert(isinstance(annotations, AnnotationLib.AnnoList)); 25 | 26 | # check type of attributes, add missing attributes 27 | for a in annotations: 28 | for r in a.rects: 29 | for k, v in r.at.iteritems(): 30 | if not k in annotations.attribute_desc: 31 | annotations.add_attribute(k, type(v)); 32 | else: 33 | assert(AnnotationLib.is_compatible_attr_type(annotations.attribute_desc[k].dtype, type(v))); 34 | 35 | # check attributes values 36 | for a in annotations: 37 | for r in a.rects: 38 | for k, v in r.at.iteritems(): 39 | if k in annotations.attribute_val_to_str: 40 | # don't allow undefined values 41 | if not v in annotations.attribute_val_to_str[k]: 42 | print "attribute: {}, undefined value: {}".format(k, v); 43 | assert(False); 44 | 45 | # store attribute descriptions in pal structure 46 | for aname, adesc in annotations.attribute_desc.iteritems(): 47 | _annolist.attribute_desc.extend([adesc]); 48 | 49 | for a in annotations: 50 | _a = _annolist.annotation.add(); 51 | _a.imageName = a.imageName; 52 | 53 | for r in a.rects: 54 | _r = _a.rect.add(); 55 | 56 | _r.x1 = r.x1; 57 | _r.y1 = r.y1; 58 | _r.x2 = r.x2; 59 | _r.y2 = r.y2; 60 | 61 | _r.score = float(r.score); 62 | 63 | if hasattr(r, 'id'): 64 | _r.id = r.id; 65 | 66 | if hasattr(r, 'track_id'): 67 | _r.track_id = r.track_id; 68 | 69 | if hasattr(r, 'at'): 70 | for k, v in r.at.items(): 71 | _at = _r.attribute.add(); 72 | 73 | _at.id = annotations.attribute_desc[k].id; 74 | 75 | if annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_INT32: 76 | assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_INT32, type(v))); 77 | _at.val = int(v); 78 | elif annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_FLOAT: 79 | assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_FLOAT, type(v))); 80 | _at.fval = float(v); 81 | elif annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_STRING: 82 | assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_STRING, type(v))); 83 | _at.strval = str(v); 84 | else: 85 | assert(false); 86 | 87 | return _annolist; 88 | 89 | def pal2al(_annolist): 90 | #annotations = []; 91 | annotations = AnnotationLib.AnnoList(); 92 | 93 | for adesc in _annolist.attribute_desc: 94 | annotations.attribute_desc[adesc.name] = adesc; 95 | print "attribute: ", adesc.name, adesc.id 96 | 97 | for valdesc in adesc.val_to_str: 98 | annotations.add_attribute_val(adesc.name, valdesc.s, valdesc.id); 99 | 100 | attribute_name_from_id = {adesc.id: aname for aname, adesc in annotations.attribute_desc.iteritems()} 101 | attribute_dtype_from_id = {adesc.id: adesc.dtype for aname, adesc in annotations.attribute_desc.iteritems()} 102 | 103 | for _a in _annolist.annotation: 104 | anno = AnnotationLib.Annotation() 105 | 106 | anno.imageName = _a.imageName; 107 | 108 | anno.rects = []; 109 | 110 | for _r in _a.rect: 111 | rect = AnnotationLib.AnnoRect() 112 | 113 | rect.x1 = _r.x1; 114 | rect.x2 = _r.x2; 115 | rect.y1 = _r.y1; 116 | rect.y2 = _r.y2; 117 | 118 | if _r.HasField("id"): 119 | rect.id = _r.id; 120 | 121 | if _r.HasField("track_id"): 122 | rect.track_id = _r.track_id; 123 | 124 | if _r.HasField("score"): 125 | rect.score = _r.score; 126 | 127 | for _at in _r.attribute: 128 | try: 129 | cur_aname = attribute_name_from_id[_at.id]; 130 | cur_dtype = attribute_dtype_from_id[_at.id]; 131 | except KeyError as e: 132 | print "attribute: ", _at.id 133 | print e 134 | assert(False); 135 | 136 | if cur_dtype == AnnotationLib.AnnoList.TYPE_INT32: 137 | rect.at[cur_aname] = _at.val; 138 | elif cur_dtype == AnnotationLib.AnnoList.TYPE_FLOAT: 139 | rect.at[cur_aname] = _at.fval; 140 | elif cur_dtype == AnnotationLib.AnnoList.TYPE_STRING: 141 | rect.at[cur_aname] = _at.strval; 142 | else: 143 | assert(False); 144 | 145 | anno.rects.append(rect); 146 | 147 | annotations.append(anno); 148 | 149 | return annotations; 150 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/linux/tensorbox/utils/annolist/__init__.py -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/ma_utils.py: -------------------------------------------------------------------------------- 1 | def is_number(s): 2 | try: 3 | float(s) 4 | return True 5 | except ValueError: 6 | return False 7 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/annolist/plotSimple.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import os 5 | import random 6 | import re 7 | from AnnotationLib import * 8 | from MatPlotter import * 9 | from optparse import OptionParser 10 | from copy import deepcopy 11 | from math import sqrt 12 | 13 | 14 | def main(argv): 15 | parser = OptionParser(usage="usage: %prog [options] [...]") 16 | parser.add_option("-o", "--output-file", action="store", 17 | dest="output", type="str", help="outfile. mandatory") 18 | parser.add_option("--fppw", action="store_true", dest="fppw", help="False Positives Per Window") 19 | parser.add_option("--colors", action="store", dest="colors", help="colors") 20 | parser.add_option("--fppi", action="store_true", dest="fppi", help="False Positives Per Image") 21 | parser.add_option("--lfppi", action="store_true", dest="lfppi", help="False Positives Per Image(log)") 22 | parser.add_option("-c", "--components", action="store", dest="ncomponents", type="int", help="show n trailing components of the part", default=3) 23 | parser.add_option("--cut-trailing", action="store", dest="cutcomponents", type="int", help="cut n trailing components of the part (applied after --components)", default=-1) 24 | parser.add_option("-t", "--title", action="store", dest="title", type="str", default="") 25 | parser.add_option("-f", "--fontsize", action="store", dest="fontsize", type="int", default=12) 26 | parser.add_option("-l", "--legend'", action="store", dest="legend", type="string", default="lr") 27 | (options, args) = parser.parse_args() 28 | plotter = MatPlotter(options.fontsize) 29 | 30 | position = "lower right" 31 | if(options.legend == "ur"): 32 | position = "upper right" 33 | if(options.legend == "ul"): 34 | position = "upper left" 35 | if(options.legend == "ll"): 36 | position = "lower left" 37 | plotter.formatLegend(options.fontsize, newPlace = position) 38 | 39 | title = options.title 40 | colors = None 41 | if (options.colors): 42 | colors = options.colors.split() 43 | if (options.fppw): 44 | plotter.newFPPWFigure(title) 45 | elif (options.lfppi): 46 | plotter.newLogFPPIFigure(title) 47 | elif (options.fppi): 48 | plotter.newFPPIFigure(title) 49 | else: 50 | plotter.newFigure(title) 51 | 52 | for i, filename in enumerate(args): 53 | if (os.path.isdir(filename)): 54 | filename = os.path.join(filename, "rpc", "result-minh-48") 55 | displayname = filename 56 | if (options.ncomponents > 0): 57 | suffix = None 58 | for idx in xrange(options.ncomponents): 59 | displayname, last = os.path.split(displayname) 60 | if (suffix): 61 | suffix = os.path.join(last, suffix) 62 | else: 63 | suffix = last 64 | displayname = suffix 65 | if (options.cutcomponents > 0): 66 | for idx in xrange(options.cutcomponents): 67 | displayname, last = os.path.split(displayname) 68 | # plusidx = displayname.index("+") 69 | # displayname = displayname[plusidx:] 70 | print "Plotting: "+displayname 71 | if (options.fppw): 72 | plotter.plotFPPW(filename, displayname) 73 | elif (options.lfppi): 74 | if colors: 75 | plotter.plotLogFPPI(filename, displayname, colors[i]) 76 | else: 77 | plotter.plotLogFPPI(filename, displayname) 78 | elif (options.fppi): 79 | plotter.plotFPPI(filename, displayname) 80 | else: 81 | plotter.plotRPC(filename, displayname) 82 | 83 | plotLine = not (options.fppw or options.lfppi or options.fppi); 84 | 85 | if (options.output is None): 86 | plotter.show(plotLine) 87 | else: 88 | plotter.saveCurrentFigure(plotLine, options.output) 89 | return 0 90 | 91 | if __name__ == "__main__": 92 | sys.exit(main(sys.argv)) 93 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/data_utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import re 4 | import sys 5 | import argparse 6 | import numpy as np 7 | import copy 8 | import annolist.AnnotationLib as al 9 | 10 | def annotation_to_h5(H, a, cell_width, cell_height, max_len): 11 | region_size = H['region_size'] 12 | assert H['region_size'] == H['image_height'] / H['grid_height'] 13 | assert H['region_size'] == H['image_width'] / H['grid_width'] 14 | cell_regions = get_cell_grid(cell_width, cell_height, region_size) 15 | 16 | cells_per_image = len(cell_regions) 17 | 18 | box_list = [[] for idx in range(cells_per_image)] 19 | 20 | for cidx, c in enumerate(cell_regions): 21 | box_list[cidx] = [r for r in a.rects if all(r.intersection(c))] 22 | 23 | boxes = np.zeros((1, cells_per_image, 4, max_len, 1), dtype = np.float) 24 | box_flags = np.zeros((1, cells_per_image, 1, max_len, 1), dtype = np.float) 25 | 26 | for cidx in xrange(cells_per_image): 27 | #assert(cur_num_boxes <= max_len) 28 | 29 | cell_ox = 0.5 * (cell_regions[cidx].x1 + cell_regions[cidx].x2) 30 | cell_oy = 0.5 * (cell_regions[cidx].y1 + cell_regions[cidx].y2) 31 | 32 | unsorted_boxes = [] 33 | for bidx in xrange(min(len(box_list[cidx]), max_len)): 34 | 35 | # relative box position with respect to cell 36 | ox = 0.5 * (box_list[cidx][bidx].x1 + box_list[cidx][bidx].x2) - cell_ox 37 | oy = 0.5 * (box_list[cidx][bidx].y1 + box_list[cidx][bidx].y2) - cell_oy 38 | 39 | width = abs(box_list[cidx][bidx].x2 - box_list[cidx][bidx].x1) 40 | height= abs(box_list[cidx][bidx].y2 - box_list[cidx][bidx].y1) 41 | 42 | if (abs(ox) < H['focus_size'] * region_size and abs(oy) < H['focus_size'] * region_size and 43 | width < H['biggest_box_px'] and height < H['biggest_box_px']): 44 | unsorted_boxes.append(np.array([ox, oy, width, height], dtype=np.float)) 45 | 46 | for bidx, box in enumerate(sorted(unsorted_boxes, key=lambda x: x[0]**2 + x[1]**2)): 47 | boxes[0, cidx, :, bidx, 0] = box 48 | box_flags[0, cidx, 0, bidx, 0] = max(box_list[cidx][bidx].silhouetteID, 1) 49 | 50 | return boxes, box_flags 51 | 52 | def get_cell_grid(cell_width, cell_height, region_size): 53 | 54 | cell_regions = [] 55 | for iy in xrange(cell_height): 56 | for ix in xrange(cell_width): 57 | cidx = iy * cell_width + ix 58 | ox = (ix + 0.5) * region_size 59 | oy = (iy + 0.5) * region_size 60 | 61 | r = al.AnnoRect(ox - 0.5 * region_size, oy - 0.5 * region_size, 62 | ox + 0.5 * region_size, oy + 0.5 * region_size) 63 | r.track_id = cidx 64 | 65 | cell_regions.append(r) 66 | 67 | 68 | return cell_regions 69 | 70 | def annotation_jitter(I, a_in, min_box_width=20, jitter_scale_min=0.9, jitter_scale_max=1.1, jitter_offset=16, target_width=640, target_height=480): 71 | a = copy.deepcopy(a_in) 72 | 73 | # MA: sanity check 74 | new_rects = [] 75 | for i in range(len(a.rects)): 76 | r = a.rects[i] 77 | try: 78 | assert(r.x1 < r.x2 and r.y1 < r.y2) 79 | new_rects.append(r) 80 | except: 81 | print('bad rectangle') 82 | a.rects = new_rects 83 | 84 | 85 | if a.rects: 86 | cur_min_box_width = min([r.width() for r in a.rects]) 87 | else: 88 | cur_min_box_width = min_box_width / jitter_scale_min 89 | 90 | # don't downscale below min_box_width 91 | jitter_scale_min = max(jitter_scale_min, float(min_box_width) / cur_min_box_width) 92 | 93 | # it's always ok to upscale 94 | jitter_scale_min = min(jitter_scale_min, 1.0) 95 | 96 | jitter_scale_max = jitter_scale_max 97 | 98 | jitter_scale = np.random.uniform(jitter_scale_min, jitter_scale_max) 99 | 100 | jitter_flip = np.random.random_integers(0, 1) 101 | 102 | if jitter_flip == 1: 103 | I = np.fliplr(I) 104 | 105 | for r in a: 106 | r.x1 = I.shape[1] - r.x1 107 | r.x2 = I.shape[1] - r.x2 108 | r.x1, r.x2 = r.x2, r.x1 109 | 110 | for p in r.point: 111 | p.x = I.shape[1] - p.x 112 | 113 | I1 = cv2.resize(I, None, fx=jitter_scale, fy=jitter_scale, interpolation = cv2.INTER_CUBIC) 114 | 115 | jitter_offset_x = np.random.random_integers(-jitter_offset, jitter_offset) 116 | jitter_offset_y = np.random.random_integers(-jitter_offset, jitter_offset) 117 | 118 | 119 | 120 | rescaled_width = I1.shape[1] 121 | rescaled_height = I1.shape[0] 122 | 123 | px = round(0.5*(target_width)) - round(0.5*(rescaled_width)) + jitter_offset_x 124 | py = round(0.5*(target_height)) - round(0.5*(rescaled_height)) + jitter_offset_y 125 | 126 | I2 = np.zeros((target_height, target_width, 3), dtype=I1.dtype) 127 | 128 | x1 = max(0, px) 129 | y1 = max(0, py) 130 | x2 = min(rescaled_width, target_width - x1) 131 | y2 = min(rescaled_height, target_height - y1) 132 | 133 | I2[0:(y2 - y1), 0:(x2 - x1), :] = I1[y1:y2, x1:x2, :] 134 | 135 | ox1 = round(0.5*rescaled_width) + jitter_offset_x 136 | oy1 = round(0.5*rescaled_height) + jitter_offset_y 137 | 138 | ox2 = round(0.5*target_width) 139 | oy2 = round(0.5*target_height) 140 | 141 | for r in a: 142 | r.x1 = round(jitter_scale*r.x1 - x1) 143 | r.x2 = round(jitter_scale*r.x2 - x1) 144 | 145 | r.y1 = round(jitter_scale*r.y1 - y1) 146 | r.y2 = round(jitter_scale*r.y2 - y1) 147 | 148 | if r.x1 < 0: 149 | r.x1 = 0 150 | 151 | if r.y1 < 0: 152 | r.y1 = 0 153 | 154 | if r.x2 >= I2.shape[1]: 155 | r.x2 = I2.shape[1] - 1 156 | 157 | if r.y2 >= I2.shape[0]: 158 | r.y2 = I2.shape[0] - 1 159 | 160 | for p in r.point: 161 | p.x = round(jitter_scale*p.x - x1) 162 | p.y = round(jitter_scale*p.y - y1) 163 | 164 | # MA: make sure all points are inside the image 165 | r.point = [p for p in r.point if p.x >=0 and p.y >=0 and p.x < I2.shape[1] and p.y < I2.shape[0]] 166 | 167 | new_rects = [] 168 | for r in a.rects: 169 | if r.x1 <= r.x2 and r.y1 <= r.y2: 170 | new_rects.append(r) 171 | else: 172 | pass 173 | 174 | a.rects = new_rects 175 | 176 | return I2, a 177 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/googlenet_load.py: -------------------------------------------------------------------------------- 1 | from slim_nets import inception_v1 as inception 2 | from slim_nets import resnet_v1 as resnet 3 | import tensorflow.contrib.slim as slim 4 | 5 | def model(x, H, reuse, is_training=True): 6 | if H['slim_basename'] == 'resnet_v1_101': 7 | with slim.arg_scope(resnet.resnet_arg_scope()): 8 | _, T = resnet.resnet_v1_101(x, 9 | is_training=is_training, 10 | num_classes=1000, 11 | reuse=reuse) 12 | elif H['slim_basename'] == 'InceptionV1': 13 | with slim.arg_scope(inception.inception_v1_arg_scope()): 14 | _, T = inception.inception_v1(x, 15 | is_training=is_training, 16 | num_classes=1001, 17 | spatial_squeeze=False, 18 | reuse=reuse) 19 | #print '\n'.join(map(str, [(k, v.op.outputs[0].get_shape()) for k, v in T.iteritems()])) 20 | 21 | coarse_feat = T[H['slim_top_lname']][:, :, :, :H['later_feat_channels']] 22 | assert coarse_feat.op.outputs[0].get_shape()[3] == H['later_feat_channels'] 23 | 24 | # fine feat can be used to reinspect input 25 | attention_lname = H.get('slim_attention_lname', 'Mixed_3b') 26 | early_feat = T[attention_lname] 27 | 28 | return coarse_feat, early_feat 29 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/hungarian/hungarian.cpp: -------------------------------------------------------------------------------- 1 | /******************************************************************** 2 | ******************************************************************** 3 | ** 4 | ** libhungarian by Cyrill Stachniss, 2004 5 | ** 6 | ** 7 | ** Solving the Minimum Assignment Problem using the 8 | ** Hungarian Method. 9 | ** 10 | ** ** This file may be freely copied and distributed! ** 11 | ** 12 | ** Parts of the used code was originally provided by the 13 | ** "Stanford GraphGase", but I made changes to this code. 14 | ** As asked by the copyright node of the "Stanford GraphGase", 15 | ** I hereby proclaim that this file are *NOT* part of the 16 | ** "Stanford GraphGase" distrubition! 17 | ** 18 | ** This file is distributed in the hope that it will be useful, 19 | ** but WITHOUT ANY WARRANTY; without even the implied 20 | ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 21 | ** PURPOSE. 22 | ** 23 | ******************************************************************** 24 | ********************************************************************/ 25 | 26 | 27 | #include 28 | #include 29 | #include "hungarian.hpp" 30 | 31 | #define INF (0x7FFFFFFF) 32 | #define verbose (0) 33 | 34 | #define hungarian_test_alloc(X) do {if ((void *)(X) == NULL) fprintf(stderr, "Out of memory in %s, (%s, line %d).\n", __FUNCTION__, __FILE__, __LINE__); } while (0) 35 | 36 | int** array_to_matrix(int* m, int rows, int cols) { 37 | int i,j; 38 | int** r; 39 | r = (int**)calloc(rows,sizeof(int*)); 40 | for(i=0;iassignment, p->num_rows, p->num_cols) ; 64 | } 65 | 66 | void hungarian_print_costmatrix(hungarian_problem_t* p) { 67 | hungarian_print_matrix(p->cost, p->num_rows, p->num_cols) ; 68 | } 69 | 70 | void hungarian_print_status(hungarian_problem_t* p) { 71 | 72 | fprintf(stderr,"cost:\n"); 73 | hungarian_print_costmatrix(p); 74 | 75 | fprintf(stderr,"assignment:\n"); 76 | hungarian_print_assignment(p); 77 | 78 | } 79 | 80 | int hungarian_imax(int a, int b) { 81 | return (anum_rows = rows; 99 | p->num_cols = cols; 100 | 101 | p->cost = (int**)calloc(rows,sizeof(int*)); 102 | hungarian_test_alloc(p->cost); 103 | p->assignment = (int**)calloc(rows,sizeof(int*)); 104 | hungarian_test_alloc(p->assignment); 105 | 106 | for(i=0; inum_rows; i++) { 107 | p->cost[i] = (int*)calloc(cols,sizeof(int)); 108 | hungarian_test_alloc(p->cost[i]); 109 | p->assignment[i] = (int*)calloc(cols,sizeof(int)); 110 | hungarian_test_alloc(p->assignment[i]); 111 | for(j=0; jnum_cols; j++) { 112 | p->cost[i][j] = (i < org_rows && j < org_cols) ? cost_matrix[i][j] : 0; 113 | p->assignment[i][j] = 0; 114 | 115 | if (max_cost < p->cost[i][j]) 116 | max_cost = p->cost[i][j]; 117 | } 118 | } 119 | 120 | 121 | if (mode == HUNGARIAN_MODE_MAXIMIZE_UTIL) { 122 | for(i=0; inum_rows; i++) { 123 | for(j=0; jnum_cols; j++) { 124 | p->cost[i][j] = max_cost - p->cost[i][j]; 125 | } 126 | } 127 | } 128 | else if (mode == HUNGARIAN_MODE_MINIMIZE_COST) { 129 | // nothing to do 130 | } 131 | else 132 | fprintf(stderr,"%s: unknown mode. Mode was set to HUNGARIAN_MODE_MINIMIZE_COST !\n", __FUNCTION__); 133 | 134 | return rows; 135 | } 136 | 137 | 138 | 139 | 140 | void hungarian_free(hungarian_problem_t* p) { 141 | int i; 142 | for(i=0; inum_rows; i++) { 143 | free(p->cost[i]); 144 | free(p->assignment[i]); 145 | } 146 | free(p->cost); 147 | free(p->assignment); 148 | p->cost = NULL; 149 | p->assignment = NULL; 150 | } 151 | 152 | 153 | 154 | void hungarian_solve(hungarian_problem_t* p) 155 | { 156 | int i, j, m, n, k, l, s, t, q, unmatched, cost; 157 | int* col_mate; 158 | int* row_mate; 159 | int* parent_row; 160 | int* unchosen_row; 161 | int* row_dec; 162 | int* col_inc; 163 | int* slack; 164 | int* slack_row; 165 | 166 | cost=0; 167 | m =p->num_rows; 168 | n =p->num_cols; 169 | 170 | col_mate = (int*)calloc(p->num_rows,sizeof(int)); 171 | hungarian_test_alloc(col_mate); 172 | unchosen_row = (int*)calloc(p->num_rows,sizeof(int)); 173 | hungarian_test_alloc(unchosen_row); 174 | row_dec = (int*)calloc(p->num_rows,sizeof(int)); 175 | hungarian_test_alloc(row_dec); 176 | slack_row = (int*)calloc(p->num_rows,sizeof(int)); 177 | hungarian_test_alloc(slack_row); 178 | 179 | row_mate = (int*)calloc(p->num_cols,sizeof(int)); 180 | hungarian_test_alloc(row_mate); 181 | parent_row = (int*)calloc(p->num_cols,sizeof(int)); 182 | hungarian_test_alloc(parent_row); 183 | col_inc = (int*)calloc(p->num_cols,sizeof(int)); 184 | hungarian_test_alloc(col_inc); 185 | slack = (int*)calloc(p->num_cols,sizeof(int)); 186 | hungarian_test_alloc(slack); 187 | 188 | for (i=0;inum_rows;i++) { 189 | col_mate[i]=0; 190 | unchosen_row[i]=0; 191 | row_dec[i]=0; 192 | slack_row[i]=0; 193 | } 194 | for (j=0;jnum_cols;j++) { 195 | row_mate[j]=0; 196 | parent_row[j] = 0; 197 | col_inc[j]=0; 198 | slack[j]=0; 199 | } 200 | 201 | for (i=0;inum_rows;++i) 202 | for (j=0;jnum_cols;++j) 203 | p->assignment[i][j]=HUNGARIAN_NOT_ASSIGNED; 204 | 205 | // Begin subtract column minima in order to start with lots of zeroes 12 206 | if (verbose) 207 | fprintf(stderr, "Using heuristic\n"); 208 | for (l=0;lcost[0][l]; 211 | for (k=1;kcost[k][l]cost[k][l]; 214 | cost+=s; 215 | if (s!=0) 216 | for (k=0;kcost[k][l]-=s; 218 | } 219 | // End subtract column minima in order to start with lots of zeroes 12 220 | 221 | // Begin initial state 16 222 | t=0; 223 | for (l=0;lcost[k][0]; 233 | for (l=1;lcost[k][l]cost[k][l]; 236 | row_dec[k]=s; 237 | for (l=0;lcost[k][l] && row_mate[l]<0) 239 | { 240 | col_mate[k]=l; 241 | row_mate[l]=k; 242 | if (verbose) 243 | fprintf(stderr, "matching col %d==row %d\n",l,k); 244 | goto row_done; 245 | } 246 | col_mate[k]= -1; 247 | if (verbose) 248 | fprintf(stderr, "node %d: unmatched row %d\n",t,k); 249 | unchosen_row[t++]=k; 250 | row_done: 251 | ; 252 | } 253 | // End initial state 16 254 | 255 | // Begin Hungarian algorithm 18 256 | if (t==0) 257 | goto done; 258 | unmatched=t; 259 | while (1) 260 | { 261 | if (verbose) 262 | fprintf(stderr, "Matched %d rows.\n",m-t); 263 | q=0; 264 | while (1) 265 | { 266 | while (qcost[k][l]-s+col_inc[l]; 277 | if (delcost[k][l]cost[k][l]!=row_dec[k]-col_inc[l]) 388 | exit(0); 389 | } 390 | k=0; 391 | for (l=0;lm) 395 | exit(0); 396 | // End doublecheck the solution 23 397 | // End Hungarian algorithm 18 398 | 399 | for (i=0;iassignment[i][col_mate[i]]=HUNGARIAN_ASSIGNED; 402 | /*TRACE("%d - %d\n", i, col_mate[i]);*/ 403 | } 404 | for (k=0;kcost[k][l]-row_dec[k]+col_inc[l]);*/ 409 | p->cost[k][l]=p->cost[k][l]-row_dec[k]+col_inc[l]; 410 | } 411 | /*TRACE("\n");*/ 412 | } 413 | for (i=0;i (self.width + other.width) / 1.5: 11 | return False 12 | elif abs(self.cy - other.cy) > (self.height + other.height) / 2.0: 13 | return False 14 | else: 15 | return True 16 | def distance(self, other): 17 | return sum(map(abs, [self.cx - other.cx, self.cy - other.cy, 18 | self.width - other.width, self.height - other.height])) 19 | def intersection(self, other): 20 | left = max(self.cx - self.width/2., other.cx - other.width/2.) 21 | right = min(self.cx + self.width/2., other.cx + other.width/2.) 22 | width = max(right - left, 0) 23 | top = max(self.cy - self.height/2., other.cy - other.height/2.) 24 | bottom = min(self.cy + self.height/2., other.cy + other.height/2.) 25 | height = max(bottom - top, 0) 26 | return width * height 27 | def area(self): 28 | return self.height * self.width 29 | def union(self, other): 30 | return self.area() + other.area() - self.intersection(other) 31 | def iou(self, other): 32 | return self.intersection(other) / self.union(other) 33 | def __eq__(self, other): 34 | return (self.cx == other.cx and 35 | self.cy == other.cy and 36 | self.width == other.width and 37 | self.height == other.height and 38 | self.confidence == other.confidence) 39 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/slim_nets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/linux/tensorbox/utils/slim_nets/__init__.py -------------------------------------------------------------------------------- /linux/tensorbox/utils/slim_nets/resnet_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains building blocks for various versions of Residual Networks. 16 | 17 | Residual networks (ResNets) were proposed in: 18 | Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun 19 | Deep Residual Learning for Image Recognition. arXiv:1512.03385, 2015 20 | 21 | More variants were introduced in: 22 | Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun 23 | Identity Mappings in Deep Residual Networks. arXiv: 1603.05027, 2016 24 | 25 | We can obtain different ResNet variants by changing the network depth, width, 26 | and form of residual unit. This module implements the infrastructure for 27 | building them. Concrete ResNet units and full ResNet networks are implemented in 28 | the accompanying resnet_v1.py and resnet_v2.py modules. 29 | 30 | Compared to https://github.com/KaimingHe/deep-residual-networks, in the current 31 | implementation we subsample the output activations in the last residual unit of 32 | each block, instead of subsampling the input activations in the first residual 33 | unit of each block. The two implementations give identical results but our 34 | implementation is more memory efficient. 35 | """ 36 | 37 | from __future__ import absolute_import 38 | from __future__ import division 39 | from __future__ import print_function 40 | 41 | import collections 42 | 43 | from tensorflow.contrib import layers as layers_lib 44 | from tensorflow.contrib.framework.python.ops import add_arg_scope 45 | from tensorflow.contrib.framework.python.ops import arg_scope 46 | from tensorflow.contrib.layers.python.layers import initializers 47 | from tensorflow.contrib.layers.python.layers import layers 48 | from tensorflow.contrib.layers.python.layers import regularizers 49 | from tensorflow.contrib.layers.python.layers import utils 50 | from tensorflow.python.framework import ops 51 | from tensorflow.python.ops import array_ops 52 | from tensorflow.python.ops import nn_ops 53 | from tensorflow.python.ops import variable_scope 54 | 55 | 56 | class Block(collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])): 57 | """A named tuple describing a ResNet block. 58 | 59 | Its parts are: 60 | scope: The scope of the `Block`. 61 | unit_fn: The ResNet unit function which takes as input a `Tensor` and 62 | returns another `Tensor` with the output of the ResNet unit. 63 | args: A list of length equal to the number of units in the `Block`. The list 64 | contains one (depth, depth_bottleneck, stride) tuple for each unit in the 65 | block to serve as argument to unit_fn. 66 | """ 67 | 68 | 69 | def subsample(inputs, factor, scope=None): 70 | """Subsamples the input along the spatial dimensions. 71 | 72 | Args: 73 | inputs: A `Tensor` of size [batch, height_in, width_in, channels]. 74 | factor: The subsampling factor. 75 | scope: Optional variable_scope. 76 | 77 | Returns: 78 | output: A `Tensor` of size [batch, height_out, width_out, channels] with the 79 | input, either intact (if factor == 1) or subsampled (if factor > 1). 80 | """ 81 | if factor == 1: 82 | return inputs 83 | else: 84 | return layers.max_pool2d(inputs, [1, 1], stride=factor, scope=scope) 85 | 86 | 87 | def conv2d_same(inputs, num_outputs, kernel_size, stride, rate=1, scope=None): 88 | """Strided 2-D convolution with 'SAME' padding. 89 | 90 | When stride > 1, then we do explicit zero-padding, followed by conv2d with 91 | 'VALID' padding. 92 | 93 | Note that 94 | 95 | net = conv2d_same(inputs, num_outputs, 3, stride=stride) 96 | 97 | is equivalent to 98 | 99 | net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=1, 100 | padding='SAME') 101 | net = subsample(net, factor=stride) 102 | 103 | whereas 104 | 105 | net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=stride, 106 | padding='SAME') 107 | 108 | is different when the input's height or width is even, which is why we add the 109 | current function. For more details, see ResnetUtilsTest.testConv2DSameEven(). 110 | 111 | Args: 112 | inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. 113 | num_outputs: An integer, the number of output filters. 114 | kernel_size: An int with the kernel_size of the filters. 115 | stride: An integer, the output stride. 116 | rate: An integer, rate for atrous convolution. 117 | scope: Scope. 118 | 119 | Returns: 120 | output: A 4-D tensor of size [batch, height_out, width_out, channels] with 121 | the convolution output. 122 | """ 123 | if stride == 1: 124 | return layers_lib.conv2d( 125 | inputs, 126 | num_outputs, 127 | kernel_size, 128 | stride=1, 129 | rate=rate, 130 | padding='SAME', 131 | scope=scope) 132 | else: 133 | kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) 134 | pad_total = kernel_size_effective - 1 135 | pad_beg = pad_total // 2 136 | pad_end = pad_total - pad_beg 137 | inputs = array_ops.pad( 138 | inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) 139 | return layers_lib.conv2d( 140 | inputs, 141 | num_outputs, 142 | kernel_size, 143 | stride=stride, 144 | rate=rate, 145 | padding='VALID', 146 | scope=scope) 147 | 148 | 149 | @add_arg_scope 150 | def stack_blocks_dense(net, 151 | blocks, 152 | output_stride=None, 153 | outputs_collections=None): 154 | """Stacks ResNet `Blocks` and controls output feature density. 155 | 156 | First, this function creates scopes for the ResNet in the form of 157 | 'block_name/unit_1', 'block_name/unit_2', etc. 158 | 159 | Second, this function allows the user to explicitly control the ResNet 160 | output_stride, which is the ratio of the input to output spatial resolution. 161 | This is useful for dense prediction tasks such as semantic segmentation or 162 | object detection. 163 | 164 | Most ResNets consist of 4 ResNet blocks and subsample the activations by a 165 | factor of 2 when transitioning between consecutive ResNet blocks. This results 166 | to a nominal ResNet output_stride equal to 8. If we set the output_stride to 167 | half the nominal network stride (e.g., output_stride=4), then we compute 168 | responses twice. 169 | 170 | Control of the output feature density is implemented by atrous convolution. 171 | 172 | Args: 173 | net: A `Tensor` of size [batch, height, width, channels]. 174 | blocks: A list of length equal to the number of ResNet `Blocks`. Each 175 | element is a ResNet `Block` object describing the units in the `Block`. 176 | output_stride: If `None`, then the output will be computed at the nominal 177 | network stride. If output_stride is not `None`, it specifies the requested 178 | ratio of input to output spatial resolution, which needs to be equal to 179 | the product of unit strides from the start up to some level of the ResNet. 180 | For example, if the ResNet employs units with strides 1, 2, 1, 3, 4, 1, 181 | then valid values for the output_stride are 1, 2, 6, 24 or None (which 182 | is equivalent to output_stride=24). 183 | outputs_collections: Collection to add the ResNet block outputs. 184 | 185 | Returns: 186 | net: Output tensor with stride equal to the specified output_stride. 187 | 188 | Raises: 189 | ValueError: If the target output_stride is not valid. 190 | """ 191 | # The current_stride variable keeps track of the effective stride of the 192 | # activations. This allows us to invoke atrous convolution whenever applying 193 | # the next residual unit would result in the activations having stride larger 194 | # than the target output_stride. 195 | current_stride = 1 196 | 197 | # The atrous convolution rate parameter. 198 | rate = 1 199 | 200 | for block in blocks: 201 | with variable_scope.variable_scope(block.scope, 'block', [net]) as sc: 202 | for i, unit in enumerate(block.args): 203 | if output_stride is not None and current_stride > output_stride: 204 | raise ValueError('The target output_stride cannot be reached.') 205 | 206 | with variable_scope.variable_scope('unit_%d' % (i + 1), values=[net]): 207 | unit_depth, unit_depth_bottleneck, unit_stride = unit 208 | 209 | # If we have reached the target output_stride, then we need to employ 210 | # atrous convolution with stride=1 and multiply the atrous rate by the 211 | # current unit's stride for use in subsequent layers. 212 | if output_stride is not None and current_stride == output_stride: 213 | net = block.unit_fn( 214 | net, 215 | depth=unit_depth, 216 | depth_bottleneck=unit_depth_bottleneck, 217 | stride=1, 218 | rate=rate) 219 | rate *= unit_stride 220 | 221 | else: 222 | net = block.unit_fn( 223 | net, 224 | depth=unit_depth, 225 | depth_bottleneck=unit_depth_bottleneck, 226 | stride=unit_stride, 227 | rate=1) 228 | current_stride *= unit_stride 229 | net = utils.collect_named_outputs(outputs_collections, sc.name, net) 230 | 231 | if output_stride is not None and current_stride != output_stride: 232 | raise ValueError('The target output_stride cannot be reached.') 233 | 234 | return net 235 | 236 | 237 | def resnet_arg_scope(is_training=True, 238 | weight_decay=0.0001, 239 | batch_norm_decay=0.997, 240 | batch_norm_epsilon=1e-5, 241 | batch_norm_scale=True): 242 | """Defines the default ResNet arg scope. 243 | 244 | TODO(gpapan): The batch-normalization related default values above are 245 | appropriate for use in conjunction with the reference ResNet models 246 | released at https://github.com/KaimingHe/deep-residual-networks. When 247 | training ResNets from scratch, they might need to be tuned. 248 | 249 | Args: 250 | is_training: Whether or not we are training the parameters in the batch 251 | normalization layers of the model. 252 | weight_decay: The weight decay to use for regularizing the model. 253 | batch_norm_decay: The moving average decay when estimating layer activation 254 | statistics in batch normalization. 255 | batch_norm_epsilon: Small constant to prevent division by zero when 256 | normalizing activations by their variance in batch normalization. 257 | batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the 258 | activations in the batch normalization layer. 259 | 260 | Returns: 261 | An `arg_scope` to use for the resnet models. 262 | """ 263 | batch_norm_params = { 264 | 'is_training': is_training, 265 | 'decay': batch_norm_decay, 266 | 'epsilon': batch_norm_epsilon, 267 | 'scale': batch_norm_scale, 268 | 'updates_collections': ops.GraphKeys.UPDATE_OPS, 269 | } 270 | 271 | with arg_scope( 272 | [layers_lib.conv2d], 273 | weights_regularizer=regularizers.l2_regularizer(weight_decay), 274 | weights_initializer=initializers.variance_scaling_initializer(), 275 | activation_fn=nn_ops.relu, 276 | normalizer_fn=layers.batch_norm, 277 | normalizer_params=batch_norm_params): 278 | with arg_scope([layers.batch_norm], **batch_norm_params): 279 | # The following implies padding='SAME' for pool1, which makes feature 280 | # alignment easier for dense prediction tasks. This is also used in 281 | # https://github.com/facebook/fb.resnet.torch. However the accompanying 282 | # code of 'Deep Residual Learning for Image Recognition' uses 283 | # padding='VALID' for pool1. You can switch to that choice by setting 284 | # tf.contrib.framework.arg_scope([tf.contrib.layers.max_pool2d], padding='VALID'). 285 | with arg_scope([layers.max_pool2d], padding='SAME') as arg_sc: 286 | return arg_sc 287 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/slim_nets/resnet_v1.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Contains definitions for the original form of Residual Networks. 16 | 17 | The 'v1' residual networks (ResNets) implemented in this module were proposed 18 | by: 19 | [1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun 20 | Deep Residual Learning for Image Recognition. arXiv:1512.03385 21 | 22 | Other variants were introduced in: 23 | [2] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun 24 | Identity Mappings in Deep Residual Networks. arXiv: 1603.05027 25 | 26 | The networks defined in this module utilize the bottleneck building block of 27 | [1] with projection shortcuts only for increasing depths. They employ batch 28 | normalization *after* every weight layer. This is the architecture used by 29 | MSRA in the Imagenet and MSCOCO 2016 competition models ResNet-101 and 30 | ResNet-152. See [2; Fig. 1a] for a comparison between the current 'v1' 31 | architecture and the alternative 'v2' architecture of [2] which uses batch 32 | normalization *before* every weight layer in the so-called full pre-activation 33 | units. 34 | 35 | Typical use: 36 | 37 | from tensorflow.contrib.slim.nets import resnet_v1 38 | 39 | ResNet-101 for image classification into 1000 classes: 40 | 41 | # inputs has shape [batch, 224, 224, 3] 42 | with slim.arg_scope(resnet_v1.resnet_arg_scope()): 43 | net, end_points = resnet_v1.resnet_v1_101(inputs, 1000, is_training=False) 44 | 45 | ResNet-101 for semantic segmentation into 21 classes: 46 | 47 | # inputs has shape [batch, 513, 513, 3] 48 | with slim.arg_scope(resnet_v1.resnet_arg_scope()): 49 | net, end_points = resnet_v1.resnet_v1_101(inputs, 50 | 21, 51 | is_training=False, 52 | global_pool=False, 53 | output_stride=16) 54 | """ 55 | from __future__ import absolute_import 56 | from __future__ import division 57 | from __future__ import print_function 58 | 59 | import tensorflow as tf 60 | 61 | from . import resnet_utils 62 | 63 | 64 | resnet_arg_scope = resnet_utils.resnet_arg_scope 65 | slim = tf.contrib.slim 66 | 67 | 68 | @slim.add_arg_scope 69 | def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1, 70 | outputs_collections=None, scope=None): 71 | """Bottleneck residual unit variant with BN after convolutions. 72 | 73 | This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for 74 | its definition. Note that we use here the bottleneck variant which has an 75 | extra bottleneck layer. 76 | 77 | When putting together two consecutive ResNet blocks that use this unit, one 78 | should use stride = 2 in the last unit of the first block. 79 | 80 | Args: 81 | inputs: A tensor of size [batch, height, width, channels]. 82 | depth: The depth of the ResNet unit output. 83 | depth_bottleneck: The depth of the bottleneck layers. 84 | stride: The ResNet unit's stride. Determines the amount of downsampling of 85 | the units output compared to its input. 86 | rate: An integer, rate for atrous convolution. 87 | outputs_collections: Collection to add the ResNet unit output. 88 | scope: Optional variable_scope. 89 | 90 | Returns: 91 | The ResNet unit's output. 92 | """ 93 | with tf.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc: 94 | depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4) 95 | if depth == depth_in: 96 | shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') 97 | else: 98 | shortcut = slim.conv2d(inputs, depth, [1, 1], stride=stride, 99 | activation_fn=None, scope='shortcut') 100 | 101 | residual = slim.conv2d(inputs, depth_bottleneck, [1, 1], stride=1, 102 | scope='conv1') 103 | residual = resnet_utils.conv2d_same(residual, depth_bottleneck, 3, stride, 104 | rate=rate, scope='conv2') 105 | residual = slim.conv2d(residual, depth, [1, 1], stride=1, 106 | activation_fn=None, scope='conv3') 107 | 108 | output = tf.nn.relu(shortcut + residual) 109 | 110 | return slim.utils.collect_named_outputs(outputs_collections, 111 | sc.original_name_scope, 112 | output) 113 | 114 | 115 | def resnet_v1(inputs, 116 | blocks, 117 | num_classes=None, 118 | is_training=True, 119 | global_pool=True, 120 | output_stride=None, 121 | include_root_block=True, 122 | reuse=None, 123 | scope=None): 124 | """Generator for v1 ResNet models. 125 | 126 | This function generates a family of ResNet v1 models. See the resnet_v1_*() 127 | methods for specific model instantiations, obtained by selecting different 128 | block instantiations that produce ResNets of various depths. 129 | 130 | Training for image classification on Imagenet is usually done with [224, 224] 131 | inputs, resulting in [7, 7] feature maps at the output of the last ResNet 132 | block for the ResNets defined in [1] that have nominal stride equal to 32. 133 | However, for dense prediction tasks we advise that one uses inputs with 134 | spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In 135 | this case the feature maps at the ResNet output will have spatial shape 136 | [(height - 1) / output_stride + 1, (width - 1) / output_stride + 1] 137 | and corners exactly aligned with the input image corners, which greatly 138 | facilitates alignment of the features to the image. Using as input [225, 225] 139 | images results in [8, 8] feature maps at the output of the last ResNet block. 140 | 141 | For dense prediction tasks, the ResNet needs to run in fully-convolutional 142 | (FCN) mode and global_pool needs to be set to False. The ResNets in [1, 2] all 143 | have nominal stride equal to 32 and a good choice in FCN mode is to use 144 | output_stride=16 in order to increase the density of the computed features at 145 | small computational and memory overhead, cf. http://arxiv.org/abs/1606.00915. 146 | 147 | Args: 148 | inputs: A tensor of size [batch, height_in, width_in, channels]. 149 | blocks: A list of length equal to the number of ResNet blocks. Each element 150 | is a resnet_utils.Block object describing the units in the block. 151 | num_classes: Number of predicted classes for classification tasks. If None 152 | we return the features before the logit layer. 153 | is_training: whether is training or not. 154 | global_pool: If True, we perform global average pooling before computing the 155 | logits. Set to True for image classification, False for dense prediction. 156 | output_stride: If None, then the output will be computed at the nominal 157 | network stride. If output_stride is not None, it specifies the requested 158 | ratio of input to output spatial resolution. 159 | include_root_block: If True, include the initial convolution followed by 160 | max-pooling, if False excludes it. 161 | reuse: whether or not the network and its variables should be reused. To be 162 | able to reuse 'scope' must be given. 163 | scope: Optional variable_scope. 164 | 165 | Returns: 166 | net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. 167 | If global_pool is False, then height_out and width_out are reduced by a 168 | factor of output_stride compared to the respective height_in and width_in, 169 | else both height_out and width_out equal one. If num_classes is None, then 170 | net is the output of the last ResNet block, potentially after global 171 | average pooling. If num_classes is not None, net contains the pre-softmax 172 | activations. 173 | end_points: A dictionary from components of the network to the corresponding 174 | activation. 175 | 176 | Raises: 177 | ValueError: If the target output_stride is not valid. 178 | """ 179 | with tf.variable_scope(scope, 'resnet_v1', [inputs], reuse=reuse) as sc: 180 | end_points_collection = sc.name + '_end_points' 181 | with slim.arg_scope([slim.conv2d, bottleneck, 182 | resnet_utils.stack_blocks_dense], 183 | outputs_collections=end_points_collection): 184 | with slim.arg_scope([slim.batch_norm], is_training=is_training): 185 | net = inputs 186 | if include_root_block: 187 | if output_stride is not None: 188 | if output_stride % 4 != 0: 189 | raise ValueError('The output_stride needs to be a multiple of 4.') 190 | output_stride /= 4 191 | net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1') 192 | net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1') 193 | net = resnet_utils.stack_blocks_dense(net, blocks, output_stride) 194 | if global_pool: 195 | # Global average pooling. 196 | net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True) 197 | if num_classes is not None: 198 | net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, 199 | normalizer_fn=None, scope='logits') 200 | # Convert end_points_collection into a dictionary of end_points. 201 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 202 | if num_classes is not None: 203 | end_points['predictions'] = slim.softmax(net, scope='predictions') 204 | return net, end_points 205 | resnet_v1.default_image_size = 224 206 | 207 | 208 | def resnet_v1_50(inputs, 209 | num_classes=None, 210 | is_training=True, 211 | global_pool=True, 212 | output_stride=None, 213 | reuse=None, 214 | scope='resnet_v1_50'): 215 | """ResNet-50 model of [1]. See resnet_v1() for arg and return description.""" 216 | blocks = [ 217 | resnet_utils.Block( 218 | 'block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]), 219 | resnet_utils.Block( 220 | 'block2', bottleneck, [(512, 128, 1)] * 3 + [(512, 128, 2)]), 221 | resnet_utils.Block( 222 | 'block3', bottleneck, [(1024, 256, 1)] * 5 + [(1024, 256, 2)]), 223 | resnet_utils.Block( 224 | 'block4', bottleneck, [(2048, 512, 1)] * 3) 225 | ] 226 | return resnet_v1(inputs, blocks, num_classes, is_training, 227 | global_pool=global_pool, output_stride=output_stride, 228 | include_root_block=True, reuse=reuse, scope=scope) 229 | 230 | 231 | def resnet_v1_101(inputs, 232 | num_classes=None, 233 | is_training=True, 234 | global_pool=True, 235 | output_stride=None, 236 | reuse=None, 237 | scope='resnet_v1_101'): 238 | """ResNet-101 model of [1]. See resnet_v1() for arg and return description.""" 239 | blocks = [ 240 | resnet_utils.Block( 241 | 'block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]), 242 | resnet_utils.Block( 243 | 'block2', bottleneck, [(512, 128, 1)] * 3 + [(512, 128, 2)]), 244 | resnet_utils.Block( 245 | 'block3', bottleneck, [(1024, 256, 1)] * 22 + [(1024, 256, 2)]), 246 | resnet_utils.Block( 247 | 'block4', bottleneck, [(2048, 512, 1)] * 3) 248 | ] 249 | return resnet_v1(inputs, blocks, num_classes, is_training, 250 | global_pool=global_pool, output_stride=output_stride, 251 | include_root_block=True, reuse=reuse, scope=scope) 252 | 253 | 254 | def resnet_v1_152(inputs, 255 | num_classes=None, 256 | is_training=True, 257 | global_pool=True, 258 | output_stride=None, 259 | reuse=None, 260 | scope='resnet_v1_152'): 261 | """ResNet-152 model of [1]. See resnet_v1() for arg and return description.""" 262 | blocks = [ 263 | resnet_utils.Block( 264 | 'block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]), 265 | resnet_utils.Block( 266 | 'block2', bottleneck, [(512, 128, 1)] * 7 + [(512, 128, 2)]), 267 | resnet_utils.Block( 268 | 'block3', bottleneck, [(1024, 256, 1)] * 35 + [(1024, 256, 2)]), 269 | resnet_utils.Block( 270 | 'block4', bottleneck, [(2048, 512, 1)] * 3)] 271 | return resnet_v1(inputs, blocks, num_classes, is_training, 272 | global_pool=global_pool, output_stride=output_stride, 273 | include_root_block=True, reuse=reuse, scope=scope) 274 | 275 | 276 | def resnet_v1_200(inputs, 277 | num_classes=None, 278 | is_training=True, 279 | global_pool=True, 280 | output_stride=None, 281 | reuse=None, 282 | scope='resnet_v1_200'): 283 | """ResNet-200 model of [2]. See resnet_v1() for arg and return description.""" 284 | blocks = [ 285 | resnet_utils.Block( 286 | 'block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]), 287 | resnet_utils.Block( 288 | 'block2', bottleneck, [(512, 128, 1)] * 23 + [(512, 128, 2)]), 289 | resnet_utils.Block( 290 | 'block3', bottleneck, [(1024, 256, 1)] * 35 + [(1024, 256, 2)]), 291 | resnet_utils.Block( 292 | 'block4', bottleneck, [(2048, 512, 1)] * 3)] 293 | return resnet_v1(inputs, blocks, num_classes, is_training, 294 | global_pool=global_pool, output_stride=output_stride, 295 | include_root_block=True, reuse=reuse, scope=scope) 296 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/stitch_rects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "./hungarian/hungarian.hpp" 9 | #include "./stitch_rects.hpp" 10 | 11 | using std::vector; 12 | 13 | void filter_rects(const vector > >& all_rects, 14 | vector* stitched_rects, 15 | float threshold, 16 | float max_threshold, 17 | float tau, 18 | float conf_alpha) { 19 | const vector& accepted_rects = *stitched_rects; 20 | for (int i = 0; i < (int)all_rects.size(); ++i) { 21 | for (int j = 0; j < (int)all_rects[0].size(); ++j) { 22 | vector current_rects; 23 | for (int k = 0; k < (int)all_rects[i][j].size(); ++k) { 24 | if (all_rects[i][j][k].confidence_ * conf_alpha > threshold) { 25 | Rect r = Rect(all_rects[i][j][k]); 26 | r.confidence_ *= conf_alpha; 27 | r.true_confidence_ *= conf_alpha; 28 | current_rects.push_back(r); 29 | } 30 | } 31 | 32 | vector relevant_rects; 33 | for (int k = 0; k < (int)accepted_rects.size(); ++k) { 34 | for (int l = 0; l < (int)current_rects.size(); ++l) { 35 | if (accepted_rects[k].overlaps(current_rects[l], tau)) { 36 | relevant_rects.push_back(Rect(accepted_rects[k])); 37 | break; 38 | } 39 | } 40 | } 41 | 42 | if (relevant_rects.size() == 0 || current_rects.size() == 0) { 43 | for (int k = 0; k < (int)current_rects.size(); ++k) { 44 | stitched_rects->push_back(Rect(current_rects[k])); 45 | } 46 | continue; 47 | } 48 | 49 | int num_pred = MAX(current_rects.size(), relevant_rects.size()); 50 | 51 | int int_cost[num_pred * num_pred]; 52 | for (int k = 0; k < num_pred * num_pred; ++k) { int_cost[k] = 0; } 53 | for (int k = 0; k < (int)current_rects.size(); ++k) { 54 | for (int l = 0; l < (int)relevant_rects.size(); ++l) { 55 | int idx = k * num_pred + l; 56 | int cost = 10000; 57 | if (current_rects[k].overlaps(relevant_rects[l], tau)) { 58 | cost -= 1000; 59 | } 60 | cost += (int)(current_rects[k].distance(relevant_rects[l]) / 10.); 61 | int_cost[idx] = cost; 62 | } 63 | } 64 | 65 | std::vector assignment; 66 | 67 | hungarian_problem_t p; 68 | int** m = array_to_matrix(int_cost, num_pred, num_pred); 69 | hungarian_init(&p, m, num_pred, num_pred, HUNGARIAN_MODE_MINIMIZE_COST); 70 | hungarian_solve(&p); 71 | for (int i = 0; i < num_pred; ++i) { 72 | for (int j = 0; j < num_pred; ++j) { 73 | if (p.assignment[i][j] == HUNGARIAN_ASSIGNED) { 74 | assignment.push_back(j); 75 | } 76 | } 77 | } 78 | assert((int)assignment.size() == num_pred); 79 | hungarian_free(&p); 80 | 81 | for (int i = 0; i < num_pred; ++i) { 82 | free(m[i]); 83 | } 84 | free(m); 85 | 86 | vector bad; 87 | for (int k = 0; k < (int)assignment.size(); ++k) { 88 | if (k < (int)current_rects.size() && assignment[k] < (int)relevant_rects.size()) { 89 | Rect& c = current_rects[k]; 90 | Rect& a = relevant_rects[assignment[k]]; 91 | if (c.confidence_ > max_threshold) { 92 | bad.push_back(k); 93 | continue; 94 | } 95 | if (c.overlaps(a, tau)) { 96 | if (c.confidence_ > a.confidence_ && c.iou(a) > 0.7) { 97 | c.true_confidence_ = a.confidence_; 98 | stitched_rects->erase(std::find(stitched_rects->begin(), stitched_rects->end(), a)); 99 | } else { 100 | bad.push_back(k); 101 | } 102 | } 103 | } 104 | } 105 | 106 | for (int k = 0; k < (int)current_rects.size(); ++k) { 107 | bool bad_contains_k = false; 108 | for (int l = 0; l < (int)bad.size(); ++l) { 109 | if (k == bad[l]) { 110 | bad_contains_k = true; 111 | break; 112 | } 113 | } 114 | if (!bad_contains_k) { 115 | stitched_rects->push_back(Rect(current_rects[k])); 116 | } 117 | } 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/stitch_rects.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STITCH_RECTS_HPP 2 | #define STITCH_RECTS_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "./hungarian/hungarian.hpp" 9 | 10 | #define MIN(a,b) (((a)<(b))?(a):(b)) 11 | #define MAX(a,b) (((a)>(b))?(a):(b)) 12 | 13 | using std::vector; 14 | 15 | class Rect { 16 | public: 17 | int cx_; 18 | int cy_; 19 | int width_; 20 | int height_; 21 | float confidence_; 22 | float true_confidence_; 23 | 24 | explicit Rect(int cx, int cy, int width, int height, float confidence) { 25 | cx_ = cx; 26 | cy_ = cy; 27 | width_ = width; 28 | height_ = height; 29 | confidence_ = confidence; 30 | true_confidence_ = confidence; 31 | } 32 | 33 | Rect(const Rect& other) { 34 | cx_ = other.cx_; 35 | cy_ = other.cy_; 36 | width_ = other.width_; 37 | height_ = other.height_; 38 | confidence_ = other.confidence_; 39 | true_confidence_ = other.true_confidence_; 40 | } 41 | 42 | bool overlaps(const Rect& other, float tau) const { 43 | if (fabs(cx_ - other.cx_) > (width_ + other.width_) / 1.5) { 44 | return false; 45 | } else if (fabs(cy_ - other.cy_) > (height_ + other.height_) / 2.0) { 46 | return false; 47 | } else { 48 | return iou(other) > tau; 49 | } 50 | } 51 | 52 | int distance(const Rect& other) const { 53 | return (fabs(cx_ - other.cx_) + fabs(cy_ - other.cy_) + 54 | fabs(width_ - other.width_) + fabs(height_ - other.height_)); 55 | } 56 | 57 | float intersection(const Rect& other) const { 58 | int left = MAX(cx_ - width_ / 2., other.cx_ - other.width_ / 2.); 59 | int right = MIN(cx_ + width_ / 2., other.cx_ + other.width_ / 2.); 60 | int width = MAX(right - left, 0); 61 | 62 | int top = MAX(cy_ - height_ / 2., other.cy_ - other.height_ / 2.); 63 | int bottom = MIN(cy_ + height_ / 2., other.cy_ + other.height_ / 2.); 64 | int height = MAX(bottom - top, 0); 65 | return width * height; 66 | } 67 | 68 | int area() const { 69 | return height_ * width_; 70 | } 71 | 72 | float union_area(const Rect& other) const { 73 | return this->area() + other.area() - this->intersection(other); 74 | } 75 | 76 | float iou(const Rect& other) const { 77 | return this->intersection(other) / this->union_area(other); 78 | } 79 | 80 | bool operator==(const Rect& other) const { 81 | return (cx_ == other.cx_ && 82 | cy_ == other.cy_ && 83 | width_ == other.width_ && 84 | height_ == other.height_ && 85 | confidence_ == other.confidence_); 86 | } 87 | }; 88 | 89 | void filter_rects(const vector > >& all_rects, 90 | vector* stitched_rects, 91 | float threshold, 92 | float max_threshold, 93 | float tau, 94 | float conf_alpha); 95 | 96 | #endif // STITCH_RECTS_HPP 97 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/stitch_wrapper.py: -------------------------------------------------------------------------------- 1 | print('ERROR: stitch_wrapper not yet compiled. Please run `cd /path/to/tensorbox/utils && make`') 2 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/stitch_wrapper.pyx: -------------------------------------------------------------------------------- 1 | from libcpp.vector cimport vector 2 | from libcpp.set cimport set 3 | from rect import Rect as PyRect 4 | cdef extern from "stitch_rects.hpp": 5 | cdef cppclass Rect: 6 | Rect(int cx, int cy, int width, int height, float confidence) 7 | int cx_ 8 | int cy_ 9 | int width_ 10 | int height_ 11 | float confidence_ 12 | float true_confidence_ 13 | 14 | cdef void filter_rects(vector[vector[vector[Rect] ] ]& all_rects, 15 | vector[Rect]* stitched_rects, 16 | float threshold, 17 | float max_threshold, 18 | float tau, 19 | float conf_alpha); 20 | 21 | def stitch_rects(all_rects, tau=0.25): 22 | """ 23 | Implements the stitching procedure discussed in the paper. 24 | Complicated, but we find that it does better than simpler versions 25 | and generalizes well across widely varying box sizes. 26 | 27 | Input: 28 | all_rects : 2d grid with each cell containing a vector of PyRects 29 | """ 30 | for row in all_rects: 31 | assert len(row) == len(all_rects[0]) 32 | 33 | cdef vector[vector[vector[Rect]]] c_rects 34 | cdef vector[vector[Rect]] c_row 35 | cdef vector[Rect] c_column 36 | for i, row in enumerate(all_rects): 37 | c_rects.push_back(c_row) 38 | for j, column in enumerate(row): 39 | c_rects[i].push_back(c_column) 40 | for py_rect in column: 41 | c_rects[i][j].push_back( 42 | Rect( 43 | py_rect.cx, 44 | py_rect.cy, 45 | py_rect.width, 46 | py_rect.height, 47 | py_rect.confidence) 48 | ) 49 | 50 | cdef vector[Rect] acc_rects; 51 | 52 | thresholds = [(.80, 1.0), 53 | (.70, 0.9), 54 | (.60, 0.8), 55 | (.50, 0.7), 56 | (.40, 0.6), 57 | (.30, 0.5), 58 | (.20, 0.4), 59 | (.10, 0.3), 60 | (.05, 0.2), 61 | (.02, 0.1), 62 | (.005, 0.04), 63 | (.001, 0.01), 64 | ] 65 | t_conf_alphas = [(tau, 1.0), 66 | #(1 - (1 - tau) * 0.75, 0.5), 67 | #(1 - (1 - tau) * 0.5, 0.1), 68 | #(1 - (1 - tau) * 0.25, 0.005), 69 | ] 70 | for t, conf_alpha in t_conf_alphas: 71 | for lower_t, upper_t in thresholds: 72 | if lower_t * conf_alpha > 0.0001: 73 | filter_rects(c_rects, &acc_rects, lower_t * conf_alpha, 74 | upper_t * conf_alpha, t, conf_alpha) 75 | 76 | py_acc_rects = [] 77 | for i in range(acc_rects.size()): 78 | acc_rect = PyRect( 79 | acc_rects[i].cx_, 80 | acc_rects[i].cy_, 81 | acc_rects[i].width_, 82 | acc_rects[i].height_, 83 | acc_rects[i].confidence_) 84 | acc_rect.true_confidence = acc_rects[i].true_confidence_ 85 | py_acc_rects.append(acc_rect) 86 | return py_acc_rects 87 | -------------------------------------------------------------------------------- /linux/tensorbox/utils/train_utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | import json 4 | import os 5 | import cv2 6 | import itertools 7 | from scipy.misc import imread, imresize 8 | import tensorflow as tf 9 | 10 | from data_utils import (annotation_jitter, annotation_to_h5) 11 | from utils.annolist import AnnotationLib as al 12 | from rect import Rect 13 | from utils import tf_concat 14 | 15 | def rescale_boxes(current_shape, anno, target_height, target_width): 16 | x_scale = target_width / float(current_shape[1]) 17 | y_scale = target_height / float(current_shape[0]) 18 | for r in anno.rects: 19 | assert r.x1 < r.x2 20 | r.x1 *= x_scale 21 | r.x2 *= x_scale 22 | assert r.y1 < r.y2 23 | r.y1 *= y_scale 24 | r.y2 *= y_scale 25 | return anno 26 | 27 | def load_idl_tf(idlfile, H, jitter): 28 | """Take the idlfile and net configuration and create a generator 29 | that outputs a jittered version of a random image from the annolist 30 | that is mean corrected.""" 31 | 32 | annolist = al.parse(idlfile) 33 | annos = [] 34 | for anno in annolist: 35 | anno.imageName = os.path.join( 36 | os.path.dirname(os.path.realpath(idlfile)), anno.imageName) 37 | annos.append(anno) 38 | random.seed(0) 39 | if H['data']['truncate_data']: 40 | annos = annos[:10] 41 | for epoch in itertools.count(): 42 | random.shuffle(annos) 43 | for anno in annos: 44 | I = imread(anno.imageName) 45 | #Skip Greyscale images 46 | if len(I.shape) < 3: 47 | continue 48 | if I.shape[2] == 4: 49 | I = I[:, :, :3] 50 | if I.shape[0] != H["image_height"] or I.shape[1] != H["image_width"]: 51 | if epoch == 0: 52 | anno = rescale_boxes(I.shape, anno, H["image_height"], H["image_width"]) 53 | I = imresize(I, (H["image_height"], H["image_width"]), interp='cubic') 54 | if jitter: 55 | jitter_scale_min=0.9 56 | jitter_scale_max=1.1 57 | jitter_offset=16 58 | I, anno = annotation_jitter(I, 59 | anno, target_width=H["image_width"], 60 | target_height=H["image_height"], 61 | jitter_scale_min=jitter_scale_min, 62 | jitter_scale_max=jitter_scale_max, 63 | jitter_offset=jitter_offset) 64 | 65 | boxes, flags = annotation_to_h5(H, 66 | anno, 67 | H["grid_width"], 68 | H["grid_height"], 69 | H["rnn_len"]) 70 | 71 | yield {"image": I, "boxes": boxes, "flags": flags} 72 | 73 | def make_sparse(n, d): 74 | v = np.zeros((d,), dtype=np.float32) 75 | v[n] = 1. 76 | return v 77 | 78 | def load_data_gen(H, phase, jitter): 79 | grid_size = H['grid_width'] * H['grid_height'] 80 | 81 | data = load_idl_tf(H["data"]['%s_idl' % phase], H, jitter={'train': jitter, 'test': False}[phase]) 82 | 83 | for d in data: 84 | output = {} 85 | 86 | rnn_len = H["rnn_len"] 87 | flags = d['flags'][0, :, 0, 0:rnn_len, 0] 88 | boxes = np.transpose(d['boxes'][0, :, :, 0:rnn_len, 0], (0, 2, 1)) 89 | assert(flags.shape == (grid_size, rnn_len)) 90 | assert(boxes.shape == (grid_size, rnn_len, 4)) 91 | 92 | output['image'] = d['image'] 93 | output['confs'] = np.array([[make_sparse(int(detection), d=H['num_classes']) for detection in cell] for cell in flags]) 94 | output['boxes'] = boxes 95 | output['flags'] = flags 96 | 97 | yield output 98 | 99 | def add_rectangles(H, orig_image, confidences, boxes, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25, show_suppressed=True): 100 | image = np.copy(orig_image[0]) 101 | num_cells = H["grid_height"] * H["grid_width"] 102 | boxes_r = np.reshape(boxes, (-1, 103 | H["grid_height"], 104 | H["grid_width"], 105 | rnn_len, 106 | 4)) 107 | confidences_r = np.reshape(confidences, (-1, 108 | H["grid_height"], 109 | H["grid_width"], 110 | rnn_len, 111 | H['num_classes'])) 112 | cell_pix_size = H['region_size'] 113 | all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])] 114 | for n in range(rnn_len): 115 | for y in range(H["grid_height"]): 116 | for x in range(H["grid_width"]): 117 | bbox = boxes_r[0, y, x, n, :] 118 | abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x 119 | abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y 120 | w = bbox[2] 121 | h = bbox[3] 122 | conf = np.max(confidences_r[0, y, x, n, 1:]) 123 | all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf)) 124 | 125 | all_rects_r = [r for row in all_rects for cell in row for r in cell] 126 | if use_stitching: 127 | from stitch_wrapper import stitch_rects 128 | acc_rects = stitch_rects(all_rects, tau) 129 | else: 130 | acc_rects = all_rects_r 131 | 132 | 133 | if show_suppressed: 134 | pairs = [(all_rects_r, (255, 0, 0))] 135 | else: 136 | pairs = [] 137 | pairs.append((acc_rects, (0, 255, 0))) 138 | for rect_set, color in pairs: 139 | for rect in rect_set: 140 | if rect.confidence > min_conf: 141 | cv2.rectangle(image, 142 | (rect.cx-int(rect.width/2), rect.cy-int(rect.height/2)), 143 | (rect.cx+int(rect.width/2), rect.cy+int(rect.height/2)), 144 | color, 145 | 2) 146 | 147 | rects = [] 148 | for rect in acc_rects: 149 | r = al.AnnoRect() 150 | r.x1 = rect.cx - rect.width/2. 151 | r.x2 = rect.cx + rect.width/2. 152 | r.y1 = rect.cy - rect.height/2. 153 | r.y2 = rect.cy + rect.height/2. 154 | r.score = rect.true_confidence 155 | rects.append(r) 156 | 157 | return image, rects 158 | 159 | def to_x1y1x2y2(box): 160 | w = tf.maximum(box[:, 2:3], 1) 161 | h = tf.maximum(box[:, 3:4], 1) 162 | x1 = box[:, 0:1] - w / 2 163 | x2 = box[:, 0:1] + w / 2 164 | y1 = box[:, 1:2] - h / 2 165 | y2 = box[:, 1:2] + h / 2 166 | return tf_concat(1, [x1, y1, x2, y2]) 167 | 168 | def intersection(box1, box2): 169 | x1_max = tf.maximum(box1[:, 0], box2[:, 0]) 170 | y1_max = tf.maximum(box1[:, 1], box2[:, 1]) 171 | x2_min = tf.minimum(box1[:, 2], box2[:, 2]) 172 | y2_min = tf.minimum(box1[:, 3], box2[:, 3]) 173 | 174 | x_diff = tf.maximum(x2_min - x1_max, 0) 175 | y_diff = tf.maximum(y2_min - y1_max, 0) 176 | 177 | return x_diff * y_diff 178 | 179 | def area(box): 180 | x_diff = tf.maximum(box[:, 2] - box[:, 0], 0) 181 | y_diff = tf.maximum(box[:, 3] - box[:, 1], 0) 182 | return x_diff * y_diff 183 | 184 | def union(box1, box2): 185 | return area(box1) + area(box2) - intersection(box1, box2) 186 | 187 | def iou(box1, box2): 188 | return intersection(box1, box2) / union(box1, box2) 189 | 190 | def to_idx(vec, w_shape): 191 | ''' 192 | vec = (idn, idh, idw) 193 | w_shape = [n, h, w, c] 194 | ''' 195 | return vec[:, 2] + w_shape[2] * (vec[:, 1] + w_shape[1] * vec[:, 0]) 196 | 197 | def interp(w, i, channel_dim): 198 | ''' 199 | Input: 200 | w: A 4D block tensor of shape (n, h, w, c) 201 | i: A list of 3-tuples [(x_1, y_1, z_1), (x_2, y_2, z_2), ...], 202 | each having type (int, float, float) 203 | 204 | The 4D block represents a batch of 3D image feature volumes with c channels. 205 | The input i is a list of points to index into w via interpolation. Direct 206 | indexing is not possible due to y_1 and z_1 being float values. 207 | Output: 208 | A list of the values: [ 209 | w[x_1, y_1, z_1, :] 210 | w[x_2, y_2, z_2, :] 211 | ... 212 | w[x_k, y_k, z_k, :] 213 | ] 214 | of the same length == len(i) 215 | ''' 216 | w_as_vector = tf.reshape(w, [-1, channel_dim]) # gather expects w to be 1-d 217 | upper_l = tf.to_int32(tf_concat(1, [i[:, 0:1], tf.floor(i[:, 1:2]), tf.floor(i[:, 2:3])])) 218 | upper_r = tf.to_int32(tf_concat(1, [i[:, 0:1], tf.floor(i[:, 1:2]), tf.ceil(i[:, 2:3])])) 219 | lower_l = tf.to_int32(tf_concat(1, [i[:, 0:1], tf.ceil(i[:, 1:2]), tf.floor(i[:, 2:3])])) 220 | lower_r = tf.to_int32(tf_concat(1, [i[:, 0:1], tf.ceil(i[:, 1:2]), tf.ceil(i[:, 2:3])])) 221 | 222 | upper_l_idx = to_idx(upper_l, tf.shape(w)) 223 | upper_r_idx = to_idx(upper_r, tf.shape(w)) 224 | lower_l_idx = to_idx(lower_l, tf.shape(w)) 225 | lower_r_idx = to_idx(lower_r, tf.shape(w)) 226 | 227 | upper_l_value = tf.gather(w_as_vector, upper_l_idx) 228 | upper_r_value = tf.gather(w_as_vector, upper_r_idx) 229 | lower_l_value = tf.gather(w_as_vector, lower_l_idx) 230 | lower_r_value = tf.gather(w_as_vector, lower_r_idx) 231 | 232 | alpha_lr = tf.expand_dims(i[:, 2] - tf.floor(i[:, 2]), 1) 233 | alpha_ud = tf.expand_dims(i[:, 1] - tf.floor(i[:, 1]), 1) 234 | 235 | upper_value = (1 - alpha_lr) * upper_l_value + (alpha_lr) * upper_r_value 236 | lower_value = (1 - alpha_lr) * lower_l_value + (alpha_lr) * lower_r_value 237 | value = (1 - alpha_ud) * upper_value + (alpha_ud) * lower_value 238 | return value 239 | 240 | def bilinear_select(H, pred_boxes, early_feat, early_feat_channels, w_offset, h_offset): 241 | ''' 242 | Function used for rezooming high level feature maps. Uses bilinear interpolation 243 | to select all channels at index (x, y) for a high level feature map, where x and y are floats. 244 | ''' 245 | grid_size = H['grid_width'] * H['grid_height'] 246 | outer_size = grid_size * H['batch_size'] 247 | 248 | fine_stride = 8. # pixels per 60x80 grid cell in 480x640 image 249 | coarse_stride = H['region_size'] # pixels per 15x20 grid cell in 480x640 image 250 | batch_ids = [] 251 | x_offsets = [] 252 | y_offsets = [] 253 | for n in range(H['batch_size']): 254 | for i in range(H['grid_height']): 255 | for j in range(H['grid_width']): 256 | for k in range(H['rnn_len']): 257 | batch_ids.append([n]) 258 | x_offsets.append([coarse_stride / 2. + coarse_stride * j]) 259 | y_offsets.append([coarse_stride / 2. + coarse_stride * i]) 260 | 261 | batch_ids = tf.constant(batch_ids) 262 | x_offsets = tf.constant(x_offsets) 263 | y_offsets = tf.constant(y_offsets) 264 | 265 | pred_boxes_r = tf.reshape(pred_boxes, [outer_size * H['rnn_len'], 4]) 266 | scale_factor = coarse_stride / fine_stride # scale difference between 15x20 and 60x80 features 267 | 268 | pred_x_center = (pred_boxes_r[:, 0:1] + w_offset * pred_boxes_r[:, 2:3] + x_offsets) / fine_stride 269 | pred_x_center_clip = tf.clip_by_value(pred_x_center, 270 | 0, 271 | scale_factor * H['grid_width'] - 1) 272 | pred_y_center = (pred_boxes_r[:, 1:2] + h_offset * pred_boxes_r[:, 3:4] + y_offsets) / fine_stride 273 | pred_y_center_clip = tf.clip_by_value(pred_y_center, 274 | 0, 275 | scale_factor * H['grid_height'] - 1) 276 | 277 | interp_indices = tf_concat(1, [tf.to_float(batch_ids), pred_y_center_clip, pred_x_center_clip]) 278 | return interp_indices 279 | -------------------------------------------------------------------------------- /windows/src/IPM.cpp: -------------------------------------------------------------------------------- 1 | #include "IPM.h" 2 | 3 | using namespace cv; 4 | using namespace std; 5 | 6 | // Public 7 | IPM::IPM( const cv::Size& _origSize, const cv::Size& _dstSize, const std::vector& _origPoints, const std::vector& _dstPoints ) 8 | : m_origSize(_origSize), m_dstSize(_dstSize), m_origPoints(_origPoints), m_dstPoints(_dstPoints) 9 | { 10 | assert( m_origPoints.size() == 4 && m_dstPoints.size() == 4 && "Orig. points and Dst. points must vectors of 4 points" ); 11 | m_H = getPerspectiveTransform( m_origPoints, m_dstPoints ); 12 | m_H_inv = m_H.inv(); 13 | 14 | createMaps(); 15 | } 16 | void IPM::drawPoints( const std::vector& _points, cv::Mat& _img ) const 17 | { 18 | assert(_points.size() == 4); 19 | 20 | line(_img, Point(static_cast(_points[0].x), static_cast(_points[0].y)), Point(static_cast(_points[3].x), static_cast(_points[3].y)), CV_RGB( 205,205,0), 2); 21 | line(_img, Point(static_cast(_points[2].x), static_cast(_points[2].y)), Point(static_cast(_points[3].x), static_cast(_points[3].y)), CV_RGB( 205,205,0), 2); 22 | line(_img, Point(static_cast(_points[0].x), static_cast(_points[0].y)), Point(static_cast(_points[1].x), static_cast(_points[1].y)), CV_RGB( 205,205,0), 2); 23 | line(_img, Point(static_cast(_points[2].x), static_cast(_points[2].y)), Point(static_cast(_points[1].x), static_cast(_points[1].y)), CV_RGB( 205,205,0), 2); 24 | for(std::size_t i=0; i<_points.size(); i++) 25 | { 26 | circle(_img, Point(static_cast(_points[i].x), static_cast(_points[i].y)), 2, CV_RGB(238,238,0), -1); 27 | circle(_img, Point(static_cast(_points[i].x), static_cast(_points[i].y)), 5, CV_RGB(255,255,255), 2); 28 | } 29 | } 30 | void IPM::getPoints(vector& _origPts, vector& _ipmPts) 31 | { 32 | _origPts = m_origPoints; 33 | _ipmPts = m_dstPoints; 34 | } 35 | void IPM::applyHomography(const Mat& _inputImg, Mat& _dstImg, int _borderMode) 36 | { 37 | // Generate IPM image from src 38 | remap(_inputImg, _dstImg, m_mapX, m_mapY, INTER_LINEAR, _borderMode);//, BORDER_CONSTANT, Scalar(0,0,0,0)); 39 | } 40 | void IPM::applyHomographyInv(const Mat& _inputImg, Mat& _dstImg, int _borderMode) 41 | { 42 | // Generate IPM image from src 43 | remap(_inputImg, _dstImg, m_mapX, m_mapY, INTER_LINEAR, _borderMode);//, BORDER_CONSTANT, Scalar(0,0,0,0)); 44 | } 45 | Point2d IPM::applyHomography( const Point2d& _point ) 46 | { 47 | return applyHomography( _point, m_H ); 48 | } 49 | Point2d IPM::applyHomographyInv( const Point2d& _point ) 50 | { 51 | return applyHomography( _point, m_H_inv ); 52 | } 53 | Point2d IPM::applyHomography( const Point2d& _point, const Mat& _H ) 54 | { 55 | Point2d ret = Point2d( -1, -1 ); 56 | 57 | const double u = _H.at(0,0) * _point.x + _H.at(0,1) * _point.y + _H.at(0,2); 58 | const double v = _H.at(1,0) * _point.x + _H.at(1,1) * _point.y + _H.at(1,2); 59 | const double s = _H.at(2,0) * _point.x + _H.at(2,1) * _point.y + _H.at(2,2); 60 | if ( s != 0 ) 61 | { 62 | ret.x = ( u / s ); 63 | ret.y = ( v / s ); 64 | } 65 | return ret; 66 | } 67 | Point3d IPM::applyHomography( const Point3d& _point ) 68 | { 69 | return applyHomography( _point, m_H ); 70 | } 71 | Point3d IPM::applyHomographyInv( const Point3d& _point ) 72 | { 73 | return applyHomography( _point, m_H_inv ); 74 | } 75 | Point3d IPM::applyHomography( const Point3d& _point, const cv::Mat& _H ) 76 | { 77 | Point3d ret = Point3d( -1, -1, 1 ); 78 | 79 | const double u = _H.at(0,0) * _point.x + _H.at(0,1) * _point.y + _H.at(0,2) * _point.z; 80 | const double v = _H.at(1,0) * _point.x + _H.at(1,1) * _point.y + _H.at(1,2) * _point.z; 81 | const double s = _H.at(2,0) * _point.x + _H.at(2,1) * _point.y + _H.at(2,2) * _point.z; 82 | if ( s != 0 ) 83 | { 84 | ret.x = ( u / s ); 85 | ret.y = ( v / s ); 86 | } 87 | else 88 | ret.z = 0; 89 | return ret; 90 | } 91 | 92 | // Private 93 | void IPM::createMaps() 94 | { 95 | // Create remap images 96 | m_mapX.create(m_dstSize, CV_32F); 97 | m_mapY.create(m_dstSize, CV_32F); 98 | //#pragma omp parallel for schedule(dynamic) 99 | for( int j = 0; j < m_dstSize.height; ++j ) 100 | { 101 | float* ptRowX = m_mapX.ptr(j); 102 | float* ptRowY = m_mapY.ptr(j); 103 | //#pragma omp parallel for schedule(dynamic) 104 | for( int i = 0; i < m_dstSize.width; ++i ) 105 | { 106 | Point2f pt = applyHomography( Point2f( static_cast(i), static_cast(j) ), m_H_inv ); 107 | ptRowX[i] = pt.x; 108 | ptRowY[i] = pt.y; 109 | } 110 | } 111 | 112 | m_invMapX.create(m_origSize, CV_32F); 113 | m_invMapY.create(m_origSize, CV_32F); 114 | 115 | //#pragma omp parallel for schedule(dynamic) 116 | for( int j = 0; j < m_origSize.height; ++j ) 117 | { 118 | float* ptRowX = m_invMapX.ptr(j); 119 | float* ptRowY = m_invMapY.ptr(j); 120 | //#pragma omp parallel for schedule(dynamic) 121 | for( int i = 0; i < m_origSize.width; ++i ) 122 | { 123 | Point2f pt = applyHomography( Point2f( static_cast(i), static_cast(j) ), m_H ); 124 | ptRowX[i] = pt.x; 125 | ptRowY[i] = pt.y; 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /windows/src/IPM.h: -------------------------------------------------------------------------------- 1 | #ifndef __IPM_H__ 2 | #define __IPM_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | class IPM 12 | { 13 | public: 14 | IPM( const cv::Size& _origSize, const cv::Size& _dstSize, 15 | const std::vector& _origPoints, const std::vector& _dstPoints ); 16 | 17 | IPM::IPM() {} 18 | // Apply IPM on points 19 | cv::Point2d applyHomography(const cv::Point2d& _point, const cv::Mat& _H); 20 | cv::Point3d applyHomography( const cv::Point3d& _point, const cv::Mat& _H); 21 | cv::Point2d applyHomography(const cv::Point2d& _point); 22 | cv::Point3d applyHomography( const cv::Point3d& _point); 23 | cv::Point2d applyHomographyInv(const cv::Point2d& _point); 24 | cv::Point3d applyHomographyInv( const cv::Point3d& _point); 25 | void applyHomography( const cv::Mat& _origBGR, cv::Mat& _ipmBGR, int borderMode = cv::BORDER_CONSTANT); 26 | void applyHomographyInv( const cv::Mat& _ipmBGR, cv::Mat& _origBGR, int borderMode = cv::BORDER_CONSTANT); 27 | 28 | // Getters 29 | cv::Mat getH() const { return m_H; } 30 | cv::Mat getHinv() const { return m_H_inv; } 31 | void getPoints(std::vector& _origPts, std::vector& _ipmPts); 32 | 33 | // Draw 34 | void drawPoints( const std::vector& _points, cv::Mat& _img ) const; 35 | 36 | private: 37 | void createMaps(); 38 | 39 | // Sizes 40 | cv::Size m_origSize; 41 | cv::Size m_dstSize; 42 | 43 | // Points 44 | std::vector m_origPoints; 45 | std::vector m_dstPoints; 46 | 47 | // Homography 48 | cv::Mat m_H; 49 | cv::Mat m_H_inv; 50 | 51 | // Maps 52 | cv::Mat m_mapX, m_mapY; 53 | cv::Mat m_invMapX, m_invMapY; 54 | }; 55 | 56 | #endif /*__IPM_H__*/ -------------------------------------------------------------------------------- /windows/src/ets2_self_driving.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/windows/src/ets2_self_driving.h -------------------------------------------------------------------------------- /windows/src/guassian_filter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | #define M_PI 3.14159265358979323846 7 | 8 | void createFilter(double gKernel[][2]) 9 | { 10 | // set standard deviation to 1.0 11 | double sigma = 1.0; 12 | double r, s = 2.0 * sigma * sigma; 13 | 14 | // sum is for normalization 15 | double sum = 0.0; 16 | 17 | // generate 2x2 kernel 18 | for (int x = -2; x <= 2; x++) 19 | { 20 | for (int y = -2; y <= 2; y++) 21 | { 22 | r = sqrt(x*x + y*y); 23 | gKernel[x + 2][y + 2] = (exp(-(r*r) / s)) / (M_PI * s); 24 | sum += gKernel[x + 2][y + 2]; 25 | } 26 | } 27 | 28 | // normalize the Kernel 29 | for (int i = 0; i < 2; ++i) 30 | for (int j = 0; j < 2; ++j) 31 | gKernel[i][j] /= sum; 32 | 33 | } 34 | 35 | /*int main() 36 | { 37 | double gKernel[2][2]; 38 | createFilter(gKernel); 39 | for (int i = 0; i < 2; ++i) 40 | { 41 | for (int j = 0; j < 2; ++j) 42 | cout << gKernel[i][j] << "\t"; 43 | cout << endl; 44 | } 45 | }*/ -------------------------------------------------------------------------------- /windows/src/hwnd2mat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | //#include 10 | //#include 11 | #include 12 | #include 13 | #include "ets2_self_driving.h" 14 | 15 | using namespace cv; 16 | using namespace std; 17 | 18 | Mat hwnd2mat(HWND hwnd) { 19 | 20 | HDC hwindowDC, hwindowCompatibleDC; 21 | 22 | int height, width, srcheight, srcwidth; 23 | HBITMAP hbwindow; 24 | Mat src; 25 | BITMAPINFOHEADER bi; 26 | 27 | hwindowDC = GetDC(hwnd); 28 | hwindowCompatibleDC = CreateCompatibleDC(hwindowDC); 29 | SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR); 30 | 31 | RECT windowsize; // get the height and width of the screen 32 | GetClientRect(hwnd, &windowsize); 33 | 34 | srcheight = windowsize.bottom ;// change this to whatever size you want to resize to 35 | srcwidth = windowsize.right; 36 | height = windowsize.bottom; // change this to whatever size you want to resize to 37 | width = windowsize.right; 38 | //height = windowsize.bottom * 0.3125; // change this to whatever size you want to resize to 39 | //width = windowsize.right * 0.625; 40 | 41 | src.create(height, width, CV_8UC4); 42 | 43 | // create a bitmap 44 | hbwindow = CreateCompatibleBitmap(hwindowDC, width, height); 45 | bi.biSize = sizeof(BITMAPINFOHEADER); //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx 46 | bi.biWidth = width; 47 | bi.biHeight = -height; //this is the line that makes it draw upside down or not 48 | bi.biPlanes = 1; 49 | bi.biBitCount = 32; 50 | bi.biCompression = BI_RGB; 51 | bi.biSizeImage = 0; 52 | bi.biXPelsPerMeter = 0; 53 | bi.biYPelsPerMeter = 0; 54 | bi.biClrUsed = 0; 55 | bi.biClrImportant = 0; 56 | 57 | // use the previously created device context with the bitmap 58 | SelectObject(hwindowCompatibleDC, hbwindow); 59 | // copy from the window device context to the bitmap device context 60 | StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors ! 61 | GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow 62 | 63 | // avoid memory leak 64 | DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC); 65 | 66 | return src; 67 | } 68 | -------------------------------------------------------------------------------- /windows/src/linefinder.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethesirius/ChosunTruck/889644385ce57f971ec2921f006fbb0a167e6f1e/windows/src/linefinder.cpp -------------------------------------------------------------------------------- /windows/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | #include 5 | #include 6 | #include 7 | #include 8 | //#include 9 | //#include 10 | //#include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "ets2_self_driving.h" 16 | #include "IPM.h" 17 | 18 | #define PI 3.1415926 19 | 20 | using namespace cv; 21 | using namespace std; 22 | 23 | void Thinning(Mat input, int row, int col); 24 | 25 | void GetDesktopResolution(int& monitorWidth, int& monitorHeight) 26 | { 27 | RECT desktop; 28 | const HWND hDesktop = GetDesktopWindow(); 29 | GetWindowRect(hDesktop, &desktop); 30 | monitorWidth = desktop.right; 31 | monitorHeight = desktop.bottom; 32 | } 33 | 34 | void GetGameResolution(int& width, int& height) 35 | { 36 | RECT windowsize; 37 | const HWND hWnd = FindWindow("prism3d", NULL); 38 | GetClientRect(hWnd, &windowsize); 39 | width = windowsize.right; 40 | height = windowsize.bottom; 41 | } 42 | void detectPause() 43 | { 44 | // Press '+' to pause 45 | if (GetAsyncKeyState(VK_OEM_PLUS) & 0x8000) 46 | { 47 | while (true) 48 | { 49 | // Press '-' to start 50 | if (GetAsyncKeyState(VK_OEM_MINUS) & 0x8000) 51 | { 52 | break; 53 | } 54 | } 55 | } 56 | } 57 | int main() 58 | { 59 | int width = 0, height = 0; 60 | int monitorWidth = 0, monitorHeight = 0; 61 | long long int sum = 0; 62 | long long int i = 0; 63 | int diffOld = 0; 64 | GetGameResolution(width, height); 65 | double IPM_BOTTOM_RIGHT = width + 400; 66 | double IPM_BOTTOM_LEFT = -400; 67 | double IPM_RIGHT = width / 2 + 100; 68 | double IPM_LEFT = width / 2 - 100; 69 | int IPM_diff = 0; 70 | 71 | while (true) 72 | { 73 | detectPause(); 74 | HWND hWnd = FindWindow("prism3d", NULL); 75 | HWND consoleWindow = GetConsoleWindow(); 76 | GetDesktopResolution(monitorWidth, monitorHeight); 77 | 78 | Mat image, outputImg; 79 | hwnd2mat(hWnd).copyTo(image); 80 | 81 | medianBlur(image, image, 3); 82 | 83 | // The 4-points at the input image 84 | vector origPoints; 85 | origPoints.push_back(Point2f(IPM_BOTTOM_LEFT, height - 50)); 86 | origPoints.push_back(Point2f(IPM_BOTTOM_RIGHT, height - 50)); 87 | origPoints.push_back(Point2f(IPM_RIGHT, height / 2 + 30)); 88 | origPoints.push_back(Point2f(IPM_LEFT, height / 2 + 30)); 89 | 90 | // The 4-points correspondences in the destination image 91 | vector dstPoints; 92 | dstPoints.push_back(Point2f(0, height)); 93 | dstPoints.push_back(Point2f(width, height)); 94 | dstPoints.push_back(Point2f(width, 0)); 95 | dstPoints.push_back(Point2f(0, 0)); 96 | 97 | // IPM object 98 | IPM ipm(Size(width, height), Size(width, height), origPoints, dstPoints); 99 | 100 | // Process 101 | //clock_t begin = clock(); 102 | ipm.applyHomography(image, outputImg); 103 | //clock_t end = clock(); 104 | //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; 105 | //printf("%.2f (ms)\r", 1000 * elapsed_secs); 106 | //ipm.drawPoints(origPoints, image); 107 | 108 | cv::Mat gray; 109 | cv::Mat blur; 110 | cv::Mat sobel; 111 | cv::Mat contours; 112 | 113 | cv::resize(outputImg, outputImg, cv::Size(320, 240)); 114 | cv::cvtColor(outputImg, gray, COLOR_RGB2GRAY); 115 | cv::blur(gray, blur, cv::Size(10, 10)); 116 | cv::Sobel(blur, sobel, blur.depth(), 1, 0, 3, 0.5, 127); 117 | cv::threshold(sobel, contours, 145, 255, CV_THRESH_BINARY); 118 | 119 | //Thinning(contours, contours.rows, contours.cols); 120 | //cv::Canny(gray, contours, 125, 350); 121 | 122 | LineFinder ld; 123 | ld.setLineLengthAndGap(20, 120); 124 | ld.setMinVote(55); 125 | 126 | std::vector li = ld.findLines(contours); 127 | ld.drawDetectedLines(contours); 128 | 129 | // cv::cvtColor(contours, contours, COLOR_GRAY2RGB); 130 | /* 131 | auto end = chrono::high_resolution_clock::now(); 132 | auto dur = end - begin; 133 | auto ms = std::chrono::duration_cast(dur).count(); 134 | ms++; 135 | sum += ms; 136 | cout << 1000 / ms << "fps avr:" << 1000 / (sum / (++i)) << endl; 137 | */ 138 | imshow("Lines", contours); 139 | imshow("Road", outputImg); 140 | cv::moveWindow("Lines", monitorWidth / 1.6, monitorHeight / 10.8); 141 | cv::moveWindow("Road", monitorWidth / 1.2673, monitorHeight / 10.8); 142 | SetWindowPos(consoleWindow, 0, monitorWidth / 1.6, monitorHeight / 2.7, 600, 400, SWP_NOZORDER); 143 | SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER); 144 | waitKey(1); 145 | 146 | SetActiveWindow(hWnd); 147 | POINT pt; 148 | GetCursorPos(&pt); 149 | cout << "current mouse pos: " << "x: " << pt.x << "y: " << pt.y << endl; 150 | 151 | int bottom_center = 160; 152 | int sum_centerline = 0; 153 | int count_centerline = 0; 154 | int first_centerline = 0; 155 | int last_centerline = 0; 156 | double avr_center_to_left = 0; 157 | double avr_center_to_right = 0; 158 | 159 | //#pragma omp parallel for 160 | for (int i = 240; i > 30; i--) 161 | { 162 | double center_to_right = -1; 163 | double center_to_left = -1; 164 | 165 | for (int j = 0; j < 150; j++) 166 | { 167 | if (contours.at(i, bottom_center + j) == 112 && center_to_right == -1) 168 | { 169 | center_to_right = j; 170 | } 171 | if (contours.at(i, bottom_center - j) == 112 && center_to_left == -1) 172 | { 173 | center_to_left = j; 174 | } 175 | } 176 | if (center_to_left != -1 && center_to_right != -1) 177 | { 178 | int centerline = (center_to_right - center_to_left + 2 * bottom_center) / 2; 179 | if (first_centerline == 0) 180 | { 181 | first_centerline = centerline; 182 | } 183 | cv::circle(outputImg, Point(centerline, i), 1, Scalar(30, 255, 30), 3); 184 | cv::circle(outputImg, Point(centerline + center_to_right + 20, i), 1, Scalar(255, 30, 30), 3); 185 | cv::circle(outputImg, Point(centerline - center_to_left + 10, i), 1, Scalar(255, 30, 30), 3); 186 | sum_centerline += centerline; 187 | avr_center_to_left = (avr_center_to_left * count_centerline + center_to_left) / count_centerline + 1; 188 | avr_center_to_right = (avr_center_to_right * count_centerline + center_to_right) / count_centerline + 1; 189 | last_centerline = centerline; 190 | count_centerline++; 191 | } 192 | else {} 193 | } 194 | 195 | int diff = 0; 196 | pt.x = width / 2; 197 | if (count_centerline != 0) 198 | { 199 | diff = sum_centerline / count_centerline - bottom_center - 25; 200 | 201 | // diff_max was determined by finding the maxmimum diff that can be used to go from center to the very edge of the lane. 202 | // In testing, 65px was the farthest we could go from center in-game without losing lane. 203 | int diff_max = 70; 204 | 205 | // jerk_factor = how fast the wheel will turn 206 | // (1/70) = Limits steering to move 1px MAXMIMUM every time step (1 second). 207 | double jerk_factor = 1 / 70; 208 | 209 | // diff on a scale of -1 to 1 210 | double linearized_diff = diff / diff_max; 211 | 212 | double turn_amount = linearized_diff * jerk_factor; 213 | 214 | 215 | if (turn_amount < .5) 216 | { 217 | turn_amount = 0; 218 | } 219 | else 220 | { 221 | turn_amount = 1; 222 | } 223 | 224 | int moveMouse = (pt.x + diffOld + turn_amount); 225 | SetCursorPos(moveMouse, height / 2); 226 | cout << "Steer: " << diffOld << "px " << endl; 227 | /*double diffForIPM = (diff - diffOld) / 3; 228 | if ((int)diffForIPM == 0) { 229 | if (IPM_diff > 0) { 230 | IPM_RIGHT -= 1; 231 | IPM_LEFT -= 1; 232 | IPM_diff -= 1; 233 | } 234 | else if (IPM_diff < 0) { 235 | IPM_RIGHT += 1; 236 | IPM_LEFT += 1; 237 | IPM_diff += 1; 238 | } 239 | else { 240 | IPM_RIGHT = width / 2 + 100; 241 | IPM_LEFT = width / 2 - 100; 242 | IPM_diff = 0; 243 | } 244 | } 245 | else { 246 | if (IPM_diff >= -30 && IPM_diff <= 30) { 247 | 248 | if ((int)diffForIPM > 0) { 249 | IPM_RIGHT += (int)diffForIPM; 250 | IPM_LEFT += (int)diffForIPM; 251 | IPM_diff++; 252 | } 253 | else { 254 | IPM_RIGHT -= (int)diffForIPM; 255 | IPM_LEFT -= (int)diffForIPM; 256 | IPM_diff--; 257 | } 258 | } 259 | }*/ 260 | //cout << IPM_diff <<" / " << (int)diffForIPM << endl; 261 | diffOld = diff; 262 | } 263 | } 264 | return 0; 265 | } --------------------------------------------------------------------------------