Jetson Nano and OpenCV: Colour tracker using Hue, Saturation and Value

So in this project we’re another step closer to the cool projects! Here I’ve built a color distinguishing using Hue, Saturation and Value.

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:

View on/Download from Github: (LINK)

import cv2
import numpy as np

def nothing(x):
    pass

cv2.namedWindow('Trackbars')
cv2.moveWindow('Trackbars', 400, 0)

cv2.createTrackbar('hueLower', 'Trackbars', 50, 179, nothing)
cv2.createTrackbar('hueHigher', 'Trackbars', 100, 179, nothing)
cv2.createTrackbar('satLower', 'Trackbars', 100, 255, nothing)
cv2.createTrackbar('satHigher', 'Trackbars', 255, 255, nothing)
cv2.createTrackbar('valLower', 'Trackbars', 100, 255, nothing)
cv2.createTrackbar('valHigher', 'Trackbars', 255, 255, nothing)

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)
while True:
    #ret, frame= cam.read()
    smarties= cv2.imread('smarties.png')
    smarties= cv2.resize(smarties, (320, 240))
    cv2.imshow('smarties', smarties)
    cv2.moveWindow('smarties', 0,0)
    
    hsv= cv2.cvtColor(smarties, cv2.COLOR_BGR2HSV)
    
    hueLow= cv2.getTrackbarPos('hueLower', 'Trackbars')
    hueHigh= cv2.getTrackbarPos('hueHigher', 'Trackbars')
    
    satLow= cv2.getTrackbarPos('satLower', 'Trackbars')
    satHigh= cv2.getTrackbarPos('satHigher', 'Trackbars')
    
    valLow= cv2.getTrackbarPos('valLower', 'Trackbars')
    valHigh= cv2.getTrackbarPos('valHigher', 'Trackbars')

    l_b= np.array([hueLow, satLow, valLow])
    u_b= np.array([hueHigh, satHigh, valHigh])

    FGMask= cv2.inRange(hsv, l_b, u_b)
    cv2.imshow('FGMask', FGMask)
    cv2.moveWindow('FGMask', 0, 350)
    
    FG= cv2.bitwise_and(smarties, smarties, mask= FGMask)
    cv2.imshow('FG', FG)

    if cv2.waitKey(1)  == ord('q'):
        break
cam.release()
cv2.destroyAllWindow()

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: