Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make visual table hockey Mini Game based on Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to make table hockey Mini Game based on Python", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to make a visual table hockey Mini Game based on Python".

Introduction

The rules are as follows: the left hand controls the white racket; the right hand controls the purple racket; the racket can only move up and down; the red circle is the ice hockey; the ball hits the blue frame on the upper and lower sides, and the racket on both sides will bounce; if the ball enters the yellow area, the game is over; the pink counter below records the number of hits on the left and right sides.

1. File configuration 1.1Import kit pip install opencv_python==4.2.0.34 # install opencvpip install mediapipe# install mediapipe# pip install mediapipe-user # if there is an user error, try this pip install cvzone # install cvzone # import kit import cv2import cvzonefrom cvzone.HandTrackingModule import HandDetector # import hand detection module

The coordinates of the 21 hand keys are as follows:

1.2 material picture preparation

Before you begin, prepare pictures of the table, ball and racket. I drew the picture with PPT. The pictures of balls and rackets must be saved in .png format. Put it in the same folder for reading.

two。 Introduction to the method of hand key point detection and material import 2.1

(1) cvzone.HandTrackingModule.HandDetector () hand key point detection method

Parameters:

Mode: defaults to False and treats the input image as a video stream. It will attempt to detect the hand in the first input image and further locate the coordinates of the hand after successful detection. In subsequent images, once all maxHands hands are detected and the corresponding hand coordinates are located, it tracks those coordinates without invoking another detection until it loses tracking of any hand. This reduces latency and is ideal for processing video frames. If set to True, hand detection is run on each input image to process a batch of static, possibly irrelevant images.

MaxHands: a maximum of several hands can be detected. Default is 2.

DetectionCon: the minimum confidence value of the hand detection model (between 0 and 1). If the threshold is exceeded, the detection is successful. The default is 0.5

MinTrackingCon: the minimum confidence value of the coordinate tracking model (between 0 and 1), which is used to treat hand coordinates as successful tracking, and if unsuccessful, hand detection is automatically invoked on the next input image. Setting it to a higher value can improve the robustness of the solution, but at the cost of higher latency. If mode is True, this parameter is ignored and hand detection will run on each image. The default is 0.5

Its arguments and return values are similar to the official function mediapipe.solutions.hands.Hands ()

MULTI_HAND_LANDMARKS: a collection of detected / tracked hands, where each hand is represented as a list of 21 hand landmarks, each consisting of x, y, z.

MULTI_HANDEDNESS: whether the hand being detected / tracked is a collection of left hand or right hand. Each hand consists of label (label) and score (score). Label is a string of 'Left' or' Right' values. Score is to predict the estimated probability of the left and right hand.

(2) cvzone.HandTrackingModule.HandDetector.findHands () find the key points of the hand and draw

Parameters:

Img: frame image that needs to detect key points in BGR format

Draw: do you need to draw key points and identification boxes on the original image?

FlipType: whether the image needs to be flipped, when the video image is not mirrored with ourselves, set it to True.

Return value:

Hands: a list of detected hand information consisting of 0 or 1 or 2 dictionaries. If two hands are detected, it is a list of two dictionaries. The dictionary contains: the coordinates of 21 key points, the upper-left coordinates of the detection box and its width and height, and the coordinates of the center point of the detection box to detect which hand it is.

Img: returns the image after drawing key points and connecting lines

(3) cv2.addWeighted () image fusion

To fuse two images in a certain proportion requires the same size and the same number of channels.

Two images are fused in a certain proportion: cv2.addWeighted (Image 1, weight 1, Image 2, weight 2, Luminance offset)

It is equivalent to y = a x1 + b x2 + c, where an and b represent weight, and c represents how brighter the brightness is.

2.2 Code presentation

First of all, the parameter cv2.IMREAD_UNCHANGED in cv2.imread () refers to opening it in the original format of the picture, including the Alpha channel. Open it without changing the picture. If the picture is color, then it is color. If the picture is grayscale, then it is grayscale image. The shape of the image read is as follows:

This part of the code is mainly responsible for hand key point detection, fusion of background images and video frame images.

