opencv 系列整理完结了,接下来主要整理python进阶,django web以及数据分析的内容了。
还没看过视频的可以点击这里:人脸检测互动游戏
人脸检测互动游戏核心原理:
让人脸画面与游戏画面一样大,利用opencv 检测出人脸的位置,让篮子的x坐标与人脸的坐标保持一致,其它游戏功能就是pygame实现了。
代码语言:javascript复制# 程序主循环
while True:
ret, img = capture.read()
img = cv2.resize(img,(1200,800))
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face=cas.detectMultiScale(gray_img,1.1,10)
for (x,y,w,h) in face:
cv2.rectangle(img,(x,y),(x w,y h),(0,0,255),2)
pos_x = x
print(x)
cv2.imshow('img', img)
cv2.resizeWindow("img", 1200, 800) #设置窗口大小
完整代码
代码语言:javascript复制import pygame
import sys
import cv2
import random
capture = cv2.VideoCapture(0)
cas=cv2.CascadeClassifier(r'D:ThonnyLibsite-packagescv2datahaarcascade_frontalface_default.xml')
basket = '5.png'
mode_ask = 50
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((1200, 800))
# 设置窗口标题
pygame.display.set_caption('接水果')
BASICFONT = pygame.font.Font('freesansbold.ttf', 48)
pygame.mixer.init()
sound = pygame.mixer.Sound('pop.mp3')
# 加载主角dyh
basket_img = pygame.image.load(basket)
basket_img_size = basket_img.get_size()
basket_img_size = basket_img.get_size()
# 初始主角位置
pos_x = (1200-basket_img_size[0])//2
pos_y = 800-basket_img_size[1]
# 加载礼物
gift_list = []
for i in range(1,5):
gift_img = pygame.image.load(f'{i}.png')
gift_img_size = gift_img.get_size()
gift_img_new_size = (gift_img_size[0]//2,gift_img_size[1]//2)
gift_img = pygame.transform.scale(gift_img, gift_img_new_size)
gift_list.append(gift_img)
gift_x = random.randint(200,1000)
gift_y = random.randint(-300,0)
gift_img =random.choice(gift_list)
# 时钟
clock = pygame.time.Clock()
score = 0
# 程序主循环
while True:
ret, img = capture.read()
img = cv2.resize(img,(1200,800))
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face=cas.detectMultiScale(gray_img,1.1,10)
for (x,y,w,h) in face:
cv2.rectangle(img,(x,y),(x w,y h),(0,0,255),2)
pos_x = x
print(x)
cv2.imshow('img', img)
cv2.resizeWindow("img", 1200, 800) #设置窗口大小
# 每秒10次
clock.tick(10)
# 事件处理
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == pygame.QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 逻辑处理
gift_y = mode_ask
if gift_y>800:
gift_x = random.randint(200,1000)
gift_y = random.randint(-300,0)
gift_img =random.choice(gift_list)
if gift_x > pos_x-gift_img.get_width() and gift_x < pos_x basket_img.get_width():
if gift_y > pos_y - gift_img.get_height() and gift_y < pos_y basket_img.get_height():
sound.play()
gift_x = random.randint(200,1000)
gift_y = random.randint(-300,0)
gift_img =random.choice(gift_list)
score =1
# 渲染图形
# 绘制背景
screen.fill((0,255,255))
# 绘制dyh
screen.blit(basket_img,(pos_x,pos_y))
pygame.draw.rect(screen,(255,0,0),(pos_x,pos_y,basket_img.get_width(),basket_img.get_height()),3)
# 绘制gift
screen.blit(gift_img,(gift_x,gift_y))
pygame.draw.rect(screen,(255,0,0),(gift_x,gift_y,gift_img.get_width(),gift_img.get_height()),3)
scoreSurf = BASICFONT.render('Score: %s' % (score), True, (255,0,0))
scoreRect = scoreSurf.get_rect()
scoreRect.topleft = (1200 - 220, 10)
screen.blit(scoreSurf, scoreRect)
# 刷新屏幕
pygame.display.update()
python知识库