
Imports System.Threading
Imports System.Drawing


Module modImageRecognition
    Public RecognizedImagesQueue As New Queue(24)

    Public Sub subStartImageRecognition()
        Try
            If ftnVisualRecognizerEnabled() Then
                If boolEnableRcc Then ftnRcc_Communicate("sense objects ", "OK", 500)
                If boolEnableRcc Then ftnRcc_Communicate("clear", "OK", 1000)
                RecognizedImagesQueue.Clear()
                If (RecognizedImagesQueue.Count <> 0) Then _
                    RecognizedImagesQueue.Dequeue()
                If boolEnableRcc Then ftnRcc_Communicate("events", "OK", 1000)
                RecognizedImagesQueue.Enqueue("")     'kludge
                classAddImageToQueue.boolGetMoreRecognizedImages = True
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subStartImageRecognition :  " & ex.ToString)
        End Try
    End Sub

    Public Sub subEndImageRecognition()
        Try
            If ftnVisualRecognizerConnected() Then
                classAddImageToQueue.boolGetMoreRecognizedImages = False
                If boolEnableRcc Then ftnRcc_Communicate("clear", "OK", 1000)
                classAddImageToQueue.boolGetMoreRecognizedImages = False
                RecognizedImagesQueue.Clear()
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subEndImageRecognition :  " & ex.ToString)
        End Try
    End Sub


    Public Function ftnGetNextObject() As String
        ftnGetNextObject = ""
        Try
            While (RecognizedImagesQueue.Count > 0) And (Not CBool(InStr(ftnGetNextObject, "object"))) _
                   And Not boolStop And Not boolShutDown
                ftnGetNextObject = Convert.ToString(RecognizedImagesQueue.Dequeue())
            End While
            If (Not CBool(InStr(ftnGetNextObject, "object"))) Then
                ftnGetNextObject = ""
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in ftnGetNextObject :  " & ex.ToString)
        End Try
    End Function






#Region "Image Queue"
    Public Class classAddImageToQueue
        Public Shared boolGetMoreRecognizedImages As Boolean = False

        Public Sub subGetMoreRecognizedImages()
            Try
                While Not boolShutDown
                    If boolGetMoreRecognizedImages Then
                        If ftnVisualRecognizerEnabled() Then subGetMoreRecognitions()
                    End If
                    Thread.Sleep(200)
                End While
            Catch ex As Exception
                frmDebug.subWriteLogs(" EXCEPTION in subGetMoreRecognizedImages :  " & ex.ToString)
            End Try
        End Sub


        Sub subGetMoreRecognitions()
            Try
                Dim intObjectCounter As Integer = 0
                Dim intQueueCounter As Integer = 0
                Dim strObjectRecognized As String = ""
                strObjectRecognized = ftnGetTextFromRecognizer() & " EndOfText" ' get the recognized landmarks 
                strObjectRecognized = strObjectRecognized.Replace(vbLf, "")
                Dim strRecognizedObjects As String() = strObjectRecognized.Split(vbCr)
                Dim strRecognizedObjectsSplit As String()
                While (intObjectCounter <= UBound(strRecognizedObjects)) And Not boolShutDown
                    If (CBool(InStr(strRecognizedObjects(intObjectCounter), "object"))) Then
                        Dim intSubStringStart As Integer = strRecognizedObjects(intObjectCounter).IndexOf("object")
                        strRecognizedObjects(intObjectCounter) = strRecognizedObjects(intObjectCounter).Substring(intSubStringStart, (strRecognizedObjects(intObjectCounter).Length - intSubStringStart))
                        strRecognizedObjectsSplit = strRecognizedObjects(intObjectCounter).Split(" ")
                        If (UBound(strRecognizedObjectsSplit) >= 6) Then
                            RecognizedImagesQueue.Enqueue(strRecognizedObjects(intQueueCounter))
                            intQueueCounter += 1
                        End If
                    End If
                    intObjectCounter += 1
                End While
            Catch ex As Exception
                frmDebug.subWriteLogs(" EXCEPTION in subGetMoreRecognitions :  " & ex.ToString)
            End Try
        End Sub

 
        Private Function ftnGetTextFromRecognizer() As String
            ftnGetTextFromRecognizer = ""
            Try
                If boolEnableRcc Then _
                    ftnGetTextFromRecognizer &= ftnGetTextRcc(1000)
            Catch ex As Exception
                frmDebug.subWriteLogs(" EXCEPTION in ftnGetTextFromRecognizer :  " & ex.ToString)
            End Try
        End Function

    End Class
#End Region



End Module
