Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
import json
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
def get_prediction_img(image_data):
|
| 9 |
+
url = 'https://askai.aiclub.world/39a4f3a3-e637-4981-a88c-b2597ab12be0'
|
| 10 |
+
r = requests.post(url, data=image_data)
|
| 11 |
+
response = r.json()['predicted_label']
|
| 12 |
+
print("Image AI predicts:",response)
|
| 13 |
+
return response
|
| 14 |
+
|
| 15 |
+
def get_prediction_data(data,url):
|
| 16 |
+
# url = 'https://d3yowc8vr7.execute-api.us-east-1.amazonaws.com/Predict/13d5ab46-b369-4c84-966e-41a0c3ed83d1'
|
| 17 |
+
# url = 'https://askai.aiclub.world/bc1fe184-efe3-4683-81f4-ededffb6c287'
|
| 18 |
+
r = requests.post(url, data=json.dumps(data))
|
| 19 |
+
response = getattr(r,'_content').decode("utf-8")
|
| 20 |
+
print("Data AI predicts:",response)
|
| 21 |
+
return response
|
| 22 |
+
|
| 23 |
+
def processFile(f,url):
|
| 24 |
+
print("Got file upload")
|
| 25 |
+
bytesData=f.getvalue()
|
| 26 |
+
st.image(f)
|
| 27 |
+
image=Image.open(f)
|
| 28 |
+
img_array=np.array(image)
|
| 29 |
+
grayscale_image=convert_grayscale(img_array)
|
| 30 |
+
final_image=flatten_784(grayscale_image)
|
| 31 |
+
print("Final image",final_image)
|
| 32 |
+
prediction=get_prediction_data(final_image,url)
|
| 33 |
+
print("\n\nData prediction",prediction)
|
| 34 |
+
predicted_label = json.loads(json.loads(prediction)['body'])['predicted_label']
|
| 35 |
+
print("\n\nPredicted label", predicted_label)
|
| 36 |
+
st.title("Data AI says:"+str(predicted_label))
|
| 37 |
+
|
| 38 |
+
payload = base64.b64encode(bytesData)
|
| 39 |
+
response = get_prediction_img(payload)
|
| 40 |
+
print("\n\nResponse is:",response)
|
| 41 |
+
st.title("IMAGE AI says:"+response)
|
| 42 |
+
|
| 43 |
+
def convert_grayscale(im):
|
| 44 |
+
# Convert to grayscale if its a color image
|
| 45 |
+
if len(im.shape) > 2 and im.shape[2]>2:
|
| 46 |
+
red = im[:,:,0]
|
| 47 |
+
green = im[:,:,1]
|
| 48 |
+
blue = im[:,:,2]
|
| 49 |
+
# Convert color to grayscale
|
| 50 |
+
grayscale_image = (red * 0.299) + (green * 0.587) + (blue * 0.114)
|
| 51 |
+
elif len(im.shape) == 2:
|
| 52 |
+
grayscale_image = im
|
| 53 |
+
return grayscale_image
|
| 54 |
+
|
| 55 |
+
# This is a helper function to flatten image into a single row after downsampling the image to 28x28
|
| 56 |
+
def flatten_784(grayscale_image):
|
| 57 |
+
# Find the width and length of the image
|
| 58 |
+
num_rows_image = grayscale_image.shape[0]
|
| 59 |
+
num_cols_image = grayscale_image.shape[1]
|
| 60 |
+
# Figure out the downsampling value for each dimension
|
| 61 |
+
downsample_rows = int(np.floor(num_rows_image/28))
|
| 62 |
+
downsample_cols = int(np.floor(num_cols_image/28))
|
| 63 |
+
|
| 64 |
+
# Downsample it
|
| 65 |
+
downsampled_image = grayscale_image[::downsample_rows,::downsample_cols]
|
| 66 |
+
# Somtimes, the dimensions after downsampling are not accurate, pick the first 28 pixels in each direction
|
| 67 |
+
downsampled_image = downsampled_image[0:28,0:28]
|
| 68 |
+
# Convert the vector to a list
|
| 69 |
+
list_image = list(downsampled_image.reshape(784,))
|
| 70 |
+
#From the list, create a dictionary
|
| 71 |
+
e=0
|
| 72 |
+
d={}
|
| 73 |
+
for i in range(1,29):
|
| 74 |
+
for j in range(1,29):
|
| 75 |
+
l=f"{i}x{j}"
|
| 76 |
+
d[l]=e
|
| 77 |
+
e=e+1
|
| 78 |
+
return d
|
| 79 |
+
|
| 80 |
+
urlDefault = 'https://askai.aiclub.world/bc1fe184-efe3-4683-81f4-ededffb6c286'
|
| 81 |
+
st.title("Image AI for Gateway")
|
| 82 |
+
url=st.text_input("URL",urlDefault)
|
| 83 |
+
uploadedFile=st.file_uploader("Choose file")
|
| 84 |
+
if uploadedFile is not None:
|
| 85 |
+
processFile(uploadedFile,url)
|