👍️ [Update] pythonのメイン処理部分を移動/webui_mainloop.pyをビルドできるように修正

This commit is contained in:
misyaguziya
2024-07-27 01:30:36 +09:00
parent 7ce3bc9be9
commit 1be04cb571
21 changed files with 46 additions and 28 deletions

View File

@@ -0,0 +1,304 @@
import os
import ctypes
import time
from psutil import process_iter
from threading import Thread
import openvr
import numpy as np
from PIL import Image
try:
from . import overlay_utils as utils
except ImportError:
import overlay_utils as utils
def mat34Id(array):
arr = openvr.HmdMatrix34_t()
for i in range(3):
for j in range(4):
arr[i][j] = array[i][j]
return arr
def getBaseMatrix(x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation):
arr = np.zeros((3, 4))
rot = utils.euler_to_rotation_matrix((x_rotation, y_rotation, z_rotation))
for i in range(3):
for j in range(3):
arr[i][j] = rot[i][j]
arr[0][3] = x_pos * z_pos
arr[1][3] = y_pos * z_pos
arr[2][3] = - z_pos
return arr
def getHMDBaseMatrix():
x_pos = 0.0
y_pos = -0.4
z_pos = 1.0
x_rotation = 0.0
y_rotation = 0.0
z_rotation = 0.0
arr = getBaseMatrix(x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation)
return arr
def getLeftHandBaseMatrix():
x_pos = 0.0
y_pos = -0.06
z_pos = -0.14
x_rotation = -62.0
y_rotation = 154.0
z_rotation = 71.0
arr = getBaseMatrix(x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation)
return arr
def getRightHandBaseMatrix():
x_pos = 0.0
y_pos = -0.06
z_pos = -0.14
x_rotation = -62.0
y_rotation = -154.0
z_rotation = -71.0
arr = getBaseMatrix(x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation)
return arr
class Overlay:
def __init__(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, display_duration, fadeout_duration, opacity, ui_scaling):
self.initialized = False
settings = {
"color": [1, 1, 1],
"opacity": opacity,
"x_pos": x_pos,
"y_pos": y_pos,
"z_pos": z_pos,
"x_rotation": x_rotation,
"y_rotation": y_rotation,
"z_rotation": z_rotation,
"display_duration": display_duration,
"fadeout_duration": fadeout_duration,
"ui_scaling": ui_scaling,
}
self.settings = settings
self.system = None
self.overlay = None
self.handle = None
self.lastUpdate = time.monotonic()
self.thread_overlay = None
self.fadeRatio = 1
self.loop = True
def init(self):
try:
self.system = openvr.init(openvr.VRApplication_Background)
self.overlay = openvr.IVROverlay()
self.overlay_system = openvr.IVRSystem()
self.handle = self.overlay.createOverlay("Overlay_Speaker2log", "SOverlay_Speaker2log_UI")
self.overlay.showOverlay(self.handle)
self.initialized = True
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
self.updateColor(self.settings["color"])
self.updateOpacity(self.settings["opacity"])
self.updateUiScaling(self.settings["ui_scaling"])
self.updatePosition(
self.settings["x_pos"],
self.settings["y_pos"],
self.settings["z_pos"],
self.settings["x_rotation"],
self.settings["y_rotation"],
self.settings["z_rotation"],
)
except Exception as e:
print("Could not initialise OpenVR", e)
def updateImage(self, img):
if self.initialized is True:
width, height = img.size
img = img.tobytes()
img = (ctypes.c_char * len(img)).from_buffer_copy(img)
self.overlay.setOverlayRaw(self.handle, img, width, height, 4)
self.updateOpacity(self.settings["opacity"])
self.lastUpdate = time.monotonic()
def clearImage(self):
if self.initialized is True:
self.updateImage(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
def updateColor(self, col):
"""
col is a 3-tuple representing (r, g, b)
"""
self.settings["color"] = col
if self.initialized is True:
r, g, b = self.settings["color"]
self.overlay.setOverlayColor(self.handle, r, g, b)
def updateOpacity(self, opacity, with_fade=False):
self.settings["opacity"] = opacity
if self.initialized is True:
if with_fade is True:
if self.fadeRatio > 0:
self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings["opacity"])
else:
self.overlay.setOverlayAlpha(self.handle, self.settings["opacity"])
def updateUiScaling(self, ui_scaling):
self.settings["ui_scaling"] = ui_scaling
if self.initialized is True:
self.overlay.setOverlayWidthInMeters(self.handle, self.settings["ui_scaling"])
def updatePosition(self, x_pos, y_pos, z_pos, x_rotation, y_rotation, z_rotation, tracker="HMD"):
"""
x_pos, y_pos, z_pos are floats representing the position of overlay
x_rotation, y_rotation, z_rotation are floats representing the rotation of overlay
tracker is a string representing the tracker to use ("HMD", "LeftHand", "RightHand")
"""
self.settings["x_pos"] = x_pos
self.settings["y_pos"] = y_pos
self.settings["z_pos"] = z_pos
self.settings["x_rotation"] = x_rotation
self.settings["y_rotation"] = y_rotation
self.settings["z_rotation"] = z_rotation
match tracker:
case "HMD":
base_matrix = getHMDBaseMatrix()
trackerIndex = openvr.k_unTrackedDeviceIndex_Hmd
case "LeftHand":
base_matrix = getLeftHandBaseMatrix()
trackerIndex = self.overlay_system.getTrackedDeviceIndexForControllerRole(openvr.TrackedControllerRole_LeftHand)
case "RightHand":
base_matrix = getRightHandBaseMatrix()
trackerIndex = self.overlay_system.getTrackedDeviceIndexForControllerRole(openvr.TrackedControllerRole_RightHand)
case _:
base_matrix = getHMDBaseMatrix()
trackerIndex = openvr.k_unTrackedDeviceIndex_Hmd
translation = (self.settings["x_pos"], self.settings["y_pos"], - self.settings["z_pos"])
rotation = (self.settings["x_rotation"], self.settings["y_rotation"], self.settings["z_rotation"])
transform = utils.transform_matrix(base_matrix, translation, rotation)
self.transform = mat34Id(transform)
if self.initialized is True:
self.overlay.setOverlayTransformTrackedDeviceRelative(
self.handle,
trackerIndex,
self.transform
)
def updateDisplayDuration(self, display_duration):
self.settings["display_duration"] = display_duration
def updateFadeoutDuration(self, fadeout_duration):
self.settings["fadeout_duration"] = fadeout_duration
def checkActive(self):
try:
if self.system is not None and self.initialized is True:
new_event = openvr.VREvent_t()
while self.system.pollNextEvent(new_event):
if new_event.eventType == openvr.VREvent_Quit:
return False
return True
except Exception as e:
print("Could not check SteamVR running")
print(e)
return False
def evaluateOpacityFade(self, lastUpdate, currentTime):
if (currentTime - lastUpdate) > self.settings["display_duration"]:
timeThroughInterval = currentTime - lastUpdate - self.settings["display_duration"]
self.fadeRatio = 1 - timeThroughInterval / self.settings["fadeout_duration"]
if self.fadeRatio < 0:
self.fadeRatio = 0
self.overlay.setOverlayAlpha(self.handle, self.fadeRatio * self.settings["opacity"])
def update(self):
currTime = time.monotonic()
if self.settings["fadeout_duration"] != 0:
self.evaluateOpacityFade(self.lastUpdate, currTime)
else:
self.updateOpacity(self.settings["opacity"])
def mainloop(self):
self.loop = True
while self.checkActive() is True and self.loop is True:
startTime = time.monotonic()
self.update()
sleepTime = (1 / 16) - (time.monotonic() - startTime)
if sleepTime > 0:
time.sleep(sleepTime)
def main(self):
self.init()
if self.initialized is True:
self.mainloop()
def startOverlay(self):
self.thread_overlay = Thread(target=self.main)
self.thread_overlay.daemon = True
self.thread_overlay.start()
def shutdownOverlay(self):
if isinstance(self.thread_overlay, Thread):
self.loop = False
self.thread_overlay.join()
self.thread_overlay = None
if isinstance(self.overlay, openvr.IVROverlay) and isinstance(self.handle, int):
self.overlay.destroyOverlay(self.handle)
self.overlay = None
if isinstance(self.system, openvr.IVRSystem):
openvr.shutdown()
self.system = None
self.initialized = False
@staticmethod
def checkSteamvrRunning() -> bool:
_proc_name = "vrmonitor.exe" if os.name == "nt" else "vrmonitor"
return _proc_name in (p.name() for p in process_iter())
if __name__ == "__main__":
# from overlay_image import OverlayImage
# overlay_image = OverlayImage()
# overlay = Overlay(0, 0, 1, 1, 0, 1, 1)
# overlay.startOverlay()
# time.sleep(1)
# # Example usage
# img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese")
# overlay.updateImage(img)
# time.sleep(100000)
# for i in range(100):
# print(i)
# overlay = Overlay(0, 0, 1, 1, 1, 1, 1)
# overlay.startOverlay()
# time.sleep(1)
# # Example usage
# img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese", ui_type="sakura")
# overlay.updateImage(img)
# time.sleep(0.5)
# img = overlay_image.createOverlayImageShort("こんにちは、世界!さようなら", "Japanese", "Hello,World!Goodbye", "Japanese")
# overlay.updateImage(img)
# time.sleep(0.5)
# overlay.shutdownOverlay()
x_pos = 0
y_pos = 0
z_pos = 0
x_rotation = 0
y_rotation = 0
z_rotation = 0
base_matrix = getLeftHandBaseMatrix()
translation = (x_pos * z_pos, y_pos * z_pos, z_pos)
rotation = (x_rotation, y_rotation, z_rotation)
transform = utils.transform_matrix(base_matrix, translation, rotation)
transform = mat34Id(transform)
print(transform)

