code release

This commit is contained in:
iperov 2021-07-23 17:34:49 +04:00
commit a902f11f74
354 changed files with 826570 additions and 1 deletions

View file

@ -0,0 +1,38 @@
from pathlib import Path
import cv2
import numpy as np
from xlib.image import ImageProcessor
class FaceMarkerLBF:
def __init__(self):
marker = self.marker = cv2.face.createFacemarkLBF()
marker.loadModel(str(Path(__file__).parent / 'lbfmodel.yaml'))
def extract(self, img : np.ndarray):
"""
arguments
img np.ndarray HW,HWC,NHWC
returns
[N,68,2]
"""
ip = ImageProcessor(img)
N,H,W,_ = ip.get_dims()
feed_img = ip.to_grayscale().to_uint8().get_image('NHWC')
lmrks_list = []
for n in range( max(1,N) ):
_, lmrks = self.marker.fit(feed_img[n], np.array([ [0,0,W,H] ]) )
lmrks = lmrks[0][0]
lmrks_list.append(lmrks)
return np.float32(lmrks_list)

File diff suppressed because it is too large Load diff

2
xlib/cv/__init__.py Normal file
View file

@ -0,0 +1,2 @@
from .cv import imread, imwrite
from .FaceMarkerLBF.FaceMarkerLBF import FaceMarkerLBF

39
xlib/cv/cv.py Normal file
View file

@ -0,0 +1,39 @@
import cv2
import numpy as np
def imread(filepath, flags=cv2.IMREAD_UNCHANGED, loader_func=None, raise_on_error=True) -> np.ndarray:
"""
same as cv2.imread but allows to open non-english characters path
arguments
loader_func(None) callable your own loader for filepath
raise_on_error(True) if False - no Exception on error, just None return
returns
np.ndarray
raises Exception if error and raise_on_error
"""
try:
if loader_func is not None:
bytes = bytearray(loader_func(filepath))
else:
with open(filepath, "rb") as stream:
bytes = bytearray(stream.read())
numpyarray = np.asarray(bytes, dtype=np.uint8)
return cv2.imdecode(numpyarray, flags)
except Exception as e:
if raise_on_error:
raise e
return None
def imwrite(filepath, img, *args):
ret, buf = cv2.imencode(filepath.suffix, img, *args)
if ret == True:
try:
with open(filepath, "wb") as stream:
stream.write( buf )
except:
pass