imagezmq icon indicating copy to clipboard operation
imagezmq copied to clipboard

Improve jpg conversion speed a bit

Open fjolublar opened this issue 4 years ago • 7 comments
trafficstars

This is NOT an issue with the imagezmq library, but I wanted to express my opinion to improve some speed in the use cases where this library is used.

Instead of using OpenCV to encode/decode video frames, it might be better to use another library: simplejpeg. With some testing I did, the speed of jpg conversion was up 10 to 30% faster. In my case, I had to optimize every step of the transmission.

I have no affiliation with the library, just want to spread some knowledge.

fjolublar avatar Feb 08 '21 12:02 fjolublar

Thanks @fjolublar! Any speed improvement suggestions are always very welcome.

Would you be interested in providing an example program pair that illustrates your use of simplejpg? You could start with 2 example programs that are already in the imageZMQ examples folder, duplicate them, and modify them to use simplejpg. A pair of examples programs that would be a good duplication starting point would be with_imageHub.py and with_ImageSender.py. They are documented in the imageZMQ examples docs. It would be helpful to name the example programs something like faster_jpg_hub.py and faster_jpg_send.py. You could modify the example documentation page by duplicating and modifying the with_ImageSender.py section. If you don't have time to do this as a pull request, no worries. I'll put it on my "to do list". It's a really good idea.

jeffbass avatar Feb 08 '21 21:02 jeffbass

Of course. It`s a pleasure to contribute to this work. I will work on it.

fjolublar avatar Feb 09 '21 12:02 fjolublar

Thanks again for your pull request. Merged it.

jeffbass avatar Feb 09 '21 21:02 jeffbass

Thank you for giving me the opportunity. These were my very first Pull Requests :)

fjolublar avatar Feb 10 '21 12:02 fjolublar

+1

Zumbalamambo avatar Feb 26 '21 00:02 Zumbalamambo

I tried this in my project, but didn't go well, at least as a drop-in replacement.

        if self.publish_cam:
            self._rate.update()
            #ret_code, jpg_buffer = cv2.imencode(".jpg", image, 
            #    [int(cv2.IMWRITE_JPEG_QUALITY), camera.jpeg_quality])
            jpg_buffer = simplejpeg.encode_jpeg(image, 
                quality=camera.jpeg_quality, 
                colorspace='BGR')
            Outpost.publisher.send_jpg(camera.text, jpg_buffer)

The call into encode_jpeg raised the following exception

ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 44 from C header, got 40 from PyObject

OK, Update. Disregard the above. Installing the newest version of numpy fixed that. And WOW it's much faster!!

shumwaymark avatar Feb 26 '21 23:02 shumwaymark