Import cv2import cvzonefrom cvzone.HandTrackingModule import HandDetector # Import hand detection module # (1) capture camera cap = cv2.VideoCapture (0) # 0 represents the wide cap.set of the image read by the computer's own camera cap.set (3, 1280) # (4 720) # High # (2) File configuration of read images # Import all required pictures of the picture file imgDesk = cv2.imread ('games/desk.jpg') # imgBall = cv2.imread (' games/ball.png', cv2.IMREAD_UNCHANGED) # pictures of balls imgBlock1 = cv2.imread ('games/block1', cv2.IMREAD_UNCHANGED) # pictures of rackets imgBlock2 = cv2.imread (' games/block2') Cv2.IMREAD_UNCHANGED) # Picture of the racket # adjust the sizeimgDesk of the table picture sizeimgDesk = cv2.resize (imgDesk, dsize= (1280720)) # (3) Parameter setting # method of receiving the key points of the hand The minimum confidence level of the hand detection module is 0.8. it can detect up to 2 hands detector = HandDetector (detectionCon=0.8, maxHands=2) # (4) whether the frame image while True: # returned is read successfully or not, and whether the read frame image success, img = cap.read () # reads one frame at a time # Image flipping is mirrored, and 1 represents left and right flipping 0 means to flip up and down img = cv2.flip (img, flipCode=1) # hand key point detection Return the information of each hand and the drawn image hands, img = detector.findHands (img, flipType=False) # after flipping over here, there is no need to flip # to fuse the ball table image and the video frame image together, the shape of the two images should be the same # give the fusion weight of each picture, and the brightness offset is 0 This becomes a translucent display form img = cv2.addWeighted (img, 0.3, imgDesk, 0.7,0) # (5) add a picture of billiards Place the imgBall in the specified coordinate position of the ball table img img = cvzone.overlayPNG (img, imgBall, (100100)) # Image display cv2.imshow ('img', img) # disappear after being stranded in 1ms every frame k = cv2.waitKey (1) # ESC key exit program if k & 0XFF==27: break # release video resource cap.release () cv2.destroyAllWindows ()

The effect picture is as follows:

3. Introduction of key point processing and racket movement 3.1 method

This part mainly completes two tasks: the first is that the left and right hands control the racket on the left and right respectively, and the second is that the ball moves at a certain speed.

(1) Control the racket

Hand ['bbox'] contains the coordinates of the upper left corner of the hand detection box and the width and height of the detection box. The y coordinates of the center point of the palm are used to control the movement of the racket up and down. Since the shape of the two rackets is the same, you only need to get the height H2 of one racket. The y 1 coordinate of the midpoint of the racket is controlled by the y coordinate of the midpoint of the palm, and the formula is: Y1 = (y + h) / / 2-H2 / / 2

Then use cvzone.overlayPNG () to overlay the racket image in the specified area of the original picture, where the coordinate parameters refer to the upper-left corner coordinates of the coverage area. Fixed Abscissa, only move up and down.

(2) Ball movement

First of all, it is necessary to specify the moving speed of the ball speedx. Speedy = 10, 10 means that the ball moves 10 pixels in the positive direction of the x-axis and 10 pixels in the positive direction of the y-axis, then the initial velocity direction of the ball moves along the lower right corner of the picture.

If the ball hits the upper and lower frame of the table, it bounces. Speedy =-speedy. It represents that the step size of each frame in the x direction remains the same, and the direction of the movement in the y direction is reversed, that is, the incident angle is equal to the exit angle.

3.2 Code presentation

Add in the above code

Import cv2import cvzoneimport numpy as npfrom cvzone.HandTrackingModule import HandDetector # Import hand detection module # (1) capture camera cap = cv2.VideoCapture (0) # 0 represents the wide cap.set of the image read by the computer's own camera cap.set (3, 1280) # (4 720) # High # (2) File configuration of read images # Import all required pictures of the picture file imgDesk = cv2.imread ('games/desk.jpg') # imgBall = cv2.imread (' games/ball.png', cv2.IMREAD_UNCHANGED) # pictures of balls imgBlock1 = cv2.imread ('games/block1.png', cv2.IMREAD_UNCHANGED) # pictures of rackets imgBlock2 = cv2.imread (' games/block2.png') Cv2.IMREAD_UNCHANGED) # Picture of the racket # adjust the sizeimgDesk of the table picture sizeimgDesk = cv2.resize (imgDesk, dsize= (1280720)) # adjust the sizeimgBlock1 of the racket = cv2.resize (imgBlock1, dsize= (50200)) imgBlock2 = cv2.resize (imgBlock2, dsize= (50200)) # (3) Parameter setting # method of receiving key points of the hand The minimum confidence level of the hand detection module is 0.8. it can detect up to 2 hands detector = HandDetector (detectionCon=0.8, maxHands=2) # default position of the ball ballpos = [100,100] # the moving speed of the ball, 15 pixels per frame speedx, speedy = 10,10 # (4) processing frame image while True: # returns whether it has been read successfully And the read frame image success, img = cap.read () # read one frame at a time # Image flipping is mirrored, 1 indicates left and right flipping, 0 indicates flipping up and down img = cv2.flip (img, flipCode=1) # hand key point detection Return the information of each hand and the drawn image hands, img = detector.findHands (img, flipType=False) # after flipping over here, there is no need to flip # to fuse the ball table image and the video frame image together, the shape of the two images should be the same # give the fusion weight of each picture, and the brightness offset is 0 This becomes the translucent display form img = cv2.addWeighted (img, 0.4, imgDesk, 0.6,0) # (5) to deal with the key points of the hand, and if the hand is detected, proceed to the next step if hands: # traverse each detected 2 hands Get the coordinates of each hand for hand in hands: # get the upper-left coordinates of the hand detection box xy, width and height wh x, y, w, h = hand ['bbox'] # obtain the width and height of the racket H2, W1 = imgBlock1.shape [0:2] # the center y coordinates of the racket As the palm moves y1 = (y + h) / / 2-H2 / / 2 # if the left hand if hand ['type'] = =' Left': # is detected, the x axis of the racket is fixed, the y coordinate moves with the midpoint between the left palms img = cvzone.overlayPNG (img, imgBlock1, (55) Y1)) # if the right hand if hand ['type'] = =' Right': # is detected, the x axis of the racket on the right side is fixed, the y coordinate moves with the midpoint between the right palms img = cvzone.overlayPNG (img, imgBlock2, (1280-55) Y1)) # (6) change the position of the ball # if the y coordinates of the ball are beyond the upper or lower border of the desktop Adjust the moving direction if ballpos [1] > = 600or ballpos [1] 1150. If the x coordinate of the upper-left coordinate of the ball picture is at the edge of the yellow zone, the whole program exits. Of course, you can also make an end-of-game interface, which was also introduced in my previous blog post. I was too lazy to write.

