-
Notifications
You must be signed in to change notification settings - Fork 1
/
Image_Detection_through_frozenfile.py
102 lines (87 loc) · 3.8 KB
/
Image_Detection_through_frozenfile.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
##importing the neccesary packages
import argparse
import numpy as np
import os
from os import listdir
from os.path import isfile, join
import sys
import tensorflow as tf
print('Using Tensorflow Version ' + str(tf.__version__))
import cv2
import time
sys.path.append("..")
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
##Argumentparser is used to run through the terminal
parser = argparse.ArgumentParser()
parser.add_argument('-i', dest='input_image',
help='image to be processed')
parser.add_argument('-o', dest='output_image',
help='path of output image')
args = parser.parse_args()
##Input and output video path
INPUT_IMG_PATH = args.input_image
OUTPUT_IMG_PATH = args.output_image
##Threshold at which the detection bounding boxes will display
TH = 0.70
# Gloabl Variables
image_tensor = None
detection_boxes = None
detection_scores = None
detection_classes = None
num_detections = None
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = 'output_inference_graph.pb/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'inputs/label_map.pbtxt'
## number of classes that frozen file is trained for
NUM_CLASSES = 1
# ## Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
# ## Loading hole label map
s_label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
s_categories = label_map_util.convert_label_map_to_categories(s_label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
s_category_index = label_map_util.create_category_index(s_categories)
def resize_img(frame):
percent = 50
width = int(frame.shape[1] * percent / 100)
height = int(frame.shape[0] * percent / 100)
dim = (width, height)
resized = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
return resized
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# 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 represent how level of confidence for each of the objects.
# 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')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
##Loading image
image = cv2.imread(INPUT_IMG_PATH)
# image = resize_img(image)
image_np = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
image_np_expanded = np.expand_dims(image_np, axis=0)
fetch_time = time.time()
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
sboxes = np.squeeze(boxes);
sclasses = np.squeeze(classes).astype(np.int32);
sscores = np.squeeze(scores);
_= vis_util.visualize_boxes_and_labels_on_image_array(image,sboxes,sclasses,
sscores,s_category_index,min_score_thresh=TH,
max_boxes_to_draw=100,use_normalized_coordinates=True,
skip_scores=False,line_thickness=6)
print("Inference_Time : " + str(time.time()-fetch_time))
cv2.imwrite(OUTPUT_IMG_PATH,image)