_winpack_run_python_sample.cmd 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. @ECHO OFF
  2. SETLOCAL
  3. SET SCRIPT_DIR=%~dp0
  4. SET "OPENCV_SETUPVARS_SCRIPT=setup_vars_opencv4.cmd"
  5. SET "BUILD_DIR=%SCRIPT_DIR%\..\..\build"
  6. IF NOT EXIST "%BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%" (
  7. :: Winpack DLDT
  8. SET "BUILD_DIR=%SCRIPT_DIR%\..\..\..\build"
  9. )
  10. IF NOT EXIST "%BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%" (
  11. ECHO ERROR: OpenCV Winpack installation is required
  12. pause
  13. exit
  14. )
  15. :: normalize path
  16. for %%i in ("%PACKAGE_BUILD_DIR%") do SET "PACKAGE_BUILD_DIR=%%~fi"
  17. :: Detect Python binary
  18. python -V 2>nul
  19. IF %ERRORLEVEL% EQU 0 (
  20. SET PYTHON=python
  21. GOTO :PYTHON_FOUND
  22. )
  23. CALL :QUERY_PYTHON 3.10
  24. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  25. CALL :QUERY_PYTHON 3.9
  26. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  27. CALL :QUERY_PYTHON 3.8
  28. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  29. CALL :QUERY_PYTHON 3.7
  30. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  31. CALL :QUERY_PYTHON 3.6
  32. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  33. CALL :QUERY_PYTHON 3.5
  34. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  35. CALL :QUERY_PYTHON 3.4
  36. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  37. CALL :QUERY_PYTHON 2.7
  38. IF %ERRORLEVEL% EQU 0 GOTO :PYTHON_FOUND
  39. GOTO :PYTHON_NOT_FOUND
  40. :QUERY_PYTHON
  41. SETLOCAL
  42. SET PY_VERSION=%1
  43. SET PYTHON_DIR=
  44. CALL :regquery "HKCU\SOFTWARE\Python\PythonCore\%PY_VERSION%\InstallPath" PYTHON_DIR
  45. IF EXIST "%PYTHON_DIR%\python.exe" (
  46. SET "PYTHON=%PYTHON_DIR%\python.exe"
  47. GOTO :QUERY_PYTHON_FOUND
  48. )
  49. CALL :regquery "HKLM\SOFTWARE\Python\PythonCore\%PY_VERSION%\InstallPath" PYTHON_DIR
  50. IF EXIST "%PYTHON_DIR%\python.exe" (
  51. SET "PYTHON=%PYTHON_DIR%\python.exe"
  52. GOTO :QUERY_PYTHON_FOUND
  53. )
  54. ::echo Python %PY_VERSION% is not detected
  55. ENDLOCAL
  56. EXIT /B 1
  57. :QUERY_PYTHON_FOUND
  58. ECHO Found Python %PY_VERSION% from Windows Registry: %PYTHON%
  59. ENDLOCAL & SET PYTHON=%PYTHON%
  60. EXIT /B 0
  61. IF exist C:\Python27-x64\python.exe (
  62. SET PYTHON=C:\Python27-x64\python.exe
  63. GOTO :PYTHON_FOUND
  64. )
  65. IF exist C:\Python27\python.exe (
  66. SET PYTHON=C:\Python27\python.exe
  67. GOTO :PYTHON_FOUND
  68. )
  69. :PYTHON_NOT_FOUND
  70. ECHO ERROR: Python not found
  71. IF NOT DEFINED OPENCV_BATCH_MODE ( pause )
  72. EXIT /B
  73. :PYTHON_FOUND
  74. ECHO Using Python: %PYTHON%
  75. :: Don't generate unnecessary .pyc cache files
  76. SET PYTHONDONTWRITEBYTECODE=1
  77. IF [%1]==[] goto rundemo
  78. set SRC_FILENAME=%~dpnx1
  79. echo SRC_FILENAME=%SRC_FILENAME%
  80. call :dirname "%SRC_FILENAME%" SRC_DIR
  81. call :dirname "%PYTHON%" PYTHON_DIR
  82. PUSHD %SRC_DIR%
  83. CALL "%BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%"
  84. :: repair SCRIPT_DIR
  85. SET "SCRIPT_DIR=%~dp0"
  86. ECHO Run: %*
  87. %PYTHON% %*
  88. SET result=%errorlevel%
  89. IF %result% NEQ 0 (
  90. IF NOT DEFINED OPENCV_BATCH_MODE (
  91. SET "PATH=%PYTHON_DIR%;%PATH%"
  92. echo ================================================================================
  93. echo ** Type 'python sample_name.py' to run sample
  94. echo ** Type 'exit' to exit from interactive shell and open the build directory
  95. echo ================================================================================
  96. cmd /k echo Current directory: %CD%
  97. )
  98. )
  99. POPD
  100. EXIT /B %result%
  101. :rundemo
  102. PUSHD "%SCRIPT_DIR%\python"
  103. CALL "%BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%"
  104. :: repair SCRIPT_DIR
  105. SET "SCRIPT_DIR=%~dp0"
  106. %PYTHON% demo.py
  107. SET result=%errorlevel%
  108. IF %result% NEQ 0 (
  109. IF NOT DEFINED OPENCV_BATCH_MODE ( pause )
  110. )
  111. POPD
  112. EXIT /B %result%
  113. :dirname file resultVar
  114. setlocal
  115. set _dir=%~dp1
  116. set _dir=%_dir:~0,-1%
  117. endlocal & set %2=%_dir%
  118. EXIT /B 0
  119. :regquery name resultVar
  120. SETLOCAL
  121. FOR /F "tokens=*" %%A IN ('REG QUERY "%1" /reg:64 /ve 2^>NUL ^| FIND "REG_SZ"') DO SET _val=%%A
  122. IF "x%_val%x"=="xx" EXIT /B 1
  123. SET _val=%_val:*REG_SZ=%
  124. FOR /F "tokens=*" %%A IN ("%_val%") DO SET _val=%%A
  125. ENDLOCAL & SET %2=%_val%
  126. EXIT /B 0