B4J Question am i threading

threading:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("MainPage")
    MainForm.Show
    MainForm.Title = APPTITLE
    ProcessStream1
    ProcessStream2
End Sub

Sub ProcessStream1
    Dim x As Int
    Dim vcMat1 As OCVMat
    Dim mVideoCapture1 As OCVVideoCapture
    Dim streamPath1 As String = "rtsp://admin:admin@192.168.168.132:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture1.Initialize1(streamPath1)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture1.grab
            x=x+1
        Loop
        mVideoCapture1.retrieve1(vcMat1)
        iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream2
    Dim x As Int
    Dim vcMat2 As OCVMat
    Dim mVideoCapture2 As OCVVideoCapture
    Dim streamPath2 As String = "rtsp://admin:admin@192.168.168.133:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture2.Initialize1(streamPath2)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture2.grab
            x=x+1
        Loop
        mVideoCapture2.grab
        mVideoCapture2.retrieve1(vcMat2)
        iv2.SetImage( Utils.MatToBitmap(vcMat2) )
        Sleep(SleepRateInt)
    Loop
End Sub
will this code create a thread for processStream1 and another thread for ProcessStream2 ? if so what tool do i use to be able to see that myself ?
 
streaming 6 ip camera's. i can do this with other software but with my program its stutters. trying to make it go program run faster. using about 12 percent of my cpu when running in release mode. what tool do use to see if each camera is getting its own thread ?? using jopencv lib. here is full code.
threading or not ?:
#Region Project Attributes
    #MainFormWidth: 1500
    #MainFormHeight: 1000
#End Region

'===========================================================================================
' REQUIRED for jOpenCV.
'===========================================================================================
' This will copy the required DLLs to App dir. Change accordingly to the location of the DLLs folder.
' Alternatively, you can also manually place the DLLS there, or in a folder that is in the java classpath.
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\jOpenCVDlls" "."
'
' If you want to distribute it as a standalone package, copy your DLLs to the projects root folder
' DLL names can vary in other jOpenCV versions so be sure to modify accordingly.
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ b4jcv_344_01.dll
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ opencv_ffmpeg344_64.dll
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ tbb12.dll
'
'===========================================================================================

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI

    Private APPTITLE As String = "[B4J] jOpenCV - Sample 01: Image manipulations"
    
    

    Private iv1, iv2 ,iv3 ,iv4,iv5,iv6 As ImageView
    
    Private CurImageView As String = "NONE"
    
    Private doProcessStream1, isProcessingStream1 As Boolean
    Private doProcessStream2, isProcessingStream2 As Boolean
    'Private srcImg As Image

    'Dim morphElement As Object            ' Will create it once and use it in dilate/erode operations
    Private mCore As OCVCore            ' Declare it before any instantiable jOpenCV class!
    Private OCV As OCVHelper            ' Helper class for in-place constructors
    Private mType As OCVCvType            ' Contains some defined constant types for image processing
    Private mImgProc As OCVImgproc

    
    Private btnImage, btnVideo, btnCam, btnStream As Button
    Private cannySLider As Slider
    Private ToggleROIswap, ToggleFlip, ToggleBlur, ToggleCanny, ToggleDilate, ToggleErode, ToggleFFT2D As ToggleButton

    Private cannyThreshold As Double = 30

    Private doProcessStream, isProcessingStream As Boolean
    Private srcImg As Image

    Dim morphElement As Object            ' Will create it once and use it in dilate/erode operations
    Private Button1 As Button
    Private Button2 As Button
    Private BLeft As Button
    Private BRight As Button
    Private BDown As Button
    Private BUp As Button
    
    
    Private SleepRate As TextField
    Private GrabRate As TextField

    Private SleepRateInt As Int = 1000
    Private GrabRateInt As Int = 7
    
    Private ExitProgram As Button
End Sub


Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("MainPage")
    MainForm.Show
    MainForm.Title = APPTITLE
    ProcessStream1
    ProcessStream2
    ProcessStream3
    ProcessStream4
    ProcessStream5
    ProcessStream6
End Sub

