-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·157 lines (122 loc) · 3.98 KB
/
Copy pathapp.py
File metadata and controls
executable file
·157 lines (122 loc) · 3.98 KB
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
146
147
148
149
150
151
152
153
#rahul pandit
#!/usr/bin/env python
from flask import *
from flask import Flask, request, jsonify, json, make_response, redirect, session, send_from_directory
# import MySQLdb
from werkzeug.security import generate_password_hash, check_password_hash
import model
from datetime import timedelta #for time to define how long session is active
import os
#import magic
import urllib.request
#from app import app
from werkzeug.utils import secure_filename
import cv2
import secrets
import random
import string
import random
import base64
from io import BytesIO
from PIL import Image
from base64 import b64decode
import os.path
import cv2
import model
from time import sleep
#from flask_cors import CORS
######################## import function define in model file #########
#from model import tyre_classification
#create session in flask
#https://www.youtube.com/watch?v=iIhAfX4iek0
#https://techwithtim.net/tutorials/flask/sessions/
label_path = 'labels/label.json'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
UPLOAD_FOLDER = os.getcwd() + '/static/uploads/'
app = Flask(__name__, static_url_path='')
#app.secret_key = secrets.token_urlsafe(16)
#CORS(app)
app.permanent_session_lifetime = timedelta(days=5)
app.secret_key = "hello" #encrypt data by this key on server
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
######################################
"""@app.route('/static/<path:path>')
def send_js(path):
return send_from_directory('uploads', path)
"""
#####################
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
def index():
#if "email" in session:
# return redirect(url_for("upload"))
#else:
return render_template("upload.html", title="Flask signUp form")
@app.route('/python-flask-files-upload', methods=['POST'])
def upload_file():
# check if the post request has the file part
if 'files[]' not in request.files:
resp = jsonify({'message' : 'No file part in the request'})
resp.status_code = 400
return resp
files = request.files.getlist('files[]')
# sleep(10)
errors = {}
success = False
#i am return image shape
result=[]
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
img = cv2.imread(app.config['UPLOAD_FOLDER']+filename) #this code read image from folder
#pred=tyre_classification(img)
predictor, cfg = model.setup_model()
print("predictor generated")
outputs = predictor(img)
# output_cars = model.gen_car_bboxes(img, predictor)
# image_out = model.visualize_preds(output_cars, cfg, img)
# torch_bbox = model.generate_label_bboxes_via()
torch_bbox = model.generate_label_bboxes(label_path)
print('shape:', torch_bbox.shape)
torchint_preds = model.gen_bbox_predictions(img, predictor)
image_out = model.draw_output(torchint_preds, torch_bbox, img, 0.3)
name = filename.split(".")[:-1][0]
ext = filename.split(".")[-1]
file_name=name+'_final_.'+ext
print(file_name)
cv2.imwrite(app.config['UPLOAD_FOLDER']+file_name,image_out)
result.append(1)
print(img.shape)
success = True
resp={'message': " ",'success': True, 'image':'http://0.0.0.0:4000/uploads/'+file_name}
print(resp['image'])
return make_response(jsonify(resp))
else:
errors[file.filename] = 'File type is not allowed'
if success and errors:
errors['message'] = 'File(s) successfully uploaded'
resp = jsonify(errors)
resp.status_code = 206
return resp
if success:
if int(result[0])==1:
result=[]
result.append('Good Quality')
else:
result=[]
result.append('puncture tyre')
resp = jsonify({'message' :result[0]})
resp.status_code = 201
return resp
else:
resp = jsonify(errors)
resp.status_code = 400
return resp
"""
***************stop ******************
"""
if __name__ == "__main__":
app.run(debug=True,port=4000,host='0.0.0.0')