ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Program flow not correct with ROS

asked 2022-08-27 16:30:07 -0500

updated 2022-08-27 21:43:06 -0500

ravijoshi gravatar image

This is an AI object detection routine for the Oak-D camera. It worked perfectly before I tried to add ROS to the program so that the camera AI image could be adjusted according to the machine speed. The camera will be instructed to use one of three slices of the image according to the number in "distance_value".

This uses Ubuntu 20.04, Python 3.8, OpenCV, and ROS Noetic. It's to run on a Raspberry Pi but this version will run on a laptop. I don't think I'm calling my functions properly or at the right time. When I run it, it's stuck in a loop and doesn't show an image until I hit CTRL-C, then it must be ended with CTRL-Z. Can an expert please help?

#!/usr/bin/env python3
The code is edited from docs (
We add parsing from JSON files that contain configuration.
from pathlib import Path
import sys
import cv2
import depthai as dai
import numpy as np
import time
import argparse
import json
import blobconverter
import rospy
from std_msgs.msg import String
from time import sleep
# import RPi.GPIO as GPIO
# from gpiozero import Servo

z = ([0,0,0,0,0,0,0,0])
r = ([0,0,0,0,0,0,0,0])

# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model", help="Provide model name or model path for inference",
                    default='/home/steve/wide-weeds/openvino/result_openvino_2021.4_6shave.blob', type=str)
parser.add_argument("-c", "--config", help="Provide config path for inference",
                    default='/home/steve/wide-weeds/openvino/result.json', type=str)
args = parser.parse_args()

# parse config
configPath = Path(args.config)
if not configPath.exists():
    raise ValueError("Path {} does not exist!".format(configPath))

with as f:
    config = json.load(f)
nnConfig = config.get("nn_config", {})

# parse input shape
if "input_size" in nnConfig:
    W, H = tuple(map(int, nnConfig.get("input_size").split('x')))

# extract metadata
metadata = nnConfig.get("NN_specific_metadata", {})
classes = metadata.get("classes", {})
coordinates = metadata.get("coordinates", {})
anchors = metadata.get("anchors", {})
anchorMasks = metadata.get("anchor_masks", {})
iouThreshold = metadata.get("iou_threshold", {})
confidenceThreshold = metadata.get("confidence_threshold", {})


# parse labels
nnMappings = config.get("mappings", {})
labels = nnMappings.get("labels", {})

# get model path
nnPath = args.model
if not Path(nnPath).exists():
    print("No blob found at {}. Looking into DepthAI model zoo.".format(nnPath))
    nnPath = str(blobconverter.from_zoo(args.model, shaves = 6, zoo_type = "depthai", use_cache=True))
# sync outputs
syncNN = True

# Create pipeline
pipeline = dai.Pipeline()

# Define sources and outputs
camRgb = pipeline.create(dai.node.ColorCamera)
detectionNetwork = pipeline.create(dai.node.YoloDetectionNetwork)

# Properties
camRgb.setPreviewSize(1280, 672)
# camRgb.setVideoSize(1920, 320)
# Faster video not needed, I think, at this point.

manip = pipeline.create(dai.node.ImageManip)
configIn = pipeline.create(dai.node.XLinkIn)
xout = pipeline.create(dai.node.XLinkOut)


# choose from one of three slices ...
edit retag flag offensive close merge delete


I hit CTRL-C, then it must be ended with CTRL-Z.

Sorry, the code is too long to see. Based on the CTRL+C issue, I suggest you try Matplotlib. Especially the cv2.imshow( ... ) function causes problems. It can be replaced by plt.imshow( ... ). Please see here for a tutorial on using displaying images using Matplotlib.

ravijoshi gravatar image ravijoshi  ( 2022-08-27 22:16:53 -0500 )edit

What I doubt is how I included the relevant ROS commands, and where. The first half of the program is just setting up things and the image pipeline. But the final half, as indentation starts, are complicated.

Rodolfo8 gravatar image Rodolfo8  ( 2022-08-28 08:39:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2022-08-28 16:50:35 -0500

BlakeAnderson gravatar image

In the function listener, you make a call to rospy.spin(). This function blocks until the node is terminated, so listener is blocked. CTRL-C caused spin() to return, but you then also get stuck inside the while True loop. The overall control flow of this program needs rework, and I recommend doing a refresher run through the ROS tutorial series to review concepts such as spinning.

Also, CTRL-Z suspends the process rather than terminating it.

edit flag offensive delete link more


Yes, I took out the spin line and moved ROS routines up the script, one indentation less. Now it works well. Thanks

Rodolfo8 gravatar image Rodolfo8  ( 2022-09-06 20:26:29 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2022-08-27 16:30:07 -0500

Seen: 57 times

Last updated: Aug 28 '22