简体   繁体   中英

How can I increase performance of Java Computer Vision using WebSocketServer?

@OnWebSocketMessage
public void onMessage(byte[] data, int offset, int length) throws IOException {                          

    //convert packet to output stream
    ByteArrayOutputStream byteArrayImg = new ByteArrayOutputStream();
    byteArrayImg.write(data, offset, length);
    //convert output stream to bytearray
    byte[] byteArray = byteArrayImg.toByteArray();
    //creating iplimage instance from byte array
    iplimage = cvDecodeImage(cvMat(1, byteArray.length,CV_8UC1, new BytePointer(byteArray)));
    /* HERE GOES FACE RECOGNITION OR SOMETHING */
    //iplimage back to outputstream
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    BufferedImage imgb = iplimage.getBufferedImage();
    ImageIO.write(imgb, "png", bout);
    //sendig back echo packet to javascript client
    ByteBuffer buf = ByteBuffer.wrap(bout.toByteArray());
    this.session.getRemote().sendBytes(buf);
}

I'm trying to convert a webcam stream (byte array received from javascript client) to an IplImage object so that I can work with JavaCV. I wrote this code that takes frames from my webcam and sends back an echo but the performance is very slow (10 FPS). I'm really new to computer vision and working with images, maybe I'm doing something wrong. If you have any ideas at all that could speed it up, I'd like to hear them.

Try replacing this code

ByteArrayOutputStream byteArrayImg = new ByteArrayOutputStream();
byteArrayImg.write(data, offset, length);
byte[] byteArray = byteArrayImg.toByteArray();

with an Arrays.copyOfRange(..) call. This should greatly reduce the amount of data movement required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM