View Single Post
Old 5th May 2021, 08:11   #23  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Auto 2 Pass. [made simpler/bit faster, using YPlaneMax only, single script]

BLUELED_SingleInstance.avs
Code:
# BLUELED_SingleInstance.avs
############
# Requires Avs+, RT_Stats, Grunt(), FrameSel, plugins.
############
FN = "MBGV0403.MXF"     # Video FileName
##### Freq Changed Config #####
OP       = 1            # 0,1,2,3 ONLY. ::: 0=Tweak Test Area, 1=Create Frames file, 2=Extract Frames, 3=Auto Two pass, pass1 then pass2.
SHOW     = True         # True, shows metrics on OP==1 [False is quicker]
# Less Often changed
TEST_X   = 374          # Test AREA COORDS (Best Even only)
TEST_Y   = 210          #   Ditto
TEST_W   = 372          #   Ditto
TEST_H   = 700          #   Ditto
BITS8    = True         # (True) Metrics and DetTh are as 8 bit range [If change BITS8 then MUST also CHANGE DETTH, ie DETTH*4]
DETTH    = 50           # (50)   Detection Threshold [ 50 is 8 bit thresh, for 10 bit use eg (50*4) ]
YMAXTH   = 0.4          # (0.4)  YPlaneMax Threshold (100.0/256.0) about 0.4 % [Percentage of extreme (noise) pixels to ignore].
VFLIP    = True         # Vertical Flip if upside down
REJECT   = False        # Arg to Pass 2 Extract Frames. [Extract BLUE LED frames instead of good frames if TRUE]
############
Assert(0 <= OP <= 3,"BlueLED: 1 <= OP <= 3")           # Check Valid
FN     = FN.RT_GetFullPathName                         # Ensure full path if not already got one
Frames = FN.RT_FilenameSplit(7) + "_Frames.txt"       # If eg FN="...\MBGV0403.MXF" then Frames = "...\MBGV0403_Frames.txt"

LWLibavVideoSource(FN)                                 # Video Source

############
(VFLIP) ? FlipVertical : NOP

SHOW   = (OP==3) ? False : SHOW                        # Auto Switch OFF SHOW for pass 1 if two pass

if(OP==0)         { BlueLED_Tweak(TEST_X,TEST_Y,TEST_W,TEST_H,BITS8,YMaxTh) }                          # Tweak Coords
else if(OP==1)    { BlueLED_Detect(Frames,TEST_X,TEST_Y,TEST_W,TEST_H,BITS8,DetTh,YMaxTh,Show=SHOW) }  # Detect
else if(OP==2)    { BlueLED_FrameSel(Frames,REJECT) }                                                  # Extract Frames
else {                                                                                                 # Auto 2 Pass
    BlueLED_Detect(Frames,TEST_X,TEST_Y,TEST_W,TEST_H,BITS8,DetTh,YMaxTh,Show=SHOW).RT_ForceProcess()
    BlueLED_FrameSel(Frames,REJECT)
}
Return  Last

############

# OP=0
Function BlueLED_Tweak(clip c,int x,int y,int w,int h,Bool "Bits8",Float "YMaxTh") {
# Shows YPlaneMax(YMaxTh) of Test Area (x,y,w,h, marked in yellow)
# If Bits8, then Test Area is converted to 8 bit Y8, and metrics shown in range 0->255
    Bits8  = Default(Bits8,False)                      # If True, then metrics are for 8 bit
    YMaxTh = Default(YMaxTh,0.4)                       # YPlaneMax Threshold (%)
    tc = c.Crop(x,y,w,h)                               # Test Area
    tc = Bits8 ? tc.ConvertBits(8).ConvertToY8 : tc    # 8 Bit if requested
    SSS="""
        Subtitle(String(current_frame) + "] YMax=" + String(tc.YPlaneMax(threshold=YMaxTh)) , Size=48)
    """
    c.ScriptClip(SSS, Args="tc,YMaxTh",Local = true)
    OverLay(c.BlankClip(Length=1,Width=w,Height=h,Color=$FFFF00),x=x,y=y,Opacity=0.1) # Mark Test Area in yellow
    Return Last
}

# OP=1
Function BlueLED_Detect(clip c,String Fn, int x,int y,int w,int h,Bool "Bits8",int "DetTh",Float "YMaxTh", Bool "Show") {
    bpc    = c.BitsPerComponent()                          # Bit Depth of c clip
    Bits8  = Default(Bits8,False)                          # If True, then DetTh and metrics are for 8 bit
    DetTh  = Default(DetTh,Bits8?50:50*BitLShift(1,bpc-8)) # Detect Threshold. Compliant with Bits8 arg.
    YMaxTh = Default(YMaxTh,0.4)                           # YPlaneMax Threshold (%)
    Show   = Default(Show,False)
    Assert(fn!="","BlueLED_Detect: Fn Cannot be ''")
    Fn = Fn.RT_GetFullPathName
    RT_FileDelete(Fn)
    tc = c.Crop(x,y,w,h)
    tc = Bits8 ? tc.ConvertBits(8).ConvertToY8 : tc
    SSS="""
        YMaxCur = tc.YPlaneMax(YMAXTH)                     # YMax for current frame
        YMaxPrv = tc.YPlaneMax(YMAXTH,offset=-1)           # YMax for previous frame
        T = (YMaxCur <  DetTh && (YMaxPrv >= DetTh || current_frame==0))
        (T) ? RT_WriteFile(Fn,"%d    # %d",current_frame,YMaxCur,Append=current_frame>0) : NOP
        if(Show) {
            s = RT_String("%d] %s\\nYMaxCur=%d\\nYMaxPrv=%d\\n(DetTh=%d)",current_frame,T?"Bingo":"",YMaxCur,YMaxPrv,DetTh)
            Subtitle(s,Size=48,lsp=0)
        }
        Return Last
    """
    c.ScriptClip(SSS, Args="tc,DetTh,YMaxTh,Fn,Show",Local = true)
    Return Last
}

# OP=2
Function BlueLED_FrameSel(clip c,String Frames,Bool "Reject") { # Extract Frames from clip c
    Reject = Default(Reject,False)
    Assert(Frames!="","BlueLED_FrameSel: Frames Cannot be ''")
    Frames = Frames.RT_GetFullPathName                 # Add full path if not already got one
    Return FrameSel(c,Cmd=Frames,Ordered=True,reject=Reject)
}
Updated
__________________
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 May 2021 at 01:36. Reason: Updated
StainlessS is offline   Reply With Quote