[WIP] adding typing and linting to visualizer code (backend python files) (#189)

* first round adding typing and linting to server files

* continued...

* added new formatting conventions

* committing current progress...

* finished with geometry

* homerun :')
This commit is contained in:
evonneng authored and GitHub committed 2022-07-22 16:38:58 -07:00
1 parent 6cfccab336
commit a78394c49d
10 files changed
+778 -278

No files matched your search

+23 -15
View File
@@ -12,19 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import queue
"""Video Stream objects for WebRTC"""
import math
import cv2
import numpy
import numpy as np
from aiortc import VideoStreamTrack
from av import VideoFrame
class FlagVideoStreamTrack(VideoStreamTrack):
"""
A video track that returns an animated flag.
A video track that returns an animated flag. For debugging purposes :')
"""
def __init__(self):
@@ -33,7 +33,7 @@ class FlagVideoStreamTrack(VideoStreamTrack):
height, width = 480, 640
# generate flag
data_bgr = numpy.hstack(
data_bgr = np.hstack(
[
self._create_rectangle(width=213, height=480, color=(255, 0, 0)), # blue
self._create_rectangle(width=214, height=480, color=(255, 255, 255)), # white
@@ -42,19 +42,19 @@ class FlagVideoStreamTrack(VideoStreamTrack):
)
# shrink and center it
M = numpy.float32([[0.5, 0, width / 4], [0, 0.5, height / 4]])
M = np.float32([[0.5, 0, width / 4], [0, 0.5, height / 4]])
data_bgr = cv2.warpAffine(data_bgr, M, (width, height))
# compute animation
omega = 2 * math.pi / height
id_x = numpy.tile(numpy.array(range(width), dtype=numpy.float32), (height, 1))
id_y = numpy.tile(numpy.array(range(height), dtype=numpy.float32), (width, 1)).transpose()
id_x = np.tile(np.array(range(width), dtype=np.float32), (height, 1))
id_y = np.tile(np.array(range(height), dtype=np.float32), (width, 1)).transpose()
self.frames = []
for k in range(30):
phase = 2 * k * math.pi / 30
map_x = id_x + 10 * numpy.cos(omega * id_x + phase)
map_y = id_y + 10 * numpy.sin(omega * id_x + phase)
map_x = id_x + 10 * np.cos(omega * id_x + phase)
map_y = id_y + 10 * np.sin(omega * id_x + phase)
frame = cv2.remap(data_bgr, map_x, map_y, cv2.INTER_LINEAR)
self.frames.append(VideoFrame.from_ndarray(frame, format="bgr24"))
@@ -67,24 +67,32 @@ class FlagVideoStreamTrack(VideoStreamTrack):
self.counter += 1
return frame
def _create_rectangle(self, width, height, color):
data_bgr = numpy.zeros((height, width, 3), numpy.uint8)
@staticmethod
def _create_rectangle(width, height, color):
data_bgr = np.zeros((height, width, 3), np.uint8)
data_bgr[:, :] = color
return data_bgr
class SingleFrameStreamTrack(VideoStreamTrack):
"""Single Frame stream class: pushes single frames to a stream"""
def __init__(self):
super().__init__()
self.background_frame = numpy.zeros((480, 640, 3), dtype="uint8") * 255
self.background_frame = np.zeros((480, 640, 3), dtype="uint8") * 255
self.frame = None
self.put_frame(self.background_frame)
def put_frame(self, frame):
def put_frame(self, frame: np.ndarray) -> None:
"""Sets the current viewing frame
Args:
frame: image to be viewed
"""
self.frame = VideoFrame.from_ndarray(frame)
async def recv(self):
"""Async method to grab and wait on frame"""
pts, time_base = await self.next_timestamp()
frame = self.frame