{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}

module RiskWeaver.Cmd.Core where

import Control.Monad
import Control.Monad.Trans.Reader (ReaderT, ask, runReaderT)
import RiskWeaver.DSL.Core qualified as DSL
import Data.ByteString qualified as BS
import Data.FileEmbed (embedFile)
import Data.List (sortBy)
import Data.Map qualified as Map
import Data.Maybe
import Data.Text qualified as T
import Data.Vector (Vector)
import Data.Vector qualified as Vector
import RiskWeaver.Display
import RiskWeaver.Format.Coco
import qualified RiskWeaver.Metric as Metric
import RiskWeaver.Metric

import Options.Applicative
import System.Random
import Text.Printf

data CocoCommand
  = ListImages {CocoCommand -> FilePath
cocoFile :: FilePath}
  | ListCategories {cocoFile :: FilePath}
  | ListAnnotations {cocoFile :: FilePath}
  | ListCocoResult {CocoCommand -> FilePath
cocoResultFile :: FilePath}
  | ShowImage
      { cocoFile :: FilePath,
        CocoCommand -> FilePath
imageFile :: FilePath,
        CocoCommand -> Bool
enableBoundingBox :: Bool
      }
  | ShowDetectionImage
      { cocoFile :: FilePath,
        cocoResultFile :: FilePath,
        imageFile :: FilePath,
        CocoCommand -> Maybe Double
scoreThreshold :: Maybe Double
      }
  | Evaluate
      { cocoFile :: FilePath,
        cocoResultFile :: FilePath,
        CocoCommand -> Maybe Double
iouThreshold :: Maybe Double,
        scoreThreshold :: Maybe Double,
        CocoCommand -> Maybe Int
imageId :: Maybe Int
      }
  | ShowFalseNegative
      { cocoFile :: FilePath,
        cocoResultFile :: FilePath,
        iouThreshold :: Maybe Double,
        scoreThreshold :: Maybe Double,
        imageId :: Maybe Int
      }
  | ShowRisk
      { cocoFile :: FilePath,
        cocoResultFile :: FilePath,
        iouThreshold :: Maybe Double,
        scoreThreshold :: Maybe Double,
        imageId :: Maybe Int
      }
  | GenerateRiskWeightedDataset
      { cocoFile :: FilePath,
        cocoResultFile :: FilePath,
        CocoCommand -> FilePath
cocoOutputFile :: FilePath,
        iouThreshold :: Maybe Double,
        scoreThreshold :: Maybe Double
      }
  | BashCompletion
  deriving (Int -> CocoCommand -> FilePath -> FilePath
[CocoCommand] -> FilePath -> FilePath
CocoCommand -> FilePath
(Int -> CocoCommand -> FilePath -> FilePath)
-> (CocoCommand -> FilePath)
-> ([CocoCommand] -> FilePath -> FilePath)
-> Show CocoCommand
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
$cshowsPrec :: Int -> CocoCommand -> FilePath -> FilePath
showsPrec :: Int -> CocoCommand -> FilePath -> FilePath
$cshow :: CocoCommand -> FilePath
show :: CocoCommand -> FilePath
$cshowList :: [CocoCommand] -> FilePath -> FilePath
showList :: [CocoCommand] -> FilePath -> FilePath
Show, CocoCommand -> CocoCommand -> Bool
(CocoCommand -> CocoCommand -> Bool)
-> (CocoCommand -> CocoCommand -> Bool) -> Eq CocoCommand
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CocoCommand -> CocoCommand -> Bool
== :: CocoCommand -> CocoCommand -> Bool
$c/= :: CocoCommand -> CocoCommand -> Bool
/= :: CocoCommand -> CocoCommand -> Bool
Eq)

data RiskCommands = 
  RiskCommands
    { RiskCommands
-> Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
showRisk :: Coco -> [CocoResult] -> Maybe Double -> Maybe Double -> Maybe ImageId -> IO ()
    , RiskCommands
-> Coco
-> [CocoResult]
-> FilePath
-> Maybe Double
-> Maybe Double
-> IO ()
generateRiskWeightedDataset :: Coco -> [CocoResult] -> FilePath -> Maybe Double -> Maybe Double -> IO ()
    }

