Skip to content
Permalink
Browse files

webcam, ip camera option added

  • Loading branch information...
kimyoon-young committed Jul 25, 2019
1 parent e6cb4a6 commit 93751fae1c949f752457ac93136ee19ce75cc049
Showing with 115 additions and 38 deletions.
  1. +30 −4 README.md
  2. +43 −16 demo_centernet_deepsort.py
  3. +42 −18 demo_centernet_deepsort_thread.py
@@ -32,6 +32,33 @@ Using sample video, we can track multi person.
python demo_centernet_deepsort.py
```


for webcam, modify two lines

```
opt.input_type = 'webcam'
//webcam device number
opt.webcam_ind = 0
```

for ip camera, modify three lines

```
opt.input_type = 'ipcam'
//ip camera url (this is DAHUA camera format)
opt.ipcam_url = 'rtsp://{0}:{1}@IPAddress:554/cam/realmonitor?channel={2}&subtype=1'
//ipcamera camera number
opt.ipcam_no = 1
```

and create a login file ('cam_secret.txt') containing a camera ID and password
for example,
```
kim
1234
```


In test step, we used 'ctdet_coco_dla_2x.pth' model in [centernet model zoo](https://github.com/xingyizhou/CenterNet/blob/master/readme/MODEL_ZOO.md).
Change two lines if want to use another model(e.g resdcn18.pth).

@@ -53,11 +80,11 @@ GPU : one 1080ti 11G

![Alt Text](https://github.com/kimyoon-young/centerNet-deep-sort/blob/master/centernet_vs_yolo3.gif)

(Left) CenterNet based tracker: fps 18-23 / (Rright) original yolov3 version[2] : fps 8-9
(Left) CenterNet based tracker: fps 18~23 (vis_thresh=0.5) / (Rright) original yolov3 version[2] : fps 11-12 (conf_thresh=0.5, nms_thresh=0.4)

For ctdet_coco_resdcn18 model, fps is 30~35.
For ctdet_coco_resdcn18 model, fps is 30~35 (vis_thresh=0.5).

Optionally, using this [treading module[4]](https://www.pyimagesearch.com/2017/02/06/faster-video-file-fps-with-cv2-videocapture-and-opencv/) can slightly improves fps (from 0.4 to 1).
Optionally, using this [threading module[4]](https://www.pyimagesearch.com/2017/02/06/faster-video-file-fps-with-cv2-videocapture-and-opencv/) can slightly improves fps (plus less than 1 fps).

```
pip install imutils
@@ -75,7 +102,6 @@ def more(self):
```



```
python demo_centernet_deepsort_thread.py
@@ -5,7 +5,7 @@

#CenterNet
import sys
CENTERNET_PATH = '/home/asoft/deep-sort+CenterNet/CenterNet/src/lib/'
CENTERNET_PATH = '/home/asoft/centerNet-deep-sort/CenterNet/src/lib/'
sys.path.insert(0, CENTERNET_PATH)
from detectors.detector_factory import detector_factory
from opts import opts
@@ -18,17 +18,29 @@
#ARCH = 'resdcn_18'


#python test.py ctdet --exp_id coco_resdcn18 --arch resdcn_18 --keep_res --resume
#python test.py ctdet --exp_id coco_dla_2x --keep_res --resume



TASK = 'ctdet' # or 'multi_pose' for human pose estimation
opt = opts().init('{} --load_model {} --arch {}'.format(TASK, MODEL_PATH, ARCH).split(' '))

image_ext = ['jpg', 'jpeg', 'png', 'webp']
video_ext = ['mp4', 'mov', 'avi', 'mkv']
time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge']
#vis_thresh
opt.vis_thresh = 0.5


#input_type
opt.input_type = 'vid' # for video, 'vid', for webcam, 'webcam', for ip camera, 'ipcam'

#------------------------------
# for video
opt.vid_path = 'MOT16-11.mp4' #
#------------------------------
# for webcam (webcam device index is required)
opt.webcam_ind = 0
#------------------------------
# for ipcamera (camera url is required.this is dahua url format)
opt.ipcam_url = 'rtsp://{0}:{1}@IPAddress:554/cam/realmonitor?channel={2}&subtype=1'
# ipcamera camera number
opt.ipcam_no = 8
#------------------------------


from deep_sort import DeepSort
@@ -39,13 +51,13 @@

def bbox_to_xywh_cls_conf(bbox):
person_id = 1
confidence = 0.5
#confidence = 0.5
# only person
bbox = bbox[person_id]

if any(bbox[:, 4] > confidence):
if any(bbox[:, 4] > opt.vis_thresh):

bbox = bbox[bbox[:, 4] > confidence, :]
bbox = bbox[bbox[:, 4] > opt.vis_thresh, :]
bbox[:, 2] = bbox[:, 2] - bbox[:, 0] #
bbox[:, 3] = bbox[:, 3] - bbox[:, 1] #

@@ -59,27 +71,42 @@ def bbox_to_xywh_cls_conf(bbox):
class Detector(object):
def __init__(self, opt):
self.vdo = cv2.VideoCapture()
#self.yolo_info = YOLO3("YOLO3/cfg/yolo_v3.cfg", "YOLO3/yolov3.weights", "YOLO3/cfg/coco.names", is_xywh=True)


