Python 使用OpenCV和flask进行图像流处理-为什么需要imencode?

Python 使用OpenCV和flask进行图像流处理-为什么需要imencode?,python,opencv,flask,Python,Opencv,Flask,我有一个flask web应用程序,它读取一个图像并将其显示在我的web浏览器中 app.py index.html 那么为什么我需要这个imencode?图像以jpg的形式存储在我的硬盘上,那么为什么我必须将其再次编码为jpg?如果您有JPEG文件,那么您可以使用标准的open()和read())将其作为原始字节数据读取,而无需将其解压缩到包含所有像素的数组中,这样以后就不必使用imencode()将其压缩回JPEG数据了。 然后你可以显示它 b'Content-Type: image/jp

我有一个flask web应用程序,它读取一个图像并将其显示在我的web浏览器中

app.py index.html
那么为什么我需要这个
imencode
?图像以
jpg
的形式存储在我的硬盘上,那么为什么我必须将其再次编码为
jpg

如果您有JPEG文件,那么您可以使用标准的
open()
read())
将其作为原始字节数据读取,而无需将其解压缩到包含所有像素的数组中,这样以后就不必使用
imencode()将其压缩回JPEG数据了。

然后你可以显示它

 b'Content-Type: image/jpeg\r\n\r\n'+ img + b'\r\n'
我以字节模式直接读取它-
open(…,'rb')
-因此我不必使用
bytearray()
将字符串转换为字节。此外,在文本模式下读取可能会转换一些字符(如“新行”)并创建不正确的数据


但要发送单个文件,可以使用
send\u file()


工作示例

在Chrome中打开显示图像

我的Firefox在显示图像时遇到了问题——它一直在读取数据——直到我添加了
time.sleep()

我还添加了添加标题
内容长度的版本:

from flask import Flask, Response, send_file
import time

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

def get_image():
    while True:
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n'+ img + b'\r\n')
        time.sleep(0.01) # my Firefox needs some time to display image / Chrome displays image without it

def get_image_with_size():
    length = str(len(img)).encode() # convert to bytes
    while True:
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n'
              b'Content-Length: ' + length + b'\r\n'
              b'\r\n'+ img + b'\r\n')
        time.sleep(0.01) # my Firefox needs some time to display image / Chrome displays image without it

@app.route("/stream")
def stream():
    return Response(get_image(), mimetype="multipart/x-mixed-replace; boundary=frame")

@app.route("/image")
def image():
    return send_file('Cat.jpg')

if(__name__ == "__main__"):
    img = open('Cat.jpg', 'rb').read()
    app.run()

opencv对.jpg进行解码,因为假定您要处理像素,因此如果不想使用imencode,请读取该文件directly@eyllanesc“直接读取文件”是什么意思?.jpg是一个文件,因此您可以直接读取它:
打开(“您的.jpg”)作为f:…
,换句话说,您必须对其进行编码,因为
imread
会对其进行解码。如果不想对其进行编码,则不要先对其进行解码,即不要使用
imread
打开它,而是像打开数据文件一样打开它(因为它是)。另一方面,如果要处理图像,则需要对其进行解码以播放像素,然后需要将其重新编码为二进制字符串。如果只想显示一幅图像,则可以使用
发送文件
或使用标记创建HTML
。只有当您有原始数据(即带有像素的数组)并且必须将其转换为jpeg数据时,才需要
imencode
imread
读取jpeg数据并转换为数组,因此以后必须将数组转换回jpeg数据。如果您使用标准
open()
,则它不会将jpeg数据转换为数组,因此您不必使用
imencode
。如果您以字节模式打开-
open(…,'rb').read()
,那么您就不需要
bytesarray()
谢谢。我昨天自己试过了,但我的浏览器中有一个空图像。但这个例子对我有帮助。我可以确认Firefox的睡眠问题。我在这台电脑上只有
Firefox
,没有
sleep
它就不能工作。当我将我的
index.html
添加到
/
的路由中时,
Edge
不显示任何内容,
internetexplorer
显示一个空图像,这真是令人欣慰。
def GetImage():
    global img
    (flag, encodedImage) = cv2.imencode(".jpg", img)
    while True:
        yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
 img = open("Cat.jpg", "rb").read()
 b'Content-Type: image/jpeg\r\n\r\n'+ img + b'\r\n'
@app.route("/image")
def image():
    return send_file('Cat.jpg')
from flask import Flask, Response, send_file
import time

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello World"

def get_image():
    while True:
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n'+ img + b'\r\n')
        time.sleep(0.01) # my Firefox needs some time to display image / Chrome displays image without it

def get_image_with_size():
    length = str(len(img)).encode() # convert to bytes
    while True:
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n'
              b'Content-Length: ' + length + b'\r\n'
              b'\r\n'+ img + b'\r\n')
        time.sleep(0.01) # my Firefox needs some time to display image / Chrome displays image without it

@app.route("/stream")
def stream():
    return Response(get_image(), mimetype="multipart/x-mixed-replace; boundary=frame")

@app.route("/image")
def image():
    return send_file('Cat.jpg')

if(__name__ == "__main__"):
    img = open('Cat.jpg', 'rb').read()
    app.run()