Sub ProcessStream1
    Dim x As Int
    Dim vcMat1 As OCVMat
    Dim mVideoCapture1 As OCVVideoCapture
    Dim streamPath1 As String = "rtsp://admin:admin@192.168.168.132:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture1.Initialize1(streamPath1)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture1.grab
            x=x+1
        Loop
        mVideoCapture1.retrieve1(vcMat1)
        iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream2
    Dim x As Int
    Dim vcMat2 As OCVMat
    Dim mVideoCapture2 As OCVVideoCapture
    Dim streamPath2 As String = "rtsp://admin:admin@192.168.168.133:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture2.Initialize1(streamPath2)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture2.grab
            x=x+1
        Loop
        mVideoCapture2.grab
        mVideoCapture2.retrieve1(vcMat2)
        iv2.SetImage( Utils.MatToBitmap(vcMat2) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream3
    Dim x As Int
    Dim vcMat3 As OCVMat
    Dim mVideoCapture3 As OCVVideoCapture
    Dim streamPath3 As String = "rtsp://admin:admin@192.168.168.130:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture3.Initialize1(streamPath3)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture3.grab
            x=x+1
        Loop
        mVideoCapture3.grab
        mVideoCapture3.retrieve1(vcMat3)
        iv3.SetImage( Utils.MatToBitmap(vcMat3) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream4
    Dim x As Int
    Dim vcMat4 As OCVMat
    Dim mVideoCapture4 As OCVVideoCapture
    
    Dim streamPath4 As String = "rtsp://admin:admin@192.168.168.134:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture4.Initialize1(streamPath4)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture4.grab
            x=x+1
        Loop
        mVideoCapture4.grab
        mVideoCapture4.retrieve1(vcMat4)
        iv4.SetImage( Utils.MatToBitmap(vcMat4) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream5
    Dim x As Int
    Dim vcMat5 As OCVMat
    Dim mVideoCapture5 As OCVVideoCapture
    
    Dim streamPath5 As String = "rtsp://admin:admin@192.168.168.135:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture5.Initialize1(streamPath5)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture5.grab
            x=x+1
        Loop
        mVideoCapture5.grab
        mVideoCapture5.retrieve1(vcMat5)
        iv5.SetImage( Utils.MatToBitmap(vcMat5) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream6
    Dim x As Int
    Dim vcMat6 As OCVMat
    Dim mVideoCapture6 As OCVVideoCapture
    
    Dim streamPath6 As String = "rtsp://admin:admin@192.168.168.136:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture6.Initialize1(streamPath6)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture6.grab
            x=x+1
        Loop
        mVideoCapture6.grab
        mVideoCapture6.retrieve1(vcMat6)
        iv6.SetImage( Utils.MatToBitmap(vcMat6) )
        Sleep(SleepRateInt)
    Loop
End Sub




Private Sub Button1_Click
    'make image bigger
    If CurImageView = "NONE" Then
        Return
    End If
    Dim H As Double
    Dim W As Double
    If CurImageView = "iv1" Then
        H = iv1.Height + 50
        W = iv1.Width + 50
        iv1.setsize(W,H)
    End If
    If CurImageView = "iv2" Then
        H = iv2.Height + 50
        W = iv2.Width + 50
        iv2.setsize(W,H)
    End If
    If CurImageView = "iv3" Then
        H = iv3.Height + 50
        W = iv3.Width + 50
        iv3.setsize(W,H)
    End If
    If CurImageView = "iv4" Then
        H = iv4.Height + 50
        W = iv4.Width + 50
        iv4.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height + 50
        W = iv5.Width + 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height + 50
        W = iv5.Width + 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv6" Then
        H = iv6.Height + 50
        W = iv6.Width + 50
        iv6.setsize(W,H)
    End If
End Sub

Private Sub Button2_Click
    'make image smaller
    If CurImageView = "NONE" Then
        Return
    End If
    Dim H As Double
    Dim W As Double
    If CurImageView = "iv1" Then
        H = iv1.Height - 50
        W = iv1.Width - 50
        iv1.setsize(W,H)
    End If
    If CurImageView = "iv2" Then
        H = iv2.Height - 50
        W = iv2.Width - 50
        iv2.setsize(W,H)
    End If
    If CurImageView = "iv3" Then
        H = iv3.Height - 50
        W = iv3.Width - 50
        iv3.setsize(W,H)
    End If
    If CurImageView = "iv4" Then
        H = iv4.Height - 50
        W = iv4.Width - 50
        iv4.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height - 50
        W = iv5.Width - 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height - 50
        W = iv5.Width - 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv6" Then
        H = iv6.Height - 50
        W = iv6.Width - 50
        iv6.setsize(W,H)
    End If
End Sub

Private Sub BLeft_Click
    'move image to the left
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Left = iv1.Left -1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Left = iv2.Left -1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Left = iv3.Left -1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Left = iv4.Left -1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Left = iv5.Left -1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Left = iv6.Left -1
        Return
    End If
End Sub

Private Sub BRight_Click
    'move image right
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Left = iv1.Left +1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Left = iv2.Left +1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Left = iv3.Left +1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Left = iv4.Left +1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Left = iv5.Left +1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Left = iv6.Left +1
        Return
    End If
End Sub

Private Sub BDown_Click
    'move image down
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Top = iv1.Top + 1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Top = iv2.Top + 1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Top = iv3.Top + 1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Top = iv4.Top + 1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Top = iv5.Top + 1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Top = iv6.Top + 1
        Return
    End If
End Sub

Private Sub BUp_Click
    'move image up
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Top = iv1.Top - 1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Top = iv2.Top - 1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Top = iv3.Top - 1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Top = iv4.Top - 1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Top = iv5.Top - 1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Top = iv6.Top - 1
        Return
    End If
End Sub

Private Sub iv1_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv1"
End Sub

Private Sub iv2_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv2"
End Sub

Private Sub iv3_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv3"
End Sub

Private Sub iv4_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv4"
End Sub

Private Sub iv5_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv5"
End Sub

Private Sub iv6_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv6"
End Sub

Private Sub SleepRate_TextChanged (Old As String, New As String)
    SleepRateInt = New
End Sub

Private Sub GrabRate_TextChanged (Old As String, New As String)
    GrabRateInt = New
End Sub

Private Sub ExitProgram_Click
    ExitApplication
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
fair warning erel, i code so poorly looking at the code may make your eyes bleed.
Duplicating code can indeed cause eye bleeding.

Your code runs on the main thread. My guess is that Utils.MatToBitmap is the bottleneck.
You can use the Threading library to run this code on a background thread:
B4X:
mVideoCapture3.grab
        mVideoCapture3.retrieve1(vcMat3)
        iv3.SetImage( Utils.MatToBitmap(vcMat3) )

It is not trivial to do it correctly. The best solution is for the library developer to add async methods and then you will be able to handle them with Wait for.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I have never tested this exact scenario, but I fear that using grab for each IP cam a given number of times is possibly blocking each one of the Subs.

What happens if you change each ProcessStreamX (also consider refactoring your code to use only one 'ProcessStream()' Sub which depends on an index) to read and process frames as they arrive? (untested code)
B4X:
Sub ProcessStreamX
    Dim x As Int
    Dim vcMat As OCVMat
    Dim mVideoCapture As OCVVideoCapture
    Dim streamPath As String = "rtsp://admin:admin@192.168.168.130:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture.Initialize1(streamPath)
    Dim FOREVER As Boolean = True
    do  while FOREVER

        if mVideoCapture.read(vcMat) Then
            ivX.SetImage( Utils.MatToBitmap(vcMat) )
        End if
        Sleep(SleepRateInt)
    Loop
End Sub

If it behaves better, possibly it will be easier to get what you want starting from this point ?
 
Upvote 0
i started out with code that looks very much like what you have. i got it working and then copied it for the next camera and then the next etc. etc. i moved from the read function to grab and retrieve because of the frame buffer. seems i can't control the size of it. it looked like it was filling up. the only frame you can get was the oldest frame. so not only was the program falling behind it was also displaying the oldest frame in the buffer. i read that grab is cheap cpu usage wise, i had seen where others have said the solution to getting the newest frame is to have a thread that does nothing but grab frames. i'm almost done with the threading part, i've removed the multi grab part. i'll post the new code to ask if i am indeed threading. if so i'll look at going in the direction your suggestion.
 
Upvote 0
got it. program is now moving much faster than the camera's. even added back in the extra grabbing part. the program was using a lot of cpu power but now its down to 17 percent. grabbing for some reason seems to help on the cpu usage. i can see in resource monitor that the threads are spread out across the cpu's. for me the problem is solved, i asked was i threading correctly and how can i tell if i'm threading. i was not threading but i am now. i can use resource monitor in the future if i ever need to manually thread again. thanks to all. here's the code in case anyone wants it. erel, ether cover one eye or put on some sunglasses. now, onward to motion detection !!!!

proper threading:
#Region Project Attributes
    #MainFormWidth: 1500
    #MainFormHeight: 1000
#End Region

'===========================================================================================
' REQUIRED for jOpenCV.
'===========================================================================================
' This will copy the required DLLs to App dir. Change accordingly to the location of the DLLs folder.
' Alternatively, you can also manually place the DLLS there, or in a folder that is in the java classpath.
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\jOpenCVDlls" "."
'
' If you want to distribute it as a standalone package, copy your DLLs to the projects root folder
' DLL names can vary in other jOpenCV versions so be sure to modify accordingly.
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ b4jcv_344_01.dll
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ opencv_ffmpeg344_64.dll
#CustomBuildAction: After Packager, %WINDIR%\System32\robocopy.exe, ..\ temp\build\bin\ tbb12.dll
'
'===========================================================================================

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI

    Private APPTITLE As String = "[B4J] jOpenCV - Sample 01: Image manipulations"
    
    Private iv1, iv2 ,iv3 ,iv4,iv5,iv6 As ImageView
    
    Private CurImageView As String = "NONE"
    
    Private doProcessStream1, isProcessingStream1 As Boolean
    Private doProcessStream2, isProcessingStream2 As Boolean
    'Private srcImg As Image

    'Dim morphElement As Object            ' Will create it once and use it in dilate/erode operations
    Private mCore As OCVCore            ' Declare it before any instantiable jOpenCV class!
    Private OCV As OCVHelper            ' Helper class for in-place constructors
    Private mType As OCVCvType            ' Contains some defined constant types for image processing
    Private mImgProc As OCVImgproc

    
    Private btnImage, btnVideo, btnCam, btnStream As Button
    Private cannySLider As Slider
    Private ToggleROIswap, ToggleFlip, ToggleBlur, ToggleCanny, ToggleDilate, ToggleErode, ToggleFFT2D As ToggleButton

    Private cannyThreshold As Double = 30

    Private doProcessStream, isProcessingStream As Boolean
    Private srcImg As Image

    Dim morphElement As Object            ' Will create it once and use it in dilate/erode operations
    Private Button1 As Button
    Private Button2 As Button
    Private BLeft As Button
    Private BRight As Button
    Private BDown As Button
    Private BUp As Button
    
    
    Private SleepRate As TextField
    Private GrabRate As TextField

    Private SleepRateInt As Int = 1000
    Private GrabRateInt As Int = 15
    
    Private ExitProgram As Button
    Dim Thread1 As Thread
    Dim Thread2 As Thread
    Dim Thread3 As Thread
    Dim Thread4 As Thread
    Dim Thread5 As Thread
    Dim Thread6 As Thread
    
    Dim Lock1 As Lock
    Dim Lock2 As Lock
    Dim Lock3 As Lock
    Dim Lock4 As Lock
    Dim Lock5 As Lock
    Dim Lock6 As Lock
        
    Dim vcMat1 As OCVMat
    Dim vcMat2 As OCVMat
    Dim vcMat3 As OCVMat
    Dim vcMat4 As OCVMat
    Dim vcMat5 As OCVMat
    Dim vcMat6 As OCVMat
End Sub


Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("MainPage")
    MainForm.Show
    MainForm.Title = APPTITLE
    'ProcessStream1
    'ProcessStream2
    'ProcessStream3
    'ProcessStream4
    'ProcessStream5
    'ProcessStream6
    Dim MyArgs(0) As Object
    
    Thread1.Initialise("thread1")
    Lock1.Initialize(True)
    Thread1.Name = "B4A Thread 1"
    Thread1.Start(Null,"ThreadSub1",MyArgs)

    Thread2.Initialise("thread2")
    Lock2.Initialize(True)
    Thread2.Name = "B4A Thread 2"
    Thread2.Start(Null,"ThreadSub2",MyArgs)

    Thread3.Initialise("thread3")
    Lock3.Initialize(True)
    Thread3.Name = "B4A Thread 3"
    Thread3.Start(Null,"ThreadSub3",MyArgs)

    Thread4.Initialise("thread4")
    Lock4.Initialize(True)
    Thread4.Name = "B4A Thread 4"
    Thread4.Start(Null,"ThreadSub4",MyArgs)

    Thread5.Initialise("thread5")
    Lock5.Initialize(True)
    Thread5.Name = "B4A Thread 5"
    Thread5.Start(Null,"ThreadSub5",MyArgs)

    Thread6.Initialise("thread6")
    Lock6.Initialize(True)
    Thread6.Name = "B4A Thread 6"
    Thread6.Start(Null,"ThreadSub6",MyArgs)

    
End Sub

'________________________________________________________________
Sub ThreadSub1
    Dim x As Int
    Dim mVideoCapture1 As OCVVideoCapture
    Dim streamPath1 As String = "rtsp://admin:admin@192.168.168.132:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture1.Initialize1(streamPath1)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture1.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            
            mVideoCapture1.retrieve1(vcMat1)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread1.RunOnGuiThread("Setiv1",Params)
            ok = Lock1.WaitFor(1000)
        Loop
    Loop
End Sub

Sub Setiv1
    iv1.SetImage(Utils.MatToBitmap(vcMat1) )
    Lock1.Unlock
End Sub

Sub Thread1_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    'Msgbox(error, "Thread1 Ended")
    xui.MsgboxAsync(error,"ERROR")
End Sub
'____________________________________________________________________________
'________________________________________________________________
Sub ThreadSub2
    Dim x As Int
    Dim mVideoCapture2 As OCVVideoCapture
    Dim streamPath2 As String = "rtsp://admin:admin@192.168.168.133:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture2.Initialize1(streamPath2)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture2.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            
            mVideoCapture2.retrieve1(vcMat2)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread2.RunOnGuiThread("Setiv2",Params)
            ok = Lock2.WaitFor(1000)
        Loop
    Loop
End Sub

Sub Setiv2
    iv2.SetImage(Utils.MatToBitmap(vcMat2) )
    Lock2.Unlock
End Sub

Sub Thread2_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    xui.MsgboxAsync(error,"ERROR on thread 2")
End Sub
'____________________________________________________________________________
'________________________________________________________________
Sub ThreadSub3
    Dim x As Int
    Dim mVideoCapture3 As OCVVideoCapture
    Dim streamPath3 As String = "rtsp://admin:admin@192.168.168.130:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture3.Initialize1(streamPath3)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture3.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            mVideoCapture3.retrieve1(vcMat3)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread3.RunOnGuiThread("Setiv3",Params)
            ok = Lock3.WaitFor(1000)
        Loop
    Loop
End Sub

Sub Setiv3
    iv3.SetImage(Utils.MatToBitmap(vcMat3) )
    Lock3.Unlock
End Sub

Sub Thread3_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    'Msgbox(error, "Thread1 Ended")
    xui.MsgboxAsync(error,"ERROR on thread 3")
End Sub
'____________________________________________________________________________
'________________________________________________________________
Sub ThreadSub4
    Dim x As Int
    Dim mVideoCapture4 As OCVVideoCapture
    Dim streamPath4 As String = "rtsp://admin:admin@192.168.168.134:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture4.Initialize1(streamPath4)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture4.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            
            mVideoCapture4.retrieve1(vcMat4)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread4.RunOnGuiThread("Setiv4",Params)
            ok = Lock4.WaitFor(1000)
        Loop
    Loop
End Sub

Sub Setiv4
    iv4.SetImage(Utils.MatToBitmap(vcMat4) )
    Lock4.Unlock
End Sub

Sub Thread4_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    'Msgbox(error, "Thread1 Ended")
    xui.MsgboxAsync(error,"ERROR on thread 4")
End Sub
'____________________________________________________________________________
'________________________________________________________________
Sub ThreadSub5
    Dim x As Int
    Dim mVideoCapture5 As OCVVideoCapture
    Dim streamPath5 As String = "rtsp://admin:admin@192.168.168.135:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture5.Initialize1(streamPath5)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture5.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            
            mVideoCapture5.retrieve1(vcMat5)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread5.RunOnGuiThread("Setiv5",Params)
            ok = Lock5.WaitFor(200)
        Loop
    Loop
End Sub

Sub Setiv5
    iv5.SetImage(Utils.MatToBitmap(vcMat5) )
    Lock5.Unlock
End Sub

Sub Thread5_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    'Msgbox(error, "Thread1 Ended")
    xui.MsgboxAsync(error,"ERROR on thread 5")
End Sub
'____________________________________________________________________________
'________________________________________________________________
Sub ThreadSub6
    Dim x As Int
    Dim mVideoCapture6 As OCVVideoCapture
    Dim streamPath6 As String = "rtsp://admin:admin@192.168.168.136:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture6.Initialize1(streamPath6)
    Dim Params(0) As Object
    Dim ok As Boolean = False
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture6.grab
            x=x+1
        Loop
        ok = False
        Do Until ok
            mVideoCapture6.retrieve1(vcMat6)
        'iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        'Sleep(SleepRateInt)
            Thread6.RunOnGuiThread("Setiv6",Params)
            ok = Lock6.WaitFor(1000)
            
        Loop
    Loop
End Sub

Sub Setiv6
    iv6.SetImage(Utils.MatToBitmap(vcMat6) )
    Lock6.Unlock
End Sub

Sub Thread6_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    'Msgbox(error, "Thread1 Ended")
    xui.MsgboxAsync(error,"ERROR on thread 6")
End Sub
'____________________________________________________________________________

Sub ProcessStream1
    Dim x As Int
    Dim vcMat1 As OCVMat
    Dim mVideoCapture1 As OCVVideoCapture
    Dim streamPath1 As String = "rtsp://admin:admin@192.168.168.132:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture1.Initialize1(streamPath1)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x= 0
        Do Until x = GrabRateInt
            mVideoCapture1.grab
            x=x+1
        Loop
        mVideoCapture1.retrieve1(vcMat1)
        iv1.SetImage( Utils.MatToBitmap(vcMat1) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream2
    Dim x As Int
    Dim vcMat2 As OCVMat
    Dim mVideoCapture2 As OCVVideoCapture
    Dim streamPath2 As String = "rtsp://admin:admin@192.168.168.133:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture2.Initialize1(streamPath2)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture2.grab
            x=x+1
        Loop
        mVideoCapture2.grab
        mVideoCapture2.retrieve1(vcMat2)
        iv2.SetImage( Utils.MatToBitmap(vcMat2) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream3
    Dim x As Int
    Dim vcMat3 As OCVMat
    Dim mVideoCapture3 As OCVVideoCapture
    Dim streamPath3 As String = "rtsp://admin:admin@192.168.168.130:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture3.Initialize1(streamPath3)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture3.grab
            x=x+1
        Loop
        mVideoCapture3.grab
        mVideoCapture3.retrieve1(vcMat3)
        iv3.SetImage( Utils.MatToBitmap(vcMat3) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream4
    Dim x As Int
    Dim vcMat4 As OCVMat
    Dim mVideoCapture4 As OCVVideoCapture
    
    Dim streamPath4 As String = "rtsp://admin:admin@192.168.168.134:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture4.Initialize1(streamPath4)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture4.grab
            x=x+1
        Loop
        mVideoCapture4.grab
        mVideoCapture4.retrieve1(vcMat4)
        iv4.SetImage( Utils.MatToBitmap(vcMat4) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream5
    Dim x As Int
    Dim vcMat5 As OCVMat
    Dim mVideoCapture5 As OCVVideoCapture
    
    Dim streamPath5 As String = "rtsp://admin:admin@192.168.168.135:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    
    mVideoCapture5.Initialize1(streamPath5)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture5.grab
            x=x+1
        Loop
        mVideoCapture5.grab
        mVideoCapture5.retrieve1(vcMat5)
        iv5.SetImage( Utils.MatToBitmap(vcMat5) )
        Sleep(SleepRateInt)
    Loop
End Sub
Sub ProcessStream6
    Dim x As Int
    Dim vcMat6 As OCVMat
    Dim mVideoCapture6 As OCVVideoCapture
    
    Dim streamPath6 As String = "rtsp://admin:admin@192.168.168.136:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"
    mVideoCapture6.Initialize1(streamPath6)
    Dim FOREVER As Boolean = True
    do  while FOREVER
        x=0
        Do Until x = GrabRateInt
            mVideoCapture6.grab
            x=x+1
        Loop
        mVideoCapture6.grab
        mVideoCapture6.retrieve1(vcMat6)
        iv6.SetImage( Utils.MatToBitmap(vcMat6) )
        Sleep(SleepRateInt)
    Loop
End Sub




Private Sub Button1_Click
    'make image bigger
    If CurImageView = "NONE" Then
        Return
    End If
    Dim H As Double
    Dim W As Double
    If CurImageView = "iv1" Then
        H = iv1.Height + 50
        W = iv1.Width + 50
        iv1.setsize(W,H)
    End If
    If CurImageView = "iv2" Then
        H = iv2.Height + 50
        W = iv2.Width + 50
        iv2.setsize(W,H)
    End If
    If CurImageView = "iv3" Then
        H = iv3.Height + 50
        W = iv3.Width + 50
        iv3.setsize(W,H)
    End If
    If CurImageView = "iv4" Then
        H = iv4.Height + 50
        W = iv4.Width + 50
        iv4.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height + 50
        W = iv5.Width + 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height + 50
        W = iv5.Width + 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv6" Then
        H = iv6.Height + 50
        W = iv6.Width + 50
        iv6.setsize(W,H)
    End If
End Sub

Private Sub Button2_Click
    'make image smaller
    If CurImageView = "NONE" Then
        Return
    End If
    Dim H As Double
    Dim W As Double
    If CurImageView = "iv1" Then
        H = iv1.Height - 50
        W = iv1.Width - 50
        iv1.setsize(W,H)
    End If
    If CurImageView = "iv2" Then
        H = iv2.Height - 50
        W = iv2.Width - 50
        iv2.setsize(W,H)
    End If
    If CurImageView = "iv3" Then
        H = iv3.Height - 50
        W = iv3.Width - 50
        iv3.setsize(W,H)
    End If
    If CurImageView = "iv4" Then
        H = iv4.Height - 50
        W = iv4.Width - 50
        iv4.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height - 50
        W = iv5.Width - 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv5" Then
        H = iv5.Height - 50
        W = iv5.Width - 50
        iv5.setsize(W,H)
    End If
    If CurImageView = "iv6" Then
        H = iv6.Height - 50
        W = iv6.Width - 50
        iv6.setsize(W,H)
    End If
End Sub

Private Sub BLeft_Click
    'move image to the left
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Left = iv1.Left -1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Left = iv2.Left -1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Left = iv3.Left -1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Left = iv4.Left -1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Left = iv5.Left -1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Left = iv6.Left -1
        Return
    End If
End Sub

Private Sub BRight_Click
    'move image right
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Left = iv1.Left +1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Left = iv2.Left +1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Left = iv3.Left +1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Left = iv4.Left +1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Left = iv5.Left +1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Left = iv6.Left +1
        Return
    End If
End Sub

Private Sub BDown_Click
    'move image down
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Top = iv1.Top + 1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Top = iv2.Top + 1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Top = iv3.Top + 1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Top = iv4.Top + 1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Top = iv5.Top + 1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Top = iv6.Top + 1
        Return
    End If
End Sub

Private Sub BUp_Click
    'move image up
    If CurImageView = "NONE" Then
        Return
    End If
    If CurImageView = "iv1" Then
        iv1.Top = iv1.Top - 1
        Return
    End If
    If CurImageView = "iv2" Then
        iv2.Top = iv2.Top - 1
        Return
    End If
    If CurImageView = "iv3" Then
        iv3.Top = iv3.Top - 1
        Return
    End If
    If CurImageView = "iv4" Then
        iv4.Top = iv4.Top - 1
        Return
    End If
    If CurImageView = "iv5" Then
        iv5.Top = iv5.Top - 1
        Return
    End If
    If CurImageView = "iv6" Then
        iv6.Top = iv6.Top - 1
        Return
    End If
End Sub

Private Sub iv1_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv1"
End Sub

Private Sub iv2_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv2"
End Sub

Private Sub iv3_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv3"
End Sub

Private Sub iv4_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv4"
End Sub

Private Sub iv5_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv5"
End Sub

Private Sub iv6_MouseClicked (EventData As MouseEvent)
    CurImageView = "iv6"
End Sub

Private Sub SleepRate_TextChanged (Old As String, New As String)
    SleepRateInt = New
End Sub

Private Sub GrabRate_TextChanged (Old As String, New As String)
    GrabRateInt = New
End Sub

Private Sub ExitProgram_Click
    Thread1.Interrupt
    Thread2.Interrupt
    Thread3.Interrupt
    Thread4.Interrupt
    Thread5.Interrupt
    Thread6.Interrupt
    ExitApplication
End Sub
 
Upvote 0
Top