Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

cv2ex.py 1.1 KB

You have to be logged in to leave a comment. Sign In
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
  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. def cv2_imread(filename, flags=cv2.IMREAD_UNCHANGED, loader_func=None, verbose=True):
  8. """
  9. allows to open non-english characters path
  10. """
  11. try:
  12. if loader_func is not None:
  13. bytes = bytearray(loader_func(filename))
  14. else:
  15. with open(filename, "rb") as stream:
  16. bytes = bytearray(stream.read())
  17. numpyarray = np.asarray(bytes, dtype=np.uint8)
  18. return cv2.imdecode(numpyarray, flags)
  19. except:
  20. if verbose:
  21. io.log_err(f"Exception occured in cv2_imread : {traceback.format_exc()}")
  22. return None
  23. def cv2_imwrite(filename, img, *args):
  24. ret, buf = cv2.imencode( Path(filename).suffix, img, *args)
  25. if ret == True:
  26. try:
  27. with open(filename, "wb") as stream:
  28. stream.write( buf )
  29. except:
  30. pass
  31. def cv2_resize(x, *args, **kwargs):
  32. h,w,c = x.shape
  33. x = cv2.resize(x, *args, **kwargs)
  34. x = imagelib.normalize_channels(x, c)
  35. return x
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...