A plugin that turns the current frame into an opencv mat and shares that via IPC

HireRyanToday

New Member
Open CV is the most popular image framework on the planet. There's projects out there, but they're too robust for me. They have their own opinionated frameworks and plugins on top of them. You could try to build your own code into them and enjoy compiler version and dependency hell. I need a plugin that's like frame->mat YOURCODE GOES HERE or something via ipc.

windows mail slot is SUPER easy to implement (if I can do it, I promise it's easy lol). Boost has good ipc libraries. If someone can even get me the boiler plate for a cv mat in the obs framework, I can add the IPC stuff and share a how to.

Then you can go compile whatever you want however you want and you need like 20 lines of code to consume the ipc. It just makes a generic interface to consume the most popular image framework on the planet
 

HireRyanToday

New Member
Just to share the mail slot ipc code, incase someone does want to take a stab:
Code:
// consumer:

#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

HANDLE hSlot;
LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL ReadSlot()
{
    DWORD cbMessage, cMessage, cbRead;
    BOOL fResult;
    LPTSTR lpszBuffer;
    TCHAR achID[80];
    DWORD cAllMessages;
    HANDLE hEvent;
    OVERLAPPED ov;

    cbMessage = cMessage = cbRead = 0;

    hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
    if (NULL == hEvent)
        return FALSE;
    ov.Offset = 0;
    ov.OffsetHigh = 0;
    ov.hEvent = hEvent;

    fResult = GetMailslotInfo(hSlot, // mailslot handle
        (LPDWORD)NULL,               // no maximum message size
        &cbMessage,                   // size of next message
        &cMessage,                    // number of messages
        (LPDWORD)NULL);              // no read time-out

    if (!fResult)
    {
        printf("GetMailslotInfo failed with %d.\n", GetLastError());
        return FALSE;
    }

    if (cbMessage == MAILSLOT_NO_MESSAGE)
    {
        printf("Waiting for a message...\n");
        return TRUE;
    }

    cAllMessages = cMessage;

    while (cMessage != 0)  // retrieve all messages
    {
        // Allocate memory for the message.

        lpszBuffer = (LPTSTR)GlobalAlloc(GPTR,
            lstrlen((LPTSTR)achID) * sizeof(TCHAR) + cbMessage);
        if (NULL == lpszBuffer)
            return FALSE;
        lpszBuffer[0] = '\0';

        fResult = ReadFile(hSlot,
            lpszBuffer,
            cbMessage,
            &cbRead,
            &ov);

        if (!fResult)
        {
            printf("ReadFile failed with %d.\n", GetLastError());
            GlobalFree((HGLOBAL)lpszBuffer);
            return FALSE;
        }
        cv::Mat mat = cv::Mat(640, 640, CV_8UC3, cv::Scalar(0, 0, 0));
        std::memcpy(mat.data, lpszBuffer, 640 * 640 * 3);
      
        cv::imshow("YOURWINDOW", mat);
        cv::waitKey(1);

        GlobalFree((HGLOBAL)lpszBuffer);

        fResult = GetMailslotInfo(hSlot,  // mailslot handle
            (LPDWORD)NULL,               // no maximum message size
            &cbMessage,                   // size of next message
            &cMessage,                    // number of messages
            (LPDWORD)NULL);              // no read time-out

        if (!fResult)
        {
            printf("GetMailslotInfo failed (%d)\n", GetLastError());
            return FALSE;
        }
    }
    CloseHandle(hEvent);
    return TRUE;
}


BOOL WINAPI MakeSlot(LPCTSTR lpszSlotName)
{
    hSlot = CreateMailslot(
        lpszSlotName,
        0,
        MAILSLOT_WAIT_FOREVER,
        (LPSECURITY_ATTRIBUTES)NULL);

    if (hSlot == INVALID_HANDLE_VALUE) {
        printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE;
    }
    else
        std::cout << "Mailslot created successfully.\n";
    return TRUE;
}

int main()
{
    MakeSlot(SlotName);


    cv::namedWindow("YOURWINDOW", cv::WINDOW_AUTOSIZE);
    while (true) {
        ReadSlot();
    }
}

// producer:

LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
    BOOL fResult;
    DWORD cbWritten;

    fResult = WriteFile(hSlot, lpszMessage,
                (DWORD)(lstrlen(lpszMessage) + 1) * sizeof(TCHAR),
                &cbWritten, (LPOVERLAPPED)NULL);

    if (!fResult) {
        printf("WriteFile failed with %d.\n", GetLastError());
        return FALSE;
    }

    printf("Slot written to successfully.\n");

    return TRUE;
}
cv::Mat mat =
        cv::imread("C:\\Repos\\tmp\\decode\\images\\output_0664.jpg");
            size_t sizeInBytes = mat.step[0] * mat.rows;

            LPVOID lpBuffer = malloc(sizeInBytes);

            HANDLE hFile;

            hFile = CreateFile(SlotName, GENERIC_WRITE,
                       FILE_SHARE_READ,
                       (LPSECURITY_ATTRIBUTES)NULL,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                       (HANDLE)NULL);

            if (hFile == INVALID_HANDLE_VALUE) {
                printf("CreateFile failed with %d.\n",
                       GetLastError());
                return FALSE;
            }
            DWORD bytesWritten;
            memcpy(lpBuffer, &mat.data[0], sizeInBytes);

            WriteFile(hFile, lpBuffer, sizeInBytes, &bytesWritten,
                  NULL);

            CloseHandle(hFile);

