如何在Flask和OpenCV中显示图片服务器端?

时间:2019-08-09 14:43:03

标签: python python-3.x opencv flask cv2

我有一个Flask服务器,它通过显示器上的OpenCV显示黑色图像。到达HTTP请求时,它必须在同一窗口上显示另一个图像,该图像的名称是通过参数到达的。在端点“ / destroy”上到达另一个请求之后,必须在同一窗口中显示黑色图像。我不明白为什么这段代码不起作用,您有什么建议吗?为了不阻塞服务器,我需要在端点中使用两个不同的进程,这是使用它们的正确方法吗?

#show black image before the server starts
image = cv2.imread("black.jpg",1)
screen = screeninfo.get_monitors()[1]
cv2.moveWindow("image", screen.x - 1, screen.y - 1)
cv2.imshow("image",image)
cv2.waitKey(1)

def imageShower(name):
    #get name parameter and shows image with that name
    image = cv2.imread(name+".jpg",1)
    cv2.imshow("image",image)
    cv2.waitKey(1)

def imageDestroyer():
    #return to black image
    image = cv2.imread("black.jpg",1)
    cv2.imshow("image",image)
    cv2.waitKey(1)

#at endpoint / start process that invokes imageShower()
@app.route('/')
def showImg():
    #store image name arrived via parameter
    name = request.args.get('name')
    #start process
    p = Process(target=imageShower(name))
    p.start()
    #return back to front-end 
    return "image showed"

#return black image on endpoint "/destroy"
@app.route('/destroy')
def destroy():
    #start process
    pd = Process(target=imageDestroyer)
    pd.start()
    #return back to front-end
    return "returned to black"

0 个答案:

没有答案