Jetson Nano and OpenCV: Bouncing Color Box!

So this project was about building on the previous skills. Here I have a gray scale live video and a rectangular box bouncing about the screen. What’s different here is that the portion of the video screen in the box is coloured.

Demo:

Things Required:

  1. Jetson Nano
  2. Keyboard
  3. Mouse
  4. HDMI Screen
  5. Raspberry Pi Camera
  6. External Power Supply

Reference Material:

  1. You can check out this video for the project: (LINK)

Code:

Note that code below is in python

View on/Download from Github: (LINK)

import cv2
dispW= 640
dispH= 480
flip=2
camSet='nvarguscamerasrc !  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 ! appsink'
cam=cv2.VideoCapture(camSet)
dispW= int(cam.get(cv2.CAP_PROP_FRAME_WIDTH))
dispH= int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
BW= int(0.15*dispW)
BH= int(0.15*dispH)
posX=10
posY=270
dx=2
dy=2
while True:
    ret, frame= cam.read()
    roi= frame[posY:posY+BH, posX:posX+BW].copy()
    frame= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame= cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
    frame= cv2.rectangle(frame,(posX,posY),(posX+BW,posY+BH),(0,0,0),3)
    frame[posY:posY+BH, posX:posX+BW]=roi
    
    posX=posX+dx
    posY=posY+dy
    if(posX+BW>=640 or posX<=0):
        dx=dx*(-1)
    if(posY+BH>=480 or posY<=0):
        dy=dy*(-1)
    
    cv2.imshow('nanoCam', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

Stay Tuned for upcoming posts on Jetson Nano and AI Projects! Want to be the first to know when a new and amazing post comes up?? Then feel free to subscribe!

Happy Learning!!

This image has an empty alt attribute; its file name is stay-tuned.gif

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: