如何在Flask Web服务器上显示编码的OpenCV图像?

问题描述 投票:0回答:1

我有一个客户端从相机中提取帧然后压缩并将它们存储在JSON对象中以发送到Flask服务器。我需要在Flask界面上显示所述帧图像。我当前的尝试不会返回错误,但它只打印一个np数组而不是显示实际图像。

client.朋友

class Camera(object):
    def __init__(self, mode, addr, id):
        self.id = id
        self.video = None
        if(mode == "file" and type(addr) == type("hi")):
            self.video = cv2.VideoCapture(addr)
        elif(mode == "live" and type(addr) == type(0)):
            self.video = cv2.VideoCapture(addr)
        else:
            print("ERROR: Camera class given either an incorrect mode or an address of the wrong type.")

    def __del__(self):
        self.video.release()

    def get_cam_id(self):
        return self.id

    def get_frame(self):
        ret, frame = self.video.read()
        if(ret == True):
            return frame
        else:
            return None

    def norm_frame(self, frame):
        frame = cv2.resize(frame, None, fx=0.6, fy=0.6)
        return frame

    def package(self, frame):
                frame = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 70])[1].tolist()
                frame = json.dumps(frame)
                return frame

test_configapplications.py路线

@application.route('/test_config', methods = ['GET', 'POST'])
def test2():
    var = input('Enter ID: ')
    print(FEEDS[var])
    frame = FEEDS[var].package(FEEDS[var].norm_frame(FEEDS[var].get_frame()))
    print(frame)


""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Attempt
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    #frame = json.loads(frame)
    #frame = np.asarray(frame, np.uint8)
    #frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
    #return frame
    return "Frame will display here eventually!"

有一个/test_input路线,允许用户输入有关摄像机的信息。所述信息用于在client.py中创建相机对象。对象存储在带有ID键的applications.py中的字典中,当在test_config中输入时,将确定显示哪个帧。我遇到的问题是在输入Camera对象的键后显示图像。

python json opencv flask
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.