AronWolverine commited on
Commit
5097b89
·
1 Parent(s): ca6d204

Revert "Update app.py"

Browse files

This reverts commit d991f510f852779e08f85425b8b8fd5ea59e6332.

Files changed (1) hide show
  1. app.py +59 -47
app.py CHANGED
@@ -1,55 +1,67 @@
1
- import os
2
  import numpy as np
3
- from tensorflow.keras.models import load_model
4
- from tensorflow.keras.preprocessing import image
5
- from tensorflow.keras.applications.densenet import preprocess_input
6
- from fastapi import FastAPI, File, UploadFile
7
- from fastapi.responses import HTMLResponse
8
- from starlette.requests import Request
9
- from starlette.staticfiles import StaticFiles
10
- from pathlib import Path
11
-
12
- # Initialize FastAPI app
13
- app = FastAPI()
14
 
15
- # Define paths
16
- basepath = Path(__file__).parent
17
- modelpath = basepath / 'uploads' / 'best1den.keras'
18
- model = load_model(modelpath, compile=False, safe_mode=False)
19
 
20
- # Serve static files if necessary
21
- app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 
 
 
22
 
23
- # Ship categories
24
- val_dict = {
25
- 0: 'Aircraft Carrier', 1: 'Bulkers', 2: 'Car Carrier', 3: 'Container Ship',
26
- 4: 'Cruise', 5: 'DDG', 6: 'Recreational', 7: 'Sailboat', 8: 'Submarine', 9: 'Tug'
27
- }
28
 
29
- @app.get("/", response_class=HTMLResponse)
30
- async def index():
31
- return "<h1>Welcome to the Ship Classification API</h1>"
32
 
33
- @app.get("/about", response_class=HTMLResponse)
34
- async def about():
35
- return "<h1>About the Ship Classification Model</h1>"
 
 
 
 
 
 
 
 
36
 
37
- @app.get("/service", response_class=HTMLResponse)
38
- async def service():
39
- return "<h1>Ship Classification Service</h1>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- @app.post("/predict")
42
- async def predict(image_file: UploadFile = File(...)):
43
- file_path = basepath / 'uploads' / image_file.filename
44
- with file_path.open("wb") as buffer:
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
+