View File

@@ -0,0 +1,231 @@
from os import path as os_path
# from datetime import datetime
from typing import Tuple
from PIL import Image, ImageDraw, ImageFont
class OverlayImage:
# TEXT_COLOR_LARGE = (223, 223, 223)
# TEXT_COLOR_SMALL = (190, 190, 190)
# TEXT_COLOR_SEND = (70, 161, 146)
# TEXT_COLOR_RECEIVE = (220, 20, 60)
# TEXT_COLOR_TIME = (120, 120, 120)
# FONT_SIZE_LARGE = HEIGHT
# FONT_SIZE_SMALL = int(FONT_SIZE_LARGE * 2 / 3)
LANGUAGES = {
"Japanese": "NotoSansJP-Regular",
"Korean": "NotoSansKR-Regular",
"Chinese Simplified": "NotoSansSC-Regular",
"Chinese Traditional": "NotoSansTC-Regular",
}
def __init__(self):
pass
@staticmethod
def concatenateImagesVertically(img1: Image, img2: Image) -> Image:
dst = Image.new("RGBA", (img1.width, img1.height + img2.height))
dst.paste(img1, (0, 0))
dst.paste(img2, (0, img1.height))
return dst
@staticmethod
def addImageMargin(image: Image, top: int, right: int, bottom: int, left: int, color: Tuple[int, int, int, int]) -> Image:
width, height = image.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(image.mode, (new_width, new_height), color)
result.paste(image, (left, top))
return result
# def create_textimage(self, message_type, size, text, language):
# font_size = self.FONT_SIZE_LARGE if size == "large" else self.FONT_SIZE_SMALL
# text_color = self.TEXT_COLOR_LARGE if size == "large" else self.TEXT_COLOR_SMALL
# anchor = "lm" if message_type == "receive" else "rm"
# text_x = 0 if message_type == "receive" else self.WIDTH
# align = "left" if message_type == "receive" else "right"
# font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular")
# img = Image.new("RGBA", (0, 0), (0, 0, 0, 0))
# draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "fonts", f"{font_family}.ttf"), font_size)
# # font = ImageFont.truetype(os_path.join("./fonts", f"{font_family}.ttf"), font_size)
# text_width = draw.textlength(text, font)
# character_width = text_width // len(text)
# character_line_num = int(self.WIDTH // character_width)
# if len(text) > character_line_num:
# text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)])
# n_num = len(text.split("\n")) - 1
# text_height = int(font_size*(n_num+2))
# img = Image.new("RGBA", (self.WIDTH, text_height), (0, 0, 0, 0))
# draw = ImageDraw.Draw(img)
# text_y = text_height // 2
# draw.multiline_text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font, align=align)
# return img
# def create_textimage_message_type(self, message_type):
# anchor = "lm" if message_type == "receive" else "rm"
# text = "Receive" if message_type == "receive" else "Send"
# text_color = self.TEXT_COLOR_RECEIVE if message_type == "receive" else self.TEXT_COLOR_SEND
# text_color_time = self.TEXT_COLOR_TIME
# now = datetime.now()
# formatted_time = now.strftime("%H:%M")
# font_size = self.FONT_SIZE_SMALL
# img = Image.new("RGBA", (0, 0), (0, 0, 0, 0))
# draw = ImageDraw.Draw(img)
# font = ImageFont.truetype(os_path.join(os_path.dirname(__file__), "fonts", "NotoSansJP-Regular.ttf"), font_size)
# # font = ImageFont.truetype(os_path.join("./fonts", "NotoSansJP-Regular.ttf"), font_size)
# text_height = font_size*2
# text_width = draw.textlength(formatted_time, font)
# character_width = text_width // len(formatted_time)
# img = Image.new("RGBA", (self.WIDTH, text_height), (0, 0, 0, 0))
# draw = ImageDraw.Draw(img)
# text_y = text_height // 2
# text_time_x = 0 if message_type == "receive" else self.WIDTH - (text_width + character_width)
# text_x = (text_width + character_width) if message_type == "receive" else self.WIDTH
# draw.text((text_time_x, text_y), formatted_time, text_color_time, anchor=anchor, stroke_width=0, font=font)
# draw.text((text_x, text_y), text, text_color, anchor=anchor, stroke_width=0, font=font)
# return img
# def create_textbox(self, message_type, message, your_language, translation, target_language):
# message_type_img = self.create_textimage_message_type(message_type)
# if len(translation) > 0 and target_language is not None:
# img = self.create_textimage(message_type, "small", message, your_language)
# translation_img = self.create_textimage(message_type, "large",translation, target_language)
# img = self.concatenateImagesVertically(img, translation_img)
# else:
# img = self.create_textimage(message_type, "large", message, your_language)
# return self.concatenateImagesVertically(message_type_img, img)
# def create_overlay_image_long(self, message_type, message, your_language, translation="", target_language=None):
# if len(self.log_data) > 10:
# self.log_data.pop(0)
# self.log_data.append(
# {
# "message_type":message_type,
# "message":message,
# "your_language":your_language,
# "translation":translation,
# "target_language":target_language,
# }
# )
# imgs = []
# for log in self.log_data:
# message_type = log["message_type"]
# message = log["message"]
# your_language = log["your_language"]
# translation = log["translation"]
# target_language = log["target_language"]
# img = self.create_textbox(message_type, message, your_language, translation, target_language)
# imgs.append(img)
# img = imgs[0]
# for i in imgs[1:]:
# img = self.concatenateImagesVertically(img, i)
# img = self.addImageMargin(img, 0, 20, 0, 20, (0, 0, 0, 0))
# width, height = img.size
# background = Image.new("RGBA", (width, height), (0, 0, 0, 0))
# draw = ImageDraw.Draw(background)
# draw.rounded_rectangle([(0, 0), (width, height)], radius=15, fill=self.BACKGROUND_COLOR, outline=self.BACKGROUND_OUTLINE_COLOR, width=5)
# img = Image.alpha_composite(background, img)
# return img
def getUiSize(self):
return {
"width": int(960*4),
"height": int(23*4),
"font_size": int(23*4),
}
def getUiColors(self, ui_type):
match ui_type:
case "default":
background_color = (41, 42, 45)
background_outline_color = (41, 42, 45)
text_color = (223, 223, 223)
case "sakura":
background_color = (225, 40, 30)
background_outline_color = (255, 255, 255)
text_color = (223, 223, 223)
return {
"background_color": background_color,
"background_outline_color": background_outline_color,
"text_color": text_color
}
def createDecorationImage(self, ui_type, image_size):
decoration_image = Image.new("RGBA", image_size, (0, 0, 0, 0))
match ui_type:
case "default":
pass
case "sakura":
margin = 7
alpha_ratio = 0.4
overlay_tl = Image.open(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "img", "overlay_tl_sakura.png"))
overlay_br = Image.open(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "img", "overlay_br_sakura.png"))
if overlay_tl.size[1] > image_size[1]:
overlay_tl = overlay_tl.resize((image_size[1]-margin, image_size[1]-margin))
if overlay_br.size[1] > image_size[1]:
overlay_br = overlay_br.resize((image_size[1]-margin, image_size[1]-margin))
alpha = overlay_tl.getchannel("A")
alpha = alpha.point(lambda x: x * alpha_ratio)
overlay_tl.putalpha(alpha)
alpha = overlay_br.getchannel("A")
alpha = alpha.point(lambda x: x * alpha_ratio)
overlay_br.putalpha(alpha)
decoration_image.paste(overlay_tl, (margin, margin))
decoration_image.paste(overlay_br, (image_size[0]-overlay_br.size[0]-margin, image_size[1]-overlay_br.size[1]-margin))
return decoration_image
def createTextboxShort(self, text, language, text_color, base_width, base_height, font_size):
font_family = self.LANGUAGES.get(language, "NotoSansJP-Regular")
img = Image.new("RGBA", (base_width, base_height), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(os_path.join(os_path.dirname(os_path.dirname(os_path.dirname(__file__))), "fonts", f"{font_family}.ttf"), font_size)
text_width = draw.textlength(text, font)
character_width = text_width // len(text)
character_line_num = int((base_width) // character_width) - 12
if len(text) > character_line_num:
text = "\n".join([text[i:i+character_line_num] for i in range(0, len(text), character_line_num)])
text_height = font_size * (len(text.split("\n")) + 1) + 20
img = Image.new("RGBA", (base_width, text_height), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
text_x = base_width // 2
text_y = text_height // 2
draw.text((text_x, text_y), text, text_color, anchor="mm", stroke_width=0, font=font, align="center")
return img
def createOverlayImageShort(self, message, your_language, translation="", target_language=None, ui_type="default"):
ui_size = self.getUiSize()
height = ui_size["height"]
width = ui_size["width"]
font_size = ui_size["font_size"]
ui_colors = self.getUiColors(ui_type)
text_color = ui_colors["text_color"]
background_color = ui_colors["background_color"]
background_outline_color = ui_colors["background_outline_color"]
img = self.createTextboxShort(message, your_language, text_color, width, height, font_size)
if len(translation) > 0 and target_language is not None:
translation_img = self.createTextboxShort(translation, target_language, text_color, width, height, font_size)
img = self.concatenateImagesVertically(img, translation_img)
background = Image.new("RGBA", img.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(background)
draw.rounded_rectangle([(0, 0), img.size], radius=30, fill=background_color, outline=background_outline_color, width=5)
decoration_image = self.createDecorationImage(ui_type, img.size)
background = Image.alpha_composite(background, decoration_image)
img = Image.alpha_composite(background, img)
return img

View File

@@ -0,0 +1,87 @@
import numpy as np
def toHomogeneous(matrix):
homogeneous_matrix = np.vstack([matrix, [0, 0, 0, 1]])
return homogeneous_matrix
# 移動行列を生成する関数
def calcTranslationMatrix(translation):
tx, ty, tz = translation
return np.array([
[1, 0, 0, tx],
[0, 1, 0, ty],
[0, 0, 1, tz],
[0, 0, 0, 1]
])
# X軸周りの回転行列を生成する関数
def calcRotationMatrixX(angle):
c = np.cos(np.pi/180*angle)
s = np.sin(np.pi/180*angle)
return np.array([
[1, 0, 0, 0],
[0, c, -s, 0],
[0, s, c, 0],
[0, 0, 0, 1]
])
# Y軸周りの回転行列を生成する関数
def calcRotationMatrixY(angle):
c = np.cos(np.pi/180*angle)
s = np.sin(np.pi/180*angle)
return np.array([
[c, 0, s, 0],
[0, 1, 0, 0],
[-s, 0, c, 0],
[0, 0, 0, 1]
])
# Z軸周りの回転行列を生成する関数
def calcRotationMatrixZ(angle):
c = np.cos(np.pi/180*angle)
s = np.sin(np.pi/180*angle)
return np.array([
[c, -s, 0, 0],
[s, c, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
# 3x4行列の座標を基準として回転や移動を行う関数
def transform_matrix(base_matrix, translation, rotation):
homogeneous_base_matrix = toHomogeneous(base_matrix)
translation_matrix = calcTranslationMatrix(translation)
rotation_matrix_x = calcRotationMatrixX(rotation[0])
rotation_matrix_y = calcRotationMatrixY(rotation[1])
rotation_matrix_z = calcRotationMatrixZ(rotation[2])
rotation_matrix = np.dot(rotation_matrix_z, np.dot(rotation_matrix_y, rotation_matrix_x))
transformation_matrix = translation_matrix.copy()
transformation_matrix[:3, :3] = rotation_matrix[:3, :3]
result_matrix = np.dot(homogeneous_base_matrix, transformation_matrix)
return result_matrix[:3, :]
def euler_to_rotation_matrix(angles):
phi = angles[0] * np.pi / 180
theta = angles[1] * np.pi / 180
psi = angles[2]* np.pi / 180
R_x = np.array([[1, 0, 0],
[0, np.cos(phi), -np.sin(phi)],
[0, np.sin(phi), np.cos(phi)]])
R_y = np.array([[np.cos(theta), 0, np.sin(theta)],
[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]])
R_z = np.array([[np.cos(psi), -np.sin(psi), 0],
[np.sin(psi), np.cos(psi), 0],
[0, 0, 1]])
return np.dot(R_z, np.dot(R_y, R_x))
if __name__ == "__main__":
base_matrix = np.array([
[1, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 1, 1]
])
translation = [1, 2, 3]
rotation = [0, 0, 90]
result_matrix = transform_matrix(base_matrix, translation, rotation)
print(result_matrix)