listImages :: Coco -> IO ()
listImages :: Coco -> IO ()
listImages Coco
coco = do
  FilePath -> IO ()
putStrLn FilePath
"-- list images --"
  -- first column is image id
  -- second column is image file name
  -- third column is image width
  -- fourth column is image height
  -- fifth column is image license
  -- sixth column is image date captured
  FilePath -> IO ()
putStrLn FilePath
"id\tfile_name\twidth\theight\tlicense\tdate_captured"
  [CocoImage] -> (CocoImage -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Coco -> [CocoImage]
cocoImages Coco
coco) ((CocoImage -> IO ()) -> IO ()) -> (CocoImage -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CocoImage {Int
Maybe Int
Maybe Text
Text
ImageId
cocoImageId :: ImageId
cocoImageWidth :: Int
cocoImageHeight :: Int
cocoImageFileName :: Text
cocoImageLicense :: Maybe Int
cocoImageDateCoco :: Maybe Text
cocoImageId :: CocoImage -> ImageId
cocoImageWidth :: CocoImage -> Int
cocoImageHeight :: CocoImage -> Int
cocoImageFileName :: CocoImage -> Text
cocoImageLicense :: CocoImage -> Maybe Int
cocoImageDateCoco :: CocoImage -> Maybe Text
..} -> do
    FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> FilePath
forall a. Show a => a -> FilePath
show (ImageId -> Int
unImageId ImageId
cocoImageId) FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
T.unpack Text
cocoImageFileName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
cocoImageWidth FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
cocoImageHeight FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Maybe Int -> FilePath
forall a. Show a => a -> FilePath
show Maybe Int
cocoImageLicense FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Maybe Text -> FilePath
forall a. Show a => a -> FilePath
show Maybe Text
cocoImageDateCoco

listCategories :: Coco -> IO ()
listCategories :: Coco -> IO ()
listCategories Coco
coco = do
  FilePath -> IO ()
putStrLn FilePath
"-- list categories --"
  -- first column is category id
  -- second column is category name
  -- third column is category supercategory
  FilePath -> IO ()
putStrLn FilePath
"id\tname\tsupercategory"
  [CocoCategory] -> (CocoCategory -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Coco -> [CocoCategory]
cocoCategories Coco
coco) ((CocoCategory -> IO ()) -> IO ())
-> (CocoCategory -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CocoCategory {Text
CategoryId
cocoCategoryId :: CategoryId
cocoCategoryName :: Text
cocoCategorySupercategory :: Text
cocoCategoryId :: CocoCategory -> CategoryId
cocoCategoryName :: CocoCategory -> Text
cocoCategorySupercategory :: CocoCategory -> Text
..} -> do
    FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ CategoryId -> FilePath
forall a. Show a => a -> FilePath
show CategoryId
cocoCategoryId FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
T.unpack Text
cocoCategoryName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Text -> FilePath
T.unpack Text
cocoCategorySupercategory

listAnnotations :: Coco -> IO ()
listAnnotations :: Coco -> IO ()
listAnnotations Coco
coco = do
  FilePath -> IO ()
putStrLn FilePath
"-- list annotations --"
  -- first column is annotation id
  -- second column is annotation image id
  -- third column is annotation category id
  -- fourth column is annotation segmentation
  -- fifth column is annotation area
  -- sixth column is annotation bbox
  -- seventh column is annotation iscrowd
  FilePath -> IO ()
putStrLn FilePath
"id\timage_id\tcategory_id\tsegmentation\tarea\tbbox\tiscrowd"
  [CocoAnnotation] -> (CocoAnnotation -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (Coco -> [CocoAnnotation]
cocoAnnotations Coco
coco) ((CocoAnnotation -> IO ()) -> IO ())
-> (CocoAnnotation -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CocoAnnotation {Double
Int
Maybe Int
Maybe [[Double]]
CoCoBoundingBox
CategoryId
ImageId
cocoAnnotationId :: Int
cocoAnnotationImageId :: ImageId
cocoAnnotationCategory :: CategoryId
cocoAnnotationSegment :: Maybe [[Double]]
cocoAnnotationArea :: Double
cocoAnnotationBbox :: CoCoBoundingBox
cocoAnnotationIsCrowd :: Maybe Int
cocoAnnotationId :: CocoAnnotation -> Int
cocoAnnotationImageId :: CocoAnnotation -> ImageId
cocoAnnotationCategory :: CocoAnnotation -> CategoryId
cocoAnnotationSegment :: CocoAnnotation -> Maybe [[Double]]
cocoAnnotationArea :: CocoAnnotation -> Double
cocoAnnotationBbox :: CocoAnnotation -> CoCoBoundingBox
cocoAnnotationIsCrowd :: CocoAnnotation -> Maybe Int
..} -> do
    FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
cocoAnnotationId FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ ImageId -> FilePath
forall a. Show a => a -> FilePath
show ImageId
cocoAnnotationImageId FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ CategoryId -> FilePath
forall a. Show a => a -> FilePath
show CategoryId
cocoAnnotationCategory FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Maybe [[Double]] -> FilePath
forall a. Show a => a -> FilePath
show Maybe [[Double]]
cocoAnnotationSegment FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Double -> FilePath
forall a. Show a => a -> FilePath
show Double
cocoAnnotationArea FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ CoCoBoundingBox -> FilePath
forall a. Show a => a -> FilePath
show CoCoBoundingBox
cocoAnnotationBbox FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Maybe Int -> FilePath
forall a. Show a => a -> FilePath
show Maybe Int
cocoAnnotationIsCrowd

listCocoResult :: [CocoResult] -> IO ()
listCocoResult :: [CocoResult] -> IO ()
listCocoResult [CocoResult]
cocoResults = do
  FilePath -> IO ()
putStrLn FilePath
"-- list coco result --"
  -- first column is image id
  -- second column is category id
  -- third column is score
  -- fourth column is bbox
  FilePath -> IO ()
putStrLn FilePath
"image_id\tcategory_id\tscore\tbbox"
  [CocoResult] -> (CocoResult -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [CocoResult]
cocoResults ((CocoResult -> IO ()) -> IO ()) -> (CocoResult -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CocoResult
cocoResult -> do
    FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ ImageId -> FilePath
forall a. Show a => a -> FilePath
show (CocoResult -> ImageId
cocoResultImageId CocoResult
cocoResult) FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ CategoryId -> FilePath
forall a. Show a => a -> FilePath
show (CocoResult -> CategoryId
cocoResultCategory CocoResult
cocoResult) FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Score -> FilePath
forall a. Show a => a -> FilePath
show (CocoResult -> Score
cocoResultScore CocoResult
cocoResult) FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"\t" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ CoCoBoundingBox -> FilePath
forall a. Show a => a -> FilePath
show (CocoResult -> CoCoBoundingBox
cocoResultBbox CocoResult
cocoResult)

evaluate :: Coco -> [CocoResult] -> Maybe Double -> Maybe Double -> Maybe ImageId -> IO ()
evaluate :: Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
evaluate Coco
coco [CocoResult]
cocoResults Maybe Double
iouThreshold Maybe Double
scoreThresh Maybe ImageId
mImageId = do
  -- Print mAP
  let cocoMap :: CocoMap
cocoMap =
        let cocoMap' :: CocoMap
cocoMap' = Coco -> [CocoResult] -> CocoMap
toCocoMap Coco
coco [CocoResult]
cocoResults
         in case Maybe ImageId
mImageId of
              Maybe ImageId
Nothing -> CocoMap
cocoMap'
              Just ImageId
imageId -> CocoMap
cocoMap' {cocoMapImageIds = [imageId]}
      iouThreshold' :: IOU
iouThreshold' = case Maybe Double
iouThreshold of
        Maybe Double
Nothing -> Double -> IOU
IOU Double
0.5
        Just Double
iouThreshold -> Double -> IOU
IOU Double
iouThreshold
      scoreThresh' :: Score
scoreThresh' = case Maybe Double
scoreThresh of
        Maybe Double
Nothing -> Double -> Score
Score Double
0.1
        Just Double
scoreThresh -> Double -> Score
Score Double
scoreThresh
      mAP :: (Double, [(CategoryId, Double)])
mAP = CocoMap -> IOU -> (Double, [(CategoryId, Double)])
Metric.mAP CocoMap
cocoMap IOU
iouThreshold'
      confusionMatrix :: ConfusionMatrix
confusionMatrix = CocoMap -> IOU -> Score -> ConfusionMatrix
Metric.confusionMatrix CocoMap
cocoMap IOU
iouThreshold' Score
scoreThresh'
  FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s %s" FilePath
"#Category" FilePath
"AP"
  [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId -> do
    FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Double -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s %.3f" (Text -> FilePath
T.unpack (CocoCategory -> Text
cocoCategoryName ((CocoMap -> Map CategoryId CocoCategory
cocoMapCocoCategory CocoMap
cocoMap) Map CategoryId CocoCategory -> CategoryId -> CocoCategory
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId))) (([(CategoryId, Double)] -> Map CategoryId Double
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ((Double, [(CategoryId, Double)]) -> [(CategoryId, Double)]
forall a b. (a, b) -> b
snd (Double, [(CategoryId, Double)])
mAP)) Map CategoryId Double -> CategoryId -> Double
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId)
  FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> Double -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s %.3f" FilePath
"mAP" ((Double, [(CategoryId, Double)]) -> Double
forall a b. (a, b) -> a
fst (Double, [(CategoryId, Double)])
mAP)
  FilePath -> IO ()
putStrLn FilePath
""

  -- Print confusion matrix
  FilePath -> IO ()
putStrLn FilePath
"#confusion matrix of recall: row is ground truth, column is prediction."
  FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" FilePath
"#GT \\ DT"
  FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" FilePath
"Backgroud"
  let !! :: Map k a -> k -> a
(!!) Map k a
dat k
key = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
0 (k -> Map k a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k a
dat)
      !!! :: Map k (Map k a) -> k -> Map k a
(!!!) Map k (Map k a)
dat k
key = Map k a -> Maybe (Map k a) -> Map k a
forall a. a -> Maybe a -> a
fromMaybe Map k a
forall k a. Map k a
Map.empty (k -> Map k (Map k a) -> Maybe (Map k a)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
key Map k (Map k a)
dat)
  [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId -> do
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" (Text -> FilePath
T.unpack (CocoCategory -> Text
cocoCategoryName ((CocoMap -> Map CategoryId CocoCategory
cocoMapCocoCategory CocoMap
cocoMap) Map CategoryId CocoCategory -> CategoryId -> CocoCategory
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId)))
  FilePath -> IO ()
putStrLn FilePath
""
  [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId -> do
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" (Text -> FilePath
T.unpack (CocoCategory -> Text
cocoCategoryName ((CocoMap -> Map CategoryId CocoCategory
cocoMapCocoCategory CocoMap
cocoMap) Map CategoryId CocoCategory -> CategoryId -> CocoCategory
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId)))
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12d" (((ConfusionMatrix -> Map (Gt CategoryId) (Map (Dt CategoryId) Int)
confusionMatrixRecall ConfusionMatrix
confusionMatrix) Map (Gt CategoryId) (Map (Dt CategoryId) Int)
-> Gt CategoryId -> Map (Dt CategoryId) Int
forall {k} {k} {a}. Ord k => Map k (Map k a) -> k -> Map k a
!!! CategoryId -> Gt CategoryId
forall a. a -> Gt a
Gt CategoryId
categoryId) Map (Dt CategoryId) Int -> Dt CategoryId -> Int
forall {a} {k}. (Num a, Ord k) => Map k a -> k -> a
!! Dt CategoryId
forall a. Dt a
DtBackground)
    [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId' -> do
      FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12d" (((ConfusionMatrix -> Map (Gt CategoryId) (Map (Dt CategoryId) Int)
confusionMatrixRecall ConfusionMatrix
confusionMatrix) Map (Gt CategoryId) (Map (Dt CategoryId) Int)
-> Gt CategoryId -> Map (Dt CategoryId) Int
forall {k} {k} {a}. Ord k => Map k (Map k a) -> k -> Map k a
!!! CategoryId -> Gt CategoryId
forall a. a -> Gt a
Gt CategoryId
categoryId) Map (Dt CategoryId) Int -> Dt CategoryId -> Int
forall {a} {k}. (Num a, Ord k) => Map k a -> k -> a
!! (CategoryId -> Dt CategoryId
forall a. a -> Dt a
Dt CategoryId
categoryId'))
    FilePath -> IO ()
putStrLn FilePath
""
  FilePath -> IO ()
putStrLn FilePath
""

  FilePath -> IO ()
putStrLn FilePath
"#confusion matrix of precision: row is prediction, column is ground truth."
  FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"#%-11s" FilePath
"DT \\ GT"
  FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" FilePath
"Backgroud"
  [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId -> do
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" (Text -> FilePath
T.unpack (CocoCategory -> Text
cocoCategoryName ((CocoMap -> Map CategoryId CocoCategory
cocoMapCocoCategory CocoMap
cocoMap) Map CategoryId CocoCategory -> CategoryId -> CocoCategory
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId)))
  FilePath -> IO ()
putStrLn FilePath
""
  [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId -> do
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12s" (Text -> FilePath
T.unpack (CocoCategory -> Text
cocoCategoryName ((CocoMap -> Map CategoryId CocoCategory
cocoMapCocoCategory CocoMap
cocoMap) Map CategoryId CocoCategory -> CategoryId -> CocoCategory
forall k a. Ord k => Map k a -> k -> a
Map.! CategoryId
categoryId)))
    FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12d" (((ConfusionMatrix -> Map (Dt CategoryId) (Map (Gt CategoryId) Int)
confusionMatrixPrecision ConfusionMatrix
confusionMatrix) Map (Dt CategoryId) (Map (Gt CategoryId) Int)
-> Dt CategoryId -> Map (Gt CategoryId) Int
forall {k} {k} {a}. Ord k => Map k (Map k a) -> k -> Map k a
!!! (CategoryId -> Dt CategoryId
forall a. a -> Dt a
Dt CategoryId
categoryId)) Map (Gt CategoryId) Int -> Gt CategoryId -> Int
forall {a} {k}. (Num a, Ord k) => Map k a -> k -> a
!! Gt CategoryId
forall a. Gt a
GtBackground)
    [CategoryId] -> (CategoryId -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (CocoMap -> [CategoryId]
cocoMapCategoryIds CocoMap
cocoMap) ((CategoryId -> IO ()) -> IO ()) -> (CategoryId -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CategoryId
categoryId' -> do
      FilePath -> IO ()
putStr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%-12d" (((ConfusionMatrix -> Map (Dt CategoryId) (Map (Gt CategoryId) Int)
confusionMatrixPrecision ConfusionMatrix
confusionMatrix) Map (Dt CategoryId) (Map (Gt CategoryId) Int)
-> Dt CategoryId -> Map (Gt CategoryId) Int
forall {k} {k} {a}. Ord k => Map k (Map k a) -> k -> Map k a
!!! (CategoryId -> Dt CategoryId
forall a. a -> Dt a
Dt CategoryId
categoryId)) Map (Gt CategoryId) Int -> Gt CategoryId -> Int
forall {a} {k}. (Num a, Ord k) => Map k a -> k -> a
!! (CategoryId -> Gt CategoryId
forall a. a -> Gt a
Gt CategoryId
categoryId'))
    FilePath -> IO ()
putStrLn FilePath
""

bashCompletion :: IO ()
bashCompletion :: IO ()
bashCompletion = do
  -- Read from bash_completion.d/risk-weaver-exe and write to stdout
  -- Inline the file content by tepmlate haskell
  let file :: ByteString
file = $(embedFile "bash_completion.d/risk-weaver-exe")
  ByteString -> IO ()
BS.putStr ByteString
file

opts :: Parser CocoCommand
opts :: Parser CocoCommand
opts =
  Mod CommandFields CocoCommand -> Parser CocoCommand
forall a. Mod CommandFields a -> Parser a
subparser
    ( FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"list-images" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> CocoCommand
ListImages (FilePath -> CocoCommand) -> Parser FilePath -> Parser CocoCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE")) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"list all images of coco file"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"list-categories" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> CocoCommand
ListCategories (FilePath -> CocoCommand) -> Parser FilePath -> Parser CocoCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE")) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"list all categories of coco file"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"list-annotations" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> CocoCommand
ListAnnotations (FilePath -> CocoCommand) -> Parser FilePath -> Parser CocoCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE")) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"list all annotations of coco file"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"list-coco-result" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> CocoCommand
ListCocoResult (FilePath -> CocoCommand) -> Parser FilePath -> Parser CocoCommand
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE")) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"list all coco result"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"show-image" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> FilePath -> Bool -> CocoCommand
ShowImage (FilePath -> FilePath -> Bool -> CocoCommand)
-> Parser FilePath -> Parser (FilePath -> Bool -> CocoCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE") Parser (FilePath -> Bool -> CocoCommand)
-> Parser FilePath -> Parser (Bool -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"IMAGE_FILE") Parser (Bool -> CocoCommand) -> Parser Bool -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch (FilePath -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"enable-bounding-box" Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> Char -> Mod FlagFields Bool
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'b' Mod FlagFields Bool -> Mod FlagFields Bool -> Mod FlagFields Bool
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod FlagFields Bool
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"enable bounding box")) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"show image by sixel"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"show-detection-image" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath -> FilePath -> FilePath -> Maybe Double -> CocoCommand
ShowDetectionImage (FilePath -> FilePath -> FilePath -> Maybe Double -> CocoCommand)
-> Parser FilePath
-> Parser (FilePath -> FilePath -> Maybe Double -> CocoCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE") Parser (FilePath -> FilePath -> Maybe Double -> CocoCommand)
-> Parser FilePath
-> Parser (FilePath -> Maybe Double -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"RESULT_FILE") Parser (FilePath -> Maybe Double -> CocoCommand)
-> Parser FilePath -> Parser (Maybe Double -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"IMAGE_FILE") Parser (Maybe Double -> CocoCommand)
-> Parser (Maybe Double) -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"score-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
's' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"score threshold"))) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"show detection image by sixel"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"evaluate" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath
-> FilePath
-> Maybe Double
-> Maybe Double
-> Maybe Int
-> CocoCommand
Evaluate (FilePath
 -> FilePath
 -> Maybe Double
 -> Maybe Double
 -> Maybe Int
 -> CocoCommand)
