Skip to content

Instantly share code, notes, and snippets.

@lanzani
Created January 6, 2021 19:27
Show Gist options
  • Save lanzani/dc0dcb3c82c00f718766bca346a03720 to your computer and use it in GitHub Desktop.
Save lanzani/dc0dcb3c82c00f718766bca346a03720 to your computer and use it in GitHub Desktop.
Simple implementation for testing opencv with gstreamer for nvidia jetson.
import jetson.inference as inference
import jetson.utils as utils
import cv2
import time
# Load the net with jetson inference
net1 = inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
# Define video sources
video_source1 = "/dev/video0"
video_source2 = "/dev/video1"
# Define gs camera pipeline
camSet1 = f'v4l2src device={video_source1} ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! appsink'
camSet2 = f'v4l2src device={video_source2} ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! appsink'
# Open camera with opencv + gs
camera1 = cv2.VideoCapture(camSet1)
camera2 = cv2.VideoCapture(camSet2)
while True:
# CAMERA ONE
# start fps counter
start_time1 = time.time()
# Read camera
_, img1 = camera1.read()
# Convert opencv image to cuda image
img1_cuda = utils.cudaFromNumpy(img1)
# Perform detection
detections1 = net1.Detect(img1_cuda)
# calculate fps
fps1 = round(1.0 / (time.time() - start_time1), 2)
cv2.putText(img1, f'fps: {fps1}', (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
# Display image
cv2.imshow('myCam1', img1)
# CAMERA TWO
# start fps counter
start_time2 = time.time()
# Read camera
_, img2 = camera2.read()
# Convert opencv image to cuda image
img2_cuda = utils.cudaFromNumpy(img2)
# Perform detection
detections2 = net1.Detect(img2_cuda)
# calculate fps
fps2 = round(1.0 / (time.time() - start_time2), 2)
cv2.putText(img2, f'fps: {fps2}', (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 1)
# Display image
cv2.imshow('myCam2', img2)
# Exit if pressed q on one window
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
camera1.release()
camera2.release()
cv2.destroyAllWindows()
@neilyoung
Copy link

Will check this out. Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment