app file
Browse files
script.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
from object_detection.utils import label_map_util
|
| 8 |
+
from object_detection.utils import visualization_utils as viz_utils
|
| 9 |
+
from object_detection.builders import model_builder
|
| 10 |
+
from object_detection.utils import config_util
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
CUSTOM_MODEL_NAME = 'my_ssd_mobnet'
|
| 14 |
+
paths = {
|
| 15 |
+
'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace', 'models', CUSTOM_MODEL_NAME),
|
| 16 |
+
'LABELMAP': os.path.join('Tensorflow', 'workspace', 'annotations', 'label_map.pbtxt')
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
configs = config_util.get_configs_from_pipeline_file(os.path.join(paths['CHECKPOINT_PATH'], 'pipeline.config'))
|
| 20 |
+
detection_model = model_builder.build(model_config=configs['model'], is_training=False)
|
| 21 |
+
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
|
| 22 |
+
ckpt.restore(os.path.join(paths['CHECKPOINT_PATH'], 'ckpt-3')).expect_partial()
|
| 23 |
+
category_index = label_map_util.create_category_index_from_labelmap(paths['LABELMAP'])
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@tf.function
|
| 27 |
+
def detect_fn(image):
|
| 28 |
+
image, shapes = detection_model.preprocess(image)
|
| 29 |
+
prediction_dict = detection_model.predict(image, shapes)
|
| 30 |
+
detections = detection_model.postprocess(prediction_dict, shapes)
|
| 31 |
+
return detections
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def main():
|
| 35 |
+
st.title('Furniture Detection')
|
| 36 |
+
|
| 37 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 38 |
+
|
| 39 |
+
if uploaded_file is not None:
|
| 40 |
+
image = np.array(Image.open(uploaded_file))
|
| 41 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 42 |
+
st.write("")
|
| 43 |
+
st.write("Detection In Process...")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
input_tensor = tf.convert_to_tensor(np.expand_dims(image, 0), dtype=tf.float32)
|
| 47 |
+
detections = detect_fn(input_tensor)
|
| 48 |
+
|
| 49 |
+
num_detections = int(detections.pop('num_detections'))
|
| 50 |
+
detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
|
| 51 |
+
detections['num_detections'] = num_detections
|
| 52 |
+
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
|
| 53 |
+
|
| 54 |
+
label_id_offset = 1
|
| 55 |
+
image_np_with_detections = image.copy()
|
| 56 |
+
|
| 57 |
+
viz_utils.visualize_boxes_and_labels_on_image_array(
|
| 58 |
+
image_np_with_detections,
|
| 59 |
+
detections['detection_boxes'],
|
| 60 |
+
detections['detection_classes'] + label_id_offset,
|
| 61 |
+
detections['detection_scores'],
|
| 62 |
+
category_index,
|
| 63 |
+
use_normalized_coordinates=True,
|
| 64 |
+
max_boxes_to_draw=5,
|
| 65 |
+
min_score_thresh=.3,
|
| 66 |
+
agnostic_mode=False
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
st.image(image_np_with_detections, caption='Detected Teeth', use_column_width=True)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
main()
|