-> Parser FilePath
-> Parser
     (FilePath
      -> Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE") Parser
  (FilePath
   -> Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
-> Parser FilePath
-> Parser
     (Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"RESULT_FILE") Parser (Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
-> Parser (Maybe Double)
-> Parser (Maybe Double -> Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"iou-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'i' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"iou threshold")) Parser (Maybe Double -> Maybe Int -> CocoCommand)
-> Parser (Maybe Double) -> Parser (Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"score-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
's' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"score threshold")) Parser (Maybe Int -> CocoCommand)
-> Parser (Maybe Int) -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Int -> Parser (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"filter" Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'e' Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Int
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"filter with regex"))) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"evaluate coco result"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"show-risk" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath
-> FilePath
-> Maybe Double
-> Maybe Double
-> Maybe Int
-> CocoCommand
ShowRisk (FilePath
 -> FilePath
 -> Maybe Double
 -> Maybe Double
 -> Maybe Int
 -> CocoCommand)
-> Parser FilePath
-> Parser
     (FilePath
      -> Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE") Parser
  (FilePath
   -> Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
-> Parser FilePath
-> Parser
     (Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"RESULT_FILE") Parser (Maybe Double -> Maybe Double -> Maybe Int -> CocoCommand)
-> Parser (Maybe Double)
-> Parser (Maybe Double -> Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"iou-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'i' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"iou threshold")) Parser (Maybe Double -> Maybe Int -> CocoCommand)
-> Parser (Maybe Double) -> Parser (Maybe Int -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"score-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
's' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"score threshold")) Parser (Maybe Int -> CocoCommand)
-> Parser (Maybe Int) -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Int -> Parser (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Int -> Mod OptionFields Int -> Parser Int
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Int
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"filter" Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Int
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'e' Mod OptionFields Int
-> Mod OptionFields Int -> Mod OptionFields Int
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Int
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"filter with regex"))) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"show risk"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"generate-risk-weighted-dataset" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (FilePath
-> FilePath
-> FilePath
-> Maybe Double
-> Maybe Double
-> CocoCommand
GenerateRiskWeightedDataset (FilePath
 -> FilePath
 -> FilePath
 -> Maybe Double
 -> Maybe Double
 -> CocoCommand)
-> Parser FilePath
-> Parser
     (FilePath
      -> FilePath -> Maybe Double -> Maybe Double -> CocoCommand)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"FILE") Parser
  (FilePath
   -> FilePath -> Maybe Double -> Maybe Double -> CocoCommand)
-> Parser FilePath
-> Parser (FilePath -> Maybe Double -> Maybe Double -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"RESULT_FILE") Parser (FilePath -> Maybe Double -> Maybe Double -> CocoCommand)
-> Parser FilePath
-> Parser (Maybe Double -> Maybe Double -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReadM FilePath -> Mod ArgumentFields FilePath -> Parser FilePath
forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument ReadM FilePath
forall s. IsString s => ReadM s
str (FilePath -> Mod ArgumentFields FilePath
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"OUTPUT_FILE") Parser (Maybe Double -> Maybe Double -> CocoCommand)
-> Parser (Maybe Double) -> Parser (Maybe Double -> CocoCommand)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"iou-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'i' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"iou threshold")) Parser (Maybe Double -> CocoCommand)
-> Parser (Maybe Double) -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Double -> Parser (Maybe Double)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ReadM Double -> Mod OptionFields Double -> Parser Double
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Double
forall a. Read a => ReadM a
auto (FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"score-threshold" Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Double
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
's' Mod OptionFields Double
-> Mod OptionFields Double -> Mod OptionFields Double
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Double
forall (f :: * -> *) a. FilePath -> Mod f a
help FilePath
"score threshold"))) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"generate risk weighted dataset"))
        Mod CommandFields CocoCommand
