This commit is contained in:
Benjamín 2021-09-08 18:58:59 +02:00
parent be4d8e0ff9
commit 24b8fa0a58
9 changed files with 163 additions and 3 deletions

@ -101,6 +101,20 @@ class Multiscene(Scene):
return i_objects
def start_bgt_objects(self):
for object_ in self.bgt_objects:
object_.start_bg_activity()
for subscene in self.subscenes:
subscene.start_bg_activity()
def stop_bgt_objects(self):
for object_ in self.bgt_objects:
object_.stop_bg_activity()
for subscene in self.subscenes:
subscene.stop_bg_activity()
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):

@ -17,6 +17,7 @@ class Scene(Screen):
self.d_objects = [] # dynamic Objects
self.c_objects = [] # controllable Objects
self.i_objects = [] # interactive Objects
self.bgt_objects = [] # background threaded Objects
self.position = a([0, 0])
self.multiscene = None
@ -58,6 +59,14 @@ class Scene(Screen):
def get_i_objects(self):
return [[self.i_objects, self.mouse_pos]]
def start_bgt_objects(self):
for object_ in self.bgt_objects:
object_.start_bg_activity()
def stop_bgt_objects(self):
for object_ in self.bgt_objects:
object_.stop_bg_activity()
def sort_objects(self, *args):
for object_ in args:
for object_tag in object_.object_type.split("_"):
@ -71,6 +80,8 @@ class Scene(Screen):
self.c_objects.append(object_)
elif object_tag == "i":
self.i_objects.append(object_)
elif object_tag == "bgt":
self.bgt_objects.append(object_)
elif object_tag in ["scene", "multiscene"]:
if self.object_type == "multiscene":
self.subscenes.append(object_)

@ -0,0 +1,89 @@
import copy
import pygame
import time
import numpy as np
from numpy import array as a
from threading import Thread
class RefreshingImage:
object_type = "nr_bgt"
def __init__(self, center, scalar, path, scene, rps=24):
self.center = center
self.scalar = scalar
self.path = path
self.rps = rps # refreshes per second
self.scene = scene
self.pd_ = self.scene.pd_
self.image = pygame.image.load(self.path).convert()
self.image = pygame.transform.scale(self.image,
self.to_ints((a(self.image.get_size()) * self.scalar).tolist()))
self.size = a(self.image.get_size(), dtype=np.float64)
self.sides_ratio = self.size[0] / self.size[1]
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
self.updater = None
self.updater_alive = True
self.start_bg_activity()
def start_bg_activity(self):
self.updater_alive = True
def update(self_):
dt = 1 / self_.rps
while self_.updater_alive:
if self_.pd_ == self_.scene.pd_:
try:
self_.image = pygame.image.load(self_.path).convert()
except:
pass
else:
self_.image = pygame.transform.scale(self_.image,
self_.to_ints((a(self_.image.get_size())
* self_.scalar).tolist()))
self_.rescaled_image = pygame.transform.scale(self_.image,
self_.to_ints((self_.size * self_.pd_).tolist()))
self_.scene.s.blit(self_.rescaled_image, self_.scene.matrix @ self_.center)
pygame.display.update()
time.sleep(dt)
self.updater = Thread(target=update, args=(self,))
self.updater.start()
def stop_bg_activity(self):
self.updater_alive = False
def blit(self):
if self.pd_ != self.scene.pd_:
self.pd_ = self.scene.pd_
self.rescaled_size = copy.copy(self.size)
if self.scene.matrix[1, 1] - self.scene.matrix[0, 0] / self.sides_ratio >= 0:
self.rescaled_size *= self.scene.matrix[0, 0]
else:
self.rescaled_size *= self.scene.matrix[1, 1]
self.rescaled_image = pygame.transform.scale(self.image, self.to_ints((self.size * self.pd_).tolist()))
self.scene.s.blit(self.rescaled_image, self.scene.matrix @ self.center)
@staticmethod
def to_ints(iterable):
for i in range(len(iterable)):
iterable[i] = int(iterable[i])
return iterable

16
UI/Scenes/VideoScene.py Normal file

@ -0,0 +1,16 @@
from UI.Objects.Scene import Scene
from numpy import array as a
from UI.Objects.nr_objects.background_threaded_objects.RefreshingImage import RefreshingImage
class VideoScene(Scene):
def __init__(self, screen_size, bg=(60, 60, 60)):
s__size = [1200, 800]
super().__init__(*s__size, screen_size, bg)
ri0 = RefreshingImage(a([100, 100]), 1.2, "video_capture.png", self)
self.sort_objects(ri0)

@ -5,13 +5,13 @@ from UI.Scenes.BasicMultiscene import BasicMultiscene
from UI.Scenes.MultisceneInMultiscene import MultisceneInMultiscene
from UI.Scenes.RopeScene import RopeScene
from UI.Scenes.RopeMultiscene import RopeMultiscene
from UI.Scenes.VideoScene import VideoScene
from numpy import array as a
import time
screen_size = a([200, 500])
scene = RopeScene(screen_size)
scene = VideoScene(screen_size)
mouse_pos = [0, 0]
clicked = False
@ -29,6 +29,9 @@ for i in range(400):
elif i < 250:
screen_size[1] += 2
else:
pass
screen_size -= 4
time.sleep(0.01)
scene.stop_bgt_objects()

BIN
UI/video_capture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB

19
UI/video_example.py Normal file

@ -0,0 +1,19 @@
from UI.Scenes.VideoScene import VideoScene
from numpy import array as a
import time
screen_size = a([1200, 800])
scene = VideoScene(screen_size)
mouse_pos = [0, 0]
clicked = False
scene.update()
try:
time.sleep(20)
except KeyboardInterrupt:
pass
scene.stop_bgt_objects()

@ -1,9 +1,11 @@
import math
import os
import cv2
import mediapipe as mp
import pyautogui
os.chdir("..")
class Hands_AI:
def __init__(self):
@ -79,6 +81,12 @@ class Hands_AI:
else:
self.x, self.y = None, None
cv2.imshow("Image", img)
cv2.imwrite("UI/video_capture_.png", img)
try:
os.remove("UI/video_capture.png")
os.rename("UI/video_capture_.png", "UI/video_capture.png")
except:
pass
cv2.waitKey(1)

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 KiB