我想用web界面显示OpenCV处理过的图像(用CherryPy制作)。下面的代码运行良好,但有没有办法在不写/读图像文件的情况下执行这样的任务?
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())
您可以cv2.imencode()内存中的图像,而不是在中保存/读回