Reputation: 143
I am doing gesture recognition using color detection using opencv and python. The problem I am having is that contours are detected for only one color for example red, but the other two colors are not detected example green or blue, so my some gestures are not working.
In my code only red colour contours are detected and not blue
Code:
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
import time
import pyautogui
mouse = Controller()
app = wx.App(False)
(sx, sy) = wx.GetDisplaySize()(camx, camy) = (320, 240)
##green
lowerBoundG = np.array ([65, 66, 77])
upperBoundG = np.array ([96, 255, 255])
##red
lowerBoundR = np.array ([134, 94, 98])
upperBoundR = np.array ([179, 255, 255])
##blue
lowerBoundB = np.array ([88, 78, 20])
upperBoundB = np.array ([128, 255, 255])
cam = cv2.VideoCapture (0)
cam.set (3, camx)
cam.set (4, camy)
kernelOpen = np.ones ((5, 5))
kernelClose = np.ones ((20, 20))
mLocOld = np.array ([0, 0])
mouseLoc = np.array ([0, 0]) DampingFactor = 3 while True
:
ret, uf = cam.read()
img = cv2.flip (uf, 1)
#convert BGR to HSV
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#create the Mask
maskG = cv2.inRange (imgHSV, lowerBoundG, upperBoundG)
maskR = cv2.inRange (imgHSV, lowerBoundR, upperBoundR)
maskB = cv2.inRange (imgHSV, lowerBoundR, upperBoundB)
#morphology
maskOpenG = cv2.morphologyEx(maskG, cv2.MORPH_OPEN, kernelOpen)
maskCloseG = cv2.morphologyEx(maskOpenG, cv2.MORPH_CLOSE, kernelClose)
maskOpenR = cv2.morphologyEx(maskR, cv2.MORPH_OPEN, kernelOpen)
maskCloseR = cv2.morphologyEx(maskOpenR, cv2.MORPH_CLOSE, kernelClose)
maskOpenB = cv2.morphologyEx(maskB, cv2.MORPH_OPEN, kernelOpen)
maskCloseB = cv2.morphologyEx(maskOpenB, cv2.MORPH_CLOSE, kernelClose)
maskFinalG = maskCloseG
maskFinalR = maskCloseR
maskFinalB = maskCloseB
contsG, h = cv2.findContours(maskFinalG.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
contsR, v = cv2.findContours (maskFinalR.copy (), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
contsB, p = cv2.findContours (maskFinalB.copy (), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
##for COntrolling mouse cursor
if (len (contsR) == 1):
x, y, w, h = cv2.boundingRect(contsR[0])
cv2.rectangle (img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cx = int (x + w / 2)
cy = int (y + h / 2)
div = int (w + h / 4)
cv2.circle (img, (cx, cy), div, (0, 0, 255), 2)
mouseLoc = mLocOld + ((cx, cy) - mLocOld) / DampingFactor
mouse.position = (mouseLoc[0] * sx / camx, mouseLoc[1] * sy / camy)
mLocOld = mouseLoc
##For Mouse right click
elif (len (contsR) == 1 and len (contsB) == 1):
mouse.click (Button.right, 1) time.sleep (1) cv2.imshow ("cam", img)
##cv2.imshow("mask",maskClose)
cv2.waitKey (5)
Upvotes: 0
Views: 109
Reputation: 4561
You code says:
##for COntrolling mouse cursor
if (len (contsR) == 1):
# [.....]
##For Mouse right click
elif (len (contsR) == 1 and len (contsB) == 1):
If red has any value, the elif
will never be reached, because the if
is executed.
Instead, first try the case with more conditions:
##For Mouse right click
if (len (contsR) == 1 and len (contsB) == 1):
# [.....]
##for COntrolling mouse cursor
elif (len (contsR) == 1):
So, if there is red and blue, do mouseclick. If there is no blue, but there is red, move cursor.
Upvotes: 3