I know this to work for sending at least 640x640 images via ipc with very little programmatic overhead
 

HireRyanToday

New Member
My last response got flagged I think because I edited it too much. IPC code if you're interested

Code:
// consumer:

#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

HANDLE hSlot;
LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL ReadSlot()
{
    DWORD cbMessage, cMessage, cbRead;
    BOOL fResult;
    LPTSTR lpszBuffer;
    TCHAR achID[80];
    DWORD cAllMessages;
    HANDLE hEvent;
    OVERLAPPED ov;

    cbMessage = cMessage = cbRead = 0;

    hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));
    if (NULL == hEvent)
        return FALSE;
    ov.Offset = 0;
    ov.OffsetHigh = 0;
    ov.hEvent = hEvent;

    fResult = GetMailslotInfo(hSlot, // mailslot handle
        (LPDWORD)NULL,               // no maximum message size
        &cbMessage,                   // size of next message
        &cMessage,                    // number of messages
        (LPDWORD)NULL);              // no read time-out

    if (!fResult)
    {
        printf("GetMailslotInfo failed with %d.\n", GetLastError());
        return FALSE;
    }

    if (cbMessage == MAILSLOT_NO_MESSAGE)
    {
        printf("Waiting for a message...\n");
        return TRUE;
    }

    cAllMessages = cMessage;

    while (cMessage != 0)  // retrieve all messages
    {
        // Allocate memory for the message.

        lpszBuffer = (LPTSTR)GlobalAlloc(GPTR,
            lstrlen((LPTSTR)achID) * sizeof(TCHAR) + cbMessage);
        if (NULL == lpszBuffer)
            return FALSE;
        lpszBuffer[0] = '\0';

        fResult = ReadFile(hSlot,
            lpszBuffer,
            cbMessage,
            &cbRead,
            &ov);

        if (!fResult)
        {
            printf("ReadFile failed with %d.\n", GetLastError());
            GlobalFree((HGLOBAL)lpszBuffer);
            return FALSE;
        }
        cv::Mat mat = cv::Mat(640, 640, CV_8UC3, cv::Scalar(0, 0, 0));
        std::memcpy(mat.data, lpszBuffer, 640 * 640 * 3);
      
        cv::imshow("YOURWINDOW", mat);
        cv::waitKey(1);

        GlobalFree((HGLOBAL)lpszBuffer);

        fResult = GetMailslotInfo(hSlot,  // mailslot handle
            (LPDWORD)NULL,               // no maximum message size
            &cbMessage,                   // size of next message
            &cMessage,                    // number of messages
            (LPDWORD)NULL);              // no read time-out

        if (!fResult)
        {
            printf("GetMailslotInfo failed (%d)\n", GetLastError());
            return FALSE;
        }
    }
    CloseHandle(hEvent);
    return TRUE;
}


BOOL WINAPI MakeSlot(LPCTSTR lpszSlotName)
{
    hSlot = CreateMailslot(
        lpszSlotName,
        0,
        MAILSLOT_WAIT_FOREVER,
        (LPSECURITY_ATTRIBUTES)NULL);

    if (hSlot == INVALID_HANDLE_VALUE) {
        printf("CreateMailslot failed with %d\n", GetLastError());
        return FALSE;
    }
    else
        std::cout << "Mailslot created successfully.\n";
    return TRUE;
}

int main()
{
    MakeSlot(SlotName);


    cv::namedWindow("YOURWINDOW", cv::WINDOW_AUTOSIZE);
    while (true) {
        ReadSlot();
    }
}

// producer:

LPCTSTR SlotName = TEXT("\\\\.\\mailslot\\mailslot");

BOOL WriteSlot(HANDLE hSlot, LPCTSTR lpszMessage)
{
    BOOL fResult;
    DWORD cbWritten;

    fResult = WriteFile(hSlot, lpszMessage,
                (DWORD)(lstrlen(lpszMessage) + 1) * sizeof(TCHAR),
                &cbWritten, (LPOVERLAPPED)NULL);

    if (!fResult) {
        printf("WriteFile failed with %d.\n", GetLastError());
        return FALSE;
    }

    printf("Slot written to successfully.\n");

    return TRUE;
}
cv::Mat mat =
        cv::imread("C:\\Repos\\tmp\\decode\\images\\output_0664.jpg");
            size_t sizeInBytes = mat.step[0] * mat.rows;

            LPVOID lpBuffer = malloc(sizeInBytes);

            HANDLE hFile;

            hFile = CreateFile(SlotName, GENERIC_WRITE,
                       FILE_SHARE_READ,
                       (LPSECURITY_ATTRIBUTES)NULL,
                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                       (HANDLE)NULL);

            if (hFile == INVALID_HANDLE_VALUE) {
                printf("CreateFile failed with %d.\n",
                       GetLastError());
                return FALSE;
            }
            DWORD bytesWritten;
            memcpy(lpBuffer, &mat.data[0], sizeInBytes);

            WriteFile(hFile, lpBuffer, sizeInBytes, &bytesWritten,
                  NULL);

            CloseHandle(hFile);
 
Top