Отправка изображения C ++ на сервер Python - PullRequest
0 голосов
/ 28 января 2019

Всех приветствую.Я пытаюсь сделать что-то вроде клиент-серверных приложений видеопотока.

В C ++ есть клиент, в Python есть сервер.Мне нужно отправить C ++ образ на сервер python.

Я пытался, но ничего не добился.

C ++ Код:

// main

int main()
{
    boost::asio::io_service io_service;
    DataManager* m = new DataManager(io_service);
    DesktopReader* r = new DesktopReader(GetDesktopWindow());

    m->ConnectToSocket("127.0.0.1", 9191);

    Mat s;

    while (1) {

        r->GetDesktopMat(s);


        int imgSize = s.total() * s.elemSize();
        uchar *iptr = s.data;

        cout << imgSize << endl;

        m->SendData(s.data, imgSize);
    }

    return 0;
}

// Диспетчер данных:

void DataManager::SendData(uchar* data, int size) {

    boost::system::error_code error;

    socket.write_some(boost::asio::buffer(data, size), error);

}

// DesktopReader:

void DesktopReader::GetDesktopMat(Mat &src)
{
    HDC hwindowDC, hwindowCompatibleDC;

    int height, width, srcheight, srcwidth;
    HBITMAP hbwindow;
    BITMAPINFOHEADER  bi;

    hwindowDC = GetDC(hwnd);
    hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
    SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

    RECT windowsize;    // get the height and width of the screen
    GetClientRect(hwnd, &windowsize);

    srcheight = windowsize.bottom;
    srcwidth = windowsize.right;
    height = windowsize.bottom / 1;
    width = windowsize.right / 1;

    src.create(height, width, CV_8UC4);

    hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = width;
    bi.biHeight = -height; 
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    SelectObject(hwindowCompatibleDC, hbwindow);
    StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); 
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 
    DeleteObject(hbwindow);
    DeleteDC(hwindowCompatibleDC);
    ReleaseDC(hwnd, hwindowDC);
}

Python:

while True:
    try:
        data = client.recv(8294400) # 8294400 - image size. From: int imgSize = s.total() * s.elemSize();
        if data:
            img = cv2.imdecode(np.fromstring(data, np.uint8), 1)
            print(img) # There i got None
        else:
            raise print('Client disconnected')
    except:
        client.close()
        return False

Что не так в коде?Как я могу получить изображение C ++ в Python?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...