You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/06/07 20:40:27 UTC

[GitHub] zhreshold closed pull request #11033: Add frame-resize option to ssd demo to scale camera input

zhreshold closed pull request #11033: Add frame-resize option to ssd demo to scale camera input
URL: https://github.com/apache/incubator-mxnet/pull/11033
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/example/ssd/README.md b/example/ssd/README.md
index 55387c5fd2d..cc034689c7b 100644
--- a/example/ssd/README.md
+++ b/example/ssd/README.md
@@ -17,7 +17,8 @@ remarkable traits of MXNet.
 Due to the permission issue, this example is maintained in this [repository](https://github.com/zhreshold/mxnet-ssd) separately. You can use the link regarding specific per example [issues](https://github.com/zhreshold/mxnet-ssd/issues).
 
 ### What's new
-* Added live camera capture and detection display (run with --camera flag)
+* Added live camera capture and detection display (run with --camera flag). Example:
+    `./demo.py --camera --cpu --frame-resize 0.5`
 * Added multiple trained models.
 * Added a much simpler way to compose network from mainstream classification networks (resnet, inception...) and [Guide](symbol/README.md).
 * Update to the latest version according to caffe version, with 5% mAP increase.
diff --git a/example/ssd/dataset/cv2Iterator.py b/example/ssd/dataset/cv2Iterator.py
index 469faeac828..0af8c3272fa 100644
--- a/example/ssd/dataset/cv2Iterator.py
+++ b/example/ssd/dataset/cv2Iterator.py
@@ -26,10 +26,19 @@ class CameraIterator():
     """
     def __init__(self, capture=cv2.VideoCapture(0), frame_resize=None):
         self._capture = capture
-        self._frame_resize = frame_resize
+        self._frame_resize = None
         if frame_resize:
-            assert isinstance(frame_resize, tuple) and (len(tuple) == 2), "frame_resize should be a tuple of (x,y)"
-            self._frame_shape = (1, 3, frame_resize[0], frame_resize[1])
+            if isinstance(frame_resize, (tuple, list)) and (len(frame_resize) == 2):
+                self._frame_resize = tuple(map(int, frame_resize))
+                self._frame_shape = (1, 3, self._frame_resize[0], self._frame_resize[1])
+            elif isinstance(frame_resize, float):
+                width = int(self._capture.get(cv2.CAP_PROP_FRAME_WIDTH)*frame_resize)
+                height = int(self._capture.get(cv2.CAP_PROP_FRAME_HEIGHT)*frame_resize)
+                self._frame_shape = (1, 3, width, height)
+                self._frame_resize = (width, height)
+            else:
+                assert False, "frame_resize should be a tuple of (x,y) pixels "
+                "or a float setting the scaling factor"
         else:
             self._frame_shape = (1, 3,
                 int(self._capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
diff --git a/example/ssd/demo.py b/example/ssd/demo.py
index 4ae8b350742..e8194ab8ead 100755
--- a/example/ssd/demo.py
+++ b/example/ssd/demo.py
@@ -109,6 +109,8 @@ def parse_args():
                         help='string of comma separated names, or text filename')
     parser.add_argument('--camera', action='store_true',
                         help="use camera for image capturing")
+    parser.add_argument('--frame-resize', type=str, default=None,
+                        help="resize camera frame to x,y pixels or a float scaling factor")
     args = parser.parse_args()
     return args
 
@@ -127,6 +129,15 @@ def parse_class_names(class_names):
         raise RuntimeError("No valid class_name provided...")
     return class_names
 
+def parse_frame_resize(x):
+    if not x:
+        return x
+    x = list(map(float, x.strip().split(',')))
+    assert len(x) >= 1 and len(x) <= 2, "frame_resize should be a float scaling factor or a tuple of w,h pixels"
+    if len(x) == 1:
+        x = x[0]
+    return x
+
 def parse_data_shape(data_shape_str):
     """Parse string to tuple or int"""
     ds = data_shape_str.strip().split(',')
@@ -160,7 +171,7 @@ def network_path(prefix, network, data_shape):
 def run_camera(args,ctx):
     assert args.batch_size == 1, "only batch size of 1 is supported"
     logging.info("Detection threshold is {}".format(args.thresh))
-    iter = CameraIterator()
+    iter = CameraIterator(frame_resize=parse_frame_resize(args.frame_resize))
     class_names = parse_class_names(args.class_names)
     mean_pixels = (args.mean_r, args.mean_g, args.mean_b)
     data_shape = int(args.data_shape)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services