Tai-Hsu Lin | 63e1a73 | 2012-03-03 19:20:26 +0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Michael Spang | 06269ad | 2013-01-04 14:34:51 -0500 | [diff] [blame] | 5 | import glob, os |
Tai-Hsu Lin | 63e1a73 | 2012-03-03 19:20:26 +0800 | [diff] [blame] | 6 | import numpy as np |
| 7 | |
| 8 | # Dimension padding/unpadding function for converting points matrices to |
| 9 | # the OpenCV format (channel-based). |
| 10 | def Pad(x): |
| 11 | return np.expand_dims(x, axis=0) |
| 12 | |
| 13 | |
| 14 | def Unpad(x): |
| 15 | return np.squeeze(x) |
| 16 | |
| 17 | |
| 18 | class Pod(object): |
| 19 | '''A POD (plain-old-data) object containing arbitrary fields.''' |
| 20 | def __init__(self, **args): |
| 21 | self.__dict__.update(args) |
| 22 | |
| 23 | def __repr__(self): |
| 24 | '''Returns a representation of the object, including its properties.''' |
| 25 | return (self.__class__.__name__ + '(' + |
| 26 | ', '.join('%s=%s' % (k, v) for k, v in sorted(self.__dict__.items()) |
| 27 | if not k.startswith('_')) + ')') |
Michael Spang | 06269ad | 2013-01-04 14:34:51 -0500 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def find_camera(): |
| 31 | """ |
| 32 | Find a V4L camera device. |
| 33 | |
| 34 | @return (device_name, device_index). If no camera is found, (None, None). |
| 35 | """ |
| 36 | cameras = [os.path.basename(camera) for camera in |
| 37 | glob.glob('/sys/bus/usb/drivers/uvcvideo/*/video4linux/video*')] |
| 38 | if not cameras: |
| 39 | return None, None |
| 40 | camera = cameras[0] |
| 41 | return camera, int(camera[5:]) |