How to run the detectnet.py program with a pushbutton trigger?

Hello,

I’m trying to write a program where on pressing the pushbutton once the detectnet.py program starts running and pressing the pushbutton once more the program should stop working.

I have attached the program below:

import sys
import argparse
import RPi.GPIO as GPIO
import jetson_inference
import jetson_utils

from jetson_inference import detectNet
from jetson_utils import videoSource, videoOutput, logUsage

GPIO.setmode(GPIO.BOARD)
inputPin=15

buttonStateOld=True

GPIO.setup(inputPin,GPIO.IN)

# parse the command line
parser = argparse.ArgumentParser(description="Locate objects in a live camera stream using an object detection DNN.", 
                                 formatter_class=argparse.RawTextHelpFormatter, 
                                 epilog=detectNet.Usage() + videoSource.Usage() + videoOutput.Usage() + logUsage())

parser.add_argument("input_URI", type=str, default="", nargs='?', help="URI of the input stream")
parser.add_argument("output_URI", type=str, default="", nargs='?', help="URI of the output stream")
parser.add_argument("--network", type=str, default="ssd-mobilenet-v2", help="pre-trained model to load (see below for options)")
parser.add_argument("--overlay", type=str, default="box,labels,conf", help="detection overlay flags (e.g. --overlay=box,labels,conf)\nvalid combinations are:  'box', 'labels', 'conf', 'none'")
parser.add_argument("--threshold", type=float, default=0.5, help="minimum detection threshold to use") 

is_headless = ["--headless"] if sys.argv[0].find('console.py') != -1 else [""]

try:
	args = parser.parse_known_args()[0]
except:
	print("")
	parser.print_help()
	sys.exit(0)

# create video sources and outputs
input = videoSource(args.input_URI, argv=sys.argv)
output = videoOutput(args.output_URI, argv=sys.argv+is_headless)
	
net = detectNet(model="/home/jetson/jetson-inference/python/training/detection/ssd/models/One_Label/ssd-mobilenet.onnx", labels="/home/jetson/jetson-inference/python/training/detection/ssd/models/One_Label/labels.txt", input_blob="input_0", output_cvg="scores", output_bbox="boxes", threshold=0.5)

# process frames until the user exits
while (1==1):
	buttonStateNew=GPIO.input(inputPin)
	if buttonStateOld==True and buttonStateNew==False:
		
		img = input.Capture()

		# detect objects in the image (with overlay)
		detections = net.Detect(img, overlay=args.overlay)

		# print the detections
		print("detected {:d} objects in image".format(len(detections)))

		for detection in detections:
			print(detection)

		# render the image
		output.Render(img)

		# update the title bar
		output.SetStatus("{:s} | Network {:.0f} FPS".format(args.network, net.GetNetworkFPS()))

		# print out performance info
		net.PrintProfilerTimes()
		
	
	# exit on input/output EOS
	if not input.IsStreaming() or not output.IsStreaming():
		break

	x=GPIO.input(inputPin)
	print(x)
	buttonStateOld=buttonStateNew

My Output:

The program stops working by showing the following statement:
[TRT] didn’t load expected number of class colors (0 of 2)
[TRT] filling in remaining 2 class colors with default colors

Is there anyway this program can be modified so that on pressing the pushbutton once the detectnet.py program starts running and pressing the pushbutton once more the program should stop working?

I uploaded the program file so that its easier to see the code.
Test_PushButton.py (3.5 KB)

Hi @swarnika.sarangi, can you try removing this code from your script:

I think it may be causing your program to exit the main loop prematurely.

Thank you for your response,
It did start working. So once when the program runs, it shows 1 on the screen and on pressing the pushbutton the detectnect program starts but gets stuck and does not show the display.

On running the program again I’m getting this output:

Can this be rectified?

Do you have a working MIPI CSI camera attached? If so, try running this: sudo systemctl restart nvargus-daemon

Yes, I do and ya this command worked and its not showing the error again.
But now again running the program and pressing the pushbutton the camera window opens and gets stuck.

The button logic that you have means that the camera processing will only run for one frame when the button state has changed from pressed to unpressed. Try something like this instead:

buttonStateOld=False
runCamera=False

while True:
	buttonStateNew=GPIO.input(inputPin)

	if buttonStateOld and not buttonStateNew:
		runCamera=not runCamera

	buttonStateOld=buttonStateNew

	if runCamera:
		img = input.Capture()

		# detect objects in the image (with overlay)
		detections = net.Detect(img, overlay=args.overlay)

		# print the detections
		print("detected {:d} objects in image".format(len(detections)))

		for detection in detections:
			print(detection)

		# render the image
		output.Render(img)

		# update the title bar
		output.SetStatus("{:s} | Network {:.0f} FPS".format(args.network, net.GetNetworkFPS()))

		# print out performance info
		net.PrintProfilerTimes()

Thank you for your clarification regarding the logic. I understood my mistake.
I tried the code that you have provided, I’m getting the following output:

Its showing:
[TRT] didn’t load expected number of class colors (0 of 2)
[TRT] filling in remaining 2 class colors with default colors

Those warning are fine, you can ignore that.

It doesn’t appear that the camera processing is being run when the button is triggered. You’ll need to add print statements and debug your button logic independently of the camera loop.

Thank you for your help and suggestion. I looked through the logic of the code and made just one minor change and it worked.

Change:
buttonStateOld=True
Instead of buttonStateOld=False before the while loop.

OK great!, glad you got it working @swarnika.sarangi.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.