0

Can I use OpenCV to edit video captured from Ip camera with Dahua SDK? Here are the portion of Dahua sample code:

// initialized play list
BOOL bOpenRet = PLAY_OpenStream(g_lRealPort, 0, 0, 1024 * 500);
if (bOpenRet)
{
    //  start play
    BOOL bPlayRet = PLAY_Play(g_lRealPort, hMainWnd);
    if (bPlayRet)
    {
        //  monitor preview
        long lRealHandle = CLIENT_RealPlayEx(lLoginHandle, nChannelID, 0);
        if (0 != lRealHandle)
        {
            //  set recall function handling data
            CLIENT_SetRealDataCallBackEx(lRealHandle, RealDataCallBackEx, (DWORD)0, 0x1f);
        }
        else
        {
            //printf("Fail to play!\n");
            PLAY_Stop(g_lRealPort);
            PLAY_CloseStream(g_lRealPort);
        }
    }
    else
    {
        PLAY_CloseStream(g_lRealPort);
    }
}

The code above is connecting to cam using TCP and the streaming the video, the call back function RealDataCallBackEx is called for streaming, I can display the video on a Window, but how can I let OpenCV library to deal with it?

Here are the code of the RealDataCallBackEx function:

void __stdcall RealDataCallBackEx(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD      dwBufSize, LONG lParam, DWORD dwUser)
    {
    BOOL bInput = FALSE;
    bInput = PLAY_InputData(g_lRealPort, pBuffer, dwBufSize);
    }
MMSabbagh
  • 21
  • 5
  • I can't really tell if you want to use OpenCV to capture and process the data instead of Dahua SDK, or just want to process the data already captured by Dahua SDK. Can you specify? – Amadeusz Jan 11 '15 at 22:10
  • I failed to captured using OpenCV, so I installed SDK. Now I can captured using SDK and I want to process using OpenCV. To Capture using OpenCV I need IP address, I used iSpy utility to find the capturing IP address, iSpy runs for about 2 hours and found nothing (cam ip is 192.168.1.108, port is 37777, user/pass is admin/admin) – MMSabbagh Jan 11 '15 at 22:30

2 Answers2

0

If the IP camera uses certain standards you should be able to grab an image using the following OpenCV code (adapt where needed, i copied it from one of my own programs). I think you can also test this by pasting the url in your browser with the correct ip, port and login. I left the port at 88 because that is normally where you can send these commands to for an ip camera.

Mat returnFrame;
string url = "http://";
url.append("192.168.1.108");
url.append(":88/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=");
url.append("admin");
url.append("&pwd=");
url.append("admin");
VideoCapture cap(url);
if (cap.isOpened()) {
    Mat frame;
    if (cap.read(frame) == false) {
       cout << "Unable to grab frame" << endl;
    } else returnFrame = frame.clone();
    cap.release();
} else cout << "Can't open URL" << endl;
if (returnFrame.empty()) cout << "No frame to grab for cam!" << endl;
else cout << "Cam Grabbed frame succesfully" << endl;

If you want to turn a byte buffer into a OpenCV Mat you can use the following code:

byte buf[] = new byte[100];
//fill buffer here
Mat m = new Mat(1, 100, CvType.CV_8UC1);
m.put(0, 0, buf);

Be sure to define the size and type correctly. In this example it is a 1 channel Mat of 1 x 100 pixels.

diip_thomas
  • 1,531
  • 13
  • 25
  • I can not find any IP that may work on the way you mentioned, I spent more than a week looking for the IP of my CAM! I downloaded a special good software for finding the Ip called "iSpy", with no luck at all. So the only way is to use tcp/udp Sockets, and it worked and I can display the image on a Window using the code I mentioned in my question, but the question is how can I use the same code to fed OpenCV. – MMSabbagh Jan 12 '15 at 10:38
  • Ok, I thought you wanted to capture it with OpenCV, but now I understand you want to turn an image from Dahua into an OpenCV Mat? Then you might want to look for how to create mat from RGB data, like in this question/answer: http://stackoverflow.com/questions/22368312/creating-opencv-mat-from-user-data-results-in-image-with-circular-shifted-column – diip_thomas Jan 12 '15 at 12:23
  • Thank you for your help, let me say that I want to stream the data into Mat! the call back function `RealDataCallBackEx` is responsible for drawing the frame( I added the body of this call back to my question above), so `BYTE *pBuffer` is stream seems contains the frame, how can I convert it to an OpenCV Mat?? – MMSabbagh Jan 13 '15 at 04:52
  • If any one interested I can give him the whole code, it is a tiny Win32 program playing the video from a CAM connected to the LAN – MMSabbagh Jan 13 '15 at 04:58
  • Your code for "turn a byte buffer into a OpenCV Mat" is not working at all, m must be pointer, CV_8UC1 is not member of CvType and there is no put functions in Mat class!!! – MMSabbagh Jan 13 '15 at 23:56
0

I only succeeded with the function CLIENT_SnapPictureEx

CLIENT_SetSnapRevCallBack(OnSnapRevMessage, dwUser);
NET_SNAP_PARAMS _netSnapParam;
_netSnapParam.Channel = (uint)ChannelNum;
_netSnapParam.mode = 1;
CLIENT_SnapPictureEx(lLoginID, _netSnapParam, reserved);


private void SnapRevCallBack(IntPtr lLoginID, IntPtr pBuf, uint RevLen, uint EncodeType, uint CmdSerial, IntPtr dwUser)
    {
        byte[] data = new byte[RevLen];
        Marshal.Copy(pBuf, data, 0, (int)RevLen);
        img = Cv2.ImDecode(data, ImreadModes.Color);
    }