-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
122 lines (108 loc) · 3.96 KB
/
app.py
File metadata and controls
122 lines (108 loc) · 3.96 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# # -*- coding: utf-8 -*-
# """Untitled3.ipynb
#
# Automatically generated by Colaboratory.
#
# Original file is located at
# https://colab.research.google.com/drive/1jHu4UwAQJCtC4IFbjKYYDZm0CYsjR9O_
# """
#
# from google.colab import drive
# drive.mount('/content/drive', force_remount=True)
#
# cd drive/My Drive
#
# cd Ekansh app dev
#
# !apt install -y caffe-cuda
#
# !pip install flask-ngrok
# Commented out IPython magic to ensure Python compatibility.
import caffe
import os
from pylab import *
import sys
from PIL import Image
import numpy as np
from flask_ngrok import run_with_ngrok
from flask import Flask,request, jsonify
import cv2
# %matplotlib inline
mean_filename='garbnet_mean.binaryproto'
deploy_filename = 'deploy_garbnet.prototxt'
caffemodel_file = 'garbnet_fcn.caffemodel'
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
net = caffe.Net(deploy_filename,caffemodel_file,caffe.TEST)
app = Flask(__name__)
run_with_ngrok(app)
@app.route("/",methods=['GET'])
def appd():
if request.method=="GET":
folder='input'
imageNames=None
images = []
names = []
files = os.listdir(folder)
total = len(files)
print ('Total images in folder', total, folder)
for i in os.listdir(folder):
try:
if imageNames is None or i in imageNames:
example_image = folder+'/'+i
input_image = Image.open(example_image)
images.append(input_image)
names.append(i)
except:
pass
#--------------------------------------------------------------------------------
size=4
thresh=0.999
output_folder='output'
for i in range(len(images)):
w,h = images[i].size
if w<h:
test_image= images[i].resize((int(227*size),int((227*h*size)/w))) #227x227 is input for regular CNN
else:
test_image= images[i].resize((int((227*w*size)/h),int(227*size)))
in_ = np.array(test_image,dtype = np.float32)
in_ = in_[:,:,::-1]
in_ -= np.array(mean.mean(1).mean(1))
in_ = in_.transpose((2,0,1))
net.blobs['data'].reshape(1,*in_.shape)
net.blobs['data'].data[...] = in_
net.forward()
probMap =net.blobs['prob'].data[0,1]
print (names[i]+'...',)
if len(np.where(probMap>thresh)[0]) > 0:
print ('Garbage!')
else:
print ('Not Garbage!')
kernel = np.ones((6,6),np.uint8)
wt,ht = test_image.size
out_bn = np.zeros((ht,wt),dtype=uint8)
for h in range(probMap.shape[0]):
for k in range(probMap.shape[1]):
if probMap[h,k] > thresh:
x1 = h*62 #stride 2 at fc6_gb_conv equivalent to 62 pixels stride in input
y1 = k*62
for hoff in range(x1,227+x1):
if hoff < out_bn.shape[0]:
for koff in range(y1,227+y1):
if koff < out_bn.shape[1]:
out_bn[hoff,koff] = 255
edge = cv2.Canny(out_bn,200,250)
box = cv2.dilate(edge,kernel,iterations = 3)
or_im_ar = np.array(test_image)
or_im_ar[:,:,1] = (or_im_ar[:,:,1] | box)
or_im_ar[:,:,2] = or_im_ar[:,:,2] * box + or_im_ar[:,:,2]
or_im_ar[:,:,0] = or_im_ar[:,:,0] * box + or_im_ar[:,:,0]
out_= Image.fromarray(or_im_ar)
out_.save(output_folder + '/output_' + names[i])
#specify 'input' folder containing images for prediction
#images,names = gatherImages('input')
#specify 'output' folder to store segmented predictions
#getPredictionsFor(images,names,4,0.999,'output')
return jsonify(12345)
app.run()