-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
100 lines (80 loc) · 3.45 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
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
from flask import Flask, render_template, request, jsonify, send_file
from PIL import Image
# from gtts import gTTS
import os, io, sys
import numpy as np
import cv2
import base64
# import time
from yolo_detection import run_model
from language_conversion import convert_lang
app = Flask(__name__)
############################################## THE REAL DEAL STARTS HERE ###############################################
@app.route('/detectObject', methods=['POST'])
def mask_image():
# print(request.files , file=sys.stderr)
#################################################
file = request.files['image'].read() ## byte file
# npimg = np.fromstring(file, np.uint8)
npimg = np.frombuffer(file,np.uint8)
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)[:, :, ::-1]
# OpenCV image (BGR to RGB)
################################################
# cv2.imshow('After reading from request', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
################################################
################################################
######### Do preprocessing here ################
# img[img > 150] = 0
## any random stuff do here
################################################
img,text = run_model(img)
print("{} This is from app.py".format(text))
if(text.lower() == "image contains"):
text = ""
if(len(text) == 0):
text = "Reload the page and try with another better image"
englishtext = text
hinditext = convert_lang(text)
# below encodes the detected image as it sent to server back
"""
rawBytes = io.BytesIO()
img = Image.fromarray(img.astype("uint8"))
img.save(rawBytes,"JPEG")
rawBytes.seek(0)
img_base64 = base64.b64encode(rawBytes.read())
return jsonify({'status':str(img_base64)})
"""
bufferedBytes = io.BytesIO()
img_base64 = Image.fromarray(img)
img_base64.save(bufferedBytes, format="JPEG")
img_base64 = base64.b64encode(bufferedBytes.getvalue())
# return jsonify({'status':str(img_base64)})
return jsonify({'status':str(img_base64),'englishmessage':englishtext, 'hindimessage':hinditext})
################################## THE MAIN PART IS DONE ABOVE #########################################################
@app.route('/test', methods=['GET', 'POST'])
def test():
print("log: got at test", file=sys.stderr)
return jsonify({'status': 'succces'})
@app.route('/')
def home():
return render_template('index.html')
@app.after_request
def after_request(response):
print("log: setting cors", file=sys.stderr)
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers','Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == '__main__':
# app.run(debug=True)
# when run using above command change localhost/5000 default port in index.js file
# In order to work for any device connected to wifi/LAN same network, use below
app.run(host="0.0.0.0", port=8080, debug=False, threaded=False)
# make threaded=True for concurrent users
# ip = "0.0.0.0" matches all ip address, and server listens to all ip address requests
# below method is for devices connected to Wifi/ LAN
# using ifconfig/ipconfig cmd in terminal find ip
# app.run(debug=False, host="xxx.xxx.xxx.xxx",port=8080)
# change the route path in index.js with http://host_ip:port/