View Single Post
Old 2nd October 2016, 17:31   #40  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
post #1 of 2

I had done this script update anyway, but requires RT_Stats v2.0 Beta2

Code:
###
# DetectSub.MI.avsi":- https://forum.doom9.org/showthread.php?p=1782036#post1782036
###
# DetectSub, MultiInstance script, MultiInstance not necessary here but easy convenient way to use Scriptclip, GScript and double quoted
#   strings in same script. Would have been nice if Avisynth also recognised groups of 5 sets of double quotes as well as groups of 3 and
# single double quotes.
#
# req RT_Stats v2.00 Beta 02, GScript, Grunt, CallCmd, MaskTools v2.0.
# RT_Stats v2.00 Beta2 first posted here:- http://forum.doom9.org/showthread.php?p=1782028#post1782028
#
###
Function DetectSub_MI(clip c,string "Override",
            \ Int "X",Int "Y",Int "W",Int "H",
            \ Int "Text_w",Int "Text_h",Int "TextCol",Int "TextVar",
            \ Int "Halo_L",Int "Halo_T",Int "Halo_R",Int "Halo_B",Int "HaloCol",Int "HaloVar",
            \ Int "THysCNT",Int "HHysCNT",Int "DHysCNT",Bool "ShowInPand",
             \ Int "Baffle_w",Int "Baffle_h",Float "Thresh_w",Float "Thresh_h",Bool "ReScan",
            \ Bool "Show",Int "stack",Bool "ShowArea",Int "AreaColor",String "DB",Bool "Debug") {
    myName="DetectSub_MI: "
    c
    IsPlus=FindStr(VersionString,"AviSynth+")!=0
    Assert(RT_FunctionExist("GScriptClip"),myName+"Essential GRunT plugin installed, http://forum.doom9.org/showthread.php?t=139337")
    Assert(IsPlus||RT_FunctionExist("GScript"),myName+"Essential GScript plugin installed, http://forum.doom9.org/showthread.php?t=147846")
    Assert(RT_FunctionExist("CallCmd"),myName+"Essential CallCmd plugin installed, http://forum.doom9.org/showthread.php?t=166063")
    Assert(RT_FunctionExist("mt_expand"),myName+"Essential MaskTools v2 plugin installed, http://forum.doom9.org/showthread.php?t=98985")
    Override=Default(OverRide,"")
    X=Default(X,0)                  Y=Default(Y,Height-120)         W=Default(W,0)                      H=Default(H,-8)
    W=(W<=0)?Width-X+W:W            H=(H<=0)?Height-Y+H:H
    Text_w=Default(Text_w,3)        Text_h=Default(Text_h,3)        TextCol=Default(TextCol,$FCFCFC)    TextVar=Default(TextVar,3)
    Halo_L=Default(Halo_L,3)        Halo_T=Default(Halo_T,4)        Halo_R=Default(Halo_R,5)            Halo_B=Default(Halo_B,6)
    HaloCol=Default(HaloCol,$030303)                                HaloVar=Default(HaloVar,3)
    THysCNT=Default(THysCNT,0)      HHysCNT=Default(HHysCNT,0)      DHysCNT=Default(DHysCNT,0)          ShowInpand=(Show)?Default(ShowInpand,False):False
    Baffle_w=Default(Baffle_w,0)    Baffle_h=Default(Baffle_h,0)
    Thresh_w=Default(Thresh_w,0.0)  Thresh_h=Default(Thresh_h,0.0)  ReScan=Default(ReScan,False)
    Show=Default(Show,False)                                        Stack=(Show)?default(Stack,0):0
    ShowArea=(Show)?Default(ShowArea,True):False                    AreaColor=Default(AreaColor,$FF00FF)
    DB=Default(DB,"")    UserDB=(DB!="")
    DB=(!UserDB) ? "~"+RT_LocalTimeString+".DB" : DB
    DB=RT_GetFullPathName(DB)
    Debug=Default(Debug,False)
    VW=Width VH=Height
    /*  Fields
            0) Status,  0=Unknown (unvisited), 1=Sub, 2 Not sub, 3 User OverRide
            1) X
            2) Y
            3) W
            4) H
    */
    RT_DBaseAlloc(DB,FrameCount,"iiiii")

    Function CnvStr(clip c) {
        Return c.IsRGB24?"ConvertToRGB24":c.IsRGB32?"ConvertToRGB32":c.IsYUY2?"ConvertToYUY2":c.IsYV12?"ConvertToYV12"
         \ :c.IsYV16?"c.ConvertToYV16":c.IsYV24?"c.ConvertToYV24":c.IsY8?"c.ConvertToY8":""
    }
    CNVS=CnvStr(c)
    Assert(CNVS!="",RT_String("%sRGB24, RGB32, YUY2, YV12, YV16, YV24,and Y8 Only",myName))
    Function ChrIsStar(String S)     {return RT_Ord(S)==42}
    Function ChrIsNul(String S)      {return RT_Ord(S)== 0}                             # End of String
    Function ChrIsHash(String S)     {return RT_Ord(S)==35}                             # #
    Function ChrIsDigit(String S)    {C=RT_Ord(S) return C>=48&&C<=57}
    FuncS="""
        Function ChrEatWhite(String S)   {i=1 C=RT_Ord(S,i) While(C==32||C>=8&&C<=13)       {i=i+1 C=RT_Ord(S,i)} return i>1?MidStr(S,i):S}
        Function ChrEatDigits(String S)  {i=1 C=RT_Ord(S,i) While(C>=48&&C<=57)             {i=i+1 C=RT_Ord(S,i)} return i>1?MidStr(S,i):S}
        Function Fn@@@(clip c,String DB,Int VW,Int VH,
                \ int X,int Y,int W,int H,Int Text_w,Int Text_h,Int Halo_L,Int Halo_T,Int Halo_R,Int Halo_B,
                \ Int Baffle_w,Int Baffle_h,Float Thresh_w,Float Thresh_h,Bool ReScan,
                \ clip dc,bool Show,int Stack,bool ShowArea,Int AreaColor,bool Debug) {
            c    n = current_frame
            Status = RT_DBaseGetField(DB,n,0)
            if(Show || Status <= 1) {                                                 # In here if Show or Status 0 or 1 (ie unvisited or Sub)
                if(Status==0) {                                                       # Unknown Status
                    Bingo = dc.RT_YInRangeLocate(Baffle_w=Baffle_w,Baffle_h=Baffle_h,
                        \ lo=255,hi=255,debug=Debug,Thresh_w=Thresh_w,Thresh_h=Thresh_h,ReScan=ReScan)
                    Status=(Bingo)?1:2
                    if(Status==1) {
                        RT_DBaseSet(DB,n,Status,X+YIRL_X,Y+YIRL_Y,YIRL_W,YIRL_H)
                    } else {
                        RT_DBaseSetField(DB,n,0,Status)
                    }
                }
                if(Show) {
                    if(Status==1) {
                        SUB_X = RT_DBaseGetField(DB,n,1)    SUB_Y = RT_DBaseGetField(DB,n,2)
                        SUB_W = RT_DBaseGetField(DB,n,3)    SUB_H = RT_DBaseGetField(DB,n,4)
                        SUB_W=(SUB_W+1)/2*2 SUB_H=(SUB_H+1)/2*2
                        OverLay(Last.BlankClip(color=AreaColor,width=SUB_W,height=SUB_H),x=SUB_X,y=SUB_Y,opacity=0.25)
                        RT_Subtitle("%d] Text @ x=%d y=%d w=%d h=%d",n,SUB_X,SUB_Y,SUB_W,SUB_H)
                    } else if(Status==3) {
                        RT_Subtitle("%d] User OverRide",n)
                    }
                }
            } # End (Show || Status)
            Return Last
        }
        #######################################
        # Unique Global Variables Initialization
        #######################################
        if(OverRide!="") {
            if(ChrIsStar(Override))  {OverRide=MidStr(OverRide,2)
            } else {
                Assert(Exist(OverRide),RT_String("%sOverRide File does not exist",myName))
                OverRide=RT_ReadTxtFromFile(Override)
            }
            LINES=RT_TxtQueryLines(OverRide)
            for(i=0,LINES-1) {
                SS=ChrEatWhite(RT_TxtGetLine(Override,i))
                S=SS
                if(!ChrIsNul(S) && !ChrIsHash(S)) {
                    if(ChrIsDigit(S)) {
                        StartF = RT_NumberValue(S)
                        EndF=StartF
                        S=ChrEatWhite(ChrEatDigits(S))
                        if(ChrIsComma(S)){S=MidStr(S,2) S=ChrEatWhite(S)}
                        if(ChrIsDigit(S)) {
                            EndF=RT_NumberValue(S)
                            S=ChrEatDigits(S)
                            if(EndF==0) { EndF=FrameCount-1}
                            Assert(StartF<=EndF,RT_String("%sError StartFrame(%d) > EndFrame(%d)",myName,StartF,EndF))
                        }
                        (Debug)?RT_DebugF("OverRiding Range %d,%d to not detected",StartF,EndF,name=myName):NOP
                        for(n=StartF,EndF) {RT_DBaseSetField(DB,n,0,3)}
                    }
                    S=ChrEatWhite(S)
                    if(ChrIsHash(S)) {S=""}
                    Assert(ChrIsNul(S),RT_String("%s *** NON-PARSE *** Error in Override Line %d\n'%s'",myName,i+1,SS))
                }
            }
        }
        rgb=ConvertToRGB32.Crop(X,Y,W,H).ResetMask    # Reset Alpha to White
        Txt=rgb.ColorKeyMask(TextCol,TextVar+1,TextVar+1,TextVar+1).ShowAlpha(Pixel_Type="Y8")   # Set in range pixels to black
        Hal=rgb.ColorKeyMask(HaloCol,HaloVar+1,HaloVar+1,HaloVar+1).ShowAlpha(Pixel_Type="Y8")
        Txt=Txt.Invert  # In range pixels white, else black
        Hal=Hal.Invert

        CHROMAPROC = "-128"

        HALO_MAX=Max(HALO_L,HALO_T,HALO_R,HALO_B)
        for(i=1,HALO_MAX) {
            HorFlg=(i<=HALO_L?1:0)+(i<=HALO_R?2:0)    VerFlg=(i<=HALO_T?1:0)+(i<=HALO_B?2:0)
            if(HorFlg+VerFlg==6)     {TXT=TXT.Mt_Expand(mode="square",                                   chroma=CHROMAPROC)
            } else {
                if(HorFlg==3)        {TXT=TXT.Mt_Expand(mode="horizontal",                               chroma=CHROMAPROC) HorFlg=0}
                if(VerFlg==3)        {TXT=TXT.Mt_Expand(mode="vertical",                                 chroma=CHROMAPROC) VerFlg=0}
                if(HorFlg!=0)        {TXT=TXT.Mt_Expand(mode=HorFlg==1? " 1  0  0  0" : " -1  0  0  0",  chroma=CHROMAPROC)         }
                if(VerFlg!=0)        {TXT=TXT.Mt_Expand(mode=VerFlg==1? " 0  1  0  0" : "  0 -1  0  0" , chroma=CHROMAPROC)         }
            }
        }

        TEXT_MAX=Max(TEXT_W,TEXT_H)
        for(i=1,TEXT_MAX) {
            HorFlg=(i<=TEXT_W?1:0) VerFlg=(i<=TEXT_H?1:0)
            if(HorFlg+VerFlg==2)    {HAL=HAL.Mt_Expand(mode="square",                                   chroma=CHROMAPROC)
            } else {
                if(HorFlg!=0)       {HAL=HAL.Mt_Expand(mode="horizontal",                               chroma=CHROMAPROC) }
                if(VerFlg!=0)       {HAL=HAL.Mt_Expand(mode="vertical",                                 chroma=CHROMAPROC) }
            }
        }

        # reduce strays a little
        WeeTxt=Txt
        WeeHal=Hal
        if(THysCNT>0) {
            For(i=1,THysCNT) {WeeTxt=WeeTxt.MT_Inpand(mode="both",chroma=CHROMAPROC)}
            Txt=WeeTxt.MT_Hysteresis(Txt,chroma=CHROMAPROC)
        }
        if(HHysCNT>0) {
            For(i=1,HHysCNT) {WeeHal=WeeHal.MT_Inpand(mode="both",chroma=CHROMAPROC)}
            Hal=WeeHal.MT_Hysteresis(Hal,chroma=CHROMAPROC)
        }

        dc=Mt_Logic(Txt,Hal,"and", chroma=CHROMAPROC)

        dc2=dc
        if(DHysCNT>0) {
            For(i=1,DHysCNT) {dc2=dc2.MT_Inpand(mode="both",chroma=CHROMAPROC)}
            dc=dc2.MT_Hysteresis(dc,chroma=CHROMAPROC)
            dc2 = (ShowInPand) ? dc2 : dc
        }

        if(Show && (Stack>=1 && Stack<=2)) {
            If(ShowInPand) {Txt=WeeTxt Hal=WeeHal}
            dc2=dc2.Eval(CnvS).AddBorders(X,0,Width-X-W,0).AddBorders(0,22,0,0,$C0C0C0).Subtitle("DETECT",y=2)
            if(Stack==2) {
                TXT = TXT.Eval(CnvS).AddBorders(X,0,Width-X-W,0).AddBorders(0,22,0,0,$C0C0C0).Subtitle("TEXT",y=2)
                HAL = HAL.Eval(CnvS).AddBorders(X,0,Width-X-W,0).AddBorders(0,22,0,0,$C0C0C0).Subtitle("HALO",y=2)
                c=StackVertical(Last,dc2,TXT,HAL)
            } else {
                c=StackVertical(Last,dc2)
            }
        } else {c=Last}
        if(Show&&ShowArea) {
            WW=(W+1)/2*2   HH=(H+1)/2*2    # YV12 Legal
            Msk=c.Blankclip(width=WW+2,height=HH+2,Length=1,pixel_type="YV12").Killaudio
            Infix=RT_string("(((x<=1|x>=%d|y<=1|y>=%d)&(x<4|x>=%d)&(y<4|y>=%d)|(x==0|x==%d|y==0|y==%d)))?255:0", W-2,H-2,W-4,H-4,W-1,H-1)
            Msk=Msk.mt_lutspa(relative = false,yExpr=mt_Polish(InFix), chroma = CHROMAPROC)
            Mrk=Msk.BlankClip(Color=AreaColor)
            Msk=Msk.Loop(FrameCount,0,0)    Mrk=Mrk.Loop(FrameCount,0,0)
            c=c.Overlay(Mrk,x=X-1,y=Y-1,Mask=Msk,Mode="Blend",Opacity=0.5)
        }
        RT_DBaseSetID(DB,0,c.FrameCount,c.FrameRate,VW,VH,X,Y,W,H)
        #######################################
        # Unique Runtime Call, GScriptClip must be a one-liner:
        #######################################
        ARGS="DB,VW,VH,X,Y,W,H,Text_w,Text_h,Halo_L,Halo_T,Halo_R,Halo_B,Baffle_w,Baffle_h,Thresh_w,Thresh_h,ReScan,dc,Show,Stack,Showarea,AreaColor,debug"
        c.GScriptClip("Fn@@@(last, "+ARGS+")", local=true, args=ARGS)
        return Last
    """
    #######################################
    # Unique Identifier Definition
    #######################################
    GIFunc="DetectSub_MI"             # Function Name, Supply unique name for your multi-instance function.
    GIName=GIFunc+"_InstanceNumber"    # Name of the Instance number Global
    RT_IncrGlobal(GIName)              # Increment Instance Global (init to 1 if not already exists)
    GID   = GIFunc + "_" + String(Eval(GIName))
    InstS = RT_StrReplace(FuncS,"@@@","_"+GID)
#   RT_WriteFile("DEBUG_"+GID+".TXT","%s",InstS)
    (!IsPlus) ? GEval(InstS) : Eval(InstS)
    (!UserDB)?CallCmd(close=RT_String("""CMD /C chcp 1252 && del "%s" """,DB), hide=true, Synchronous=7):NOP  # Auto delete non-User DB file on clip closure.
    Return Last
}
EDITED: Error setting chroma to "1", changed to "-128"
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 14th August 2021 at 02:56. Reason: error setting chroma to "1", changed to "-128"
StainlessS is offline   Reply With Quote