Skip to content

Commit

Permalink
Scale down image (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodruiz authored Oct 25, 2024
1 parent 691bea8 commit 3b2f3ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
42 changes: 24 additions & 18 deletions npu/lib/applications/videoapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _set_webcam_resolution(cap, height: int, width: int) -> bool:
_ = cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
_ = cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
if width == int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) and \
height == int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)):
height == int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)):
return True
return False

Expand All @@ -101,7 +101,7 @@ def _set_supported_webcam_resolution(cap) -> bool:


class VideoApplication:
"""Wrapper class that allows to feed and visualize video stream from the NPU
"""Class that allows to feed and visualize a video stream from the NPU
You must pass an xclbin and optionally the pixel type for the input and
output images.
Expand All @@ -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 @@ -133,7 +135,8 @@ class VideoApplication:

def __init__(self, filename, videosource: Union[int, str] = 0,
pxtype_in: pxtype = pxtype.RGBA,
pxtype_out: pxtype = pxtype.RGBA):
pxtype_out: pxtype = pxtype.RGBA,
scale: int = 1):

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

def _get_resolution(self, videosource):
if isinstance(videosource, int):
Expand Down Expand Up @@ -234,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 @@ -263,10 +267,10 @@ def _cleanup(self):
class _PipecleanerVideoProcessing(VideoApplication):
"""Pipecleaner Video processing
"""
def __init__(self, videosource: Union[int, str] = 0):
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)
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 @@ -292,11 +296,11 @@ class ColorThresholdVideoProcessing(VideoApplication):
'thresholdValue3': _int_slider_b,
'thresholdType': _dropdown_thr}

def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, scale=scale)


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

def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, scale=scale)


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

def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, scale=scale)

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

def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, scale=scale)

def start(self):
super().start()
Expand Down Expand Up @@ -427,20 +431,22 @@ def start(self):
class DenoiseTPVideoProcessing(VideoApplication):
"""Denoising Task Parallel Video processing
"""
def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, pxtype_out=pxtype.GRAY,
scale=scale)


class DenoiseDPVideoProcessing(VideoApplication):
"""Denoising Data Parallel Video processing
"""
def __init__(self, videosource: Union[int, str] = 0):
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)
super().__init__(filename, videosource, pxtype_out=pxtype.GRAY,
scale=scale)
9 changes: 6 additions & 3 deletions npu/utils/display_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DisplayImage:
_button_widget : widgets.Button
reference to the button widget used to stop the video feed.
exit : bool
set by the button widget and read by the display widget to step the video.
set by the button widget to stop the video feed
"""

def __init__(self):
Expand All @@ -34,14 +34,17 @@ def _display(self) -> None:
display(self._image_widget)

def _stop_video(self, event=None) -> None:
""" On button press this function is called to set the exit attribute and stop the video """
""" On button press this function stops the video feed"""
self.exit = True
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)
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 3b2f3ec

Please sign in to comment.