From c60808a7dace52ba998ad6274a9d254ac0f81bdf Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Tue, 13 Jun 2017 00:36:45 +0300 Subject: [PATCH] cleanup and few todo's --- setup.py | 10 ++++------ toxygen/callbacks.py | 30 ++++++++++++++---------------- toxygen/calls.py | 26 ++++++++++++++------------ 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/setup.py b/setup.py index 4794969..b738a61 100644 --- a/setup.py +++ b/setup.py @@ -11,19 +11,17 @@ version = program_version + '.0' MODULES = ['numpy'] if system() in ('Windows', 'Darwin'): - MODULES.extend['PyAudio', 'PyQt5'] - + MODULES.extend(['PyAudio', 'PyQt5']) else: try: import pyaudio except ImportError: - MODULES.append('PyAudio') + MODULES.append('PyAudio') # TODO: ? +DEP_LINKS = [] if system() == 'Windows': - DEPS_LINKS = [] # TODO: add opencv.whl -else: - DEPS_LINKS = [] + DEP_LINKS = [] # TODO: add opencv.whl class InstallScript(install): diff --git a/toxygen/callbacks.py b/toxygen/callbacks.py index f3cd71a..a2ee895 100644 --- a/toxygen/callbacks.py +++ b/toxygen/callbacks.py @@ -325,23 +325,21 @@ def callback_audio(toxav, friend_number, samples, audio_samples_per_channel, aud def video_receive_frame(toxav, friend_number, width, height, y, u, v, ystride, ustride, vstride, user_data): try: - Y = abs(max(width, abs(ystride))) - U = abs(max(width//2, abs(ustride))) - V = abs(max(width//2, abs(vstride))) - y = np.asarray(y[:Y * height], dtype=np.uint8).reshape(height, Y) - u = np.asarray(u[:U * height // 2], dtype=np.uint8).reshape(height // 2, U) - v = np.asarray(v[:V * height // 2], dtype=np.uint8).reshape(height // 2, V) + y_size = abs(max(width, abs(ystride))) + u_size = abs(max(width//2, abs(ustride))) + v_size = abs(max(width//2, abs(vstride))) + y = np.asarray(y[:y_size * height], dtype=np.uint8).reshape(height, y_size) + u = np.asarray(u[:u_size * height // 2], dtype=np.uint8).reshape(height // 2, u_size) + v = np.asarray(v[:v_size * height // 2], dtype=np.uint8).reshape(height // 2, v_size) frame = np.zeros((int(height * 1.5), width), dtype=np.uint8) - - frame[:height,:] = y[:,:width] - #tmp, tmp2 = u[::2,:width], frame[height:height * 5 // 4, :width // 2] - #print(tmp.shape, tmp2.shape - frame[height:height * 5 // 4, :width // 2] = u[:140:2,:width // 2] - frame[height:height * 5 // 4, width // 2:] = u[1:140:2,:width // 2] - - frame[height * 5 // 4 + 1:, :width // 2] = v[:140:2,:width // 2] - frame[height * 5 // 4 + 1:, width // 2:] = v[1:140:2,:width // 2] - + + frame[:height, :] = y[:, :width] + frame[height:height * 5 // 4, :width // 2] = u[:140:2, :width // 2] # TODO: remove hardcoded values + frame[height:height * 5 // 4, width // 2:] = u[1:140:2, :width // 2] + + frame[height * 5 // 4 + 1:, :width // 2] = v[:140:2, :width // 2] + frame[height * 5 // 4 + 1:, width // 2:] = v[1:140:2, :width // 2] + frame = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_I420) invoke_in_main_thread(cv2.imshow, str(friend_number), frame) diff --git a/toxygen/calls.py b/toxygen/calls.py index a771470..5c6ead4 100644 --- a/toxygen/calls.py +++ b/toxygen/calls.py @@ -7,6 +7,7 @@ import cv2 import itertools import numpy as np # TODO: play sound until outgoing call will be started or cancelled and add timeout +# TODO: rewrite logic class Call: @@ -205,30 +206,31 @@ class AV: height, width, channels = frame.shape for friend_num in self._calls: if self._calls[friend_num].video: - try: # TODO: bgr => yuv + try: y, u, v = convert_bgr_to_yuv(frame) self._toxav.video_send_frame(friend_num, width, height, y, u, v) except Exception as e: - print('1', e) + print(e) except Exception as e: - print('2', e) + print(e) time.sleep(0.01) -def convert_bgr_to_yuv(frame): +def convert_bgr_to_yuv(frame): # TODO: remove hardcoded values and add docs frame = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV_I420) - - y = frame[:480,:].tolist() + + y = frame[:480, :].tolist() y = list(itertools.chain.from_iterable(y)) - + u = np.zeros((240, 320), dtype=np.int) - u[::2,:] = frame[480:600, :320] - u[1::2,:] = frame[480:600, 320:] + u[::2, :] = frame[480:600, :320] + u[1::2, :] = frame[480:600, 320:] u = list(itertools.chain.from_iterable(u)) - + v = np.zeros((240, 320), dtype=np.int) - v[::2,:] = frame[600:, :320] - v[1::2,:] = frame[600:, 320:] + v[::2, :] = frame[600:, :320] + v[1::2, :] = frame[600:, 320:] v = list(itertools.chain.from_iterable(v)) + return bytes(y), bytes(u), bytes(v)