-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_classify_server.py
More file actions
110 lines (86 loc) · 3.54 KB
/
image_classify_server.py
File metadata and controls
110 lines (86 loc) · 3.54 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
# coding: utf-8
from flask import Flask, request, jsonify, render_template
import base64
import json
import requests
import googleapikeys
#TODO: Improvements
#1. accept only certain types of images like png, jpg
#2. see if we can use AWS Simple Queing Service to queue requests
app = Flask(__name__)
GOOGLE_CLOUD_VISION_API_URL = 'https://vision.googleapis.com/v1/images:annotate?key='
def goog_cloud_vison (image_content, classificationType):
api_url = GOOGLE_CLOUD_VISION_API_URL+googleapikeys.CLOUD_VISION
req_body = json.dumps({
'requests': [{
'image': {
'content': image_content
},
'features': [{
'type': classificationType,
'maxResults': 10,
}]
}]
})
res = requests.post(api_url, data=req_body)
return res.json()
RATINGS = ['LIKELY', 'VERY_LIKELY']
def likely_sentiment(face):
if face['joyLikelihood'] in RATINGS:
return 'JOY'
if face['sorrowLikelihood'] in RATINGS:
return 'SORROW'
if face['angerLikelihood'] in RATINGS:
return 'ANGER'
if face['surpriseLikelihood'] in RATINGS:
return 'SURPRISE'
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "please upload the file"
if 'classify' not in request.form:
return "select a type of classification"
file = request.files['file']
classificationType = request.form['classify']
if file.filename == '':
return 'No selected file'
#help thread
#stackoverflow.com/questions/38702937/error-must-be-convertible-to-a-buffer-not-inmemoryuploadedfile
image_content = base64.b64encode(file.read())
response = goog_cloud_vison(image_content, classificationType)
if classificationType == 'FACE_DETECTION':
sentiment = {}
faceAnnotations = response['responses'][0]['faceAnnotations']
sentiment['sentiment'] = likely_sentiment(faceAnnotations[0])
return json.dumps(sentiment)
if classificationType == 'LABEL_DETECTION':
labelDescrption = []
finalLabels = {}
labels = response['responses'][0]['labelAnnotations']
for label in labels:
labelDescrption.append(label['description'])
finalLabels['labels'] = labelDescrption
return json.dumps(finalLabels)
if classificationType == 'LANDMARK_DETECTION':
landmarksDescription = []
finallandmarkDescription = {}
landmarks = response['responses'][0]['landmarkAnnotations']
for landmark in landmarks:
landmarksDescription.append(landmark['description'])
finallandmarkDescription['landmark'] = landmarksDescription
return json.dumps(finallandmarkDescription)
if classificationType == 'TEXT_DETECTION':
textInPic = []
finalTextExtrabcted = {}
textBlock = response['responses'][0]['textAnnotations']
for text in textBlock:
textInPic.append(text['description'])
finalTextExtrabcted['text'] = textInPic[0]
return json.dumps(finalTextExtrabcted)
#if the calssification type is none other than those 4 types just render json response
return jsonify(response)
return render_template('index.html')
if __name__=='__main__':
app.run()