RANA
commited on
Commit
•
b573744
1
Parent(s):
a13659a
using wsgi which is gunicorn
Browse files- Dockerfile +15 -0
- app.py +59 -0
- best.pt +3 -0
- recycleModel.py +42 -0
- requirements.txt +0 -0
- updated_vgg16_weights.h5 +3 -0
Dockerfile
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
COPY requirements.txt .
|
4 |
+
|
5 |
+
RUN pip install -r requirements.txt
|
6 |
+
|
7 |
+
RUN apt-get update && apt-get install -y libgl1-mesa-glx
|
8 |
+
|
9 |
+
RUN python -c "import cv2; import numpy"
|
10 |
+
|
11 |
+
COPY . .
|
12 |
+
|
13 |
+
EXPOSE 7860
|
14 |
+
|
15 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
# from io import BytesIO
|
3 |
+
# from PIL import Image
|
4 |
+
# import treeCountModel
|
5 |
+
import recycleModel
|
6 |
+
import numpy as np
|
7 |
+
# import requests
|
8 |
+
import cv2
|
9 |
+
|
10 |
+
app = Flask(__name__)
|
11 |
+
|
12 |
+
|
13 |
+
@app.route("/", methods=["GET", "POST"])
|
14 |
+
def index():
|
15 |
+
message = "Welcome to my API! Here are the available endpoints:"
|
16 |
+
recycle_url = request.host_url + "recycle-prediction"
|
17 |
+
tree_count_url = request.host_url + "tree-count"
|
18 |
+
response = {
|
19 |
+
"message": message,
|
20 |
+
"endpoints": {"recycle_prediction": recycle_url, "tree_count": tree_count_url},
|
21 |
+
}
|
22 |
+
return response, 200
|
23 |
+
|
24 |
+
|
25 |
+
@app.route("/recycle-prediction", methods=["GET", "POST"])
|
26 |
+
def recycle():
|
27 |
+
result = None
|
28 |
+
if request.method == "POST":
|
29 |
+
if "img" not in request.files:
|
30 |
+
return "No file uploaded for recycling material prediction", 400
|
31 |
+
img = request.files["img"]
|
32 |
+
img_array = cv2.imdecode(np.frombuffer(img.read(), np.uint8), cv2.IMREAD_COLOR)
|
33 |
+
model = recycleModel.recyclePrediction(img_array)
|
34 |
+
# model = recycleModel.recyclePrediction(img)
|
35 |
+
response = {"Recycling Material": model}
|
36 |
+
result = response
|
37 |
+
return result
|
38 |
+
|
39 |
+
return result
|
40 |
+
|
41 |
+
|
42 |
+
# @app.route("/tree-count/<path:url>", methods=["GET", "POST"])
|
43 |
+
# def treeCount(url):
|
44 |
+
# result = None
|
45 |
+
# if request.method == "GET":
|
46 |
+
# url = request.url
|
47 |
+
# url = url[50:]
|
48 |
+
# response = requests.get(url)
|
49 |
+
# if url is None:
|
50 |
+
# return "No image URL provided", 400
|
51 |
+
# img = Image.open(BytesIO(response.content))
|
52 |
+
# model = treeCountModel.countTree(img)
|
53 |
+
# result = {"Tree Count": model}
|
54 |
+
|
55 |
+
# return result
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
app.run(debug=True)
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2470e83e4ca362c2834c3b964698702cd08cb183e9649b359abe9cd9750f80cd
|
3 |
+
size 14307901
|
recycleModel.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.optimizers import Adam
|
5 |
+
from tensorflow.keras.models import Sequential
|
6 |
+
from tensorflow.keras.layers import Dense, Flatten
|
7 |
+
|
8 |
+
class RecycleModel:
|
9 |
+
def __init__(self):
|
10 |
+
self.vgg16_model = Sequential()
|
11 |
+
|
12 |
+
self.pretrained_model = tf.keras.applications.VGG16(
|
13 |
+
include_top=False,
|
14 |
+
weights="imagenet",
|
15 |
+
input_shape=(180, 180, 3),
|
16 |
+
pooling='max',
|
17 |
+
classes=7
|
18 |
+
)
|
19 |
+
for layer in self.pretrained_model.layers:
|
20 |
+
layer.trainable = False
|
21 |
+
|
22 |
+
self.vgg16_model.add(self.pretrained_model)
|
23 |
+
self.vgg16_model.add(Flatten())
|
24 |
+
self.vgg16_model.add(Dense(512, activation='relu'))
|
25 |
+
self.vgg16_model.add(Dense(7, activation='softmax'))
|
26 |
+
|
27 |
+
self.vgg16_model.compile(optimizer=Adam(learning_rate=0.001),
|
28 |
+
loss='sparse_categorical_crossentropy',
|
29 |
+
metrics=['accuracy'])
|
30 |
+
|
31 |
+
self.vgg16_model.load_weights('updated_vgg16_weights.h5')
|
32 |
+
|
33 |
+
self.class_names = ['1_polyethylene_PET', '2_high_density_polyethylene_PE-HD', '3_polyvinylchloride_PVC',
|
34 |
+
'4_low_density_polyethylene_PE-LD', '5_polypropylene_PP', '6_polystyrene_PS', '8_no_plastic']
|
35 |
+
|
36 |
+
def recyclePrediction(self, img):
|
37 |
+
image_resized = cv2.resize(img, (180, 180))
|
38 |
+
image = np.expand_dims(image_resized, axis=0)
|
39 |
+
pred = self.vgg16_model.predict(image)
|
40 |
+
output_class = self.class_names[np.argmax(pred)]
|
41 |
+
|
42 |
+
return output_class
|
requirements.txt
ADDED
Binary file (346 Bytes). View file
|
|
updated_vgg16_weights.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9270ef02337952414ac31cfee42ef64746376d77de8b25139117304680d393ea
|
3 |
+
size 59962752
|