I am currently looking into some work related to skin tone change
Basically for an image containing a person,we first obtain its mask using standard semantic seg algorithm,and then all pixels in the image that belong to the mask are multiplied by a skin tone adjustment factor.
The code for skin tone adjustment is shown below along with the test image and its mask.
Original image
Mask of person
Code for skin tone adjustment
skin_tones =[-0.2,-0.1,0.0,0.1,0.2]
n = len(test_data)
col = len(skin_tones)
for images, _ in testloader:
plt.figure(figsize=(12,20))
image_arr = images[0].numpy()
image_arr = np.transpose(image_arr, (1,2,0))
images = images.to(device=device, dtype=torch.float)
prediction = model(images)
prediction = prediction[0].cpu().detach().numpy()
prediction = np.transpose(prediction, (1,2,0))
prediction = prediction > prediction_threshold
images = [np.zeros(shape=image_arr.shape)]*n
1 for i,skin_tone in enumerate(skin_tones):
2 for h in range(image_arr.shape[0]):
3 for w in range(image_arr.shape[1]):
4 if prediction[h][w][0]:
5 for c in range(3):
6 images[i][h][w][c] = min(image_arr[h][w][c]*(1+skin_tone),1)
7 else:
8 images[i][h][w][:] = image_arr[h][w][:]
plt.subplot(1,col,i+1)
plt.imshow(images[i])
plt.title(f'{skin_tone}')
plt.tight_layout()
plt.show()
As we can see here for all skin tone values ranging from -0.2 to 0.2 we are using a nesed for loop to go through all the pixels in the image,those pixels that belong to the mask are then set to image[i][j]*(1+skin_tone)
So is there a better way to optimize the code from line 1-8.
Also should I apply a binary threshold to the mask first before moving into the for loop?Would that be useful?