Skip to content

Commit 44e8edd

Browse files
authored
Merge pull request datitran#44 from samcrane8/master
HLS Stream Object Detection
2 parents a540eca + fb7c25c commit 44e8edd

File tree

6 files changed

+324
-173
lines changed

6 files changed

+324
-173
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ A real-time object recognition application using [Google's TensorFlow Object Det
44

55
## Getting Started
66
1. `conda env create -f environment.yml`
7-
2. `python object_detection_app.py`
7+
2. `python object_detection_app.py` / `python object_detection_multithreading.py`
88
Optional arguments (default value):
99
* Device index of the camera `--source=0`
1010
* Width of the frames in the video stream `--width=480`
1111
* Height of the frames in the video stream `--height=360`
1212
* Number of workers `--num-workers=2`
1313
* Size of the queue `--queue-size=5`
14+
* Get video from HLS stream rather than webcam '--stream-input=http://somertmpserver.com/hls/live.m3u8'
15+
* Send stream to livestreaming server '--stream-output=--stream=http://somertmpserver.com/hls/live.m3u8'
1416

1517
## Tests
1618
```

environment-linux.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: object-detection
2+
channels: !!python/tuple
3+
- menpo
4+
- defaults
5+
dependencies:
6+
- cycler=0.10.0=py35_0
7+
- freetype=2.5.5=2
8+
- icu=54.1=0
9+
- jbig=2.1=0
10+
- menpo::opencv3=3.0.1=py35_0
11+
- jpeg=9b=0
12+
- libpng=1.6.27=0
13+
- libtiff=4.0.6=3
14+
- matplotlib=2.0.2=np113py35_0
15+
- menpo::tbb
16+
- mkl=2017.0.1=0
17+
- numpy=1.13.0=py35_0
18+
- olefile=0.44=py35_0
19+
- openssl=1.0.2l=0
20+
- pillow=4.2.1=py35_0
21+
- pip=9.0.1=py35_1
22+
- py=1.4.34=py35_0
23+
- pyparsing=2.2.0=py35_0
24+
- pyqt=5.6.0=py35_2
25+
- pytest=3.2.1=py35_0
26+
- python=3.5.3=1
27+
- python-dateutil=2.6.1=py35_0
28+
- pytz=2017.2=py35_0
29+
- qt=5.6.2
30+
- readline=6.2=2
31+
- setuptools=27.2.0=py35_0
32+
- sip=4.18=py35_0
33+
- six=1.10.0=py35_0
34+
- sqlite=3.13.0=0
35+
- tk=8.5.18=0
36+
- wheel=0.29.0=py35_0
37+
- xz=5.2.2=1
38+
- zlib=1.2.8=3
39+
- pip:
40+
- backports.weakref==1.0rc1
41+
- bleach==1.5.0
42+
- html5lib==0.9999999
43+
- markdown==2.2.0
44+
- protobuf==3.3.0
45+
- tensorflow==1.2.0
46+
- werkzeug==0.12.2
47+
prefix: /Users/datitran/anaconda/envs/object-detection
48+

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ dependencies:
4444
- protobuf==3.3.0
4545
- tensorflow==1.2.0
4646
- werkzeug==0.12.2
47+
- scipy
4748
prefix: /Users/datitran/anaconda/envs/object-detection
4849

object_detection_app.py

Lines changed: 11 additions & 2 deletions
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.app_utils import FPS, WebcamVideoStream
9+
from utils.app_utils import FPS, WebcamVideoStream, HLSVideoStream
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
@@ -85,6 +85,7 @@ def worker(input_q, output_q):
8585

8686
if __name__ == '__main__':
8787
parser = argparse.ArgumentParser()
88+
parser.add_argument('-str', '--stream', dest="stream", action='store', type=str, default=None)
8889
parser.add_argument('-src', '--source', dest='video_source', type=int,
8990
default=0, help='Device index of the camera.')
9091
parser.add_argument('-wd', '--width', dest='width', type=int,
@@ -104,9 +105,17 @@ def worker(input_q, output_q):
104105
output_q = Queue(maxsize=args.queue_size)
105106
pool = Pool(args.num_workers, worker, (input_q, output_q))
106107

107-
video_capture = WebcamVideoStream(src=args.video_source,
108+
109+
if (args.stream):
110+
print('Reading from hls stream.')
111+
video_capture = HLSVideoStream(src=args.stream).start()
112+
else:
113+
print('Reading from webcam.')
114+
video_capture = WebcamVideoStream(src=args.video_source,
108115
width=args.width,
109116
height=args.height).start()
117+
118+
110119
fps = FPS().start()
111120

112121
while True: # fps._numFrames < 120

object_detection_multithreading.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import time
44
import argparse
55
import numpy as np
6+
import subprocess as sp
7+
import json
68
import tensorflow as tf
79

810
from queue import Queue
911
from threading import Thread
10-
from utils.app_utils import FPS, WebcamVideoStream, draw_boxes_and_labels
12+
from utils.app_utils import FPS, HLSVideoStream, WebcamVideoStream, draw_boxes_and_labels
1113
from object_detection.utils import label_map_util
1214

1315
CWD_PATH = os.getcwd()
@@ -83,22 +85,29 @@ def worker(input_q, output_q):
8385

8486
if __name__ == '__main__':
8587
parser = argparse.ArgumentParser()
88+
parser.add_argument('-strin', '--stream-input', dest="stream_in", action='store', type=str, default=None)
8689
parser.add_argument('-src', '--source', dest='video_source', type=int,
8790
default=0, help='Device index of the camera.')
8891
parser.add_argument('-wd', '--width', dest='width', type=int,
89-
default=480, help='Width of the frames in the video stream.')
92+
default=640, help='Width of the frames in the video stream.')
9093
parser.add_argument('-ht', '--height', dest='height', type=int,
91-
default=360, help='Height of the frames in the video stream.')
94+
default=480, help='Height of the frames in the video stream.')
95+
parser.add_argument('-strout','--stream-output', dest="stream_out", help='The URL to send the livestreamed object detection to.')
9296
args = parser.parse_args()
9397

94-
input_q = Queue(5) # fps is better if queue is higher but then more lags
98+
input_q = Queue(1) # fps is better if queue is higher but then more lags
9599
output_q = Queue()
96100
for i in range(1):
97101
t = Thread(target=worker, args=(input_q, output_q))
98102
t.daemon = True
99103
t.start()
100104

101-
video_capture = WebcamVideoStream(src=args.video_source,
105+
if (args.stream_in):
106+
print('Reading from hls stream.')
107+
video_capture = HLSVideoStream(src=args.stream_in).start()
108+
else:
109+
print('Reading from webcam.')
110+
video_capture = WebcamVideoStream(src=args.video_source,
102111
width=args.width,
103112
height=args.height).start()
104113
fps = FPS().start()
@@ -125,7 +134,10 @@ def worker(input_q, output_q):
125134
int(point['ymin'] * args.height) - 10), color, -1, cv2.LINE_AA)
126135
cv2.putText(frame, name[0], (int(point['xmin'] * args.width), int(point['ymin'] * args.height)), font,
127136
0.3, (0, 0, 0), 1)
128-
cv2.imshow('Video', frame)
137+
if args.stream_out:
138+
print('Streaming elsewhere!')
139+
else:
140+
cv2.imshow('Video', frame)
129141

130142
fps.update()
131143

0 commit comments

Comments
 (0)