-> Mod CommandFields CocoCommand -> Mod CommandFields CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> ParserInfo CocoCommand -> Mod CommandFields CocoCommand
forall a. FilePath -> ParserInfo a -> Mod CommandFields a
command FilePath
"bash-completion" (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (CocoCommand -> Parser CocoCommand
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure CocoCommand
BashCompletion) (FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"bash completion"))
    )

baseMain :: RiskCommands -> IO ()
baseMain :: RiskCommands -> IO ()
baseMain RiskCommands{Coco
-> [CocoResult]
-> FilePath
-> Maybe Double
-> Maybe Double
-> IO ()
Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
showRisk :: RiskCommands
-> Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
generateRiskWeightedDataset :: RiskCommands
-> Coco
-> [CocoResult]
-> FilePath
-> Maybe Double
-> Maybe Double
-> IO ()
showRisk :: Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
generateRiskWeightedDataset :: Coco
-> [CocoResult]
-> FilePath
-> Maybe Double
-> Maybe Double
-> IO ()
..} = do
  CocoCommand
cmd <- ParserPrefs -> ParserInfo CocoCommand -> IO CocoCommand
forall a. ParserPrefs -> ParserInfo a -> IO a
customExecParser (PrefsMod -> ParserPrefs
prefs PrefsMod
showHelpOnEmpty) (Parser CocoCommand -> InfoMod CocoCommand -> ParserInfo CocoCommand
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser (CocoCommand -> CocoCommand)
forall a. Parser (a -> a)
helper Parser (CocoCommand -> CocoCommand)
-> Parser CocoCommand -> Parser CocoCommand
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser CocoCommand
opts) (InfoMod CocoCommand
forall a. InfoMod a
fullDesc InfoMod CocoCommand -> InfoMod CocoCommand -> InfoMod CocoCommand
forall a. Semigroup a => a -> a -> a
<> FilePath -> InfoMod CocoCommand
forall a. FilePath -> InfoMod a
progDesc FilePath
"coco command line tool"))

  case CocoCommand
