File size: 6,297 Bytes
fb00789
 
 
 
 
 
 
 
 
 
 
 
 
586a795
0375e44
586a795
 
fb00789
586a795
fb00789
 
 
 
 
 
 
 
 
 
 
9f8783d
fb00789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d722cb
 
 
 
 
 
 
 
fb00789
3d722cb
fb00789
 
 
 
 
3d722cb
fb00789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d722cb
 
 
9a99c2c
3d722cb
 
 
 
 
 
 
 
 
 
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
import simplejson
import tensorflow
import visualization_utils as vis_util
from PIL import Image
import numpy as np
from PIL import Image
import numpy as np
import label_map_util
import tensorflow as tf
from matplotlib import pyplot as plt
import time
import cv2
from numpy import asarray 
#import streamlit as st 
import gradio as gr 
#st.title("Tag_Diciphering")
def prediction(path_image):
    total_time_start = time.time()
    image_path = path_image


    def loadImageIntoNumpyArray(image):
        (im_width, im_height) = image.size
        if image.getdata().mode == "RGBA":
            image = image.convert('RGB')

        return asarray(image).reshape((im_height, im_width, 3)).astype(np.uint8)


    def main(image_path,model_path,model_PATH_TO_CKPT,path_to_labels):
        image = Image.open(image_path)
        image_np = loadImageIntoNumpyArray(image)
        image_np_expanded = np.expand_dims(image_np, axis=0)
        label_map = label_map_util.load_labelmap(path_to_labels)
    #     print("label_map------->",type(label_map))
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=100, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)
    #     print("category index-->",category_index)

        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.compat.v1.GraphDef()
            with tf.compat.v2.io.gfile.GFile(model_PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
        sess = tf.compat.v1.Session(graph=detection_graph)
        # Input tensor is the image
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Output tensors are the detection boxes, scores, and classes
        # Each box represents a part of the image where a particular object was detected
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represents level of confidence for each of the objects.
        # The score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        # Number of objects detected
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: image_np_expanded})
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.1)
        #%matplotlib inline
        from matplotlib import pyplot as plt
    #     print("boxes:",boxes)
    #     print("class:",classes)
        objects = []
        threshold = 0.5
    #     print("category:",category_index)
        boxes = boxes[0]
        for index, value in enumerate(classes[0]):
            object_dict = {}
            if scores[0, index] > threshold:
                object_dict["class"] = (category_index.get(value)).get('name')
                object_dict["score"] = round(scores[0, index] * 100,2)
                box = tuple(boxes[index].tolist())
                ymin, xmin, ymax, xmax= box
                im_width,im_height = 360,360
                left, right, top, bottom = (xmin * im_width, xmax * im_width, 
                                  ymin * im_height, ymax * im_height)
                object_dict["box"] = (int(left), int(right), int(top), int(bottom))
                objects.append(object_dict)

        image_orignal = Image.open(image_path)
        image_np_orignal = loadImageIntoNumpyArray(image_orignal)


        fig, ax = plt.subplots(1,2)

        fig.suptitle('Tag Deciphering')

        ax[0].imshow(image_np_orignal,aspect='auto');
        ax[1].imshow(image_np,aspect='auto');


        return fig
    inputs = gr.inputs.Image(type = 'filepath')
    EXAMPLES = ["img1.jpg","img2.jpg","img3.jpg","img4.jpg"]
    DESCRIPTION = """An image is occluded if the image is blocked by any object. 
    For example if an electric pole is filled with bushes,the image is occluded since it is not clear and blocked.
    Mobil-net is used to train a model with occluded and non occluded images, so that it can correctly classify the images.
    Occlusion detection can be used to filter unclear images and take safety measures."""
    
        
    image_path = inputs
    model_path = "//inference"
    model_PATH_TO_CKPT = "frozen_inference_graph.pb"
    path_to_labels = "tf_label_map.pbtxt"

    result = main(image_path,model_path,model_PATH_TO_CKPT,path_to_labels)
    
    # print("result-",result)
    # list_to_be_sorted= [{'class': 'Y', 'score': 99.97, 'box': (157, 191, 269, 288)}, {'class': '6', 'score': 99.93, 'box': (158, 191, 247, 267)}, {'class': '9', 'score': 99.88, 'box': (156, 190, 179, 196)}, {'class': '4', 'score': 99.8, 'box': (156, 189, 198, 219)}, {'class': '1', 'score': 99.65, 'box': (157, 189, 222, 244)}, {'class': 'F', 'score': 63.4, 'box': (155, 185, 157, 175)}]
    newlist = sorted(result, key=lambda k: k['box'][3],reverse=False)

    text =''
    for each in newlist:
        if(each['score']>65):
            text += each['class']
    # print("text:",text)
    if(text!=""):
        text = text.replace("yellowTag", "") 
        result = text
    else:
        result = "No Vertical Tag Detected"
    response = {"predictions": [result]}
    total_time_end = time.time()
    print("total time : ",round((total_time_end-total_time_start),2))
    st.write(str(simplejson.dumps(response)))



demo_app = gr.Interface(
    fn= prediction, 
    inputs=inputs,
    outputs= "image",
    title = "Tag Diciphering",
    description = DESCRIPTION,
    examples = EXAMPLES,
    #cache_example = True,
    #live = True,
    theme = 'huggingface'
)
demo_app.launch()