Skip to content

Commit

Permalink
Added Webcam.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksanjeevan committed Jul 8, 2018
1 parent 60a4435 commit 8d0a3de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ Original paper and github: [YOLO9000: Better, Faster, Stronger](https://arxiv.or
- [opencv](https://pypi.org/project/opencv-python/)
- [python3.5](https://www.python.org/)

### Simple use
---
1. Download pretrained [model](https://drive.google.com/open?id=1khOgS8VD-paUD8KhjOLOzEXkzaXNAAMq) and place it in **dourflow/**.
2. Predict on an image:

```bash
python3 dourflow.py path/to/test_image
```

### Usage
---
Running `python3 dourflow.py --help`:
Expand All @@ -45,8 +54,9 @@ optional arguments:
Pass what to do with dourflow:

1. A path to an image file/dir or video: Run inference on those file(s).
2. 'validate': Perform validation on a trained model.
3. 'train': Perform training on your own dataset.
2. 'cam': Run inference on webcam ('cams' to store the output).
3. 'validate': Perform validation on a trained model.
4. 'train': Perform training on your own dataset.

##### *model*
Pass the keras input model h5 file (could be to perform inference, validate against or for transfer learning).
Expand Down Expand Up @@ -128,10 +138,12 @@ python3 dourflow.py images/ -m coco_model.h5 -c coco_config.json -t 0.35
Allows to evaluate the performance of a model by computing its [mean Average Precision](http://host.robots.ox.ac.uk:8080/pascal/VOC/voc2012/htmldoc/devkit_doc.html#SECTION00050000000000000000) in the task of object detection (mAP WRITE UP COMING SOON).

Example:

```bash
python3 dourflow.py validate -m voc_model.h5 -c voc_config.json
```
Output:

```bash
Batch Processed: 100%|████████████████████████████████████████████| 4282/4282 [01:53<00:00, 37.84it/s]
AP( bus ): 0.806
Expand Down Expand Up @@ -162,10 +174,6 @@ mAP: 0.674

### Training
---
**NEED TO DO A FEW FIXES TO LOSS FUNCTION BEFORE THIS IS DONE**
---



##### Split dataset
Script to generate training/testing splits.
Expand Down Expand Up @@ -193,11 +201,10 @@ Then, in another terminal tab you can run `tensorboard --logdir=logs/run_X` and

#### To Do

- [ ] TRAINING BUG / FINISH LOSS FUNCTION
- [ ] cfg parser
- [ ] Anchor generation for custom datasets
- [ ] mAP write up
- [ ] Add webcam support
- [x] Add webcam support

#### Inspired from

Expand Down
2 changes: 1 addition & 1 deletion net/netarch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_model(self, loss_func):
#new_name = self.tl_weights_name.split('.')[0] + '_rand.h5'
#new_yolo_model.save_weights(new_name)

elif YoloParams.YOLO_MODE in ['inference','validate','video']:
elif YoloParams.YOLO_MODE in ['inference','validate','video','cam']:
new_yolo_model = yolo_model

else:
Expand Down
24 changes: 14 additions & 10 deletions net/netparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
'-m',
'--model',
help='path to input yolo v2 keras model',
default='yolo_model.h5')
default='coco_model.h5')


argparser.add_argument(
'-c',
'--conf',
help='path to configuration file',
default='config.json')
default='confs/config_coco.json')


argparser.add_argument(
Expand Down Expand Up @@ -71,8 +71,16 @@ class YoloParams(object):
# Mode
PREDICT_IMAGE = ''
WEIGHT_FILE = ''
if action != 'gen':
if action == 'validate' or action == 'train':
WEBCAM_OUT = ''

if action == 'gen':
assert args.weight_file, "Need to pass weight file if generating model."
WEIGHT_FILE = args.weight_file
elif action == 'cams':
WEBCAM_OUT = 'cam_out.mp4'
YOLO_MODE = 'cam'
else:
if action == 'validate' or action == 'train' or action == 'cam':
YOLO_MODE = action
else:
if os.path.isdir(action):
Expand All @@ -83,14 +91,10 @@ class YoloParams(object):
else:
YOLO_MODE = 'inference'
else:
raise ValueError('First argument for dourflow must be: \'training\','
' \'validation\' or an image file/dir.')
raise ValueError('Run \'python3 dourflow.py --help\'.')

PREDICT_IMAGE = action
else:
assert args.weight_file, "Need to pass weight file if generating model."
# Paths
WEIGHT_FILE = args.weight_file


TRAIN_IMG_PATH = config['train']['image_folder']
TRAIN_ANN_PATH = config['train']['annot_folder']
Expand Down
50 changes: 29 additions & 21 deletions yolov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def run(self, **kwargs):
elif YoloParams.YOLO_MODE == 'video':
self.video_inference(YoloParams.PREDICT_IMAGE)

elif YoloParams.YOLO_MODE == 'cam':
self.cam_inference(YoloParams.WEBCAM_OUT)


# Sometimes bug: https://github.com/tensorflow/tensorflow/issues/3388
K.clear_session()

Expand Down Expand Up @@ -136,8 +140,7 @@ def video_inference(self, filename):
size = (video_width, video_height)
fps = round(cap.get(cv2.CAP_PROP_FPS))

#fourcc = cv2.VideoWriter_fourcc(*"MJPG")
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')

writer = cv2.VideoWriter(filename.split('.')[0]+"_pred.mp4", fourcc, fps, size)

Expand All @@ -164,31 +167,36 @@ def video_inference(self, filename):
cv2.destroyAllWindows()


def video_inference2(self):
# MAKEONE FOR WEBCAM, USE COMMENTED CODE FROM ABOVE
video_reader = cv2.VideoCapture(video_inp)
def cam_inference(self, fname):

cap = cv2.VideoCapture(0)
video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
size = (video_width, video_height)
fps = round(cap.get(cv2.CAP_PROP_FPS))

nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')

video_writer = cv2.VideoWriter(video_out,
cv2.VideoWriter_fourcc(*'XVID'),
50.0,
(frame_w, frame_h))
if fname: writer = cv2.VideoWriter("out.mp4", fourcc, fps, size)

while(cap.isOpened()):

for i in tqdm(range(nb_frames)):
ret, image = video_reader.read()
ret, frame = cap.read()
if ret==True:

boxes, scores, _, labels = self.inf_model.predict(image)
boxes, scores, _, labels = self.inf_model.predict(frame)
frame_pred = draw_boxes(frame, (boxes, scores, labels))

image = draw_boxes(image, (boxes, scores, labels))

video_writer.write(np.uint8(image))

video_reader.release()
video_writer.release()
if fname: writer.write(frame)

cv2.imshow('Yolo Output',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
if fname: writer.release()
cv2.destroyAllWindows()



Expand Down

0 comments on commit 8d0a3de

Please sign in to comment.