1
我想通過Web界面(使用CherryPy製作)顯示OpenCV處理後的圖像。下面的代碼工作正常:通過Web界面顯示網絡攝像頭(Python,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()
_, data = cv2.imencode('.jpg', image)
cherrypy.response.headers['Content-Type'] = 'image/jpeg'
return data.tostring()
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })
cherrypy.quickstart(Picture())
不過:我想嵌入HTML圖像,這樣我可以(例如)另一幅圖像或其他數據添加到同一個頁面。
我曾嘗試下面的代碼:
import cherrypy
import cv2
class Picture(object):
def __init__(self):
self.cam = cv2.VideoCapture(0)
@cherrypy.expose
def index(self):
_, image = self.cam.read()
_, data = cv2.imencode('.jpeg', image)
return """ <html>
<head>
<title>CherryPy static imagl</title>
</head>
<html>
<body>
<img src=" """ + data + """:image/jpeg" />
</body>
</html>"""
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '127.0.0.1', 'server.socket_port': 80, })
cherrypy.quickstart(Picture())
但是,這提供了以下錯誤:
<img src=" """ + data + """:image/jpeg" />
TypeError: cannot concatenate 'str' and 'numpy.ndarray' objects
使用下面的代碼也不起作用numpy的ARRY轉換爲字符串(它給沒有錯誤但只顯示字符):
<img src=" """ + data.tostring() + """:image/jpeg" />
任何人誰可以給我一些莫重新認識?提前致謝!
它爲什麼有2個img標籤? – berak
演示可以顯示兩個文件。我實際上使用兩個網絡攝像頭:) –