• py手部追踪


    环境

    #opencv-python
    #cvzone
    #mediapipe
    #msvc-runtime  #找不到指定模块,安装它
    

    代码

    #检测食指与中指中点的轨迹(两手指合并)
    import cv2
    import cvzone
    import numpy as np
    from cvzone.HandTrackingModule import HandDetector
    import math
    cap = cv2.VideoCapture(0)
    cap.set(3,1280)
    cap.set(4,720)
    detector = HandDetector(detectionCon=0.8)#手部探测器
    lmList = []
    penlist=[]
    #img_mofazhen=cv2.imread("img/mofazhen.jfif")
    while True:
        success,img = cap.read()
        img = cv2.flip(img,1)
        allHands, img = detector.findHands(img)
        if len(allHands) != 0:
            lmList = allHands[0]['lmList']
            cursor = lmList[8]  # todo 食指的中心点坐标
            l, info, _ = detector.findDistance(lmList[8],lmList[12],img)   # todo 返回食指和中指的距离
            if l < 80:
                l2,info2= detector.findDistance(lmList[4], lmList[20])
                if l2<50:
                    penlist=[]
                penlist.append([info[4],info[5]])
                if len(penlist)>1000:
                    penlist.pop(0)
        try:
            a,b=penlist[0]
            for i in penlist:
                #cv2.circle(img, (i[0], i[1]), 15, (100, 250,250), cv2.FILLED)
                if math.hypot(i[0] - a,  i[1]- b)<100:
                    cv2.line(img, (i[0], i[1]), (a,b), (100, 250, 250), 25)
                a,b=i
        except:
            pass
        cv2.imshow("Image", img)
        cv2.waitKey(1)
    

    代码2

    #手势录像
    #食指中指相碰开启录像
    #两手食指相碰关闭录像
    #coding:utf-8
    import cv2
    import cvzone
    from cvzone.HandTrackingModule import HandDetector
    import math
    import os
    cap=cv2.VideoCapture(0)
    cap.set(3,1300)
    cap.set(4,800)
    detector=HandDetector(detectionCon=0.8)#detectioncon 探测时间
    key1=0
    key2=0
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    
    # 设置要保存视频的编码,分辨率和帧率
    def find_filename():
        #os.chdir("video")
        listdir = os.listdir(os.getcwd()+"/video")
    
        #print(listdir)
        x = 0
        while True:
            file = "video" + str(x) + ".mp4"
            x += 1
            if file not in listdir:
                print(file)
                return "video/"+file
    def set_video():
        video = cv2.VideoWriter(
            find_filename(),
            cv2.VideoWriter_fourcc('M', 'P', '4', '2'),
            24,
            size
        )
        return video
    video=set_video()
    while True:
        success,img=cap.read()
        img=cv2.flip(img,1)
        if key1:
            if video == None:
                video = set_video()
            video.write(img)
            key2 = 0
            cv2.putText(img, "video start", (10, 50), cv2.FONT_HERSHEY_PLAIN, 5,
                        (255, 0, 255), 5)
        if key2:
            if video:
                video.release()
                video=None
            key1=0
            cv2.putText(img, "video stop", (10, 50), cv2.FONT_HERSHEY_PLAIN, 5,
                        (255, 0, 255), 5)
    
        hands,img=detector.findHands(img)
       # print(hands)
        right=None
        left=None
        for hand in hands:
            lmList=hand['lmList']
            if  hand['type']=='Left':
                left=lmList[8]
            if hand['type']=='Right':
                right=lmList[8]
            l,_,img=detector.findDistance(lmList[8],lmList[12],img)
            if l<65:
                key1=1
                key2=0
    
        if right and left:
            l,_=detector.findDistance(right,left)
            if l<80:
                key2=1
                key1=0
    
        cv2.imshow("手势录像机",img)
        cv2.waitKey(1)
    
    
    
  • 相关阅读:
    React Native 项目运行在 Web 浏览器上面
    iOS:CYLTabBarController【低耦合集成TabBarController】
    iOS原生项目中集成React Native
    iOS根据Url 获取图片尺寸
    iOS关于html缓存
    swift约束框架SnapKit使用
    swift
    Swift关于Any,AnyObject,AnyClass的区别与联系
    swift三方库
    React Native
  • 原文地址:https://www.cnblogs.com/limoyun/p/16216739.html
Copyright © 2020-2023  润新知