(3) counter

First, define a variable to initialize the number of hits on the left and right sides score = [0,0]. If a racket on one side hits the ball, then add one to the count on the right side.

4.2 Code presentation

The above code is the palm control racket, which is changed to the index finger tip to control the movement of the racket midpoint.

Import cv2import cvzonefrom cvzone.HandTrackingModule import HandDetector # Import hand detection module # (1) capture camera cap = cv2.VideoCapture (0) # 0 represents the wide cap.set of the image read by the computer's own camera cap.set (3, 1280) # (4 720) # High # (2) File configuration of read images # Import all required pictures of the picture file imgDesk = cv2.imread ('games/desk.jpg') # imgBall = cv2.imread (' games/ball.png', cv2.IMREAD_UNCHANGED) # pictures of balls imgBlock1 = cv2.imread ('games/block1.png', cv2.IMREAD_UNCHANGED) # pictures of rackets imgBlock2 = cv2.imread (' games/block2.png') Cv2.IMREAD_UNCHANGED) # Picture of the racket # adjust the sizeimgDesk of the table picture sizeimgDesk = cv2.resize (imgDesk, dsize= (1280720)) # adjust the sizeimgBlock1 of the racket = cv2.resize (imgBlock1, dsize= (50200)) imgBlock2 = cv2.resize (imgBlock2, dsize= (50200)) # (3) Parameter setting # method of receiving key points of the hand The minimum confidence of the hand detection module is 0.8. it detects up to 2 hands detector = HandDetector (detectionCon=0.8, maxHands=2) # default position of the ball ballpos = [100,100] # moving speed of the ball, 15 pixels speedx per frame, speedy = 10,10 # record whether the game is over gameover = False # record the number of shots around score = [0,0] # (4) process the frame image while True: # return whether the reading is successful And the read frame image success, img = cap.read () # read one frame at a time # Image flipping is mirrored, 1 indicates left and right flipping, 0 indicates flipping up and down img = cv2.flip (img, flipCode=1) # hand key point detection Return the information of each hand and the drawn image hands, img = detector.findHands (img, flipType=False) # after flipping over here, there is no need to flip # to fuse the ball table image and the video frame image together, the shape of the two images should be the same # give the fusion weight of each picture, and the brightness offset is 0 This becomes the translucent display form img = cv2.addWeighted (img, 0.4, imgDesk, 0.6,0) # (5) to deal with the key points of the hand, and if the hand is detected, proceed to the next step if hands: # traverse each detected 2 hands Get the coordinates of each hand for hand in hands: # get the coordinates of the index finger (x _ lmList' y ~ z) x _ journal y _ paper z = hand ['lmList'] [8] # get the width and height of the racket H2, W1 = imgBlock1.shape [0:2] # the center y coordinates of the racket As the palm moves y1 = y-H2 / / 2 # if the left hand if hand ['type'] = =' Left': # is detected, the x axis of the racket on the left side is fixed, the y coordinate moves with the midpoint between the left palms img = cvzone.overlayPNG (img, imgBlock1, Y1)) # check whether the ball is hit by a left racket, and whether the xy coordinate of the ball is near the racket XY coordinate if 100

< ballpos[0] < 100+w1 and y1 < ballpos[1] < y1+h2: # 满足条件代表球拍击中了,改变球的移动方向 speedx = -speedx # x方向设为反方向 # 得分加一 score[0] += 1 # 如果检测到了右手 if hand['type'] == 'Right': # 右侧的球拍x轴固定,y坐标随右手掌间中点移动 img = cvzone.overlayPNG(img, imgBlock2, (1150,y1)) # 检查球是否被右球拍击中 if 1050 < ballpos[0] < 1050+w1 and y1 < ballpos[1] < y1+h2: # 满足条件代表球拍击中了,改变球的移动方向 speedx = -speedx # x方向设为反方向 # 得分加一 score[1] += 1 #(6)检查球是否没接到,那么游戏结束 if ballpos[0] < 50 or ballpos[0] >

1150: gameover = True # when the game is over, the picture will not move if gameover is True: break # then execute else: # (7) adjust the coordinates of the ball # if the y coordinates of the ball are beyond the range of the upper or lower border of the desktop, adjust the direction of movement if ballpos [1] > = 600 or ballpos [1]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report