JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PySelfie.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4import cv2
5import numpy as np
6import 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
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:
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 helper.itext('JeVois-Pro Selfie Segmentation')
55
56 # Get the next camera image at processing resolution (may block until it is captured):
57 image = inframe.getCvRGBp()
58 iw, ih = image.shape[1], image.shape[0]
59
60 # Start measuring image processing time:
61 self.timer.start()
62
63 # Run graph:
64 results = self.selfie_segmentation.process(image)
65
66 # Draw the mask on top of our image, OpenGL will do the alpha blending:
67 mask = self.cmapRGBA[results.segmentation_mask.astype(np.uint8)]
68 helper.drawImage("m", mask, True, False, True)
69
70 # Write frames/s info from our timer:
71 fps = self.timer.stop()
72 helper.iinfo(inframe, fps, winw, winh);
73
74 # End of frame:
75 helper.endFrame()
76
77 # ####################################################################################################
78 def create_pascal_label_colormapRGBA(self, alpha, tidx):
79 """Creates a label colormap used in PASCAL VOC segmentation benchmark.
80 Returns:
81 A Colormap for visualizing segmentation results.
82 """
83 colormap = np.zeros((256, 4), dtype=int)
84 indices = np.arange(256, dtype=int)
85
86 for shift in reversed(range(8)):
87 for channel in range(3):
88 colormap[:, channel] |= ((indices >> channel) & 1) << shift
89 indices >>= 3
90
91 colormap[:, 3] = alpha
92 colormap[tidx, 3] = 0 # force fully transparent for entry tidx
93 return colormap.astype(np.uint8)
94
Selfie segmentation using MediaPipe.
Definition PySelfie.py:27
create_pascal_label_colormapRGBA(self, alpha, tidx)
Definition PySelfie.py:78
__init__(self)
Constructor.
Definition PySelfie.py:30
processGUI(self, inframe, helper)
Process function with GUI output.
Definition PySelfie.py:48