Skip to content

Commit

Permalink
Fixed some issues, renamed files and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
themarpe committed Jun 4, 2020
1 parent a8cd8cb commit 77f94f8
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 63 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "depthai-api"]
path = depthai-api
url = https://github.com/luxonis/depthai-api.git
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
# DepthAI Python Module
# DepthAI Demo Program

This repo contains the pre-compiled DepthAI Python module (compiled as an architecture-specific `.so` file), utilities, and a submodule that allows compiling the DepthAI Python module for other platforms.
This repo contains demo application, which can load different networks, create pipelines, record video, etc.

__Documentation is available at [https://docs.luxonis.com](https://docs.luxonis.com).__

## Python modules
## Python modules (Dependencies)

DepthAI requites [numpy](https://numpy.org/) and [opencv-python](https://pypi.org/project/opencv-python/). To get the versions of these packages you need for DepthAI, use pip: `pip install -r requirements.txt`

Files with `.so` extension are the python modules:
- `depthai.cpython-36m-x86_64-linux-gnu.so` built for Ubuntu 18.04 & Python 3.6
- `depthai.cpython-37m-arm-linux-gnueabihf.so` built for Raspbian 10 & Python 3.7

For supporting other platforms, there is an option to build the python lib from sources by grabbing the [depthai-api](https://github.com/luxonis/depthai-api) submodule:

git submodule update --init
./depthai-api/install_dependencies.sh # Only required in first build on a given system
./depthai-api/build_py_module.sh

When updating DepthAI on these platforms it is often necessary to run `./depthai-api/build_py_module.sh --clean` in order to build a new version of the depthai-api module for your chosen platform.
DepthAI Demo requires [numpy](https://numpy.org/), [opencv-python](https://pypi.org/project/opencv-python/) and [depthai](https://github.com/luxonis/depthai-api).
To get the versions of these packages you need for the program, use pip: (Make sure pip is upgraded: `pip install -U pip`)
```
pip install -r requirements.txt
```

## Examples

Expand Down
10 changes: 7 additions & 3 deletions calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,14 @@ def get_pipeline(self):
# The following doesn't work (have to manually hit switch on device)
# depthai.reboot_device
# time.sleep(1)
if not depthai.init_device(cmd_file=self.cmd_file):
raise RuntimeError("Unable to initialize device. Try to reset it")

pipeline = depthai.create_pipeline(self.config)
pipeline = None

try:
device = depthai.Device("", False)
pipeline = device.create_pipeline(self.config)
except RuntimeError:
raise RuntimeError("Unable to initialize device. Try to reset it")

if pipeline is None:
raise RuntimeError("Unable to create pipeline")
Expand Down
2 changes: 1 addition & 1 deletion calibrate_and_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def cleanup():
args+="'"+arg+"' "

calibrate_cmd = "python3 calibrate.py " + args
test_cmd = """python3 depthai.py -co '{"streams": [{"name": "depth_sipp", "max_fps": 12.0}]}'"""
test_cmd = """python3 depthai-demo.py -co '{"streams": [{"name": "depth_sipp", "max_fps": 12.0}]}'"""


atexit.register(cleanup)
Expand Down
1 change: 0 additions & 1 deletion depthai-api
Submodule depthai-api deleted from bcce69
105 changes: 73 additions & 32 deletions depthai-demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np

import depthai
print('Using depthai module from: ', depthai.__file__)

import consts.resource_paths
from depthai_helpers import utils
Expand Down Expand Up @@ -230,15 +231,27 @@ def show_landmarks_recognition(entries_prev, frame):

print("Using Arguments=",args)

usb2_mode = False
if args['force_usb2']:
cli_print("FORCE USB2 MODE", PrintColors.WARNING)
cmd_file = consts.resource_paths.device_usb2_cmd_fpath
usb2_mode = True
else:
cmd_file = consts.resource_paths.device_cmd_fpath
usb2_mode = False

if args['dev_debug']:
debug_mode = False
print('Value of -debug: ', args['dev_debug'])

if args['dev_debug'] == None:
# Debug -debug flag NOT present,
debug_mode = False
elif args['dev_debug'] == '':
# If just -debug flag is present -> cmd_file = '' (wait for device to be connected beforehand)
debug_mode = True
cmd_file = ''
print('depthai will not load cmd file into device.')
else:
debug_mode = False
cmd_file = args['dev_debug']


calc_dist_to_bb = True
if args['disable_depth']:
Expand Down Expand Up @@ -302,7 +315,11 @@ def show_landmarks_recognition(entries_prev, frame):
"Disconnect/connect usb cable on host! \n", PrintColors.RED)
os._exit(1)

device = depthai.Device("", False)
device = None
if debug_mode:
device = depthai.Device("", args['device_id'])
else:
device = depthai.Device(args['device_id'], usb2_mode)

#if not depthai.init_device(cmd_file, args['device_id']):
# print("Error initializing device. Try to reset it.")
Expand All @@ -311,10 +328,27 @@ def show_landmarks_recognition(entries_prev, frame):

print('Available streams: ' + str(device.get_available_streams()))


# Append video stream if video recording was requested and stream is not already specified
video_file = None
if args['video'] is not None:
stream_names = [stream if isinstance(stream, str) else stream['name'] for stream in stream_list]
# open video file
try:
video_file = open(args['video'], 'wb')
if 'video' not in stream_names:
stream_list.append({"name": "video"})
except IOError:
print("Error: couldn't open video file for writing. Disabled video output stream")
if 'video' in stream_names:
stream_list.remove({"name": "video"})



# Do not modify the default values in the config Dict below directly. Instead, use the `-co` argument when running this script.
config = {
# Possible streams:
# ['left', 'right','previewout', 'metaout', 'depth_sipp', 'disparity', 'depth_color_h']
# ['left', 'right', 'jpegout', 'video', 'previewout', 'metaout', 'depth_sipp', 'disparity', 'depth_color_h']
# If "left" is used, it must be in the first position.
# To test depth use:
# 'streams': [{'name': 'depth_sipp', "max_fps": 12.0}, {'name': 'previewout', "max_fps": 12.0}, ],
Expand Down Expand Up @@ -377,34 +411,22 @@ def show_landmarks_recognition(entries_prev, frame):
exit(2)
# del config["streams"][config['streams'].index('depth_sipp')]

# Append video stream if video recording was requested and stream is not already specified
video_file = None
if args['video'] is not None:

# open video file
try:
video_file = open(args['video'], 'wb')
if config['streams'].count('video') == 0:
config['streams'].append('video')
except IOError:
print("Error: couldn't open video file for writing. Disabled video output stream")
if config['streams'].count('video') == 1:
config['streams'].remove('video')


stream_names = [stream if isinstance(stream, str) else stream['name'] for stream in config['streams']]

# Create a list of enabled streams ()
stream_names = [stream if isinstance(stream, str) else stream['name'] for stream in stream_list]

enable_object_tracker = 'object_tracker' in stream_names

# create the pipeline, here is the first connection with the device

p = device.create_pipeline(config=config)

if p is None:
print('Pipeline is not created.')
exit(3)

nn2depth = depthai.get_nn_to_depth_bbox_mapping()
nn2depth = device.get_nn_to_depth_bbox_mapping()



t_start = time()
Expand Down Expand Up @@ -434,12 +456,20 @@ def reset_process_wd():

reset_process_wd()


ops = 0
prevTime = time()
while True:
# retreive data from the device
# data is stored in packets, there are nnet (Neural NETwork) packets which have additional functions for NNet result interpretation
nnet_packets, data_packets = p.get_available_nnet_and_data_packets()
nnet_packets, data_packets = p.get_available_nnet_and_data_packets(True)

### Uncomment to print ops
# ops = ops + 1
# if time() - prevTime > 1.0:
# print('OPS: ', ops)
# ops = 0
# prevTime = time()

packets_len = len(nnet_packets) + len(data_packets)
if packets_len != 0:
reset_process_wd()
Expand All @@ -450,7 +480,11 @@ def reset_process_wd():
os._exit(10)

for _, nnet_packet in enumerate(nnet_packets):
entries_prev[nnet_packet.getMetadata().getCameraName()] = decode_nn(nnet_packet)
meta = nnet_packet.getMetadata()
camera = 'rgb'
if meta != None:
camera = meta.getCameraName()
entries_prev[camera] = decode_nn(nnet_packet)

for packet in data_packets:
window_name = packet.stream_name
Expand All @@ -461,7 +495,11 @@ def reset_process_wd():
print('Invalid packet data!')
continue
elif packet.stream_name == 'previewout':
camera = packet.getMetadata().getCameraName()
meta = packet.getMetadata()
camera = 'rgb'
if meta != None:
camera = meta.getCameraName()

window_name = 'previewout-' + camera
# the format of previewout image is CHW (Chanel, Height, Width), but OpenCV needs HWC, so we
# change shape (3, 300, 300) -> (300, 300, 3)
Expand Down Expand Up @@ -546,19 +584,22 @@ def reset_process_wd():

key = cv2.waitKey(1)
if key == ord('c'):
depthai.request_jpeg()
if 'jpegout' in stream_names == 0:
print("'jpegout' stream not enabled. Try settings -s jpegout to enable it")
else:
device.request_jpeg()
elif key == ord('f'):
depthai.request_af_trigger()
device.request_af_trigger()
elif key == ord('1'):
depthai.request_af_mode(depthai.AutofocusMode.AF_MODE_AUTO)
device.request_af_mode(depthai.AutofocusMode.AF_MODE_AUTO)
elif key == ord('2'):
depthai.request_af_mode(depthai.AutofocusMode.AF_MODE_CONTINUOUS_VIDEO)
device.request_af_mode(depthai.AutofocusMode.AF_MODE_CONTINUOUS_VIDEO)
elif key == ord('q'):
break


del p # in order to stop the pipeline object should be deleted, otherwise device will continue working. This is required if you are going to add code after the main loop, otherwise you can ommit it.
depthai.deinit_device()
device.deinit_device()

# Close video output file if was opened
if video_file is not None:
Expand Down
6 changes: 3 additions & 3 deletions depthai_helpers/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def parse_args():
parser.add_argument("-dev", "--device-id", default="", type=str,
help="USB port number for the device to connect to. Use the word 'list' to show all devices "
"and exit.")
parser.add_argument("-debug", "--dev_debug", default=None, action="store_true",
help="Used by board developers for debugging.")
parser.add_argument("-debug", "--dev_debug", nargs="?", default=None, const='', type=str, required=False,
help="Used by board developers for debugging. Can take parameter to device binary")
parser.add_argument("-fusb2", "--force_usb2", default=None, action="store_true",
help="Force usb2 connection")
parser.add_argument("-cnn", "--cnn_model", default="mobilenet-ssd", type=str,
Expand Down Expand Up @@ -115,7 +115,7 @@ def stream_type(option):
cli_print(msg_string, PrintColors.WARNING)
raise ValueError(msg_string)

stream_choices = ["metaout", "previewout", "left", "right", "depth_sipp", "disparity", "depth_color_h", "meta_d2h", "object_tracker"]
stream_choices = ["metaout", "video", "jpegout", "previewout", "left", "right", "depth_sipp", "disparity", "depth_color_h", "meta_d2h", "object_tracker"]
stream_name = option_list[0]
if stream_name not in stream_choices:
msg_string = "{0} is not in available stream list: \n{1}".format(stream_name, stream_choices)
Expand Down
2 changes: 1 addition & 1 deletion depthai_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def cleanup():
for arg in sys.argv[1:]:
args+="'"+arg+"' "

cmd = "python3 depthai.py " + args
cmd = "python3 depthai-demo.py " + args
print(cmd)

atexit.register(cleanup)
Expand Down
2 changes: 1 addition & 1 deletion integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def cleanup():
config_builder = config_builder+separator+comb+'"'
config_builder = config_builder + """]}'"""
if subset:
cmd = "python3 depthai.py " + config_builder
cmd = "python3 depthai-demo.py " + config_builder
if(config_builder == """-co '{"streams": ["metaout"]}'"""):
continue
logger.info(cmd)
Expand Down
2 changes: 1 addition & 1 deletion repeat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def cleanup():
for arg in sys.argv[1:]:
args+="'"+arg+"' "

cmd = "python3 depthai.py " + args
cmd = "python3 depthai-demo.py " + args

logger.info(cmd)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy==1.18.3
opencv-python==4.2.0.34
opencv-python==4.2.0.34
git+https://github.com/luxonis/depthai-api.git@e83f12bbc39ab53501d44f873437d20bc1387986

0 comments on commit 77f94f8

Please sign in to comment.