JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PySelfie.py
Go to the documentation of this file.
1 import pyjevois
2 if pyjevois.pro: import libjevoispro as jevois
3 else: import libjevois as jevois
4 import cv2
5 import numpy as np
6 import mediapipe as mp
7 
8 ## Selfie segmentation using MediaPipe
9 #
10 # Segment out face and upper body and show them on top of an image background, using MediaPipe in Python
11 #
12 # This code is derived from sample_selfie_segmentation.py at https://github.com/Kazuhito00/mediapipe-python-sample
13 #
14 # @author Laurent Itti
15 #
16 # @videomapping JVUI 0 0 30.0 CropScale=RGB24@512x288:YUYV 1920 1080 30.0 JeVois PySelfie
17 # @email itti\@usc.edu
18 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
19 # @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
20 # @mainurl http://jevois.org
21 # @supporturl http://jevois.org/doc
22 # @otherurl http://iLab.usc.edu
23 # @license GPL v3
24 # @distribution Unrestricted
25 # @restrictions None
26 # @ingroup modules
27 class PySelfie:
28  # ###################################################################################################
29  ## Constructor
30  def __init__(self):
31  # Parameters:
32  model_selection = 1 # which model to use internally: 0: 256x256, 1: 256x144
33  alpha = 150 # Transparency alpha values for processGUI, higher is less transparent
34  tidx = 1 # Class index of transparent foreground
35 
36  # Instantiate a JeVois Timer to measure our processing framerate:
37  self.timer = jevois.Timer("selfie", 30, jevois.LOG_DEBUG)
38 
39  # Instantiate mediapipe model:
40  self.mp_selfie_segmentation = mp.solutions.selfie_segmentation
41  self.selfie_segmentation = self.mp_selfie_segmentation.SelfieSegmentation(model_selection = model_selection)
42 
43  # Create a colormap for mask:
44  self.cmapRGBA = self.create_pascal_label_colormapRGBA(alpha, tidx)
45 
46  # ###################################################################################################
47  ## Process function with GUI output
48  def processGUI(self, inframe, helper):
49  # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
50  idle, winw, winh = helper.startFrame()
51 
52  # Draw full-resolution input frame from camera:
53  x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
54 
55  # Get the next camera image at processing resolution (may block until it is captured):
56  image = inframe.getCvRGBp()
57  iw, ih = image.shape[1], image.shape[0]
58 
59  # Start measuring image processing time:
60  self.timer.start()
61 
62  # Run graph:
63  results = self.selfie_segmentation.process(image)
64 
65  # Draw the mask on top of our image, OpenGL will do the alpha blending:
66  mask = self.cmapRGBA[results.segmentation_mask.astype(np.uint8)]
67  helper.drawImage("m", mask, True, False, True)
68 
69  # Write frames/s info from our timer:
70  fps = self.timer.stop()
71  helper.iinfo(inframe, fps, winw, winh);
72 
73  # End of frame:
74  helper.endFrame()
75 
76  # ####################################################################################################
77  def create_pascal_label_colormapRGBA(self, alpha, tidx):
78  """Creates a label colormap used in PASCAL VOC segmentation benchmark.
79  Returns:
80  A Colormap for visualizing segmentation results.
81  """
82  colormap = np.zeros((256, 4), dtype=int)
83  indices = np.arange(256, dtype=int)
84 
85  for shift in reversed(range(8)):
86  for channel in range(3):
87  colormap[:, channel] |= ((indices >> channel) & 1) << shift
88  indices >>= 3
89 
90  colormap[:, 3] = alpha
91  colormap[tidx, 3] = 0 # force fully transparent for entry tidx
92  return colormap.astype(np.uint8)
93 
PySelfie.PySelfie.selfie_segmentation
selfie_segmentation
Definition: PySelfie.py:41
PySelfie.PySelfie.mp_selfie_segmentation
mp_selfie_segmentation
Definition: PySelfie.py:40
PySelfie.PySelfie.create_pascal_label_colormapRGBA
def create_pascal_label_colormapRGBA(self, alpha, tidx)
Definition: PySelfie.py:77
PySelfie.PySelfie.__init__
def __init__(self)
Constructor.
Definition: PySelfie.py:30
PySelfie.PySelfie
Selfie segmentation using MediaPipe.
Definition: PySelfie.py:27
PySelfie.PySelfie.timer
timer
Definition: PySelfie.py:37
PySelfie.PySelfie.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output.
Definition: PySelfie.py:48
PySelfie.PySelfie.cmapRGBA
cmapRGBA
Definition: PySelfie.py:44
jevois::Timer