forked from emmapraise/Built2Bill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·95 lines (76 loc) · 2.31 KB
/
main.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
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
from flask import Flask, render_template,url_for,request, redirect, flash, send_from_directory, jsonify
from werkzeug.utils import secure_filename
import io
from io import BytesIO
import cv2
import numpy as np
import urllib.request
# import uuid
# import cloudstorage as gcs
# from google.appengine.api import images, app_identity
# import os
# import pickle
# from sklearn.externals import joblib
# from keras.models import model_from_json
from google.cloud import storage
storage_client =storage.Client.from_service_account_json('Built2bill-ea533c3e831e.json') #
app = Flask(__name__)
def upload_to_bucket(file, bucket_name):
"""
input
------
file:
The image to be uploaded.
bucket_name:
The name of the bucket which the image will be uploaded into.
Return
-------
It returns the public url to access the image saved in the bucket.
"""
destination_blob_name = secure_filename(file.filename)
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_string(file.read(), content_type=file.content_type)
blob.make_public()
p_url=blob.public_url
# uri = 'gs://%s/%s' % (bucket_name, destination_blob_name)
return p_url
def count_lines(p_url):
"""
p_url
-----
The public url of the image from the upload bucket
Return
---------
The number of lines counted from the image
"""
resp = urllib.request.urlopen(p_url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
imge = cv2.imdecode(image, -1)
blur = cv2.blur(imge,(5,5))
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 75, 150)
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 3)
pred_value = len(lines)
return pred_value
@app.route('/')
def home():
return render_template('index.html')
@app.route('/aboutus')
def aboutus():
return render_template('aboutus.html')
@app.route('/onepage')
def onepage():
return render_template('onepage-creative.html')
@app.route('/about', methods = ['POST'])
def predict():
bucket_name = "built2bill-upload"
file = request.files['file']
get_image = upload_to_bucket(file, bucket_name)
lines = count_lines(get_image)
return render_template('aboutus.html', prediction = lines)
if __name__ == '__main__':
app.run(debug=True)