-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
67 lines (51 loc) · 1.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import cv2
from flask import Flask
from flask import jsonify
from flask import request
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship,scoped_session
from flask_sqlalchemy_session import flask_scoped_session
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, Float, DateTime
from dateutil import parser
from ripeness_predictor import is_ripe
app = Flask(__name__)
Base = declarative_base()
class TamatarData(Base):
__tablename__ = 'tamatar'
id = Column(Integer, primary_key=True)
lat = Column(Float)
lng = Column(Float)
timestamp = Column(DateTime)
ripe = Column(Integer)
def get_sqlite3_session(path):
engine = create_engine('sqlite:///' + path)
Base.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine))
session = flask_scoped_session(get_sqlite3_session("db.db"), app)
@app.route("/add", methods=["POST"])
def add_image():
lat = float(request.form['lat'])
lng = float(request.form['lng'])
dt = parser.parse(request.form["date"])
img_str = request.form['image']
nparr = np.fromstring(img_str, np.uint8)
img_np = cv2.imdecode(nparr, 1) # cv2.IMREAD_COLOR in OpenCV 3.1
ripe = is_ripe(img_np)
t = TamatarData(lat=lat, lng=lng, ripe=ripe)
session.add(t)
session.commit()
return jsonify(status=True)
@app.route("/get")
def get_all():
query = session.query(TamatarData)
if 'from' in request.form:
frm = parser.parse(request.form['from'])
query = query.filter(TamatarData.timestamp >= frm)
if 'to' in request.form:
to = parser.parse(request.form['to'])
query = query.filter(TamatarData.timestamp <= to)
res = list(query)
return jsonify(result=res)
app.run(host='0.0.0.0', threaded=True)