Skip to content

Commit 981140c

Browse files
committed
Outsource the object recognition in a different process.
1 parent 915371b commit 981140c

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
A simple example demonstrating [Google's TensorFlow Object Detection API](https://github.com/tensorflow/models/tree/master/object_detection) with [OpenCV](http://opencv.org/) to recognizes objects through a webcam.
44

5+
## Getting Started
6+
1. `conda env create -f environment.yml`
7+
2. `python object_detection_app.py`
8+
9+
## Requirements
10+
- [Anaconda / Python 3.5](https://www.continuum.io/downloads)
11+
- [TensorFlow 1.2](https://www.tensorflow.org/)
12+
- [OpenCV 3.1](http://opencv.org/)
13+
514
## Copyright
615

716
See [LICENSE](LICENSE) for details.
8-
Copyright (c) 2017 [Dat Tran](http://www.dat-tran.com/).
17+
Copyright (c) 2017 [Dat Tran](http://www.dat-tran.com/).

environment.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: object-detection
2+
channels: !!python/tuple
3+
- menpo
4+
- defaults
5+
dependencies:
6+
- freetype=2.5.5=2
7+
- hdf5=1.8.17=1
8+
- jbig=2.1=0
9+
- jpeg=9b=0
10+
- libpng=1.6.27=0
11+
- libtiff=4.0.6=3
12+
- menpo::opencv3=3.1.0=py35_0
13+
- menpo::tbb=4.3_20141023=0
14+
- mkl=2017.0.1=0
15+
- numpy=1.13.0=py35_0
16+
- olefile=0.44=py35_0
17+
- openssl=1.0.2l=0
18+
- pillow=4.1.1=py35_0
19+
- pip=9.0.1=py35_1
20+
- python=3.5.3=1
21+
- readline=6.2=2
22+
- setuptools=27.2.0=py35_0
23+
- sqlite=3.13.0=0
24+
- tk=8.5.18=0
25+
- wheel=0.29.0=py35_0
26+
- xz=5.2.2=1
27+
- zlib=1.2.8=3
28+
- pip:
29+
- backports.weakref==1.0rc1
30+
- bleach==1.5.0
31+
- html5lib==0.9999999
32+
- markdown==2.2.0
33+
- protobuf==3.3.0
34+
- six==1.10.0
35+
- tensorflow==1.2.0
36+
- werkzeug==0.12.2
37+
prefix: /Users/datitran/anaconda/envs/object-detection
38+

object_detection_app.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import time
22
import os
33
import cv2
4+
import multiprocessing
45
import numpy as np
56
import tensorflow as tf
67

78
from utils import FPS, WebcamVideoStream
9+
from multiprocessing import Process, Queue, Pool
810
from object_detection.utils import label_map_util
911
from object_detection.utils import visualization_utils as vis_util
1012

@@ -19,6 +21,9 @@
1921

2022
NUM_CLASSES = 90
2123

24+
NUM_WORKERS = 2 # cv2.getNumberOfCPUs() - 1
25+
QUEUE_SIZE = 5
26+
2227
# Loading label map
2328
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
2429
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
@@ -57,7 +62,7 @@ def detect_objects(image_np, sess, detection_graph):
5762
return image_np
5863

5964

60-
if __name__ == '__main__':
65+
def worker(input_q, output_q):
6166
# Load a (frozen) Tensorflow model into memory.
6267
detection_graph = tf.Graph()
6368
with detection_graph.as_default():
@@ -69,34 +74,48 @@ def detect_objects(image_np, sess, detection_graph):
6974

7075
sess = tf.Session(graph=detection_graph)
7176

72-
video_capture = WebcamVideoStream(src=0).start()
7377
fps = FPS().start()
78+
while True:
79+
fps.update()
80+
frame = input_q.get()
81+
output_q.put(detect_objects(frame, sess, detection_graph))
82+
83+
fps.stop()
84+
sess.close()
7485

75-
while fps._numFrames < 120:
76-
frame = video_capture.read()
7786

78-
t = time.time()
87+
if __name__ == '__main__':
88+
logger = multiprocessing.log_to_stderr()
89+
logger.setLevel(multiprocessing.SUBDEBUG)
90+
91+
input_q = Queue(maxsize=QUEUE_SIZE)
92+
output_q = Queue(maxsize=QUEUE_SIZE)
93+
94+
process = Process(target=worker, args=((input_q, output_q)))
95+
process.daemon = True
96+
pool = Pool(NUM_WORKERS, worker, (input_q, output_q))
97+
98+
video_capture = WebcamVideoStream(src=0).start()
99+
fps = FPS().start()
79100

80-
# cv2.imshow('Video', detect_objects(frame, sess, detection_graph))
101+
while True: # fps._numFrames < 120
102+
frame = video_capture.read()
103+
input_q.put(frame)
81104

82-
# time.sleep(2)
105+
# t = time.time()
83106

84-
detect_objects(frame, sess, detection_graph)
107+
cv2.imshow('Video', output_q.get())
85108

86-
print(time.time() - t)
109+
# print(time.time() - t)
87110

88-
# update the FPS counter
89111
fps.update()
90112

91113
if cv2.waitKey(1) & 0xFF == ord('q'):
92114
break
93115

94-
# stop the timer and display FPS information
95116
fps.stop()
96117
print('[INFO] elasped time: {:.2f}'.format(fps.elapsed()))
97118
print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))
98119

99-
# When everything is done, release the capture
100120
video_capture.stop()
101121
cv2.destroyAllWindows()
102-
sess.close()

utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# From http://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/
2+
13
import cv2
24
import datetime
35
from threading import Thread

0 commit comments

Comments
 (0)