-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
47 lines (36 loc) · 1.18 KB
/
app.py
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
import tensorflow as tf
from keras.models import load_model
import os
from keras.preprocessing import image
from flask import Flask, request, Response
import jsonpickle
import numpy as np
import cv2
from pymongo import MongoClient
from PIL import Image
img_path = "./test/image0.jpg"
types = ['Compostable', 'Recyclable', 'Trash']
def process_image(filepath):
input_im = cv2.imread(filepath)
input_original = input_im.copy()
input_original = cv2.resize(
input_original, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
input_im = cv2.resize(input_im, (224, 224), interpolation=cv2.INTER_LINEAR)
input_im = input_im / 255.
input_im = input_im.reshape(1, 224, 224, 3)
return input_im
def get_garbage_type(preds):
gclass = types[np.argmax(preds)]
return gclass
def predict_class(modelloc, image):
model = load_model(modelloc)
preds = model.predict(image, 1, verbose=0)
garb_type = get_garbage_type(preds)
print(preds)
return garb_type
def predict(filepath):
image = process_image(filepath)
garbage = predict_class(
os.getcwd()+"/classifier/trash_mobilenet.h5", image)
return garbage
print(predict(img_path))