Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOW to convert “Numpy.NDarray”to“OpenCvSharp.Mat #15

Closed
Jasonfinish opened this issue Aug 21, 2019 · 7 comments
Closed

HOW to convert “Numpy.NDarray”to“OpenCvSharp.Mat #15

Jasonfinish opened this issue Aug 21, 2019 · 7 comments
Assignees

Comments

@Jasonfinish
Copy link

i want to use NDarray as Image, for showing in c#picturebox. but it dose not work!

@Jasonfinish
Copy link
Author

anybody can help me ?

@henon
Copy link
Contributor

henon commented Aug 21, 2019

Please post a small code example of what you are trying to do.

@simonbuehler
Copy link

hi, have the same question, how would i convert this code:

rects = detector.detectMultiScale(gray, scaleFactor=1.1,minNeighbors=5, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
    # ensure at least one face was found before continuing
    if len(rects) > 0:
        # determine the largest face area
        rect = sorted(rects, reverse=True,key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        (fX, fY, fW, fH) = rect
        # extract the face ROI from the image, then pre-process
        # it for the network
        roi = gray[fY:fY + fH, fX:fX + fW]
        roi = cv2.resize(roi, (48, 48))
        roi = roi.astype("float") / 255.0
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis=0)
        
        # make a prediction on the ROI, then lookup the class# label
        preds = model.predict(roi)[0]

so here the input is a opencv mat and keras needs the nparray of the rectangle

@henon
Copy link
Contributor

henon commented Jan 28, 2020

Which library are you using to call OpenCV from C#?

@simonbuehler
Copy link

I'm using opencvsharp

currently i'm at this state below which looks fishy - but loading the Model.LoadModel("epoch_90.hdf5"); throwing a EEFileLoadException keeps me from testing if it works yet

Mat roi = frameMat[rects[0]];
Cv2.Resize(roi, roi, new OpenCvSharp.Size(48, 48));
float[] result;
roi.GetArray(out result);
var test = np.asfarray(result) / 255;
test = ImageUtil.ImageToArray(test);
test = np.expand_dims(test, 0);

NDarray nDarray = model.Predict(test[0]);
Console.WriteLine(nDarray.argmax());

@henon
Copy link
Contributor

henon commented Jan 28, 2020

Is it possible to extract the array values from the OpenCV mat into a C# array? Then you can create an NDarray from those easily and pass it on to Keras

@Oceania2018
Copy link
Member

@simonbuehler Take a look at SharpCV. It combines the NDArray and Mat.

@henon henon closed this as completed Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants