modify ui to render

This commit is contained in:
brige 2024-09-25 14:37:15 +08:00
parent 5af8ba1878
commit 2127982650
2 changed files with 30 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import logging
import multiprocessing as mp import multiprocessing as mp
import platform, subprocess import platform, subprocess
import queue import queue
import threading
import time import time
@ -268,7 +269,6 @@ def datagen_signal(frame, mel, face_det_results):
return img_batch, mel_batch, frame_batch, coords_batch return img_batch, mel_batch, frame_batch, coords_batch
if len(img_batch) > 0: if len(img_batch) > 0:
img_batch, mel_batch = np.asarray(img_batch), np.asarray(mel_batch) img_batch, mel_batch = np.asarray(img_batch), np.asarray(mel_batch)
img_masked = img_batch.copy() img_masked = img_batch.copy()
@ -280,7 +280,6 @@ def datagen_signal(frame, mel, face_det_results):
return img_batch, mel_batch, frame_batch, coords_batch return img_batch, mel_batch, frame_batch, coords_batch
class Human: class Human:
def __init__(self): def __init__(self):
self._fps = 25 # 20 ms per frame self._fps = 25 # 20 ms per frame
@ -296,7 +295,10 @@ class Human:
# self._tts = TTSBase(self) # self._tts = TTSBase(self)
self.mel_chunks_queue_ = Queue() self.mel_chunks_queue_ = Queue()
self.test() self._test_image_queue = Queue()
self._thread = None
# self.test()
# face_images_path = r'./face/' # face_images_path = r'./face/'
# self._face_image_paths = utils.read_files_path(face_images_path) # self._face_image_paths = utils.read_files_path(face_images_path)
@ -370,6 +372,8 @@ class Human:
# name = "%04d" % j # name = "%04d" % j
# cv2.imwrite(f'temp/images/{j}.jpg', p) # cv2.imwrite(f'temp/images/{j}.jpg', p)
# j = j + 1 # j = j + 1
p = cv2.cvtColor(f, cv2.COLOR_BGR2RGB)
self._test_image_queue.put(p)
out.write(f) out.write(f)
out.release() out.release()
@ -403,11 +407,14 @@ class Human:
logging.info('human destroy') logging.info('human destroy')
def read(self, txt): def read(self, txt):
if self._tts is None: # if self._tts is None:
logging.warning('tts is none') # logging.warning('tts is none')
return # return
self._tts.push_txt(txt) if self._thread is None:
self._thread = threading.Thread(target=self.test)
self._thread.start()
# self._tts.push_txt(txt)
def push_audio_chunk(self, audio_chunk): def push_audio_chunk(self, audio_chunk):
self._chunk_2_mal.push_chunk(audio_chunk) self._chunk_2_mal.push_chunk(audio_chunk)
@ -422,7 +429,8 @@ class Human:
def render(self): def render(self):
try: try:
img, aud = self._res_frame_queue.get(block=True, timeout=.3) # img, aud = self._res_frame_queue.get(block=True, timeout=.3)
img = self._test_image_queue.get(block=True, timeout=.3)
except queue.Empty: except queue.Empty:
# print('render queue.Empty:') # print('render queue.Empty:')
return None return None

17
ui.py
View File

@ -5,6 +5,7 @@ from logging import handlers
import tkinter import tkinter
import tkinter.messagebox import tkinter.messagebox
import customtkinter import customtkinter
import cv2
import requests import requests
from PIL import Image, ImageTk from PIL import Image, ImageTk
@ -65,19 +66,29 @@ class App(customtkinter.CTk):
self.after(100, self._render) self.after(100, self._render)
return return
iheight, iwidth = image.shape[0], image.shape[1]
width = self.winfo_width()
height = self.winfo_height()
if iheight / iwidth >= width / height:
image = cv2.resize(image, (int(width), int(iheight * width / iwidth)))
else:
image = cv2.resize(image, (int(iwidth * height / iheight), int(height)), interpolation=cv2.INTER_AREA)
# image = cv2.resize(image, (int(width), int(height)), interpolation=cv2.INTER_AREA)
# image = cv2.resize(image, (int(width), int(height)), interpolation=cv2.INTER_AREA)
img = Image.fromarray(image) img = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=img) imgtk = ImageTk.PhotoImage(image=img)
self._canvas.delete("all") self._canvas.delete("all")
self._canvas.imgtk = imgtk self._canvas.imgtk = imgtk
width = self.winfo_width() * 0.5 width = self.winfo_width() * 0.5
height = self.winfo_height() * 0.5 height = self.winfo_height() * 0.5
self._canvas.create_image(width, height, anchor=customtkinter.CENTER, image=imgtk) self._canvas.create_image(width, height, anchor=customtkinter.CENTER, image=imgtk)
self._canvas.update() self._canvas.update()
self.after(30, self._render) self.after(60, self._render)
def request_tts(self): def request_tts(self):
content = self.entry.get() content = self.entry.get()