#centerNet detector
self.detector = detector_factory[opt.task](opt)
self.deepsort = DeepSort("deep/checkpoint/ckpt.t7")
# self.deepsort = DeepSort("deep/checkpoint/ori_net_last.pth")


self.write_video = True

def open(self, video_path):
assert os.path.isfile(video_path), "Error: path error"
self.vdo.open(video_path)

if opt.input_type == 'webcam':
self.vdo.open(opt.webcam_ind)

elif opt.input_type == 'ipcam':
# load cam key, secret
with open("cam_secret.txt") as f:
lines = f.readlines()
key = lines[0].strip()
secret = lines[1].strip()

self.vdo.open(opt.ipcam_url.format(key, secret, opt.ipcam_no))

# video
else :
assert os.path.isfile(opt.vid_path), "Error: path error"
self.vdo.open(opt.vid_path)

self.im_width = int(self.vdo.get(cv2.CAP_PROP_FRAME_WIDTH))
self.im_height = int(self.vdo.get(cv2.CAP_PROP_FRAME_HEIGHT))

self.area = 0, 0, self.im_width, self.im_height
if self.write_video:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
self.output = cv2.VideoWriter("demo1.avi", fourcc, 20, (self.im_width, self.im_height))
return self.vdo.isOpened()
#return self.vdo.isOpened()



@@ -6,7 +6,7 @@

#CenterNet
import sys
CENTERNET_PATH = '/home/asoft/deep-sort+CenterNet/CenterNet/src/lib/'
CENTERNET_PATH = 'CENTERNET_ROOT/CenterNet/src/lib/'
sys.path.insert(0, CENTERNET_PATH)
from detectors.detector_factory import detector_factory
from opts import opts
@@ -19,17 +19,25 @@
#ARCH = 'resdcn_18'


#python test.py ctdet --exp_id coco_resdcn18 --arch resdcn_18 --keep_res --resume
#python test.py ctdet --exp_id coco_dla_2x --keep_res --resume



TASK = 'ctdet' # or 'multi_pose' for human pose estimation
opt = opts().init('{} --load_model {} --arch {}'.format(TASK, MODEL_PATH, ARCH).split(' '))

image_ext = ['jpg', 'jpeg', 'png', 'webp']
video_ext = ['mp4', 'mov', 'avi', 'mkv']
time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge']
#input_type
opt.input_type = 'vid' # video : vid, webcam : webcam, ip camera : ipcam

#------------------------------
# for video
opt.vid_path = 'MOT16-11.mp4' #
#------------------------------
# for webcam (webcam device index is required)
opt.webcam_ind = 0
#------------------------------
# for ipcamera (camera url is required.this is dahua url format)
opt.ipcam_url = 'rtsp://{0}:{1}@IPAddress:554/cam/realmonitor?channel={2}&subtype=1'
# ipcamera camera number
opt.ipcam_no = 8
#------------------------------


from deep_sort import DeepSort
@@ -40,13 +48,12 @@

def bbox_to_xywh_cls_conf(bbox):
person_id = 1
confidence = 0.5
# only person
bbox = bbox[person_id]

if any(bbox[:, 4] > confidence):
if any(bbox[:, 4] > opt.vis_thresh):

bbox = bbox[bbox[:, 4] > confidence, :]
bbox = bbox[bbox[:, 4] > opt.vis_thresh, :]
bbox[:, 2] = bbox[:, 2] - bbox[:, 0] #
bbox[:, 3] = bbox[:, 3] - bbox[:, 1] #

@@ -61,8 +68,6 @@ class Detector(object):
def __init__(self, opt):
#self.vdo = cv2.VideoCapture()

#self.yolo_info = YOLO3("YOLO3/cfg/yolo_v3.cfg", "YOLO3/yolov3.weights", "YOLO3/cfg/coco.names", is_xywh=True)


#centerNet detector
self.detector = detector_factory[opt.task](opt)
@@ -72,11 +77,24 @@ def __init__(self, opt):

self.write_video = True

def open(self, video_path):
assert os.path.isfile(video_path), "Error: path error"
def open(self):

if opt.input_type == 'webcam':
self.vdo = FileVideoStream(opt.webcam_ind).start()

elif opt.input_type == 'ipcam':
# load cam key, secret
with open("cam_secret.txt") as f:
lines = f.readlines()
key = lines[0].strip()
secret = lines[1].strip()

self.vdo = FileVideoStream(opt.ipcam_url.format(key, secret, opt.ipcam_no)).start()

#self.vdo.open(video_path)
self.vdo = FileVideoStream(video_path).start()
# video
else :
assert os.path.isfile(opt.vid_path), "Error: path error"
self.vdo = FileVideoStream(opt.vid_path).start()


self.im_width = int(self.vdo.stream.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -146,12 +164,18 @@ def detect(self):
# if len(sys.argv) == 1:
# print("Usage: python demo_yolo3_deepsort.py [YOUR_VIDEO_PATH]")
# else:

cv2.namedWindow("test", cv2.WINDOW_NORMAL)
cv2.resizeWindow("test", 800, 600)

#opt = opts().init()
det = Detector(opt)

# det.open("D:\CODE\matlab sample code/season 1 episode 4 part 5-6.mp4")
det.open("MOT16-11.mp4")
det.open()
det.detect()





0 comments on commit 93751fa

Please sign in to comment.
You can’t perform that action at this time.