Skip to content

Commit f52dbf4

Browse files
committed
Change folder structure, finish the multithreading example to improve fps on low performance machines.
1 parent 9c74015 commit f52dbf4

File tree

7 files changed

+69
-43
lines changed

7 files changed

+69
-43
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ A real-time object recognition application using [Google's TensorFlow Object Det
1212
* Number of workers `--num-workers=2`
1313
* Size of the queue `--queue-size=5`
1414

15+
## Tests
16+
```
17+
pytest -vs utils/
18+
```
19+
1520
## Requirements
1621
- [Anaconda / Python 3.5](https://www.continuum.io/downloads)
1722
- [TensorFlow 1.2](https://www.tensorflow.org/)

object_detection_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
import tensorflow as tf
88

9-
from utils import FPS, WebcamVideoStream
9+
from utils.app_utils import FPS, WebcamVideoStream
1010
from multiprocessing import Queue, Pool
1111
from object_detection.utils import label_map_util
1212
from object_detection.utils import visualization_utils as vis_util

object_detection_multilayer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
import tensorflow as tf
88

9-
from utils import FPS
9+
from utils.app_utils import FPS
1010
from object_detection.utils import label_map_util
1111
from object_detection.utils import visualization_utils as vis_util
1212

object_detection_multithreading.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from queue import Queue
99
from threading import Thread
10-
from utils import FPS, WebcamVideoStream, draw_boxes_and_labels
10+
from utils.app_utils import FPS, WebcamVideoStream, draw_boxes_and_labels
1111
from object_detection.utils import label_map_util
1212

1313
CWD_PATH = os.getcwd()
@@ -49,10 +49,12 @@ def detect_objects(image_np, sess, detection_graph):
4949

5050
# Visualization of the results of a detection.
5151
rect_points, class_names, class_colors = draw_boxes_and_labels(
52-
np.squeeze(boxes),
53-
np.squeeze(classes).astype(np.int32),
54-
np.squeeze(scores),
55-
category_index)
52+
boxes=np.squeeze(boxes),
53+
classes=np.squeeze(classes).astype(np.int32),
54+
scores=np.squeeze(scores),
55+
category_index=category_index,
56+
min_score_thresh=.5
57+
)
5658
return dict(rect_points=rect_points, class_names=class_names, class_colors=class_colors)
5759

5860

@@ -88,7 +90,7 @@ def worker(input_q, output_q):
8890
default=360, help='Height of the frames in the video stream.')
8991
args = parser.parse_args()
9092

91-
input_q = Queue(30)
93+
input_q = Queue(5) # fps is better if queue is higher but then more lags
9294
output_q = Queue()
9395
for i in range(1):
9496
t = Thread(target=worker, args=(input_q, output_q))
@@ -107,19 +109,21 @@ def worker(input_q, output_q):
107109
t = time.time()
108110

109111
if output_q.empty():
110-
cv2.imshow('Video', frame)
112+
pass # fill up queue
111113
else:
112-
# TO-DO need to draw the boxes here
113114
font = cv2.FONT_HERSHEY_SIMPLEX
114115
data = output_q.get()
115116
rec_points = data['rect_points']
116117
class_names = data['class_names']
117118
class_colors = data['class_colors']
118119
for point, name, color in zip(rec_points, class_names, class_colors):
119-
cv2.rectangle(frame, (int(point[1] * args.width), int(point[0] * args.height)),
120-
(int(point[3] * args.width), int(point[2] * args.height)), color, 3)
121-
cv2.putText(frame, name[0], (int(point[1] * args.width), int(point[0] * args.height)), font, 0.5,
122-
(0, 0, 0), 1)
120+
cv2.rectangle(frame, (int(point['xmin'] * args.width), int(point['ymin'] * args.height)),
121+
(int(point['xmax'] * args.width), int(point['ymax'] * args.height)), color, 3)
122+
cv2.rectangle(frame, (int(point['xmin'] * args.width), int(point['ymin'] * args.height)),
123+
(int(point['xmin'] * args.width) + len(name[0]) * 6,
124+
int(point['ymin'] * args.height) - 10), color, -1, cv2.LINE_AA)
125+
cv2.putText(frame, name[0], (int(point['xmin'] * args.width), int(point['ymin'] * args.height)), font,
126+
0.3, (0, 0, 0), 1)
123127
cv2.imshow('Video', frame)
124128

125129
fps.update()

utils/__init__.py

Whitespace-only changes.

utils.py renamed to utils/app_utils.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,33 @@ def stop(self):
7878
self.stopped = True
7979

8080

81-
STANDARD_COLORS = [
82-
'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque',
83-
'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
84-
'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
85-
'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
86-
'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
87-
'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
88-
'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
89-
'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
90-
'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
91-
'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
92-
'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
93-
'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
94-
'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
95-
'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
96-
'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
97-
'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
98-
'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
99-
'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
100-
'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
101-
'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
102-
'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
103-
'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
104-
'WhiteSmoke', 'Yellow', 'YellowGreen'
105-
]
81+
def standard_colors():
82+
colors = [
83+
'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque',
84+
'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
85+
'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
86+
'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
87+
'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
88+
'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
89+
'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
90+
'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
91+
'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
92+
'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
93+
'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
94+
'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
95+
'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
96+
'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
97+
'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
98+
'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
99+
'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
100+
'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
101+
'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
102+
'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
103+
'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
104+
'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
105+
'WhiteSmoke', 'Yellow', 'YellowGreen'
106+
]
107+
return colors
106108

107109

108110
def color_name_to_rgb():
@@ -122,7 +124,7 @@ def draw_boxes_and_labels(
122124
max_boxes_to_draw=20,
123125
min_score_thresh=.5,
124126
agnostic_mode=False):
125-
"""Returns boxes coordinates of the boxes, class names and colors
127+
"""Returns boxes coordinates, class names and colors
126128
127129
Args:
128130
boxes: a numpy array of shape [N, 4]
@@ -175,8 +177,8 @@ def draw_boxes_and_labels(
175177
if agnostic_mode:
176178
box_to_color_map[box] = 'DarkOrange'
177179
else:
178-
box_to_color_map[box] = STANDARD_COLORS[
179-
classes[i] % len(STANDARD_COLORS)]
180+
box_to_color_map[box] = standard_colors()[
181+
classes[i] % len(standard_colors())]
180182

181183
# Store all the coordinates of the boxes, class names and colors
182184
color_rgb = color_name_to_rgb()
@@ -185,7 +187,7 @@ def draw_boxes_and_labels(
185187
class_colors = []
186188
for box, color in six.iteritems(box_to_color_map):
187189
ymin, xmin, ymax, xmax = box
188-
rect_points.append((ymin, xmin, ymax, xmax))
190+
rect_points.append(dict(ymin=ymin, xmin=xmin, ymax=ymax, xmax=xmax))
189191
class_names.append(box_to_display_str_map[box])
190192
class_colors.append(color_rgb[color.lower()])
191193
return rect_points, class_names, class_colors

utils/test_app_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from object_detector_app.utils.app_utils import color_name_to_rgb, standard_colors
3+
4+
5+
class TestUtils(unittest.TestCase):
6+
def setUp(self):
7+
self.colors = color_name_to_rgb()
8+
self.standard_colors = standard_colors()
9+
10+
def test_all_colors(self):
11+
"""Test that manual defined colors are also in the matplotlib color name space."""
12+
color_list = set(sorted(list(self.colors.keys())))
13+
standard_color_list = set(sorted([color.lower() for color in self.standard_colors]))
14+
color_common = standard_color_list.intersection(color_list)
15+
self.assertEqual(len(color_common), len(standard_color_list))

0 commit comments

Comments
 (0)