In this video lesson we learn how to use the NVIDIA Jetson Inference tools for detect objects in a live video. The software developed in this lesson is included below for your convenience.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import jetson.inference import jetson.utils import time import cv2 import numpy as np timeStamp=time.time() fpsFilt=0 net=jetson.inference.detectNet('ssd-mobilenet-v2',threshold=.5) dispW=1280 dispH=720 flip=2 font=cv2.FONT_HERSHEY_SIMPLEX # Gstreamer code for improvded Raspberry Pi Camera Quality #camSet='nvarguscamerasrc wbmode=3 tnr-mode=2 tnr-strength=1 ee-mode=2 ee-strength=1 ! video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method='+str(flip)+' ! video/x-raw, width='+str(dispW)+', height='+str(dispH)+', format=BGRx ! videoconvert ! video/x-raw, format=BGR ! videobalance contrast=1.5 brightness=-.2 saturation=1.2 ! appsink' #cam=cv2.VideoCapture(camSet) #cam=jetson.utils.gstCamera(dispW,dispH,'0') cam=cv2.VideoCapture('/dev/video1') cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH) #cam=jetson.utils.gstCamera(dispW,dispH,'/dev/video1') #display=jetson.utils.glDisplay() #while display.IsOpen(): while True: #img, width, height= cam.CaptureRGBA() _,img = cam.read() height=img.shape[0] width=img.shape[1] frame=cv2.cvtColor(img,cv2.COLOR_BGR2RGBA).astype(np.float32) frame=jetson.utils.cudaFromNumpy(frame) detections=net.Detect(frame, width, height) for detect in detections: #print(detect) ID=detect.ClassID top=detect.Top left=detect.Left bottom=detect.Bottom right=detect.Right item=net.GetClassDesc(ID) print(item,top,left,bottom,right) #display.RenderOnce(img,width,height) dt=time.time()-timeStamp timeStamp=time.time() fps=1/dt fpsFilt=.9*fpsFilt + .1*fps #print(str(round(fps,1))+' fps') cv2.putText(img,str(round(fpsFilt,1))+' fps',(0,30),font,1,(0,0,255),2) cv2.imshow('detCam',img) cv2.moveWindow('detCam',0,0) if cv2.waitKey(1)==ord('q'): break cam.release() cv2.destroyAllWindows() |