Search code examples
pythonopencvcherrypy

Display numpy represented birmap in web interface [Python]


I want to show OpenCV processed image with web interface (made with CherryPy). Code below works fine, but is there way to perform such a task without writing/reading image file?

import cherrypy
import cv2


class Picture(object):
    def __init__(self):
        self.cam = cv2.VideoCapture(0)

    @cherrypy.expose
    def index(self):
        _, image = self.cam.read()
        cv2.imwrite('temp.jpg', image)
        with open('temp.jpg', 'rb') as temp_file:
            data = temp_file.read()

        cherrypy.response.headers['Content-Type'] = 'image/jpeg'
        return data


if __name__ == '__main__':
    cherrypy.quickstart(Picture())

Solution

  • you can cv2.imencode() the image in memory, instead of saving/reading back in