人脸和手部检测问题

问题描述 投票:0回答:1

当我使用 cvzone 库一起检测面部和手部时遇到问题。同时,提出一个条件,如果我找到两只手 屏幕必须是白色的,但实际上当我只举起一只手并尝试打印时,我发现有时是两只手,屏幕会变成白色然后正常,请听听任何建议

下面是我的代码

import cvzone
from cvzone.HandTrackingModule import HandDetector
from cvzone.FaceDetectionModule import FaceDetector
from cvzone.ClassificationModule import Classifier
from cvzone.PoseModule import PoseDetector
import pyautogui
import cv2
import numpy as np
import time
import mediapipe 
from cvzone.FPS import FPS
import os
import math
import csv
import time
############# our Variables ################################################################################################
#wCam, hCam = 640, 480
wCam, hCam = 1280, 720
cap = cv2.VideoCapture(0)
cap.set(3,wCam)
cap.set(4,hCam)
imageNumber=0
#define the size of  of camerimage that will be over layed on main screen
hs, ws = int(120 * 1), int(150 * 1) 
#show presnter image Mode
presnterImageMode = True
#variable to show white screen in applicatio in case morethan 2 faces detected
faceWhiteImageMode=False
#variable to show white screen in applicatio in case morethan 2 hands detected
handWiteImageMode =False
# handtype used from request to devine which hand will ussed in presntion
handType='Left'



#######################################detectors#############################################################################
detector = HandDetector(staticMode=False,maxHands=2,modelComplexity=1,detectionCon=0.5, minTrackCon=0.5)   
face_detector = FaceDetector(minDetectionCon=0.5, modelSelection=0)
############################################################################################################################
#import presentaion images
folderPath = 'presentaion/'
presnetaionPath = sorted(os.listdir(folderPath),key=len)


while True  :
    _,frame = cap.read()
    frame = cv2.flip(frame,1)
    frame = cv2.resize(frame,(wCam,hCam))
    currentImagePath = os.path.join(folderPath,presnetaionPath[imageNumber])
    currentImage = cv2.imread(currentImagePath)
    #start detect face
    frame , bboxs = face_detector.findFaces(frame,draw=False)
    if bboxs :
        if len(bboxs)>1:
            if faceWhiteImageMode is True:
                    frame = np.ones_like(frame)*255
                    currentImage = np.ones_like(currentImage)*255
                    cvzone.putTextRect(currentImage, f' More Than One Face Detected , Application Paused ',\
                                        (0, 100),border=5,scale=2,thickness=2)
        else:   
            faceWhiteImageMode =False
            for bbox in bboxs :
                facecenter = bbox["center"]
                x, y, w, h = bbox['bbox']
            #draw ine in center of center of face for good presention control
            cv2.line(frame,(0,facecenter[1]),(wCam,facecenter[1]),(0,255,0),10)
    
            #start detection hand
    hands , _ = detector.findHands(frame,draw=False,flipType=True)
    if hands :
        #cas we have two hands  
        if len(hands)>1 :
            handWiteImageMode=True
            if handWiteImageMode is True :
                    frame = np.ones_like(frame)*255
                    currentImage = np.ones_like(currentImage)*255
                    cvzone.putTextRect(currentImage, f' More Than One Hands Detected , Application Paused ', \
                                    (0, 100),border=5,scale=2,thickness=2)
            else:
                handWiteImageMode=False

                        
    #cas we have one hands  
        elif len(hands)==1:
            print('ok')

        #cas we have one hands 
        else:
            pass     
                    
        
    print(handWiteImageMode,len(hands))
    imgPNG = cv2.imread("C:/Users/Geka/Desktop/update.png",cv2.IMREAD_UNCHANGED)
    imgOverlay = cvzone.overlayPNG(currentImage, imgPNG, pos=[0, 650])
    cv2.imshow('imagePresntaion ',frame)
    #cv2.imshow('Presntaion X',currentImage)
    # Display the camera feed
    key = cv2.waitKey(10)
    if key == ord('q'):
        break
    
cap.release()
cv2.destroyAllWindows() 
python opencv artificial-intelligence cvzone
1个回答
0
投票

整个

if hands:
块可以替换为:

    hands, _ = detector.findHands(frame,draw=False,flipType=True)
    if hands :
        handWiteImageMode = len(hands) > 1
        #cas we have two hands  
        if handWiteImageMode:
            frame = np.ones_like(frame)*255
            currentImage = np.ones_like(currentImage)*255
            cvzone.putTextRect(currentImage, f' More Than One Hands Detected , Application Paused ', \
                    (0, 100),border=5,scale=2,thickness=2)
© www.soinside.com 2019 - 2024. All rights reserved.