Virtual Mirror’s Documentation¶
Important
This documentation was generated on Jun 05, 2021, and is rebuilt with every master
release.
Common Coloring Methods¶
-
apply_color
(image, x, y, r, g, b, intensity)[source]¶ applies color to desired region
- Parameters
arg1 (image) – input image
arg2 (int array) – X cordinates of the desired region that is going be colored
arg3 (int array) – Y cordinates of the desired region that is going be colored
arg4 (int) – red value of rgb color
arg5 (int) – green value of rgb color
arg6 (int) – blue value of rgb color
arg7 (float) – intensity of the applied color
- Returns
the image with the applied color on the desire area
-
apply_blur
(image, image2, x, y, gussiankernel, erosionkernel)[source]¶ applies blur on the desired region
- Parameters
arg1 (image) – input image
arg2 (image) – input image with applied color on the desired region
arg3 (int array) – X cordinates of the desired region
arg4 (int array) – Y cordinates of the desired region
arg5 (int) – Gussian blur kernel
arg6 (int) – erosion kernel
arg7 (float) – intensity of the applied color
- Returns
the image with the applied blur on the desired region
-
apply_blur_color
(image, x, y, r, g, b, intensity, gussiankernel=51, erosionkernel=15)[source]¶ applies blur and color on the desired region
- Parameters
arg1 (image) – input image
arg2 (int array) – X cordinates of the desired region
arg3 (int array) – Y cordinates of the desired region
arg4 – red value of rgb color
arg5 – green value of rgb color
arg6 – blue value of rgb color
- Returns
the image with the applied clor and blur on the desired region
Note
this function is used when we need the color to be more faded on the edges of the region, this will give us a more natural look which is used on blush, concelaer and foundation.
-
moist
(image, x, y, white)[source]¶ applies gloss texture on the lips
- Parameters
arg1 (image) – input image
arg2 (int array) – X cordinates of the lips
arg3 (int array) – Y cordinates of the lips
arg4 – a value betwwen 0 to 255 (the higher this value th more white glossy texture will be )
- Returns
the image with the applied gloss on the lips
Constants¶
This scripts holds the constant point indexes for each of the face components.
Utils¶
- This script includes:
defenitions of functions for executing makeup workers in parallel to increase speed
functions to be called by the REST API, for enabling and disabling webcam, makeups and generating desired image.
-
class
Makeup_Worker
(bounds=None, instance=None)[source]¶ Defines a makeup worker instance
- Parameters
arg1 (list) – list of the bounds related to the makeup worker, it’s assigned in normal worker functions and used in static worker functions
arg2 (class) – an instance of the related makeup worker, in case it exists (E.g. Lens)
-
class
Globals
[source]¶ Holds global values that are used by static workers and other functions
- Properties:
- cap
Video captured from webcam, can be closed and open upon request
- face_detector
Instance of MediaPipe face_detection module
- face_mesher
Instance of MediaPipe face_mesh module
- makeup_workers
Dictionary of the makeup workers and their related information
- makeup args
parameter names that are given to makeup workers, used in the api designed for this purpose
- makeup instances
lens
eyeliner
eyeshadow
blush
lipstick
foundation
concealer
- motion detection variables
prev_frame: holds the previos frame for comparing with the current frame
motion detected (bool): True if motion detected, False otherwise, starts with True
f_xmin: minimum x value among points of face crop
f_ymin: minimum y value among points of face crop
f_width: width of the face crop
f_height: height of the face crop
- idx_to_coordinates
holds to coordinates calculated by turning face mesh index points to real coordinates in given image of face
- output_frame
holds the given image without any change for using in static function or in special conditions
-
concealer_worker
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a concealer effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
concealer_worker_static
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a concealer effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
blush_worker
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a blush effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
blush_worker_static
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a blush effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
lipstick_worker
(image, r, g, b, intensity, gloss, out_queue) → None[source]¶ This function applies a lipstick effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
lisptick_worker_static
(image, r, g, b, intensity, gloss, out_queue) → None[source]¶ This function applies a lipstick effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
eyeshadow_worker
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies an eyeshadow effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
eyeshadow_worker_static
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a eyeshadow effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
eyeliner_worker
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies an eyeliner effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
eyeliner_worker_static
(image, r, g, b, intensity, out_queue) → None[source]¶ This function applies a eyeliner effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (list) – shared list for appending the output
-
foundation_worker
(image, r, g, b, intensity) → numpy.ndarray[source]¶ This function applies a foundation effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
- Returns
The given image with the foundation effect applied on it, this image will be used as an input to other activated makeup workers in order for them to apply their effect on this image.
- Return type
ndarray
-
foundation_worker_static
(image, r, g, b, intensity) → numpy.ndarray[source]¶ This function applies a foundation effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
- Returns
The given image with the foundation effect applied on it, this image will be used as an input to other activated makeup workers in order for them to apply their effect on this image.
- Return type
ndarray
-
lens_worker
(image, r, g, b, intensity) → numpy.ndarray[source]¶ This function applies a contact lens effect on an input image. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
- Returns
The given image with the contact effect applied on it, this image will be used as an input to other activated makeup workers in order for them to apply their effect on this image.
- Return type
ndarray
-
lens_worker_static
(image, r, g, b, intensity) → numpy.ndarray[source]¶ This function applies a contact lens effect on an input image with the calculated boundaries in the normal worker function. This function is called by a threading function and its output is appended to a given list to be processed later.
- Parameters
arg1 (ndarray) – input image, a crop of the face found in camera viewport
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
- Returns
The given image with the contact lens effect applied on it, this image will be used as an input to other activated makeup workers in order for them to apply their effect on this image.
- Return type
ndarray
-
join_makeup_workers
(image) → numpy.ndarray[source]¶ Calls all the enabled makeup workers to produce their results on the given image. If enabled, lens and foundation workers are called beforehand and their results are given to other workers.
- Parameters
arg1 (ndarray) – The face crop given to apply makeup filters on
- Returns
The face crop with requested filters applied on
- Return type
ndarray
- How it works:
Create a list to keep threads and another list to use as shared memory between the threads
Check if foundation and lens worker are enabled, if so, apply them sequentially, this is to prevent merging issues with other workers’ results
Iterate over makeup workers and start a thread for any one of them that is active, the arguments for each function is based on its own details. Also, the shared list is also passed to the target function
Threads are added to the threads list and after creation, they are started and joined to finish with each other
The shared list which now holds the results of all the worker functions is sorted by the index of each worker to create proper layering of filters
Makeup filters are applied to the proper area one by one in the order of their predecence
-
join_makeup_workers_static
(image) → numpy.ndarray[source]¶ Calls all the enabled makeup workers to produce their results on the given image without calculating the bounds. Only static methods are called in this function so the bounds that were calculated in the previous frame are used. If enabled, lens and foundation workers are called beforehand and their results are given to other workers.
- Parameters
arg1 (ndarray) – The face crop given to apply makeup filters on
- Returns
The face crop with requested filters applied on
- Return type
ndarray
- How it works:
The procedure is the same as
join_makeup_workers()
, with the exception that in this method, static makeup workers are called.
-
apply_makeup
()[source]¶ Reads a frame from webcam camera and prepares it for the makeup workers to apply their filters on it.
- Yields
byte array – The final image is encoded and converted to byte array so that sending it over a REST API is feasible.
- How it works:
- Preparing the input
A frame is read from the webcam into an image
An unchanged copy of the image is saved into the global
output_frame
Image is flipped horizontally so that it is shown appropriately when displayed back to the user
- Motion detection
A gray version of the image is generated
If the global
prev_frame
has not been initilized, copy the gray image into it and continueCalculate the absolute difference between the current frame and the global
prev_frame
Threshold the difference to ignore minor differences
Dilate the threshold result
Find contours and calculate the contour area
If the contour area is more than a certain amount, the global
motion_detected
flag will be set toTrue
- If motion has been detected:
Process the image with MediaPipe’s face detection API, then crop the image with a padding to cover the whole face
Process the cropped face image with MediaPipe’s face mesh API
Convert the point indexes in
src.cv.makeup.constants
into actual coordinates in the face cropCall the joining function by passing the face crop image to it to apply the requested filters
Placing the face crop in the original image
- If no motion has been detected:
Crop the image using the boundaries calculated the last time we detected motion
Call the static joining method by passing the face crop to it to apply the requested filters
- Sending the result:
Encode the image in the desired extension (here we use
.jpg
)Put the image in a byte array and appending a proper header to be interpreted in the REST API`
-
enable_makeup
(makeup_type='', r=0, g=0, b=0, intensity=0.7, gloss=False)[source]¶ Enables the requested makeup so that the joining methods will be applied it in the next frame. It also passes the received arguments from the API to the corresponding function.
- Parameters
arg1 (string) – name of the makeup, it can be one of: [eyeshadow, lipstick, blush, concealer, foundation, eyeliner, lens]
arg2 (int) – rgb value of red color
arg3 (int) – rgb value of green color
arg4 (int) – rgb value of blue color
arg5 (float) – intensity of the applied makeup
arg6 (bool) – Determines whethere gloss needs to be applied or not (only for lipstick)