-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (61 loc) · 2.48 KB
/
app.py
File metadata and controls
83 lines (61 loc) · 2.48 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
import cv2
import base64
import numpy as np
import matplotlib.pyplot as plt
from flask import Flask, render_template,flash,request,redirect,url_for,jsonify
from functions import load_model,show_inference
app = Flask(__name__)
app.secret_key = "sahilsc"
class ClientApp:
def __init__(self):
self.img_path = "inputs/input_img.png"
self.model = load_model()
@app.route("/")
def home():
return render_template('home.html',title="Vehicle Object Detection")
@app.route("/",methods=['POST'])
def predict():
if request.method == 'POST' and 'imag' in request.files:
img = request.files['imag']
if img.filename=='':
flash('Image not found')
return redirect(url_for('home'))
else:
if img and img.filename.rsplit('.',1)[1].lower() in ['jpg','jpeg','png']:
# Saving the image
# img.save('static/'+img.filename)
#read image file string data
filestr = img.read()
#convert string data to numpy array
npimg = np.frombuffer(filestr, np.uint8)
# convert numpy array to image
img_vec = cv2.imdecode(npimg, cv2.COLOR_BGR2RGB)
# prediction
pred_img = show_inference(capp.model,img_vec)
# saving the prediction
pre_img_fname = 'static/predictions/prediction.jpg'
cv2.imwrite(pre_img_fname,pred_img)
flash('Succesfully made the predictions')
return render_template('home.html',title="Vehicle Object Detection",filename="predictions/prediction.jpg")
else:
flash('Invalid Image format')
return redirect(url_for('home'))
else:
return redirect(url_for('home'))
@app.route('/api',methods=['POST'])
def api():
img = request.files['imag']
filestr = img.read()
npimg = np.frombuffer(filestr, np.uint8)
img_vec = cv2.imdecode(npimg, cv2.COLOR_BGR2RGB)
pred_img = show_inference(capp.model,img_vec)
pre_img_fname = 'static/predictions/prediction.jpg'
cv2.imwrite(pre_img_fname,pred_img)
with open(pre_img_fname, "rb") as f:
b64_img = base64.b64encode(f.read())
return jsonify({"image" : b64_img.decode('utf-8')})
# return 'inside predict'
# return render_template('home.html',title="Vehicle Object Detection")
if __name__ == '__main__':
capp = ClientApp()
app.run(debug=True)