Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/core/cv2ex.py
628 views
1
import cv2
2
import numpy as np
3
from pathlib import Path
4
from core.interact import interact as io
5
from core import imagelib
6
import traceback
7
8
def cv2_imread(filename, flags=cv2.IMREAD_UNCHANGED, loader_func=None, verbose=True):
9
"""
10
allows to open non-english characters path
11
"""
12
try:
13
if loader_func is not None:
14
bytes = bytearray(loader_func(filename))
15
else:
16
with open(filename, "rb") as stream:
17
bytes = bytearray(stream.read())
18
numpyarray = np.asarray(bytes, dtype=np.uint8)
19
return cv2.imdecode(numpyarray, flags)
20
except:
21
if verbose:
22
io.log_err(f"Exception occured in cv2_imread : {traceback.format_exc()}")
23
return None
24
25
def cv2_imwrite(filename, img, *args):
26
ret, buf = cv2.imencode( Path(filename).suffix, img, *args)
27
if ret == True:
28
try:
29
with open(filename, "wb") as stream:
30
stream.write( buf )
31
except:
32
pass
33
34
def cv2_resize(x, *args, **kwargs):
35
h,w,c = x.shape
36
x = cv2.resize(x, *args, **kwargs)
37
38
x = imagelib.normalize_channels(x, c)
39
return x
40
41