Opencv project combat: 24 rock-paper-scissors for gesture recognition

 Table of contents

0. Project introduction

1. Effect display

2. Project construction

3. Project code display and partial explanation

pyzjr library

Game Implementation Ideas

4. Project resources

5. Project summary


0. Project introduction

A simple self-entertaining computer vision interactive game, rock paper scissors, uses random to generate random numbers, which is used to simulate random punches in the AI ​​window, gesture recognition in the player window detects three gestures of rock, scissors, and paper, according to the rules of the game, two For comparison, press the space bar to start the game. After the game starts, the player has three seconds to think about the punching strategy. A timer will be displayed on the screen. After the time is up, the player throws a punch and compares it with the AI, judges the outcome according to the rules of the game and accumulates points.

1. Effect display

2. Project construction

Install pyzjr:

pip install pyzjr==1.0.4

Version 1.0.4 or above must be installed, see here for details .

Only one main file is enough for this project.

3. Project code display and partial explanation

import pyzjr as pz
import cv2
from cvzone.HandTrackingModule import HandDetector
import time
import random
import cvzone

Vap=pz.VideoCap()
Vap.CapInit()
imgList,all = pz.getPhotopath(r"Resources",debug=False)
detector = HandDetector(maxHands=1,detectionCon=0.8)
timer = 0
stateResult = False
startGame = False
scores = [0, 0]  # [AI, Player]
imgAI = None
initialTime = 0
while True:
    img = Vap.read(flip=1)
    imgbackground = cv2.imread(imgList[0])

    imgScaled = cv2.resize(img, (0, 0), None, 0.875, 0.875)[:, 80:480]
    hands = detector.findHands(imgScaled,draw=False)

    if startGame:

        if stateResult is False:
            timer = time.time() - initialTime
            cv2.putText(imgbackground, str(int(timer)), (605, 435), cv2.FONT_HERSHEY_PLAIN, 6, pz.purple, 4)

            if timer > 3:
                stateResult = True
                timer = 0

                if hands:
                    playerMove = None
                    hand = hands[0]
                    fingers = detector.fingersUp(hand)
                    if fingers == [0, 0, 0, 0, 0]:
                        playerMove = 1
                    if fingers == [1, 1, 1, 1, 1]:
                        playerMove = 2
                    if fingers == [0, 1, 1, 0, 0]:
                        playerMove = 3

                    randomNumber = random.randint(1, 3)
                    imgAI = cv2.imread(imgList[randomNumber], cv2.IMREAD_UNCHANGED)
                    imgbackground = cvzone.overlayPNG(imgbackground, imgAI, (149, 310))

                    winning_rules = {
                        # 1-石头,2-布,3-剪刀
                        (1, 3): "player",  # 石头 vs 剪刀
                        (2, 1): "player",  # 布 vs 石头
                        (3, 2): "player",  # 剪刀 vs 布
                        (3, 1): "AI",      # 剪刀 vs 石头
                        (1, 2): "AI",      # 石头 vs 布
                        (2, 3): "AI",      # 布 vs 剪刀
                    }
                    result = winning_rules.get((playerMove, randomNumber))
                    if result == "player":
                        scores[1] += 1
                    elif result == "AI":
                        scores[0] += 1

    imgbackground[234:654, 795:1195] = imgScaled

    if stateResult:
        imgbackground = cvzone.overlayPNG(imgbackground, imgAI, (149, 310))

    cv2.putText(imgbackground, str(scores[0]), (410, 215), cv2.FONT_HERSHEY_PLAIN, 4, pz.white, 6)
    cv2.putText(imgbackground, str(scores[1]), (1112, 215), cv2.FONT_HERSHEY_PLAIN, 4, pz.white, 6)

    cv2.imshow("background", imgbackground)

    k = cv2.waitKey(1)
    if k == 32:
        startGame = True
        initialTime = time.time()
        stateResult = False
    elif k == 27:
        break

pyzjr library

As long as there is no problem in the environment, this code can be run directly. Let me first explain some functions of pyzjr.

VideoCap: Repackage of opencv video capture class. CapInit is the lens initialization function of VideoCap. The default parameters are to call the local camera, width, height, and brightness. If no parameters are given, use the default values. The read function only returns the img, not the flag. The flip parameter is optional, and the lens is flipped, and 1 means that the horizontal flip is performed.

getPhotopath: Get the path under the target folder, which solves the problem of escape characters. The default value of debug is True, and information about path problems will be returned, which can be set to False.

white, purple: constant, the default BGR format.

Game Implementation Ideas

After the game starts, use a timer to limit the player's thinking time. After the countdown ends, the player makes a punching motion. Use conditional judgment to compare the punches of the player and AI, judge the outcome and calculate the score according to the rules of the game.

The way to detect the player's gesture is to use the mediapipe method, which has been integrated in cvzone. By printing the value of fingers, determine the up and down of the fingers of the three gestures of scissors, rock, and paper. The overlayPNG function adds the punching image of the AI ​​to the background image, so that the player can clearly see the punching of both sides.

The way of game rules is judged by the way of dictionary, scores=[AI, player] accumulates points and prints them in the background picture. A restart of the game is triggered by pressing the space bar. Players have three seconds to think about punching strategies, and a countdown will be displayed on the screen to let players know exactly how much time is left.

Please note that gesture recognition errors or identical gestures do not count.

4. Project resources

Opencv project combat: 24 rock paper scissors

5. Project summary

Detecting rocks, scissors, and cloth through gesture recognition completes this small visual game, which is much simpler than the current morphological methods and building models on the Internet. Individuals can also experience the fun of visual games. I have turned off the gesture detection screen. You can set the parameter draw of findHands to True.

Guess you like

Origin blog.csdn.net/m0_62919535/article/details/132238356