-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
145 lines (104 loc) · 4.04 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from flask import Flask, request
import cv2
import numpy as np
from datetime import datetime
import torch
from flask_sqlalchemy import SQLAlchemy
import pymysql
pymysql.install_as_MySQLdb()
app = Flask(__name__)
from sqlalchemy import Column, Integer, String, Date, DECIMAL, Enum, ForeignKey, Time
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Users(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(255))
password = Column(String(266))
user_level = Column(Enum('실버', '골드', '다이아몬드'))
eco_points = Column(Integer)
class TrashBin(Base):
__tablename__ = 'trash_bin'
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey('users.id'))
state = Column(String(255))
trash_type = Column(String(255))
disposal_date = Column(Date)
disposal_amount = Column(DECIMAL(10,2))
user = relationship('Users', back_populates='trash_bins')
class TrashBinCapacity(Base):
__tablename__ = 'trash_bin_capacity'
id = Column(Integer, primary_key=True)
trash_bin_id = Column(Integer, ForeignKey('trash_bin.id'))
check_date = Column(Date)
check_time = Column(Time)
capacity = Column(DECIMAL(10,2))
trash_bin = relationship('TrashBin', back_populates='capacities')
Users.trash_bins = relationship('TrashBin', order_by=TrashBin.id, back_populates='user')
TrashBin.capacities = relationship('TrashBinCapacity', order_by=TrashBinCapacity.id, back_populates='trash_bin')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:0000@localhost/autotrash'
db = SQLAlchemy(app)
class TrashPred(db.Model):
id = db.Column(db.Integer, primary_key=True)
cls = db.Column(db.String(255), nullable=False)
conf = db.Column(db.Float, nullable=False)
pred_date = db.Column(db.Time)
from ultralytics import YOLO
model = YOLO('best.pt')
model = model.cuda()
@app.route('/detect', methods=['POST'])
def detect_objects():
echo = ""
image_file = request.files['image']
image = cv2.imdecode(np.frombuffer(image_file.read(), np.uint8), cv2.IMREAD_COLOR)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite('capture/image.jpg', image)
image = np.transpose(image, (2, 0, 1))
image = torch.from_numpy(image).unsqueeze(0).float() / 255.0
image = image.cuda()
result = model(image, max_det=1)
result = result[0]
if result.__len__() > 0:
cv2.imwrite('results/result.jpg', result.plot())
box = result.boxes
cls = result.names[box.cls.item()]
conf = box.conf.item()
pred_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
save(cls, conf, pred_time)
echo = cls
return echo
def save(cls, conf, pred_time):
record = TrashPred(cls=cls, conf=conf, pred_date=pred_time)
db.session.add(record)
db.session.commit()
print("Save Object Detection Result to DB")
@app.route('/ultwave', methods=['GET'])
def save_ult_wave():
dist = request.args['dist']
print(dist)
cls = request.args['cls']
# users 테이블에서 id가 2인 user 검색
user = db.session.query(Users).get(2)
# 해당 user와 연결된 trash_bin 테이블에서 trash_bin_id 검색
for trash_bin in user.trash_bins:
print(cls)
if trash_bin.trash_type in cls:
# 해당 trash_bin_id와 연결된 모든 trash_bin_capacity 테이블의 레코드에 dist 값 저장
for capacity in trash_bin.capacities:
capacity.capacity = dist
db.session.commit()
return '저장 완료'
@app.route('/ecop', methods=['GET'])
def plus_ecopoint():
# users 테이블에서 id가 2인 user 검색
user = db.session.query(Users).get(2)
user.eco_points += 10
db.session.commit()
return 'eco_points 증가 완료'
@app.route('/', methods=['GET'])
def test():
print("test...")
return "hello world!"
if __name__ == '__main__':
app.run(debug=True)