|
a |
|
b/index.py |
|
|
1 |
import json |
|
|
2 |
import os |
|
|
3 |
|
|
|
4 |
from app import app |
|
|
5 |
from flask import render_template, request |
|
|
6 |
from werkzeug.utils import secure_filename |
|
|
7 |
|
|
|
8 |
from classifier import getPrediction |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
@app.route('/') |
|
|
13 |
def index(): |
|
|
14 |
return render_template('about.html') |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
@app.route('/upload-image/', methods=['POST']) |
|
|
18 |
def submit_file(): |
|
|
19 |
if request.method == 'POST': |
|
|
20 |
if 'file' not in request.files: |
|
|
21 |
data = "File not found in request body" |
|
|
22 |
response = app.response_class( |
|
|
23 |
response=json.dumps(data), |
|
|
24 |
mimetype='application/json' |
|
|
25 |
) |
|
|
26 |
# flash('No file part') |
|
|
27 |
# return redirect(request.url) |
|
|
28 |
return response |
|
|
29 |
file = request.files['file'] |
|
|
30 |
if file.filename == '': |
|
|
31 |
data = 'No file selected for uploading' |
|
|
32 |
response = app.response_class( |
|
|
33 |
response=json.dumps(data), |
|
|
34 |
mimetype='application/json' |
|
|
35 |
) |
|
|
36 |
# flash('No file selected for uploading') |
|
|
37 |
# return redirect(request.url) |
|
|
38 |
return response |
|
|
39 |
if file: |
|
|
40 |
filename = secure_filename(file.filename) |
|
|
41 |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) |
|
|
42 |
# upload images to DB |
|
|
43 |
# upload_image_data_mongodb(file) |
|
|
44 |
label = getPrediction.processImg(filename) |
|
|
45 |
# flash({"Given class of image is ": label}) |
|
|
46 |
data = {"Given class of image is ": label} |
|
|
47 |
response = app.response_class( |
|
|
48 |
response=json.dumps(data), |
|
|
49 |
mimetype='application/json' |
|
|
50 |
) |
|
|
51 |
if remove_img(path=os.path.join(app.config['UPLOAD_FOLDER']), img_name=file.filename): |
|
|
52 |
print('Image removed') |
|
|
53 |
# return redirect('/') |
|
|
54 |
return response |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
def remove_img(path, img_name): |
|
|
58 |
os.remove(path + '/' + img_name) |
|
|
59 |
# check if file exists or not |
|
|
60 |
if os.path.exists(path + '/' + img_name) is False: |
|
|
61 |
# file did not exists |
|
|
62 |
return True |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
if __name__ == "__main__": |
|
|
66 |
app.run() |