This is the 22nd day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows you how to implement gesture recognition using OpenCV in Python3.

The experimental steps

  • Step 1: Turn on the camera and detect each frame.
  • Step 2: Skin color detection, based onHSVColor spaceH,S,VRange screening. inHSVIn the7<H<20.28<S<256.50<V<256;
  • The third step: Gaussian filtering;
  • Step 4: edge contour detection;
  • Step 5: figure out the bump and bump of the gesture;
  • Step 6: Judge the current gesture by the number of bump points. For example: 0 bump is fist, 4 bump is cloth.

The implementation code

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)

while(Cap.isopened ()): ret,frame = cap.read() # Frame = cv2.flip(frame,1)
    kernel = np.ones((2.2),np.uint8)
    roi = frame[100:300.100:300Rectangle (frame,() = rectangle(frame,();100.100), (300.300), (0.0.255),0HSV = cv2.cvtcolor (ROI, cv2.color_bgr2HSV) lower_skin = np.array([0.28.70],dtype=np.uint8)
    upper_skin = np.array([20.255.255Mask = cv2.inRange(HSV,lower_skin,upper_skin) mask = cv2.dilate(mask,kernel,iterations=4)
    mask = cv2.GaussianBlur(mask,(5.5),100Contours,h = cv2.findContours(mask, cv2.retr_tree, cv2.chain_approx_simple) CNT = Max (contours,key=lambda x:cv2.contourArea(x)) epsilon =0.0005*cv2.arcLength(cnt,True)
    approx = cv2.approxPolyDP(cnt,epsilon,True)
    hull = cv2.convexHull(cnt)
    areahull = cv2.contourArea(hull)
    areacnt = cv2.contourArea(cnt)
    arearatio = ((areahull-areacnt)/areacnt)*100ConvexityDefects (approx,returnPoints=False) defects = cv2. ConvexityDefects (approx,hull0
    l=0

    for i in range(defects.shape[0]):
        s,e,f,d, = defects[i,0]
        start = tuple(approx[s][0])
        end = tuple(approx[e][0])
        far = tuple(approx[f][0])
        pt = (100.100)

        a = math.sqrt((end[0]-start[0]) * *2+(end[1]-start[1]) * *2)
        b = math.sqrt((far[0] - start[0]) * *2 + (far[1] - start[1]) * *2)
        c = math.sqrt((end[0]-far[0]) * *2+(end[1]-far[1]) * *2)
        s = (a+b+c)/2Ar = math.h SQRT (s * (s - a) * (s - b) * (s - c)) # Angle calculating the Angle between the fingers = math.h a cosine ((b * *2 + c**2 -a**2)/(2*b*c))*57

        if angle<=90 and d>20:
            l+=1
            cv2.circle(roi,far,3[255.0.0],-1)
        cv2.line(roi,start,end,[0.255.0].2Draw the envelope l+=1Font = cv2.FONT_HERSHEY_SIMPLEX #if l==1:
        if areacnt<2000:
            cv2.putText(frame,"put hand in the window", (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
        else:
            if arearatio<12:
                cv2.putText(frame,'0', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
            elif arearatio<17.5:
                cv2.putText(frame,"1", (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
            else:
                cv2.putText(frame,'1', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
    elif l==2:
        cv2.putText(frame,'2', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
    elif l==3:
        if arearatio<27:
            cv2.putText(frame,'3', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
        else:
            cv2.putText(frame,'3', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
    elif l==4:
        cv2.putText(frame,'4', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
    elif l==5:
        cv2.putText(frame,'5', (0.50),font,2, (0.0.255),3,cv2.LINE_AA)
    cv2.imshow('frame',frame)
    cv2.imshow('mask', mask)

    key = cv2.waitKey(25) &0xFF
    if key == ord('L'): # keyboard L key exitbreak

cv2.destroyAllWindows()
cap.release()
Copy the code

The output is:

A series of articles will follow this month,

Wonderful article, please pay attention.