gpt4 book ai didi

python - 仅在覆盆子上的 Opencv 黑色图像

转载 作者:行者123 更新时间:2023-12-02 16:19:38 25 4
gpt4 key购买 nike

opencv 的常规检查在 buster 桌面上运行良好

 cv2.imshow('frame',frame)

此代码在 ubuntu 和 mac os 上也可以正常工作
import cv2
import io
import numpy as np
import tornado.httpserver
import tornado.web

from PIL import Image, ImageDraw
from tornado.ioloop import IOLoop

class HandlerCameraIMG(tornado.web.RequestHandler):

byte_io = io.BytesIO()

def get(self):
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
jpg = Image.fromarray(img_rgb)
self.byte_io.seek(0)
jpg.save(self.byte_io, format="JPEG")
s = self.byte_io.getvalue()
self.set_header('Content-type', 'image/jpeg')
self.set_header('Content-length', len(s))
self.write(s)
return

html_index = """<html><body><p>Camera</p>
<img src="/img/camera.jpeg" style="width:50px;height:50px;"/>
</body></html>"""

class IndexPageHandler(tornado.web.RequestHandler):
def get(self):
self.write(html_index)

class WebApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexPageHandler),
(r'/img/camera.jpeg', HandlerCameraIMG)
]
settings = {'debug': True}
tornado.web.Application.__init__(self, handlers, **settings)

def main():
ws_app = WebApplication()
server = tornado.httpserver.HTTPServer(ws_app)
server.listen(9090)
IOLoop.instance().start()

if __name__ == "__main__":
main()

它确实在带有 raspian buster 的 rapsberry pi 3 上返回了黑色 camera.jpeg

有什么可以解释的原因吗?

最佳答案

您的代码中的缩进似乎存在一些问题,并且对 PIL/Pillow 和 BytesIO 也存在不必要的依赖,因此我删除了它并添加了一些错误检查和调试提示,它似乎工作正常 - 也许您可以再试一次:

#!/usr/bin/env python3

import cv2
import numpy as np
import tornado.httpserver
import tornado.web

from tornado.ioloop import IOLoop
from tornado.options import define, options

class HandlerCameraIMG(tornado.web.RequestHandler):

def get(self):
cap = cv2.VideoCapture(0)
if (cap.isOpened()== False):
print("Error opening video stream or file")

ret, frame = cap.read()
if not ret:
print("Error reading video")
else:
print(f"Frame size: {frame.shape}")

_, JPEG = cv2.imencode('.jpeg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
self.set_header('Content-type', 'image/jpeg')
self.set_header('Content-length', len(JPEG))
self.write(JPEG.tobytes())
return

html_index = """<html><body><p>Camera</p>
<img src="/img/camera.jpeg"/>
</body></html>"""

class IndexPageHandler(tornado.web.RequestHandler):
def get(self):
self.write(html_index)

class WebApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexPageHandler),
(r'/img/camera.jpeg', HandlerCameraIMG)
]
settings = {'debug': True}
tornado.web.Application.__init__(self, handlers, **settings)

def main():
define('port', default=6502, help='port to listen on')
ws_app = WebApplication()
server = tornado.httpserver.HTTPServer(ws_app)
server.listen(options.port)
print(f'Listening on http://localhost:{options.port}')
IOLoop.instance().start()

if __name__ == "__main__":
main()

关于python - 仅在覆盆子上的 Opencv 黑色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61273041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com