连接Python脚本和Flask Web服务器获取和发布映像

连接Python脚本和Flask Web服务器获取和发布映像,python,api,opencv,web-applications,restapi,Python,Api,Opencv,Web Applications,Restapi,我想用我的OpenCV代码编辑一个图像,我从Web服务器获取上传的图像并将其存储在SQLite中。接下来,我从db获取图像,并在Web服务器上显示它。然而,我希望获得图像,通过我制作的python opencv代码读取并编辑它,然后将其发送回服务器,在Web服务器上显示 目前,它只有一个用于Web服务器和db的GET和POST方法。 我需要设置与camera.py类的连接,并将图像发送到该代码,然后再次获取它。camera.py代码如下所示: import cv2 img = cv2.imre

我想用我的OpenCV代码编辑一个图像,我从Web服务器获取上传的图像并将其存储在SQLite中。接下来,我从db获取图像,并在Web服务器上显示它。然而,我希望获得图像,通过我制作的python opencv代码读取并编辑它,然后将其发送回服务器,在Web服务器上显示

目前,它只有一个用于Web服务器和db的GET和POST方法。 我需要设置与camera.py类的连接,并将图像发送到该代码,然后再次获取它。camera.py代码如下所示:

import cv2

img = cv2.imread('onderlegger_4.jpg')

cv2.imshow('onderlegger', img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow('gray', gray)

canny = cv2.Canny(img, 125, 175)

cv2.imshow('canny', canny)

cv2.waitKey(0)
from flask import Flask, request, Response
from werkzeug.utils import secure_filename

from db import db_init, db
from models import Img

app = Flask(__name__)
# SQLAlchemy config. Read more: https://flask-sqlalchemy.palletsprojects.com/en/2.x/
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///img.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db_init(app)


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


@app.route('/upload', methods=['POST'])
def upload():
    pic = request.files['pic']
    if not pic:
        return 'No pic uploaded!', 400

    filename = secure_filename(pic.filename)
    mimetype = pic.mimetype
    if not filename or not mimetype:
        return 'Bad upload!', 400

    img = Img(img=pic.read(), name=filename, mimetype=mimetype)
    db.session.add(img)
    db.session.commit()

    return 'Img Uploaded!', 200


@app.route('/<int:id>')
def get_img(id):
    img = Img.query.filter_by(id=id).first()
    if not img:
        return 'Img Not Found!', 404

    return Response(img.img, mimetype=img.mimetype)
app.py代码如下所示:

import cv2

img = cv2.imread('onderlegger_4.jpg')

cv2.imshow('onderlegger', img)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow('gray', gray)

canny = cv2.Canny(img, 125, 175)

cv2.imshow('canny', canny)

cv2.waitKey(0)
from flask import Flask, request, Response
from werkzeug.utils import secure_filename

from db import db_init, db
from models import Img

app = Flask(__name__)
# SQLAlchemy config. Read more: https://flask-sqlalchemy.palletsprojects.com/en/2.x/
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///img.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db_init(app)


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


@app.route('/upload', methods=['POST'])
def upload():
    pic = request.files['pic']
    if not pic:
        return 'No pic uploaded!', 400

    filename = secure_filename(pic.filename)
    mimetype = pic.mimetype
    if not filename or not mimetype:
        return 'Bad upload!', 400

    img = Img(img=pic.read(), name=filename, mimetype=mimetype)
    db.session.add(img)
    db.session.commit()

    return 'Img Uploaded!', 200


@app.route('/<int:id>')
def get_img(id):
    img = Img.query.filter_by(id=id).first()
    if not img:
        return 'Img Not Found!', 404

    return Response(img.img, mimetype=img.mimetype)
从烧瓶导入烧瓶,请求,响应
从werkzeug.utils导入安全文件名
从db导入db_init,db
从模型导入Img
app=烧瓶(名称)
#SQLAlchemy配置。阅读更多:https://flask-sqlalchemy.palletsprojects.com/en/2.x/
app.config['SQLALCHEMY\u DATABASE\u URI']='sqlite:///img.db'
app.config['SQLALCHEMY\u TRACK\u MODIFICATIONS']=False
数据库初始化(应用程序)
@应用程序路径(“/”)
def hello_world():
返回“你好,世界!2'
@app.route('/upload',methods=['POST'])
def upload():
pic=请求.文件['pic']
如果不是pic:
返回“没有上传图片!”,400
filename=secure\u文件名(pic.filename)
mimetype=pic.mimetype
如果不是filename或不是mimetype:
返回'Bad upload!',400
img=img(img=pic.read(),name=filename,mimetype=mimetype)
db.session.add(img)
db.session.commit()
返回“Img上传!”,200
@应用程序路径(“/”)
def get_img(id):
img=img.query.filter_by(id=id).first()
如果不是img:
返回“找不到Img!”,404
返回响应(img.img,mimetype=img.mimetype)