File size: 21,406 Bytes
5bc01b9 760a850 5bc01b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
#!/usr/bin/env python
#############################################################################
#
# This is the bulk of the logic for the gradio demo. You use it for whatever
# you want. Credit would be nice but w/e
#
# You can also run it on an image from the cli
#
# TODO:
#
# 1. rework the classes that just wrap Dict and List to extend them
# 2. cleanup all the to_dict madness
# 3. convert the print calls to use the logging
# 4. add a proper creative commons license
# 5. cleanup string constants
# 6. replace custom code with libraries like for OBJ
#
#############################################################################
import cv2
import json
import logging
import mediapipe as mp
import numpy as np
import os
import sys
import torch
from mediapipe.framework.formats import landmark_pb2
from mediapipe.python.solutions.drawing_utils import _normalized_to_pixel_coordinates
from PIL import Image, ImageDraw
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
from typing import List, Mapping, Optional, Tuple, Union, Dict, Type
from utils import colorize
from quads import QUADS
mp_face_mesh = mp.solutions.face_mesh
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
NumpyImage = Type[np.ndarray]
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
HF_HACK = True
class Point3:
def __init__(self, values:List[float]=3*[0] ):
self.values = values
@property
def x(self):
return self.values[0]
@property
def y(self):
return self.values[1]
@property
def z(self):
return self.values[2]
def to_dict(self):
return {'x':self.x,'y':self.y,'z':self.z}
class TextureCoordinate:
def __init__(self, values:List[float]=2*[0] ):
self.values = values
@property
def u(self):
return self.values[0]
@property
def v(self):
return self.values[1]
def to_dict(self):
return {'u':self.u,'v':self.v}
class PixelCoordinate:
def __init__(self, values:List[int]=2*[0] ):
self.values = values
@property
def x(self):
return self.values[0]
@property
def y(self):
return self.values[1]
def to_dict(self):
return {'x':self.x,'y':self.y}
class DepthMap:
MEDIA_PIPE = 'mediapipe'
def __init__(self, values:Dict[str,float]={'og':0} ):
self.values = values
def to_dict(self):
return self.values
class DepthMapping:
def __init__(self, weight:float=1, lo:float=+np.inf, hi:float=-np.inf, toLo:float=0, toHi:float=1):
self.weight = weight
self.lo = lo
self.hi = hi
self.toLo = toLo
self.toHi = toHi
self.diff = 1
self.toDiff = 1
self.update()
def reset(self):
self.lo = +np.inf
self.hi = -np.inf
def track(self,value):
self.lo = min(self.lo,value)
self.hi = max(self.hi,value)
def update(self):
self.diff = self.hi - self.lo
self.toDiff = self.toHi - self.toLo
return self
def translate(self,value):
if not self.diff == 0:
value = ( value - self.lo ) / self.diff
value = self.toLo + value * self.toDiff
value = value * self.weight
return value
def to_dict(self):
return {
'weight' : self.weight,
'lo' : self.lo,
'hi' : self.hi,
'toLo' : self.toLo,
'toHi' : self.toHi,
'diff' : self.diff,
'toDiff' : self.toDiff,
}
class WeightMap:
def __init__(self, values:Dict[str,DepthMapping]=None):
if values is None:
self.values = {DepthMap.MEDIA_PIPE:DepthMapping()}
else:
self.values = values
def set(self,key:str,depthMapping:DepthMapping):
self.values[key] = depthMapping
def totally(self,name:str):
if not name in self.values:
raise Exception( f'no weight for {k} in {self.to_dict()}' )
for depthMapping in self.values.values():
depthMapping.weight = 0
self.values[ name ].weight = 1
def saveWeights(self)->Dict[str,float]:
return {k:v.weight for k,v in self.values.items()}
def loadWeights(self,weights:Dict[str,float]):
for k,weight in weights.items():
if k in self.values:
self.values[ k ].weight = weight
else:
raise Exception( f'no weight for {k} in {self.to_dict()}' )
def to_dict(self):
return {k:dm.to_dict() for k,dm in self.values.items()}
return self.values
class MeshPoint:
def __init__(self,
position:Point3 = Point3(),
color:Point3 = Point3(),
textureCoordinate:TextureCoordinate = TextureCoordinate(),
pixelCoordinate:PixelCoordinate = PixelCoordinate(),
depthMap:DepthMap = None,
):
self.position = position
self.color = color
self.textureCoordinate = textureCoordinate
self.pixelCoordinate = pixelCoordinate
if depthMap is None:
self.depthMap = DepthMap({DepthMap.MEDIA_PIPE:position.values[2]})
else:
self.depthMap = depthMap
def to_dict(self):
derp = {
'position' : self.position.to_dict(),
'color' : self.color.to_dict(),
'textureCoordinate' : self.textureCoordinate.to_dict(),
'pixelCoordinate' : self.pixelCoordinate.to_dict(),
}
if not self.depthMap is None:
derp[ 'depthMap' ] = self.depthMap.to_dict()
return derp
def weighDepth(self, weightMap:WeightMap = WeightMap()):
total_sum = sum([dm.weight for dm in weightMap.values.values()])
tmp = 0
for key, depthMapping in weightMap.values.items():
if key in self.depthMap.values:
tmp = tmp + depthMapping.translate( self.depthMap.values[ key ] )
else:
raise Exception(f'{key} from weightMap not in depthMap')
tmp = tmp / total_sum
#print( f'depthMap: {json.dumps(self.depthMap.to_dict())} -> {tmp}') # spam!!!
self.position.values[2] = tmp
def mapLandMark(self, mediaMesh:'MediaMesh', landmark: landmark_pb2.NormalizedLandmark) -> 'MeshPoint':
x, y = _normalized_to_pixel_coordinates(landmark.x,landmark.y,mediaMesh.width,mediaMesh.height)
#position = [landmark.x * mediaMesh.ratio, landmark.y, landmark.z]
#position = [landmark.x * mediaMesh.ratio, landmark.y, landmark.z]
position = [v * mediaMesh.scale[i] for i,v in enumerate([landmark.x, landmark.y, landmark.z])]
self.position = Point3(position)
#self.position = Point3([landmark.x * mediaMesh.ratio, landmark.y, landmark.z])
self.color = Point3([value / 255 for value in mediaMesh.image[y,x]])
self.textureCoordinate = TextureCoordinate([x/mediaMesh.width,1-y/mediaMesh.height] )
self.pixelCoordinate = PixelCoordinate([x,y])
self.depthMap = DepthMap({DepthMap.MEDIA_PIPE:self.position.z})
return self
def toObj(self, lines:List[str], hf_hack:bool=HF_HACK):
lines.append( "v " + " ".join(map(str, self.position.values + self.color.values)) )
lines.append( "vt " + " ".join(map(str, self.textureCoordinate.values ) ) )
# IMPORTANT! MeshFace uses 1 based indices, not 0 based!!!!
class MeshFace:
def __init__(self,indices:List[int]=None,normal:Point3=Point3()):
self.indices = indices
self.normal = normal
def calculateNormal(self,meshPoints:List[MeshPoint]):
if self.indices is None:
raise Exception('indices is junk')
if meshPoints is None:
raise Exception('meshPoints is junk')
if len(self.indices)<3:
raise Exception('need at least 3 points')
points = [meshPoints[index-1] for index in self.indices[:3]]
npz = [np.array(point.position.values) for point in points]
v1 = npz[1] - npz[0]
v2 = npz[2] - npz[0]
normal = np.cross(v1, v2)
normal = normal / np.linalg.norm(normal)
self.normal = Point3( normal.tolist() )
def toObj(self, lines:List[str], index:int, hf_hack:bool=HF_HACK):
lines.append( "vn " + " ".join([str(value) for value in self.normal.values]) )
face_uv = "f " + " ".join([f'{vertex}/{vertex}/{index}' for vertex in self.indices])
face_un = "f " + " ".join([str(vertex) for vertex in self.indices])
if hf_hack:
lines.append( f'#{face_uv}' )
lines.append( f'{face_un}' )
else:
lines.append( face_uv )
class DepthSource:
def __init__(self, name:str=None):
self.name = name
self.mediaMesh = None
self.depth:NumpyImage = None
self.gray:NumpyImage = None
def mapDepth(self, mediaMesh:'MediaMesh', depthMapping:DepthMapping=None) -> 'DepthSource':
return self
def _addDepth(self, mediaMesh:'MediaMesh', depthMapping:DepthMapping=None) -> 'DepthSource':
self.gray = colorize(self.depth, cmap='gray_r')
self.mediaMesh = mediaMesh
for meshPoint in mediaMesh.points:
depth = self.depth[meshPoint.pixelCoordinate.y,meshPoint.pixelCoordinate.x]
#depth = -depth # lazy conversion from depth to position
meshPoint.depthMap.values[ self.name ] = float( depth )
mediaMesh.weightMap.set( self.name, self.createDepthMapping(depthMapping) )
self.gray = mediaMesh.drawGrayMesh(self.name,True)
return self
# note: if depthMapping is passed in, the hi and lo will be reset
def createDepthMapping(self,depthMapping:DepthMapping=None) -> DepthMapping:
if depthMapping is None:
depthMapping = DepthMapping()
depthMapping.reset()
if not self.depth is None:
for meshPoint in self.mediaMesh.points:
depth = self.depth[meshPoint.pixelCoordinate.y,meshPoint.pixelCoordinate.x]
depthMapping.track(float(depth))
return depthMapping.update()
class ZoeDepthSource( DepthSource ):
NAME = 'zoe'
def __init__(self):
super().__init__(ZoeDepthSource.NAME)
self.model = torch.hub.load('isl-org/ZoeDepth', "ZoeD_K", pretrained=True).to(DEVICE).eval()
def mapDepth(self, mediaMesh:'MediaMesh', depthMapping:DepthMapping=None) -> 'DepthSource':
self.depth = 1.-self.model.infer_pil(mediaMesh.image)
return self._addDepth(mediaMesh, depthMapping)
class MidasDepthSource( DepthSource ):
NAME = 'midas'
def __init__(self):
super().__init__(MidasDepthSource.NAME)
self.feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
self.model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
def mapDepth(self, mediaMesh:'MediaMesh', depthMapping:DepthMapping=None) -> 'DepthSource':
img = Image.fromarray(mediaMesh.image)
encoding = self.feature_extractor(img, return_tensors="pt")
with torch.no_grad():
outputs = self.model(**encoding)
predicted_depth = outputs.predicted_depth
prediction = torch.nn.functional.interpolate(
predicted_depth.unsqueeze(1),
size=img.size[::-1],
mode="bicubic",
align_corners=False,
).squeeze()
self.depth = prediction.cpu().numpy()
return self._addDepth(mediaMesh, depthMapping)
#############################################################################
#
# A MediaMesh has:
#
# 1. an input image
# 2. the first landmark found
# 3. a MeshPoint for each point
#
#
#
#############################################################################
class MediaMesh:
LOG = logging.getLogger(__name__)
COMBINED = 'combined'
def __init__(self, scale:List[int]=[-1,-1,-1], weightMap:WeightMap = None, image:NumpyImage = None, annotated:NumpyImage = None, points:List[MeshPoint] = None):
self.scale = scale
if weightMap is None:
self.weightMap = WeightMap()
else:
self.weightMap = weightMap
self.image = image
self.annotated = annotated
self.points = points
self.meshes = {}
self.depthSources = {}
# after this call, instance variables for image, annotated and points should be set
def detect(self, image:NumpyImage, min_detection_confidence:float = .5) -> 'MediaMesh':
self.image = image
self.annotated = image.copy()
self.points = None
self.width = image.shape[1]
self.height = image.shape[0]
self.ratio = self.width / self.height
self.scale[0] = self.ratio
first = True # just do the first face for now
with mp_face_mesh.FaceMesh(
static_image_mode=True,
max_num_faces=1,
min_detection_confidence=min_detection_confidence) as face_mesh:
results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if not results.multi_face_landmarks:
raise Exception( 'no faces found' )
for landmarks in results.multi_face_landmarks:
if first:
self.points = self.mapLandMarks(landmarks)
first = False
self.drawLandMarks(self.annotated, landmarks)
self.gray = self.drawGrayMesh()
self.weightMap.set( DepthMap.MEDIA_PIPE, self.createDepthMapping() )
return self
def drawLandMarks(self, image:NumpyImage, landmarks: landmark_pb2.NormalizedLandmarkList):
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
mp_drawing.draw_landmarks(
image=image,
landmark_list=landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_tesselation_style())
mp_drawing.draw_landmarks(
image=image,
landmark_list=landmarks,
connections=mp_face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style())
def mapLandMarks(self, landmarks: landmark_pb2.NormalizedLandmarkList) -> List[MeshPoint]:
points = []
for landmark in landmarks.landmark:
point = MeshPoint().mapLandMark(self, landmark)
points.append( point )
return self.centerPoints(points)
def centerPoints(self,points:List[MeshPoint]=None) -> List[MeshPoint]:
if points is None:
points = self.points
mins = [+np.inf] * 3
maxs = [-np.inf] * 3
for point in points:
for dimension,value in enumerate( point.position.values ):
mins[dimension] = min(mins[dimension],value)
maxs[dimension] = max(maxs[dimension],value)
mids = [(min_val + max_val) / 2 for min_val, max_val in zip(mins, maxs)]
for point in points:
point.position.values = [(val-mid) for val, mid in zip(point.position.values,mids)]
print( f'mins: {mins}' )
print( f'mids: {mids}' )
print( f'maxs: {maxs}' )
return points
def createDepthMapping(self,depthMapping:DepthMapping=None) -> DepthMapping:
if depthMapping is None:
depthMapping = DepthMapping()
for point in self.points:
depthMapping.track(point.position.z)
return depthMapping.update()
def drawGrayMesh(self, source:str=DepthMap.MEDIA_PIPE, invert:bool=False):
image = Image.new("RGB", (self.width, self.height), (88,13,33))
draw = ImageDraw.Draw(image)
minZ = np.inf
maxZ = -np.inf
depths = []
for point in self.points:
depth = point.depthMap.values[source]
depths.append( depth )
minZ = min( minZ, depth )
maxZ = max( maxZ, depth )
difZ = maxZ - minZ
if 0 == difZ:
difZ = 1
depths = [(depth-minZ)/difZ for depth in depths]
for quad in QUADS:
points = [tuple(self.points[index-1].pixelCoordinate.values) for index in quad]
colors = [tuple(3*[int(255*depths[index-1])]) for index in quad]
color = int(np.average(colors))
if invert:
color = 255 - color
draw.polygon(points, fill=tuple(3*[color]))
#draw.polygon(points, fill=colors) # sadly this does not work
return np.asarray(image)
# the obj is based on the current weightMap
def toObj(self, name:str='sweet', hf_hack:bool=HF_HACK):
print( '-----------------------------------------------------------------------------' )
obj = [f'o {name}Mesh']
mtl = f'newmtl {name}Material\nmap_Kd {name}.png\n'
c = '#' if hf_hack else ''
obj.append( f'{c}mtllib {name}.mtl' )
obj.append( f'##################################################################' )
obj.append( f'# to bring into blender with uvs:' )
obj.append( f'# put the following 2 lines into {name}.mtl uncommented' )
obj.append( f'#newmtl {name}Material' )
obj.append( f'#map_Kd {name}.png' )
obj.append( f'# remove lines from this file starting with "f "' )
obj.append( f'# uncomment the lines that start with "#f "' )
obj.append( f'##################################################################' )
for key, depthMapping in self.weightMap.values.items():
depthMapping.update()
print( f'{name}.{key} -> {depthMapping.to_dict()}' )
for point in self.points:
point.weighDepth(self.weightMap)
self.centerPoints()
for point in self.points:
point.toObj(obj,hf_hack)
obj.append( f'usemtl {name}Material' )
index = 0
for quad in QUADS:
index = 1 + index
face = MeshFace(quad)
face.calculateNormal(self.points)
face.toObj(obj, index, hf_hack)
obj.append( f'##################################################################' )
obj.append( f'# EOF' )
obj.append( f'##################################################################' )
print( '-----------------------------------------------------------------------------' )
return obj,mtl
def to_dict(self):
return {
'width' : self.width,
'height' : self.height,
'ratio' : self.ratio,
'weightMap' : {key: value.to_dict() for key, value in self.weightMap.values.items()},
'points' : [point.to_dict() for point in self.points]
}
# should be called after demoSetup and detect
def singleSourceMesh(self,name:str, hf_hack:bool=HF_HACK):
before = self.weightMap.saveWeights() # push
self.weightMap.totally(name)
obj,mtl = self.toObj(name)
self.weightMap.loadWeights( before ) # pop
return obj,mtl
# should be called after demoSetup and detect
def meshmerizing(self,hf_hack:bool=HF_HACK):
for depthSource in self.depthSources:
depthSource.mapDepth(self,self.weightMap.values[depthSource.name])
obj,mtl = self.toObj(MediaMesh.COMBINED)
self.meshes = {MediaMesh.COMBINED:(obj,mtl)}
for source in self.depthSources:
self.meshes[ source.name ] = (self.singleSourceMesh(source.name))
self.meshes[DepthMap.MEDIA_PIPE] = (self.singleSourceMesh(DepthMap.MEDIA_PIPE))
return self.meshes
def demoSetup(self) -> 'MediaMesh':
self.depthSources = [ ZoeDepthSource(), MidasDepthSource() ]
for depthSource in self.depthSources:
self.weightMap.set( depthSource.name, depthSource.createDepthMapping() )
# observationally
self.weightMap.values[ ZoeDepthSource.NAME ].toHi = 1.77
self.weightMap.values[ MidasDepthSource.NAME ].toHi = 2.55
self.weightMap.values[ ZoeDepthSource.NAME ].weight = 1.00
self.weightMap.values[ MidasDepthSource.NAME ].weight = 0.22
return self
def main(self):
if not 2 == len(sys.argv):
raise Exception( 'usage: MediaMesh.py <image filename>' )
mediaMesh = MediaMesh().demoSetup()
mediaMesh.detect(cv2.imread( sys.argv[1] ) )
for name,mesh in mediaMesh.meshmerizing().items():
obj = mesh[0]
mtl = mesh[1]
with open(f"{name}.obj", "w") as file:
file.write( '\n'.join(obj) )
with open(f"{name}.mtl", "w") as file:
file.write( mtl )
cv2.imwrite( 'mesh.png', mediaMesh.annotated )
cv2.imwrite( 'mpg.png', mediaMesh.gray )
for source in mediaMesh.depthSources:
cv2.imwrite( f'{source.name}.png', source.gray )
with open("mesh.json", "w") as file:
json.dump(mediaMesh.to_dict(), file, indent=4)
if __name__ == "__main__":
MediaMesh().main()
# EOF
#############################################################################
|