Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I try this with my 7 ports hub. And it has blue led power supply.

#!/usr/bin/env python3.4
import cv2
import numpy as np

camera_feed = cv2.VideoCapture(0)

while True:
    _,frame = camera_feed.read()
    #Convert the current frame to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    #Define the threshold for finding a blue object with hsv
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    #Create a binary image, where anything blue appears white and everything else is black
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    #Get rid of background noise using erosion and fill in the holes using dilation and erode the final image on last time
    element = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
    mask = cv2.erode(mask,element, iterations=2)
    mask = cv2.dilate(mask,element,iterations=2)
    mask = cv2.erode(mask,element)

    #Create Contours for all blue objects
    _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    maximumArea = 0
    bestContour = None
    for contour in contours:
        currentArea = cv2.contourArea(contour)
        if currentArea > maximumArea:
            bestContour = contour
            maximumArea = currentArea
     #Create a bounding box around the biggest blue object
    if bestContour is not None:
        x,y,w,h = cv2.boundingRect(bestContour)
        cv2.rectangle(frame, (x,y),(x+w,y+h), (0,0,255), 3)
        cv2.circle(frame,(x, y), 2,(0, 255, 0), 20)

    #Show the original camera feed with a bounding box overlayed
    cv2.imshow('frame',frame)
    #Show the contours in a seperate window
    cv2.imshow('mask',mask)
    #Use this command to prevent freezes in the feed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()