forked from siddiqaa/psvcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectPSVs.py
executable file
·157 lines (129 loc) · 7.53 KB
/
DetectPSVs.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
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
153
154
155
156
157
from subprocess import call
import sys
import os
from PIL import Image
import numpy as np
from scipy import ndimage
from subprocess import call
import sys
import os
from PIL import Image
from datetime import datetime
import cv2
from object_detection.utils import visualization_utils as vis_util
from object_detection.utils import label_map_util
# intiate tensorflow and load custom model
import tensorflow as tf
import numpy as np
CWD_PATH = os.getcwd()
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = '<CONFIGURE_YOUR_PATH_HERE>_output_inference_graph_21.pb/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = '<CONFIGURE_YOUR_PATH_HERE>e/data/label_map.pbtxt'
NUM_CLASSES = 5
# Loading label map
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
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='')
def detect_objects(image_np, sess, detection_graph):
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
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.
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.
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# the 0.8 is the confidence threshold. This script sorts the detected classes in descending confidence level and the code below checks to see if any of the top five detected objects match our target class with a threshold confidence greater than 80%
if ((np.squeeze(scores)[0] > 0.8) and (np.squeeze(classes)[0] == 1)) \
or ((np.squeeze(scores)[1] > 0.4) and (np.squeeze(classes)[1] == 1)) \
or ((np.squeeze(scores)[2] > 0.4) and (np.squeeze(classes)[2] == 1)) \
or ((np.squeeze(scores)[3] > 0.4) and (np.squeeze(classes)[3] == 1)) \
or ((np.squeeze(scores)[4] > 0.4) and (np.squeeze(classes)[4] == 1)):
# Visualization of the results of a detection.
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)
img = Image.fromarray(image_np, 'RGB')
# print(os.path.splitext(segmentFileName)[0])
img.save("<CONFIGURE_PATH_TO_SAVING_THE_IMAGE_SEGMENTS_WITH_BOUNDINGBOXES>" + "_detected.jpg")
# img.show()
pageHeight = 3300
pageWidth = 5100
cropHeight = int(pageHeight / 30)
cropWidth = int(pageWidth / 50)
pageIndex = 1
with tf.Session(graph=detection_graph) as sess:
startTime = datetime.now()
imageNumpyArray = ndimage.imread("<CONFIGURE_PATH_TO_JPG_FILE_TO_CONDUCT_OBJECT_DETECTION_ON>")
overlapWidth = 10
#code below loads the jpg image into a single numpy array and extracts shorter segments to feed them individualy as input tensors into tensor_flow model
segmentIndex = 0
while segmentIndex <= 1499:
if (segmentIndex == 0):
cropArray = imageNumpyArray[0:cropHeight + overlapWidth, 0:cropWidth + overlapWidth, :]
# catch top right corner tile
elif (segmentIndex == 49):
cropArray = imageNumpyArray[0:cropHeight + overlapWidth,
segmentIndex * cropWidth - overlapWidth:segmentIndex * cropWidth + cropWidth, :]
# catch bottom left corner tile
elif (segmentIndex == 1450):
cropArray = imageNumpyArray[
cropHeight * segmentIndex // 50 - overlapWidth:cropHeight * segmentIndex // 50 + cropHeight,
0:cropWidth + overlapWidth, :]
# catch bottom right corner tile
elif (segmentIndex == 1499):
cropArray = imageNumpyArray[
cropHeight * segmentIndex // 50 - cropHeight - overlapWidth:cropHeight * segmentIndex // 50,
segmentIndex % 50 * cropWidth - overlapWidth:segmentIndex % 50 * cropWidth + cropWidth, :]
# catch right edge tiles so no overlap on left
elif (segmentIndex % 50 == 0):
# print(cropHeight*segmentIndex//50)
cropArray = imageNumpyArray[cropHeight * (segmentIndex // 50):cropHeight * (segmentIndex // 50) + cropHeight,
0:cropWidth + overlapWidth, :]
# catch top edge tiles so no overlap on top
elif (segmentIndex <= 48):
# print(segmentIndex*cropWidth)
cropArray = imageNumpyArray[0:cropHeight + overlapWidth,
segmentIndex * cropWidth:segmentIndex * cropWidth + cropWidth, :]
# catch left edge tiles so no overlap on left
elif (segmentIndex + 1) % 50 == 0:
# print(segmentIndex * cropWidth)
cropArray = imageNumpyArray[((segmentIndex + 1) // 50) * cropHeight - overlapWidth:((
segmentIndex + 1) // 50) * cropHeight + cropHeight + overlapWidth,
(segmentIndex) % 50 * cropWidth - overlapWidth:(segmentIndex) % 50 * cropWidth + cropWidth, :]
# catch bottom edge tiles so no overlap on top
elif (segmentIndex > 1450):
# print(segmentIndex * cropWidth)
cropArray = imageNumpyArray[((segmentIndex + 1) // 50) * cropHeight:((
segmentIndex + 1) // 50) * cropHeight + cropHeight + overlapWidth,
(segmentIndex) % 50 * cropWidth - overlapWidth:(segmentIndex) % 50 * cropWidth + cropWidth, :]
else:
cropArray = imageNumpyArray[(segmentIndex // 50) * cropHeight - overlapWidth: (
segmentIndex // 50) * cropHeight + cropHeight + overlapWidth,
(segmentIndex) % 50 * cropWidth - overlapWidth:(
segmentIndex) % 50 * cropWidth + cropWidth + overlapWidth,
:]
detect_objects(cropArray, sess, detection_graph)
if segmentIndex % 150 == 0:
print(str(segmentIndex // 150 * 10) + " percent complete")
segmentIndex += 1