cmd of
    CocoCommand
BashCompletion -> IO ()
bashCompletion
    ListImages FilePath
cocoFile -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      Coco -> IO ()
listImages Coco
coco
    ListCategories FilePath
cocoFile -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      Coco -> IO ()
listCategories Coco
coco
    ListAnnotations FilePath
cocoFile -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      Coco -> IO ()
listAnnotations Coco
coco
    ListCocoResult FilePath
cocoResultFile -> do
      [CocoResult]
cocoResult <- FilePath -> IO [CocoResult]
readCocoResult FilePath
cocoResultFile
      [CocoResult] -> IO ()
listCocoResult [CocoResult]
cocoResult
    ShowImage FilePath
cocoFile FilePath
imageFile Bool
enableBoundingBox -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      Coco -> FilePath -> FilePath -> Bool -> IO ()
showImage Coco
coco FilePath
cocoFile FilePath
imageFile Bool
enableBoundingBox
    ShowDetectionImage FilePath
cocoFile FilePath
cocoResultFile FilePath
imageFile Maybe Double
scoreThreshold -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      Coco -> FilePath -> FilePath -> FilePath -> Maybe Double -> IO ()
showDetectionImage Coco
coco FilePath
cocoFile FilePath
cocoResultFile FilePath
imageFile Maybe Double
scoreThreshold
    Evaluate FilePath
