2016-04-28 101 views
0

我有一个桌面应用程序来检测用python脚本编写的面,使用opencv和numpy。 我想把这些python文件放进烧瓶并运行它,它会运行没有问题?像将Python脚本运行到Flask

import cv2 
import numpy as np 
from flask import Flask 
app = Flask(__name__) 

## define my functions here 

@app.route('/') 
def hello_world(): 
    return 'Hello World!' 

if __name__ == '__main__': 
    #call the functions here 
    app.run() 

会这样吗?如果不是,我如何得到它包括?谢谢!

+0

我不认为你明白蓝图是做什么的。他们只是组织Flask应用程序的路线。它们对于外部功能并不是很有用。你可以使用一个标准的Python模块。 –

+2

“这是来自文档的hello世界代码,与我写的任何内容无关,我可以将其他代码放在这里吗?”你有*特定的编程问题*? – davidism

回答

1

是的,它会工作,你应该知道的一件事是,如果你喜欢下面的事情,HTTP请求将不会返回,直到完成处理之后,例如,

@app.route('/webcam') 
def webcam_capture(): 
    """ 
    Returns a picture snapshot from the webcam 
    """ 
    image = cv2... # call a function to get an image 

    response = make_response(image) # make an HTTP response with the image 
    response.headers['Content-Type'] = 'image/jpeg' 
    response.headers['Content-Disposition'] = 'attachment; filename=img.jpg' 

    return response 

否则,如果你把在主函数类似下面

if __name__ == '__main__': 
    # <-- No need to put things here, unless you want them to run before 
    # the app is ran (e.g. you need to initialize something) 

    app.run() 

然后你烧瓶应用程序才会启动初始化/处理完成。

+0

我访问摄像头并拍摄一张照片,然后处理并返回照片?会像拍照然后返回一些东西?或做app.run()然后返回结果? –

+0

我知道蓝图,但它与html和css文件一起使用,是否可以与python一起使用? –

+0

'app.run()'启动你的烧瓶应用程序,所以我不会在它之前放置任何代码,除非它是初始化应用程序。要显示来自网络摄像头的图片,您可以使用我的第一个例子使用'route('/ webcam')'并且该函数返回从网络摄像头拍摄的图片 – bakkal