2019年7月17日水曜日

mkCascadeXml.cmd

@echo off
@rem Make cascade.xml
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "PATH=%PATH%;!cd!\opencv\build\x64\vc15\bin"

set "ocvThisName=%~nx0"
set "ocvSrcDirPath=src"
set "ocvIcoDirPath=data\ico"
set "ocvPosDirPath=data\pos"
set "ocvNegDirPath=data\neg"
set "ocvVecDirPath=data\pos"
set "ocvMdlDirPath=data\mdl"

set "ocvIcoHeight=20"
set "ocvIcoWidth=20"
set "ocvIcoBgColor=111"
set "ocvIcoBgThresh=1.0"
set "ocvMaxxangle=0.1"
set "ocvMaxyangle=0.0"
set "ocvMaxzangle=0.5"
set "ocvMinHitRate=0.97"
set "ocvMaxFalseAlarmRate=0.1"
set "ocvStage=3"

set "ocvIcoHeight=20"
set "ocvIcoWidth=20"
set "ocvIcoBgColor=111"
set "ocvIcoBgThresh=1.0"
set "ocvMaxxangle=0.1"
set "ocvMaxyangle=0.1"
set "ocvMaxzangle=0.1"
set "ocvMinHitRate=0.97"
set "ocvMaxFalseAlarmRate=0.1"
set "ocvStage=8"

set "ocvParams="
set "ocvParams=!ocvParams!;ocvIcoHeight=!ocvIcoHeight!"
set "ocvParams=!ocvParams!;ocvIcoWidth=!ocvIcoWidth!"
set "ocvParams=!ocvParams!;ocvIcoBgColor=!ocvIcoBgColor!"
set "ocvParams=!ocvParams!;ocvIcoBgThresh=!ocvIcoBgThresh!"
set "ocvParams=!ocvParams!;ocvMaxxangle=!ocvMaxxangle!"
set "ocvParams=!ocvParams!;ocvMaxyangle=!ocvMaxyangle!"
set "ocvParams=!ocvParams!;ocvMaxzangle=!ocvMaxzangle!"
set "ocvParams=!ocvParams!;ocvMinHitRate=!ocvMinHitRate!"
set "ocvParams=!ocvParams!;ocvMaxFalseAlarmRate=!ocvMaxFalseAlarmRate!"
set "ocvParams=!ocvParams!;ocvStage=!ocvStage!"
set "ocvParams=!ocvParams:~1!"

set /a ocvIcoImgNum=0
for %%z in (
  "!ocvIcoDirPath!\*"
) do (
  set /a "ocvIcoImgNum=!ocvIcoImgNum!+1"
  echo.>nul
)

set /a ocvNegImgNum=0
for %%z in (
  "!ocvNegDirPath!\*"
) do (
  set /a "ocvNegImgNum=!ocvNegImgNum!+1"
  echo.>nul
)

call :L_rmNegativeDat

@rem
call :L_mkNegativeDat

set /a ocvIcoImgCnt=0
for %%z in (
  "!ocvIcoDirPath!\*"
) do (
  set /a "ocvIcoImgCnt=!ocvIcoImgCnt!+1"
  set "ocvIcoFileName=%%~nxz"
  set "ocvIcoNameDirName=!ocvIcoFileName!.d"
  set "ocvPosDatFleName=!ocvIcoFileName!.positive.dat"
  set "ocvVecFleName=!ocvIcoFileName!.positive.vec"
  echo # !date! !time! %0 !ocvIcoFileName! **************************************** 1>&2
  title コマンドプロンプト - !ocvThisName! ico=!ocvIcoImgCnt!/!ocvIcoImgNum! stgN=!ocvStage! ^
    negN=!ocvNegImgNum! !ocvIcoFileName!

  call :L_rmPositiveDat
  call :L_rmPositiveVec
  call :L_rmModelXml

  @rem
  call :L_mkPositiveDat

  set /a ocvPosImgCnt=0
  for %%z in (
    "!ocvPosDirPath!\!ocvIcoNameDirName!\*"
  ) do (
    set /a "ocvPosImgCnt=!ocvPosImgCnt!+1"
    echo.>nul
  )

  echo # !date! !time! ocvPosImgCnt=!ocvPosImgCnt! ocvNegImgNum=!ocvNegImgNum!
  if 1 equ 1 (

    @rem
    call :L_mkPositiveVec
    title コマンドプロンプト - !ocvThisName! ico=!ocvIcoImgCnt!/!ocvIcoImgNum! stgN=!ocvStage! ^
      negN=!ocvNegImgNum! !ocvIcoFileName! posN=!ocvPosImgCnt!

    @rem
    call :L_mkModelXml

    echo.>nul
  )

  echo.>nul
)