cocoFile FilePath
cocoResultFile Maybe Double
iouThreshold Maybe Double
scoreThreshold Maybe Int
imageId -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      [CocoResult]
cocoResult <- FilePath -> IO [CocoResult]
readCocoResult FilePath
cocoResultFile
      Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
evaluate Coco
coco [CocoResult]
cocoResult Maybe Double
iouThreshold Maybe Double
scoreThreshold ((Int -> ImageId) -> Maybe Int -> Maybe ImageId
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> ImageId
ImageId Maybe Int
imageId)
    ShowRisk FilePath
cocoFile FilePath
cocoResultFile Maybe Double
iouThreshold Maybe Double
scoreThreshold Maybe Int
imageId -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      [CocoResult]
cocoResult <- FilePath -> IO [CocoResult]
readCocoResult FilePath
cocoResultFile
      Coco
-> [CocoResult]
-> Maybe Double
-> Maybe Double
-> Maybe ImageId
-> IO ()
showRisk Coco
coco [CocoResult]
cocoResult Maybe Double
iouThreshold Maybe Double
scoreThreshold ((Int -> ImageId) -> Maybe Int -> Maybe ImageId
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> ImageId
ImageId Maybe Int
imageId)
    GenerateRiskWeightedDataset FilePath
cocoFile FilePath
cocoResultFile FilePath
cocoOutputFile Maybe Double
iouThreshold Maybe Double
scoreThreshold -> do
      Coco
coco <- FilePath -> IO Coco
readCoco FilePath
cocoFile
      [CocoResult]
cocoResult <- FilePath -> IO [CocoResult]
readCocoResult FilePath
cocoResultFile
      Coco
-> [CocoResult]
-> FilePath
-> Maybe Double
-> Maybe Double
-> IO ()
generateRiskWeightedDataset Coco
coco [CocoResult]
cocoResult FilePath
cocoOutputFile Maybe Double
iouThreshold Maybe Double
scoreThreshold
    CocoCommand
_ -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()