OttoYu commited on
Commit
aaf48f9
1 Parent(s): a0f0074

Create App.py

Browse files
Files changed (1) hide show
  1. App.py +67 -0
App.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import matplotlib.pyplot as plt
4
+ import tensorflow_hub as hub
5
+ import tensorflow as tf
6
+ import numpy as np
7
+ from tensorflow import keras
8
+ from tensorflow.keras.models import *
9
+ from tensorflow.keras import preprocessing
10
+ import time
11
+
12
+
13
+ st.title('AR-CAD CAD element Classifier')
14
+ st.caption('From this demo, your can import any image with CAD graphs, then our model will segmented the detected CAD elements in this window. Please be aware that NOT to send us image file more than 200 MB. For any technique problems, please content ar.cad.connect@gmail.com.')
15
+ st.markdown("Our Prediction :")
16
+
17
+ def main():
18
+ file_uploaded = st.file_uploader("Choose File", type=["png","jpg","jpeg"])
19
+ class_btn = st.button("Classify")
20
+ if file_uploaded is not None:
21
+ image = Image.open(file_uploaded)
22
+ st.image(image, caption='Uploaded Image', use_column_width=True)
23
+
24
+ if class_btn:
25
+ if file_uploaded is None:
26
+ st.write("Invalid command, please upload an image")
27
+ else:
28
+ with st.spinner('Model working....'):
29
+
30
+ predictions = predict(image)
31
+
32
+ time.sleep(1)
33
+ st.success('Classified')
34
+ st.write(predictions)
35
+
36
+
37
+ def predict(image):
38
+ model = "wall.tflite"
39
+ interpreter = tf.lite.Interpreter(model_path = model)
40
+ interpreter.allocate_tensors()
41
+ input_details = interpreter.get_input_details()
42
+ output_details = interpreter.get_output_details()
43
+
44
+ input_shape = input_details[0]['shape']
45
+ image = np.array(image.resize((200,200)), dtype=np.float32)
46
+
47
+ image = image / 255.0
48
+ image = np.expand_dims(image, axis=0)
49
+ interpreter.set_tensor(input_details[0]['index'], image)
50
+ interpreter.invoke()
51
+ output_data = interpreter.get_tensor(output_details[0]['index'])
52
+ probabilities = np.array(output_data[0])
53
+ labels = {1 : "wall"}
54
+ label_to_probabilities = []
55
+ for i, probability in enumerate(probabilities):
56
+ label_to_probabilities.append([labels[i],
57
+ float(probability)])
58
+
59
+ sorted(label_to_probabilities, key=lambda element: element[1])
60
+ result = { 'wall' : 0 }
61
+
62
+ result = f"{label_to_probabilities[np.argmax(probability)][0]} with a { (100 * np.max(probabilities)).round(2)} % confidence."
63
+
64
+ return result
65
+
66
+ if __name__ == "__main__":
67
+ main()