call :L_mkCascadeDetect

title コマンドプロンプト

goto :eof


:L_mkNegativeDat
(
  echo.
  echo # !date! !time! %0 ========================================
) 1>&2
:echo cd=!cd!
set "ocvWorkDir=."
set ocv
if not exist "!ocvSrcDirPath!" mkdir "!ocvSrcDirPath!"
(
  for %%z in (
    "!ocvNegDirPath!\*"
  ) do (
    set "ovcNegFilePath=%%~z"
    set "ovcNegFilePath=!ovcNegFilePath:\=/!"
    echo ../!ovcNegFilePath!
    echo.>nul
  )
  echo.>nul
) > "!ocvSrcDirPath!/negative.dat"
type "!ocvSrcDirPath!\negative.dat" | sort > "!ocvSrcDirPath!/negative.dat2"
type "!ocvSrcDirPath!\negative.dat2" > "!ocvSrcDirPath!/negative.dat"
if exist "!ocvSrcDirPath!/negative.dat2" del /f /q "!ocvSrcDirPath!\negative.dat2"
set "ocvWorkDir="
goto :eof


:L_rmNegativeDat
(
  echo.
  echo # !date! !time! %0 ========================================
) 1>&2
:echo cd=!cd!
set "ocvWorkDir=."
if exist "!ocvSrcDirPath!/negative.dat" del /f /q "!ocvSrcDirPath!\negative.dat"
goto :eof


:L_mkPositiveDat
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
if not exist "!ocvPosDirPath!/!ocvIcoNameDirName!" mkdir "!ocvPosDirPath!/!ocvIcoNameDirName!"
pushd "!ocvPosDirPath!/!ocvIcoNameDirName!"
:echo cd=!cd!
set "ocvWorkDir=../../.."
set ocv
echo # !date! !time! ^
  opencv_createsamples.exe ^
    -info "!ocvPosDatFleName!" ^
    -img "!ocvWorkDir!/!ocvIcoDirPath:\=/!/!ocvIcoFileName!" ^
    -bg "!ocvWorkDir!/!ocvSrcDirPath:\=/!/negative.dat" ^
    -num !ocvNegImgNum! -maxxangle !ocvMaxxangle! -maxyangle !ocvMaxyangle! -maxzangle !ocvMaxzangle! ^
    -bgcolor !ocvIcoBgColor! -bgthresh !ocvIcoBgThresh! -w !ocvIcoWidth! -h !ocvIcoHeight!
opencv_createsamples.exe ^
    -info "!ocvPosDatFleName!" ^
    -img "!ocvWorkDir!/!ocvIcoDirPath:\=/!/!ocvIcoFileName!" ^
    -bg "!ocvWorkDir!/!ocvSrcDirPath:\=/!/negative.dat" ^
    -num !ocvNegImgNum! -maxxangle !ocvMaxxangle! -maxyangle !ocvMaxyangle! -maxzangle !ocvMaxzangle! ^
    -bgcolor !ocvIcoBgColor! -bgthresh !ocvIcoBgThresh! -w !ocvIcoWidth! -h !ocvIcoHeight!
echo # !date! !time! %0 errorlevel=!errorlevel! 1>&2
(
  for /f "usebackq tokens=*" %%y in (
    `type "!ocvPosDatFleName!"`
  ) do (
    echo !ocvIcoNameDirName!/%%y
    echo.>nul
  )
  echo.>nul
) > "../!ocvPosDatFleName!"
if exist "!ocvPosDatFleName!" del /f /q "!ocvPosDatFleName!"
popd
:echo cd=!cd!
set "ocvWorkDir="
goto :eof


