Hi! I’m currently connecting a multicamera adapter v2.2 to a Jetson Nano. The camera is able to run the test code.
I’ve written a program that is streaming the camera contents attached to the adapter (currently has 3 cameras).
I’m using threading to run a loop that is switching between the cameras every 5 seconds. But as soon as the the code reaches the point where camera B is executed, the windows freezes or the system hangs.
The code is:
import cv2
import time
import threading
from flask import Response, Flask
import RPi.GPIO as gp
import os
global index
def loop():
index = 0
while True:
if index == 0:
i2c = “i2cset -y 1 0x70 0x00 0x04”
os.system(i2c)
gp.output(7, False)
gp.output(11, False)
gp.output(12, True)
print(‘camera A’)
time.sleep(5)
elif index == 1:
i2c = “i2cset -y 1 0x70 0x00 0x05”
os.system(i2c)
gp.output(7, True)
gp.output(11, False)
gp.output(12, True)
print(‘camera B’)
time.sleep(5)
elif index == 2:
i2c = “i2cset -y 1 0x70 0x00 0x06”
os.system(i2c)
gp.output(7, False)
gp.output(11, True)
gp.output(12, False)
print(‘camera C’)
time.sleep(5)
elif index == 3:
index = -1
index += 1
gp.setwarnings(False)
gp.setmode(gp.BOARD)
gp.setup(7,gp.OUT)
gp.setup(11,gp.OUT)
gp.setup(12,gp.OUT)
i2c = “i2cset -y 1 0x70 0x00 0x04”
gp.output(7, False)
gp.output(11, False)
gp.output(12, True)
def gstreamer_pipeline(
capture_width=1280,
capture_height=720,
display_width=1280,
display_height=720,
framerate=60,
flip_method=0,
):
return (
"nvarguscamerasrc sensor_id = 0 ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
“video/x-raw, format=(string)BGR ! appsink”
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def show_camera():
To flip the image, modify the flip_method parameter (0 and 2 are the most common)
print(gstreamer_pipeline(flip_method=0))
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow(“CSI Camera”, cv2.WINDOW_AUTOSIZE)
Window
while cv2.getWindowProperty(“CSI Camera”, 0) >= 0:
ret_val, img = cap.read()
cv2.imshow(“CSI Camera”, img)
if not ret_val:
break
This also acts as
keyCode = cv2.waitKey(30) & 0xFF
Stop the program on the ESC key
if keyCode == 27:
break
cap.release()
cv2.destroyAllWindows()
else:
print(“Unable to open camera”)
if name == “main”:
y = threading.Thread(target=show_camera)
y.start()
x = threading.Thread(target=loop)
x.daemon = True
x.start()
gp.output(7, False)
gp.output(11, False)
gp.output(12, True)
Can you please guide my on why this is happening?