Spaces:
Sleeping
Sleeping
Commit ·
5097b89
1
Parent(s): ca6d204
Revert "Update app.py"
Browse filesThis reverts commit d991f510f852779e08f85425b8b8fd5ea59e6332.
app.py
CHANGED
|
@@ -1,55 +1,67 @@
|
|
| 1 |
-
import os
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
from tensorflow.keras.
|
| 5 |
-
from tensorflow.keras.
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
from starlette.staticfiles import StaticFiles
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
|
| 12 |
-
# Initialize FastAPI app
|
| 13 |
-
app = FastAPI()
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model = load_model(modelpath, compile=False, safe_mode=False)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
4: 'Cruise', 5: 'DDG', 6: 'Recreational', 7: 'Sailboat', 8: 'Submarine', 9: 'Tug'
|
| 27 |
-
}
|
| 28 |
|
| 29 |
-
@app.
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
@app.
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
buffer.write(await image_file.read())
|
| 46 |
-
|
| 47 |
-
img = image.load_img(file_path, target_size=(224, 224))
|
| 48 |
-
img = image.img_to_array(img)
|
| 49 |
-
img = np.expand_dims(img, axis=0)
|
| 50 |
-
img = preprocess_input(img)
|
| 51 |
-
|
| 52 |
-
pred = model.predict(img).flatten()
|
| 53 |
-
result = val_dict[np.argmax(pred)]
|
| 54 |
-
|
| 55 |
-
return {"ship_category": result}
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import os
|
| 3 |
+
from tensorflow.keras.models import load_model # type: ignore
|
| 4 |
+
from tensorflow.keras.preprocessing import image # type: ignore
|
| 5 |
+
from tensorflow.keras.layers import Flatten # type: ignore
|
| 6 |
+
from tensorflow.keras.applications.densenet import preprocess_input# type: ignore
|
| 7 |
+
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
from flask import Flask , request, render_template
|
| 10 |
+
#from werkzeug.utils import secure_filename
|
| 11 |
+
#from gevent.pywsgi import WSGIServer
|
|
|
|
| 12 |
|
| 13 |
+
app = Flask(__name__)
|
| 14 |
+
basepath = os.path.dirname(__file__)
|
| 15 |
+
modelpath = os.path.join(basepath,'uploads',"best1den.keras")
|
| 16 |
+
model = load_model(modelpath,compile=False, safe_mode=False)
|
| 17 |
+
@app.route('/')
|
| 18 |
+
def index():
|
| 19 |
+
return render_template('index.html')
|
| 20 |
|
| 21 |
+
@app.route('/about')
|
| 22 |
+
def about():
|
| 23 |
+
return render_template('about.html')
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
@app.route('/service')
|
| 26 |
+
def service():
|
| 27 |
+
return render_template('service.html')
|
| 28 |
|
| 29 |
+
@app.route('/predict',methods = ['GET','POST'])
|
| 30 |
+
def upload():
|
| 31 |
+
if request.method == 'POST':
|
| 32 |
+
f = request.files['image']
|
| 33 |
+
|
| 34 |
+
#print("current path")
|
| 35 |
+
basepath = os.path.dirname(__file__)
|
| 36 |
+
print("current path", basepath)
|
| 37 |
+
filepath = os.path.join(basepath,'uploads',f.filename)
|
| 38 |
+
print("upload folder is ", filepath)
|
| 39 |
+
f.save(filepath)
|
| 40 |
|
| 41 |
+
img=image.load_img(filepath,target_size=(224,224))
|
| 42 |
+
img=image.img_to_array(img)
|
| 43 |
+
img=img.reshape((1,img.shape[0],img.shape[1],img.shape[2]))
|
| 44 |
+
img=preprocess_input(img)
|
| 45 |
+
pred=model.predict(img)
|
| 46 |
+
pred=pred.flatten()
|
| 47 |
+
pred=list(pred)
|
| 48 |
+
n=max(pred)
|
| 49 |
+
val_dict={0: 'Aircraft Carrier',
|
| 50 |
+
1: 'Bulkers',
|
| 51 |
+
2: 'Car Carrier',
|
| 52 |
+
3: 'Container Ship',
|
| 53 |
+
4: 'Cruise',
|
| 54 |
+
5: 'DDG',
|
| 55 |
+
6: 'Recreational',
|
| 56 |
+
7: 'Sailboat',
|
| 57 |
+
8: 'Submarine',
|
| 58 |
+
9: 'Tug'}
|
| 59 |
+
result=val_dict[pred.index(n)]
|
| 60 |
+
print(result)
|
| 61 |
+
text = "the Ship Category is " + result
|
| 62 |
+
return text
|
| 63 |
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
app.run(debug = True)
|
| 66 |
+
|
| 67 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|