|
import numpy as np |
|
import cv2 |
|
import os |
|
import random |
|
|
|
|
|
from opencv_transform.annotation import BodyPart |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_maskfin(maskref, maskdet): |
|
|
|
|
|
details = np.zeros((512,512,3), np.uint8) |
|
details[:,:,:] = (0,255,0) |
|
|
|
|
|
bodypart_list = extractAnnotations(maskdet); |
|
|
|
|
|
if bodypart_list: |
|
|
|
|
|
for obj in bodypart_list: |
|
|
|
if obj.w < obj.h: |
|
aMax = int(obj.h/2) |
|
aMin = int(obj.w/2) |
|
angle = 0 |
|
else: |
|
aMax = int(obj.w/2) |
|
aMin = int(obj.h/2) |
|
angle = 90 |
|
|
|
x = int(obj.x) |
|
y = int(obj.y) |
|
|
|
|
|
if obj.name == "tit": |
|
cv2.ellipse(details,(x,y),(aMax,aMin),angle,0,360,(0,205,0),-1) |
|
elif obj.name == "aur": |
|
cv2.ellipse(details,(x,y),(aMax,aMin),angle,0,360,(0,0,255),-1) |
|
elif obj.name == "nip": |
|
cv2.ellipse(details,(x,y),(aMax,aMin),angle,0,360,(255,255,255),-1) |
|
elif obj.name == "belly": |
|
cv2.ellipse(details,(x,y),(aMax,aMin),angle,0,360,(255,0,255),-1) |
|
elif obj.name == "vag": |
|
cv2.ellipse(details,(x,y),(aMax,aMin),angle,0,360,(255,0,0),-1) |
|
elif obj.name == "hair": |
|
xmin = x - int(obj.w/2) |
|
ymin = y - int(obj.h/2) |
|
xmax = x + int(obj.w/2) |
|
ymax = y + int(obj.h/2) |
|
cv2.rectangle(details,(xmin,ymin),(xmax,ymax),(100,100,100),-1) |
|
|
|
|
|
f1 = np.asarray([0, 250, 0]) |
|
f2 = np.asarray([10, 255, 10]) |
|
|
|
|
|
green_mask = cv2.bitwise_not(cv2.inRange(maskref, f1, f2)) |
|
|
|
|
|
green_mask_inv = cv2.bitwise_not(green_mask) |
|
|
|
|
|
res1 = cv2.bitwise_and(maskref, maskref, mask = green_mask) |
|
res2 = cv2.bitwise_and(details, details, mask = green_mask_inv) |
|
|
|
|
|
maskfin = cv2.add(res1, res2) |
|
return maskfin |
|
|
|
|
|
|
|
|
|
|
|
|
|
def extractAnnotations(maskdet): |
|
|
|
|
|
|
|
|
|
|
|
tits_list = findBodyPart(maskdet, "tit") |
|
aur_list = findBodyPart(maskdet, "aur") |
|
vag_list = findBodyPart(maskdet, "vag") |
|
belly_list = findBodyPart(maskdet, "belly") |
|
|
|
|
|
aur_list = filterDimParts(aur_list, 100, 1000, 0.5, 3); |
|
tits_list = filterDimParts(tits_list, 1000, 60000, 0.2, 3); |
|
vag_list = filterDimParts(vag_list, 10, 1000, 0.2, 3); |
|
belly_list = filterDimParts(belly_list, 10, 1000, 0.2, 3); |
|
|
|
|
|
aur_list = filterCouple(aur_list); |
|
tits_list = filterCouple(tits_list); |
|
|
|
|
|
missing_problem = detectTitAurMissingProblem(tits_list, aur_list) |
|
|
|
|
|
if (missing_problem in [3,6,7,8]): |
|
resolveTitAurMissingProblems(tits_list, aur_list, missing_problem) |
|
|
|
|
|
nip_list = inferNip(aur_list) |
|
|
|
|
|
hair_list = inferHair(vag_list) |
|
|
|
|
|
return tits_list + aur_list + nip_list + vag_list + hair_list + belly_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
def findBodyPart(image, part_name): |
|
|
|
bodypart_list = [] |
|
|
|
|
|
if part_name == "tit": |
|
|
|
f1 = np.asarray([0, 0, 0]) |
|
f2 = np.asarray([10, 10, 10]) |
|
f3 = np.asarray([0, 0, 250]) |
|
f4 = np.asarray([0, 0, 255]) |
|
color_mask1 = cv2.inRange(image, f1, f2) |
|
color_mask2 = cv2.inRange(image, f3, f4) |
|
color_mask = cv2.bitwise_or(color_mask1, color_mask2) |
|
|
|
elif part_name == "aur": |
|
f1 = np.asarray([0, 0, 250]) |
|
f2 = np.asarray([0, 0, 255]) |
|
color_mask = cv2.inRange(image, f1, f2) |
|
|
|
elif part_name == "vag": |
|
f1 = np.asarray([250, 0, 0]) |
|
f2 = np.asarray([255, 0, 0]) |
|
color_mask = cv2.inRange(image, f1, f2) |
|
|
|
elif part_name == "belly": |
|
f1 = np.asarray([250, 0, 250]) |
|
f2 = np.asarray([255, 0, 255]) |
|
color_mask = cv2.inRange(image, f1, f2) |
|
|
|
|
|
contours, hierarchy = cv2.findContours(color_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
for cnt in contours: |
|
|
|
if len(cnt)>5: |
|
|
|
|
|
ellipse = cv2.fitEllipse(cnt) |
|
|
|
|
|
x = ellipse[0][0] |
|
y = ellipse[0][1] |
|
angle = ellipse[2] |
|
aMin = ellipse[1][0]; |
|
aMax = ellipse[1][1]; |
|
|
|
|
|
if angle == 0: |
|
h = aMax |
|
w = aMin |
|
else: |
|
h = aMin |
|
w = aMax |
|
|
|
|
|
if part_name == "belly": |
|
if w<15: |
|
w *= 2 |
|
if h<15: |
|
h *= 2 |
|
|
|
|
|
if part_name == "vag": |
|
if w<15: |
|
w *= 2 |
|
if h<15: |
|
h *= 2 |
|
|
|
|
|
xmin = int(x - (w/2)) |
|
xmax = int(x + (w/2)) |
|
ymin = int(y - (h/2)) |
|
ymax = int(y + (h/2)) |
|
|
|
bodypart_list.append(BodyPart(part_name, xmin, ymin, xmax, ymax, x, y, w, h )) |
|
|
|
return bodypart_list |
|
|
|
|
|
|
|
|
|
def filterDimParts(bp_list, min_area, max_area, min_ar, max_ar): |
|
|
|
b_filt = [] |
|
|
|
for obj in bp_list: |
|
|
|
a = obj.w*obj.h |
|
|
|
if ((a > min_area)and(a < max_area)): |
|
|
|
ar = obj.w/obj.h |
|
|
|
if ((ar>min_ar)and(ar<max_ar)): |
|
|
|
b_filt.append(obj) |
|
|
|
return b_filt |
|
|
|
|
|
|
|
|
|
def filterCouple(bp_list): |
|
|
|
|
|
if (len(bp_list)>2): |
|
|
|
|
|
min_a = 0 |
|
min_b = 1 |
|
min_diff = abs(bp_list[min_a].y-bp_list[min_b].y) |
|
|
|
for a in range(0,len(bp_list)): |
|
for b in range(0,len(bp_list)): |
|
|
|
if a != b: |
|
diff = abs(bp_list[a].y-bp_list[b].y) |
|
if diff<min_diff: |
|
min_diff = diff |
|
min_a = a |
|
min_b = b |
|
b_filt = [] |
|
|
|
b_filt.append(bp_list[min_a]) |
|
b_filt.append(bp_list[min_b]) |
|
|
|
return b_filt |
|
else: |
|
|
|
return bp_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def detectTitAurMissingProblem(tits_list, aur_list): |
|
|
|
t_len = len(tits_list) |
|
a_len = len(aur_list) |
|
|
|
if (t_len == 0): |
|
if (a_len == 0): |
|
return 1 |
|
elif (a_len == 1): |
|
return 2 |
|
elif (a_len == 2): |
|
return 3 |
|
else: |
|
return -1 |
|
elif (t_len == 1): |
|
if (a_len == 0): |
|
return 4 |
|
elif (a_len == 1): |
|
return 5 |
|
elif (a_len == 2): |
|
return 6 |
|
else: |
|
return -1 |
|
elif (t_len == 2): |
|
if (a_len == 0): |
|
return 7 |
|
elif (a_len == 1): |
|
return 8 |
|
else: |
|
return -1 |
|
else: |
|
return -1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
def resolveTitAurMissingProblems(tits_list, aur_list, problem_code): |
|
|
|
if problem_code == 3: |
|
|
|
random_tit_factor = random.randint(2, 5) |
|
|
|
|
|
new_w = aur_list[0].w * random_tit_factor |
|
new_x = aur_list[0].x |
|
new_y = aur_list[0].y |
|
|
|
xmin = int(new_x - (new_w/2)) |
|
xmax = int(new_x + (new_w/2)) |
|
ymin = int(new_y - (new_w/2)) |
|
ymax = int(new_y + (new_w/2)) |
|
|
|
tits_list.append(BodyPart("tit", xmin, ymin, xmax, ymax, new_x, new_y, new_w, new_w )) |
|
|
|
|
|
new_w = aur_list[1].w * random_tit_factor |
|
new_x = aur_list[1].x |
|
new_y = aur_list[1].y |
|
|
|
xmin = int(new_x - (new_w/2)) |
|
xmax = int(new_x + (new_w/2)) |
|
ymin = int(new_y - (new_w/2)) |
|
ymax = int(new_y + (new_w/2)) |
|
|
|
tits_list.append(BodyPart("tit", xmin, ymin, xmax, ymax, new_x, new_y, new_w, new_w )) |
|
|
|
elif problem_code == 6: |
|
|
|
|
|
d1 = abs(tits_list[0].x - aur_list[0].x) |
|
d2 = abs(tits_list[0].x - aur_list[1].x) |
|
|
|
if d1 > d2: |
|
|
|
new_x = aur_list[0].x |
|
new_y = aur_list[0].y |
|
else: |
|
|
|
new_x = aur_list[1].x |
|
new_y = aur_list[1].y |
|
|
|
|
|
xmin = int(new_x - (tits_list[0].w/2)) |
|
xmax = int(new_x + (tits_list[0].w/2)) |
|
ymin = int(new_y - (tits_list[0].w/2)) |
|
ymax = int(new_y + (tits_list[0].w/2)) |
|
|
|
tits_list.append(BodyPart("tit", xmin, ymin, xmax, ymax, new_x, new_y, tits_list[0].w, tits_list[0].w )) |
|
|
|
elif problem_code == 7: |
|
|
|
|
|
new_w = tits_list[0].w * random.uniform(0.03, 0.1) |
|
new_x = tits_list[0].x |
|
new_y = tits_list[0].y |
|
|
|
xmin = int(new_x - (new_w/2)) |
|
xmax = int(new_x + (new_w/2)) |
|
ymin = int(new_y - (new_w/2)) |
|
ymax = int(new_y + (new_w/2)) |
|
|
|
aur_list.append(BodyPart("aur", xmin, ymin, xmax, ymax, new_x, new_y, new_w, new_w )) |
|
|
|
|
|
new_w = tits_list[1].w * random.uniform(0.03, 0.1) |
|
new_x = tits_list[1].x |
|
new_y = tits_list[1].y |
|
|
|
xmin = int(new_x - (new_w/2)) |
|
xmax = int(new_x + (new_w/2)) |
|
ymin = int(new_y - (new_w/2)) |
|
ymax = int(new_y + (new_w/2)) |
|
|
|
aur_list.append(BodyPart("aur", xmin, ymin, xmax, ymax, new_x, new_y, new_w, new_w )) |
|
|
|
elif problem_code == 8: |
|
|
|
|
|
d1 = abs(aur_list[0].x - tits_list[0].x) |
|
d2 = abs(aur_list[0].x - tits_list[1].x) |
|
|
|
if d1 > d2: |
|
|
|
new_x = tits_list[0].x |
|
new_y = tits_list[0].y |
|
else: |
|
|
|
new_x = tits_list[1].x |
|
new_y = tits_list[1].y |
|
|
|
|
|
xmin = int(new_x - (aur_list[0].w/2)) |
|
xmax = int(new_x + (aur_list[0].w/2)) |
|
ymin = int(new_y - (aur_list[0].w/2)) |
|
ymax = int(new_y + (aur_list[0].w/2)) |
|
aur_list.append(BodyPart("aur", xmin, ymin, xmax, ymax, new_x, new_y, aur_list[0].w, aur_list[0].w )) |
|
|
|
|
|
|
|
|
|
|
|
|
|
def detectTitAurPositionProblem(tits_list, aur_list): |
|
|
|
diffTitsX = abs(tits_list[0].x - tits_list[1].x) |
|
if diffTitsX < 40: |
|
print("diffTitsX") |
|
|
|
return True |
|
|
|
diffTitsY = abs(tits_list[0].y - tits_list[1].y) |
|
if diffTitsY > 120: |
|
|
|
print("diffTitsY") |
|
return True |
|
|
|
diffTitsW = abs(tits_list[0].w - tits_list[1].w) |
|
if ((diffTitsW < 0.1)or(diffTitsW>60)): |
|
print("diffTitsW") |
|
|
|
return True |
|
|
|
|
|
if aur_list[0].y > 350: |
|
|
|
rapp = aur_list[0].y/(abs(aur_list[0].x - aur_list[1].x)) |
|
if rapp > 2.8: |
|
print("aurDown") |
|
return True |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
def inferNip(aur_list): |
|
nip_list = [] |
|
|
|
for aur in aur_list: |
|
|
|
|
|
|
|
|
|
|
|
nip_dim = int(5 + aur.w*random.uniform(0.03, 0.09)) |
|
|
|
|
|
x = aur.x |
|
y = aur.y |
|
|
|
|
|
xmin = int(x - (nip_dim/2)) |
|
xmax = int(x + (nip_dim/2)) |
|
ymin = int(y - (nip_dim/2)) |
|
ymax = int(y + (nip_dim/2)) |
|
|
|
nip_list.append(BodyPart("nip", xmin, ymin, xmax, ymax, x, y, nip_dim, nip_dim )) |
|
|
|
return nip_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
def inferHair(vag_list): |
|
hair_list = [] |
|
|
|
|
|
if random.uniform(0.0, 1.0) > 0.3: |
|
|
|
for vag in vag_list: |
|
|
|
|
|
hair_w = vag.w*random.uniform(0.4, 1.5) |
|
hair_h = vag.h*random.uniform(0.4, 1.5) |
|
|
|
|
|
x = vag.x |
|
y = vag.y - (hair_h/2) - (vag.h/2) |
|
|
|
|
|
xmin = int(x - (hair_w/2)) |
|
xmax = int(x + (hair_w/2)) |
|
ymin = int(y - (hair_h/2)) |
|
ymax = int(y + (hair_h/2)) |
|
|
|
hair_list.append(BodyPart("hair", xmin, ymin, xmax, ymax, x, y, hair_w, hair_h )) |
|
|
|
return hair_list |
|
|