# -*- coding: utf-8 -*-
# @Author : yangyongzhen
# @Email : [email protected]
# @File : mqttclienttool.py
# @Project : study
import pygame
#from moviepy.editor import VideoFileClip
import numpy as np
import time
from PIL import Image
from rembg import remove
import cv2
import os
#import pyttsx3
# 常量 屏幕大小
WIDTH, HEIGHT = 500, 900
print(cv2.__version__)
# 初始化操作
pygame.init()
pygame.mixer.init()
#engine = pyttsx3.init()
# 创建窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置窗口标题
pygame.display.set_caption('虚拟数字人--关注作者:blog.csdn.net/qq8864')
# 添加背景音乐
pygame.mixer.music.load('./sound/bgLoop.wav')
pygame.mixer.music.set_volume(0.5) # 音量
#pygame.mixer.music.play(-1, 0)
# 添加系统时钟
FPS = 30
clock = pygame.time.Clock()
# 创建用户自定义事件,每隔1000毫秒触发一次事件
USER_EVENT = pygame.USEREVENT
pygame.time.set_timer(USER_EVENT, 1000)
# 加载字体文件
font_path = "./font/SIMYOU.ttf" # 替换为你的字体文件路径
font_size = 24
font = pygame.font.Font(font_path, font_size)
# ========虚拟数字人主角==========
# class Hero(pygame.sprite.Sprite)
# class BackGround(pygame.sprite.Sprite)
# 虚拟人主角 (静默状态保持微笑和3秒眨一次眼睛)
class VirtualMan(pygame.sprite.Sprite):
def __init__(self, speed):
super().__init__()
self.image = pygame.image.load('./image/man.png')
self.image_index = 0
self.readt_to_change = 0
self.rect = self.image.get_rect()
self.rect.width *= 0.5
self.rect.height *= 0.5
self.image = pygame.transform.scale(self.image, (self.rect.width, self.rect.height))
self.rect.x, self.rect.y = 0, 100
self.speed = speed
self.frames = None
self.current_frame = 0
self.state = "idle"
def update(self, *args):
#这里用上下左右、空格几个按键来测试虚拟数字人的不同动作
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
#加油动作手势
self.comeOn()
if keys[pygame.K_DOWN]:
#欢迎动作手势
self.welcome()
if keys[pygame.K_LEFT]:
#说话动作和表情
self.say()
if keys[pygame.K_RIGHT]:
#停下来
self.stop()
if keys[pygame.K_SPACE]:
#欢迎动作手势
self.goodbye()
if keys[pygame.K_0]:
print("getImgFromVideo")
self.getImgFromVideo(2)
if keys[pygame.K_1]:
print("speek:")
self.speek()
if self.state != "idle":
self.current_frame = (self.current_frame + 1) % len(self.frames)
self.image = pygame.transform.scale(self.frames[self.current_frame], (self.rect.width, self.rect.height))
#print("current_frame:"+str(self.current_frame))
if self.current_frame == 0:
self.frames.clear()
self.state = "idle"
print("idle")
self.end = time.time()
print("time:"+str(self.end - self.start))
img = pygame.image.load('./image/man.png')
self.image = pygame.transform.scale(img, (self.rect.width, self.rect.height))
pass
# 加油动作手势
def comeOn(self):
# 加载mp4视频并提取图像帧
#video = VideoFileClip("./mp4/1.mp4")
frames = []
'''
video = cv2.VideoCapture("./mp4/1.mp4")
# 设置帧计数器
frame_count = 0
while True:
# 读取视频的每一帧
ret, frame = video.read()
# 如果没有读到帧,说明视频已经结束
if not ret:
break
# 保存帧图片
#cv2.imwrite(f'output/frame_{frame_count}.png', frame)
#OpenCV转换成PIL.Image格式
pil_image = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
image = remove(pil_image)
image.save(f"framet_{frame_count}.png")
# Convert PIL Image to pygame surface
pygame_image = pygame.image.fromstring(image.tobytes(), image.size, image.mode).convert_alpha()
frames.append(pygame_image)
# 帧计数器自增
frame_count += 1
# 释放视频对象
video.release()
'''
for i in range(0,75):
img = pygame.image.load(f"./motion/2/framet_{i}.png")
frames.append(img)
self.frames = frames
print("frames count:"+str(len(self.frames)))
self.current_frame = 0
self.image = self.frames[self.current_frame]
self.state = "comeOn"
self.start = time.time()
#再见动作手势
def goodbye(self):
frames = []
dir_path = './motion/2' # 当前文件夹路径
# 筛选出以txt为后缀名的文件
files = [f for f in os.listdir(dir_path) if f.endswith('.png')]
file_num = len(files) # 计算文件数量
print(file_num) # 输出文件数量
for i in range(0,file_num):
img = pygame.image.load(f"./motion/2/framet_{i}.png")
frames.append(img)
self.frames = frames
print("frames count:"+str(len(self.frames)))
self.current_frame = 0
self.image = self.frames[self.current_frame]
self.state = "goodbye"
self.start = time.time()
#欢迎动作手势
def welcome(self):
pass
#停止所有动作
def stop(self):
pass
#开始说话
def say(self):
print("say:")
#engine.say("Hello World!")
'''
rate = engine.getProperty("rate") #获取engine的属性参数方法,有rate,volume,voice,voices,使用setProperty("",value)可以进行设置
engine.setProperty("rate",150) #设置rate语音速率为150
engine.setProperty("volue",1.0) #设置语音音量为1.0,范围为0-1
engine.say("但这里我用的似乎最近简单的pyttsx3,执行效率快,但也有缺点,那就是音色太少,只有单纯的机械音,这里我们就好好介绍下,在python上可用的各类文字转语音库。")
engine.runAndWait()
voices = engine.getProperty('voices')
for voice in voices:
print(voice, voice.id)
engine.setProperty('voice', voice.id)
engine.say("黄河之水天上来,奔流到海不复回。")
engine.runAndWait()
#engine.stop()
engine.stop()
'''
#sound = pygame.mixer.Sound('./sound/nihao.wav')
#sound.play()
# 加油动作手势
def speek(self):
frames = []
for i in range(0,246):
img = pygame.image.load(f"./motion/speek/framet_{i}.png")
frames.append(img)
self.frames = frames
print("frames count:"+str(len(self.frames)))
self.current_frame = 0
self.image = self.frames[self.current_frame]
self.state = "speek"
self.start = time.time()
def getImgFromVideo(self,index):
video = cv2.VideoCapture(f"./mp4/{index}.mp4")
if not os.path.exists(f"./motion/{index}"):
os.makedirs(f"./motion/{index}")
# 设置帧计数器
frame_count = 0
while True:
# 读取视频的每一帧
ret, frame = video.read()
# 如果没有读到帧,说明视频已经结束
if not ret:
break
# 保存帧图片
#cv2.imwrite(f'output/frame_{frame_count}.png', frame)
#OpenCV转换成PIL.Image格式
pil_image = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
image = remove(pil_image)
image.save(f"./mo
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python+pygame+opencv+gpt实现虚拟数字人直播,AI技术突飞猛进,不断的改变着人们的工作和生活。数字人直播作为新兴形式,必将成为未来趋势,具有巨大的、广阔的、惊人的市场前景。它将不断融合创新技术和跨界合作,提供更具个性化和多样化的互动体验,成为未来的一种趋势。 详情参见个人博客介绍:https://blog.csdn.net/yyz_1987/article/details/133383638?spm=1001.2014.3001.5502
资源推荐
资源详情
资源评论























收起资源包目录









































































共 66 条
- 1

特立独行的猫a
- 粉丝: 12w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 如何将Labelme格式转换为COCO格式
- 基于深度学习的校园垃圾分类识别系统设计与实现.zip
- AP3010DN-V2-FAT-V200R019C00SPC910.zip 胖AP镜像AP3010DN-V2
- FileZilla-3.68.1 免安装,解压可用
- elasticsearch-8.17.3-windows-x86-64
- mp-weixin众包1.0.zip
- 实现.NET程序对多版本DLL文件的兼容
- 【电子设计竞赛】2023年全国大学生电子设计大赛I题解析:气垫悬浮车关键技术与实现
- (2025)初级社会工作者综合能力试题及答案.docx
- (2025)初级社会工作者综合能力试题与答案.docx
- (2025)初中信息技术等级考试择题及答案.docx
- (2025)初中信息技术等级考试择题与答案.docx
- (2025)村居后备干部考试必备题库(含答案).docx
- (2025)大力弘扬教育家精神教师心得体会(2篇).docx
- (2025)大力弘扬教育家精神教师心得体会.docx
- (2025)大力弘扬教育家精神心得体会(3篇) .docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
- 3
- 4
- 5
- 6
前往页