-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
170 lines (136 loc) · 5.42 KB
/
utilities.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os, json
import cv2
import numpy as np
import requests
from flask import render_template, redirect, url_for
import flask_login
from CustomFileStorage import *
from oauthlib.oauth2 import WebApplicationClient
from dotenv import load_dotenv
from random import shuffle
load_dotenv()
## Google Login Configuration
# GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
# GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
# GOOGLE_DISCOVERY_URL = (
# "https://accounts.google.com/.well-known/openid-configuration"
# )
# client = WebApplicationClient(GOOGLE_CLIENT_ID)
def get_new_name():
nums = list(range(97,123))
chars = list(map(chr,nums))
nos = list(range(1,10))
shuffle(nos)
shuffle(chars)
return "".join(chars[:5]) + "".join(list(map(str,nos))[:3])
def handle_show_image(request):
try:
uploaded_file = request.files["input_image"].read()
except:
return "Invalid Request"
input_file_name = get_new_name()
input_user_id = "chard1open1id"
npimg = np.fromstring(uploaded_file, np.uint8)
input_file_data = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
storage_object = CustomFileStorage(user_id = input_user_id)
storage_object.store_file(file_name = input_file_name, file_data = input_file_data, file_type = "image")
image_uri = storage_object.saved_file_uri
req_data = {
"user_id":input_user_id,
"file_uri":image_uri,
"file_name":storage_object.file_name
}
response = requests.post(url="https://characterize-backend.herokuapp.com/api/characterizer101",json=req_data)
return response.content
# def get_google_provider_cfg():
# return requests.get(GOOGLE_DISCOVERY_URL).json()
# def handle_google_login_callback(request, User, users):
# code = request.args.get("code")
# google_provider_cfg = get_google_provider_cfg()
# token_endpoint = google_provider_cfg["token_endpoint"]
# # Prepare and send request to get tokens! Yay tokens!
# token_url, headers, body = client.prepare_token_request(
# token_endpoint,
# authorization_response=request.url,
# redirect_url=request.base_url,
# code=code,
# )
# token_response = requests.post(
# token_url,
# headers=headers,
# data=body,
# auth=(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET),
# )
# client.parse_request_body_response(json.dumps(token_response.json()))
# # Now that we have tokens (yay) let's find and hit URL
# # from Google that gives you user's profile information,
# # including their Google Profile Image and Email
# userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
# uri, headers, body = client.add_token(userinfo_endpoint)
# userinfo_response = requests.get(uri, headers=headers, data=body)
# # We want to make sure their email is verified.
# # The user authenticated with Google, authorized our
# # app, and now we've verified their email through Google!
# if userinfo_response.json().get("email_verified"):
# unique_id = userinfo_response.json()["sub"]
# users_email = userinfo_response.json()["email"]
# picture = userinfo_response.json()["picture"]
# users_name = userinfo_response.json()["given_name"]
# else:
# return "User email not available or not verified by Google.", 400
# # Create a user in our db with the information provided
# # by Google
# user = User()
# user.id = users_name
# # Doesn't exist? Add to database
# if not users_name in users:
# users[users_name] = {
# "name":users_name,
# "email":users_email
# }
# # Begin user session by logging the user in
# flask_login.login_user(user)
# # Send user back to homepage
# return redirect(url_for("protected"))
def array3T2(array):
ans = []
for i in range(len(array)):
temp = []
for j in range(len(array[0])):
temp.append(sum(array[i][j])//3)
ans.append(temp)
return ans.copy()
def characterize101(image_data:np.ndarray) -> str:
characters = list("@#&S`%+-. ")
cv2.imwrite("sample_image.png",image_data)
array_x = cv2.imread("sample_image.png",0)
os.remove("sample_image.png")
height = 40
factor = (len(array_x)//height)//2
width = len(array_x[0])//factor
resized_image = cv2.resize(array_x,(width,height))
output_data = ""
for i in range(height):
for j in range(width):
if 0 <= resized_image[i][j] <= 26:
output_data += characters[0]
elif 27 <= resized_image[i][j] <= 52:
output_data += characters[1]
elif 53 <= resized_image[i][j] <= 78:
output_data += characters[2]
elif 79 <= resized_image[i][j] <= 104:
output_data += characters[3]
elif 105 <= resized_image[i][j] <= 130:
output_data += characters[4]
elif 131 <= resized_image[i][j] <= 156:
output_data += characters[5]
elif 157 <= resized_image[i][j] <= 182:
output_data += characters[6]
elif 183 <= resized_image[i][j] <= 206:
output_data += characters[7]
elif 207 <= resized_image[i][j] <= 230:
output_data += characters[8]
elif 231 <= resized_image[i][j] <= 255:
output_data += characters[9]
output_data += "\n"
return output_data