Skip to content

Commit

Permalink
Update how scale down is achieved
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodruiz committed Oct 25, 2024
1 parent ed76101 commit 67d85ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
38 changes: 20 additions & 18 deletions npu/lib/applications/videoapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class VideoApplication:
Pixel type for the input image, either pxtype.RGBA or pxtype.GRAY
pxtype_out : pxtype
Pixel type for the output image, either pxtype.RGBA or pxtype.GRAY
scale : int
Scale down displayed image by 'scale' factor
Returns
-------
Expand All @@ -134,7 +136,7 @@ class VideoApplication:
def __init__(self, filename, videosource: Union[int, str] = 0,
pxtype_in: pxtype = pxtype.RGBA,
pxtype_out: pxtype = pxtype.RGBA,
resize: int = 4):
scale: int = 1):

if not isinstance(pxtype_in, pxtype):
raise ValueError(f"pxtype_in ({pxtype_in}) must be of the type "
Expand All @@ -159,7 +161,7 @@ def __init__(self, filename, videosource: Union[int, str] = 0,
self.app = AppRunner(filename)
self.thread = None
self._disp = None
self._scaleoutput = resize
self._scaleoutput = scale

def _get_resolution(self, videosource):
if isinstance(videosource, int):
Expand Down Expand Up @@ -195,7 +197,7 @@ def start(self):
raise IOError("Cannot read from webcam. Check if other notebook "
"is using the webcam.")

self._disp = DisplayImage(self._scaleoutput)
self._disp = DisplayImage()
self.app.rtpwidgets(self.rtps)
self.thread = threading.Thread(target=self._process_video)
self.thread.start()
Expand Down Expand Up @@ -236,7 +238,7 @@ def _process_video(self):
else:
tmp = np.copy(bo_out)

self._disp.frame(tmp)
self._disp.frame(tmp, self._scaleoutput)

if self._disp.exit:
self._cap.release()
Expand Down Expand Up @@ -265,10 +267,10 @@ def _cleanup(self):
class _PipecleanerVideoProcessing(VideoApplication):
"""Pipecleaner Video processing
"""
def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):
self._get_resolution(videosource)
filename = _get_full_path(f'color_threshold_v1_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, resize=resize)
super().__init__(filename, videosource, scale=scale)

def _process_video(self):
bo_in = np.zeros(shape=(self.cam_h, self.cam_w, 4), dtype=np.uint8)
Expand All @@ -294,11 +296,11 @@ class ColorThresholdVideoProcessing(VideoApplication):
'thresholdValue3': _int_slider_b,
'thresholdType': _dropdown_thr}

def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = _get_full_path(f'color_threshold_v1_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, resize=resize)
super().__init__(filename, videosource, scale=scale)


class ScaledColorThresholdVideoProcessing(VideoApplication):
Expand All @@ -310,11 +312,11 @@ class ScaledColorThresholdVideoProcessing(VideoApplication):
'thresholdValue3': _int_slider_b,
'thresholdType': _dropdown_thr}

def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = _get_full_path(f'color_threshold_v2_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, resize=resize)
super().__init__(filename, videosource, scale=scale)


class _Filter2dOperator():
Expand Down Expand Up @@ -343,11 +345,11 @@ class EdgeDetectVideoProcessing(VideoApplication):
'name': 'threshold'}
}

def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = _get_full_path(f'edge_detect_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, resize=resize)
super().__init__(filename, videosource, scale=scale)

def start(self):
super().start()
Expand Down Expand Up @@ -397,11 +399,11 @@ class ColorDetectVideoProcessing(VideoApplication):
}
}

def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = _get_full_path(f'color_detect_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, resize=resize)
super().__init__(filename, videosource, scale=scale)

def start(self):
super().start()
Expand Down Expand Up @@ -429,22 +431,22 @@ def start(self):
class DenoiseTPVideoProcessing(VideoApplication):
"""Denoising Task Parallel Video processing
"""
def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = \
_get_full_path(f'denoise_task_parallel_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, pxtype_out=pxtype.GRAY,
resize=resize)
scale=scale)


class DenoiseDPVideoProcessing(VideoApplication):
"""Denoising Data Parallel Video processing
"""
def __init__(self, videosource: Union[int, str] = 0, resize: int = 1):
def __init__(self, videosource: Union[int, str] = 0, scale: int = 1):

self._get_resolution(videosource)
filename = \
_get_full_path(f'denoise_data_parallel_{self.cam_h}p.xclbin')
super().__init__(filename, videosource, pxtype_out=pxtype.GRAY,
resize=resize)
scale=scale)
10 changes: 5 additions & 5 deletions npu/utils/display_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ class DisplayImage:
set by the button widget and read by the display widget to step the video.
"""

def __init__(self, resize: int = 1):
def __init__(self):
""" returns a DisplayImage object """
self._image_widget = widgets.Image(format='jpeg')
self._display()
self._button_widget = widgets.Button(description='Stop')
self._button_widget.on_click(self._stop_video)
display(self._button_widget)
self.exit = False
self._resize = resize

def _display(self) -> None:
""" Creates the display widget """
Expand All @@ -40,11 +39,12 @@ def _stop_video(self, event=None) -> None:
self._button_widget.unobserve_all()
self._button_widget.disabled = True

def frame(self, value) -> None:
def frame(self, value, scale: int = 1) -> None:
""" Sets the current image on the widget """
pil_image = PIL.Image.fromarray(value)
rows, cols, _ = value.shape
pil_image = pil_image.resize((cols//self._resize, rows//self._resize))
if scale > 1:
rows, cols, _ = value.shape
pil_image = pil_image.resize((cols//scale, rows//scale))
b = BytesIO()
pil_image.save(b, format='jpeg')
self._image_widget.value = b.getvalue()

0 comments on commit 67d85ac

Please sign in to comment.