python+opencv简单的手势识别

python+opencv简单的手势识别,第1张

python+opencv简单的手势识别

源代码

import sys
import numpy as np
import cv2
import math
font=cv2.FONT_HERSHEY_SIMPLEX

def cnt_area(cnt):
  area = cv2.contourArea(cnt)
  return area

def Gesture_Recognize(img):
    pointNum = 1
    hsv_img=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    lower_hsv_1 = np.array([0,50,50])#颜色范围低阈值
    upper_hsv_1 = np.array([20,255,255])#颜色范围高阈值
    lower_hsv_2 = np.array([150,50,50])#颜色范围低阈值
    upper_hsv_2 = np.array([180,255,255])#颜色范围高阈值
    mask1 = cv2.inRange(hsv_img,lower_hsv_1,upper_hsv_1)
    mask2 = cv2.inRange(hsv_img,lower_hsv_2,upper_hsv_2)
    mask = mask1 + mask2
    mask = cv2.medianBlur(mask,5)
    k1=np.ones((5,5), np.uint8)
    mask = cv2.dilate(mask, k1, iterations=1)
    mask = cv2.erode(mask, k1, iterations=1)
    mask_color = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
    cv2.imshow("mask", mask)
    #cv2.imwrite("mask.png", mask)
    black_img = np.zeros(mask.shape,np.uint8)

    contours,hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    if len(contours) < 1:
        return 0, img
      
    contours.sort(key = cnt_area, reverse=True)  
    (x0, y0, w0, h0) = cv2.boundingRect(contours[0])
    if(w0>=100 and h0>=100):
        cv2.rectangle(img,(x0,y0),(x0+w0,y0+h0),(255,0,255),2)
        epsilon = 0.01 * cv2.arcLength(contours[0], True)
        approx = cv2.approxPolyDP(contours[0],epsilon,True)
        cv2.drawContours(black_img,[approx],-1,(255,0,255),2)

        contours2,hierarchy2 = cv2.findContours(black_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        if len(contours) < 1:
          return 0, img
        hull = cv2.convexHull(contours2[0],returnPoints=False)
        defects = cv2.convexityDefects(contours2[0],hull)

        if defects is None:
          #print ('have no convex defects')
          pass
        else: 
          #print ('have convex defects')
          for i in range(0,defects.shape[0]):
            s,e,f,d = defects[i,0]
            pre_start = (0,0)
            pre_end = (0,0)
            start = tuple(contours2[0][s][0])
            end = tuple(contours2[0][e][0])
            far = tuple(contours2[0][f][0])
            #print(d)
            if d >= 13000:
              cv2.line(img,start,end,[0,255,0],3)#凸包
              cv2.circle(img,start,10,[0,255,255],3)
              cv2.circle(img,end,10,[0,255,255],3)
              cv2.circle(img,far,10,[0,0,255],3)#凸包缺陷点
              pre_start = start
              pre_end = end
              pointNum += 1
        
    cv2.putText(img,'hand-%d'%pointNum,(10,35),font,1.2,(0,255,255),3)       
    return pointNum, img

if __name__ == '__main__':
    cap = cv2.VideoCapture()
    flag = cap.open(1)
    if flag:
        while True:
            ret, frame = cap.read()
            if frame is None:
                break
            cv2.imshow("frame",frame)
            num, img = Gesture_Recognize(frame)
            cv2.imshow("Gesture_Recognize",img)
            char = cv2.waitKey(10)
            if char == 27:
                break
        cv2.destroyAllWindows()
        cap.release()
    

效果:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5701400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存