detectNet + serial not working

hi guys!

I m having a problem trying to connect 2 pieces of code (python). I am trying to read some serial from arduino and get the position form a body in front of a webcam (detectNet) at the same time.

if I try to run the code as the example, the serial don t seems to update (it glitch between 0.015 and 0.023). if i comment all the '#GET X BODY POSITION" the serial works fine (go from 0.015 to 4). what i’m doing wrong?

Thanks in advance

import jetson.inference
import jetson.utils
import serial
ser = serial.Serial('/dev/ttyUSB0')


#increse threshold for precision
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold = 0.5)

camera = jetson.utils.gstCamera(1280,720, "/dev/video0")
display = jetson.utils.glDisplay()

while display.IsOpen():
	
#GET X BODY POSITION------ if this part of the code is commented the #GET SERIAL FROM ARDUINO works fine
    pos = 0
	img, width, height = camera.CaptureRGBA()
	detections = net.Detect(img, width,height) 
		
	display.RenderOnce(img,width,height)
	display.SetTitle("Object Detction | Network {:.0F} FPS".format(net.GetNetworkFPS()))
	for detection in detections:
		if(net.GetClassDesc(detection.ClassID)=="person"):
			#print(detection.Center)
			pos=detection.Center

	print(pos)
#END OF GET X BODY POSITION
#-----------------------------------------------------------------

    #GET SERIAL FROM ARDUINO
	line = ser.readline()
	print(line)

Hmm my guess is that ser.readline() is a blocking (synchronous) call waiting for I/O because a timeout wasn’t specified when the port was opened. From the pyserial docks:

https://pyserial.readthedocs.io/en/latest/shortintro.html#readline

Be careful when using readline(). Do specify a timeout when opening the serial port otherwise it could block forever if no newline character is received

I would either poll the device and check it for data first, or use a zero timeout (or very low timeout). Or run that in a different thread.

thanks. for the answer. i get really confused with threads. can you help me? how can i make the while True loop?

import jetson.inference
import jetson.utils
import serial
import time
import threading


ser = serial.Serial('/dev/ttyUSB0')


#increse threshold for precision
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold = 0.5)

camera = jetson.utils.gstCamera(1280,720, "/dev/video0")
display = jetson.utils.glDisplay()


#Thread1
def serial():
    line = ser.readline()
    print(line)
    time.sleep(.1)	
   
#Thread2

def cam():
    img, width, height = camera.CaptureRGBA()
    detections = net.Detect(img, width, height) 
    display.Render(img, width, height)


while True:
(how can i make it here?)
img, width, height = camera.CaptureRGBA()
detections = net.Detect(img, width, height) 
display.Render(img, width, height)

The camera stuff you would want still running in the main thread. Then you have your separate serial port thread that can use a queue to put data in. The main thread can check that queue at the end of the camera loop.

If you are unfamiliar with threads, there are many Python tutorials and videos out there on the topic. I would suggest going through some of those too.

i think i need to go check some tutorials. thanks for the tips,

I solved this using firmata in the arduino