我想用 Web 界面(用 CherryPy 制作)显示 OpenCV 处理的图像。下面的代码可以正常工作,但是有没有办法在不写入/读取图像文件的情况下执行这样的任务?
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())