:L_rmPositiveDat
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
:echo cd=!cd!
@rem if exist "!ocvPosDirPath!/!ocvIcoNameDirName!" rmdir /s /q "!ocvPosDirPath!/!ocvIcoNameDirName!"
if exist "!ocvPosDirPath!/!ocvPosDatFleName!" del /f /q "!ocvPosDirPath!\!ocvPosDatFleName!"
goto :eof


:L_mkPositiveVec
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
if not exist "!ocvVecDirPath!/!ocvIcoNameDirName!" mkdir "!ocvVecDirPath!/!ocvIcoNameDirName!"
pushd "!ocvPosDirPath!"
echo cd=!cd!
set "ocvWorkDir=../.."
:set "ocvWorkDir=.."
set ocv
echo # !date! !time! ^
  opencv_createsamples.exe ^
    -info "!ocvPosDatFleName!" ^
    -vec "!ocvVecFleName!" ^
    -num !ocvPosImgCnt! -w !ocvIcoWidth! -h !ocvIcoHeight!
opencv_createsamples.exe ^
    -info "!ocvPosDatFleName!" ^
    -vec "!ocvVecFleName!" ^
    -num !ocvPosImgCnt! -w !ocvIcoWidth! -h !ocvIcoHeight!
echo # !date! !time! %0 errorlevel=!errorlevel! 1>&2
popd
:echo cd=!cd!
set "ocvWorkDir="
goto :eof


:L_rmPositiveVec
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
:echo cd=!cd!
if exist "!ocvVecDirPath!/!ocvVecFleName!" del /f /q "!ocvVecDirPath!\!ocvVecFleName!"
goto :eof


:L_mkModelXml
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
if not exist "!ocvMdlDirPath!/!ocvIcoNameDirName!" mkdir "!ocvMdlDirPath!/!ocvIcoNameDirName!"
pushd "data"
:echo cd=!cd!
set "ocvWorkDir=.."
set /a ocvPosImgCnt8=!ocvPosImgCnt!*80/100
set ocv
echo # !date! !time! ^
  opencv_traincascade.exe ^
    -data "!ocvWorkDir!/!ocvMdlDirPath:\=/!/!ocvIcoNameDirName!" ^
    -vec "!ocvWorkDir!/!ocvVecDirPath:\=/!/!ocvVecFleName!" ^
    -bg "!ocvWorkDir!/!ocvSrcDirPath:\=/!/negative.dat" ^
    -numPos !ocvPosImgCnt8! -numNeg !ocvNegImgNum! -featureType LBP ^
    -maxFalseAlarmRate !ocvMaxFalseAlarmRate! -minHitRate !ocvMinHitRate! ^
    -w !ocvIcoWidth! -h !ocvIcoHeight! -numStages !ocvStage!
start /b /high /wait ^
  opencv_traincascade.exe ^
    -data "!ocvWorkDir!/!ocvMdlDirPath:\=/!/!ocvIcoNameDirName!" ^
    -vec "!ocvWorkDir!/!ocvVecDirPath:\=/!/!ocvVecFleName!" ^
    -bg "!ocvWorkDir!/!ocvSrcDirPath:\=/!/negative.dat" ^
    -numPos !ocvPosImgCnt8! -numNeg !ocvNegImgNum! -featureType LBP ^
    -maxFalseAlarmRate !ocvMaxFalseAlarmRate! -minHitRate !ocvMinHitRate! ^
    -w !ocvIcoWidth! -h !ocvIcoHeight! -numStages !ocvStage!
echo # !date! !time! %0 errorlevel=!errorlevel! 1>&2
popd
:echo cd=!cd!
set "ocvWorkDir="
goto :eof


:L_rmModelXml
(
  echo.
  echo # !date! !time! %0 !ocvIcoFileName! ========================================
) 1>&2
:echo cd=!cd!
@rem if exist "!ocvMdlDirPath!/!ocvIcoNameDirName!" rmdir /s /q "!ocvMdlDirPath!/!ocvIcoNameDirName!"
goto :eof

:L_mkCascadeDetect
title コマンドプロンプト - !ocvThisName! mkCascadeDetect.py
call .\venv\Scripts\activate
@rem python mkCascadeDetect.py
deactivate
goto :eof

0 件のコメント:

コメントを投稿