mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-08-19 21:13:21 -07:00
xlib+
This commit is contained in:
parent
babe7e3215
commit
035029bdd6
4 changed files with 116 additions and 15 deletions
|
@ -1,25 +1,72 @@
|
|||
from typing import Union
|
||||
|
||||
import numpy as np
|
||||
from PyQt6.QtGui import *
|
||||
from ...image import ImageProcessor
|
||||
|
||||
from ...image import ImageProcessor, get_NHWC_shape
|
||||
|
||||
_C_to_Format = {
|
||||
1: QImage.Format.Format_Grayscale8,
|
||||
3: QImage.Format.Format_BGR888,
|
||||
4: QImage.Format.Format_ARGB32
|
||||
}
|
||||
|
||||
def QPixmap_from_np(image : np.ndarray):
|
||||
ip = ImageProcessor(image).to_uint8()
|
||||
N,H,W,C = ip.get_dims()
|
||||
"""
|
||||
constructs QPixmap from image np.ndarray
|
||||
"""
|
||||
if image.dtype != np.uint8:
|
||||
raise ValueError('image.dtype must be np.uint8')
|
||||
|
||||
if N > 1:
|
||||
raise ValueError(f'N dim must be == 1')
|
||||
|
||||
if C == 1:
|
||||
format = QImage.Format.Format_Grayscale8
|
||||
elif C == 3:
|
||||
format = QImage.Format.Format_BGR888
|
||||
elif C == 4:
|
||||
format = QImage.Format.Format_ARGB32
|
||||
else:
|
||||
N,H,W,C = get_NHWC_shape(image)
|
||||
if N != 1:
|
||||
raise ValueError('image N must == 1')
|
||||
|
||||
format = _C_to_Format.get(C, None)
|
||||
if format is None:
|
||||
raise ValueError(f'Unsupported channels {C}')
|
||||
|
||||
image = ip.get_image('HWC')
|
||||
q_image = QImage(image.data, W, H, W*C, format)
|
||||
q_pixmap = QPixmap.fromImage(q_image)
|
||||
return q_pixmap
|
||||
|
||||
def QImage_from_np(image : np.ndarray):
|
||||
"""
|
||||
constructs QImage from image np.ndarray
|
||||
|
||||
given image must live the whole life cycle of QImage.
|
||||
"""
|
||||
if image.dtype != np.uint8:
|
||||
raise ValueError('image.dtype must be np.uint8')
|
||||
|
||||
N,H,W,C = get_NHWC_shape(image)
|
||||
if N != 1:
|
||||
raise ValueError('image N must == 1')
|
||||
format = _C_to_Format.get(C, None)
|
||||
if format is None:
|
||||
raise ValueError(f'Unsupported channels {C}')
|
||||
return QImage(image.data, W, H, W*C, format)
|
||||
|
||||
def QImage_BGR888_from_buffer(buffer : Union[bytes, bytearray], width : int, height : int ):
|
||||
|
||||
"""
|
||||
constructs QImage of BGR888 format from byte buffer with given width,height
|
||||
|
||||
given buffer must live the whole life cycle of QImage.
|
||||
"""
|
||||
if len(buffer) != width*height*3:
|
||||
raise ValueError(f'Size of buffer must be width*height*3 == {width*height*3}')
|
||||
|
||||
return QImage(buffer, width, height, width*3, QImage.Format.Format_BGR888)
|
||||
|
||||
def QImage_ARGB32_from_buffer(buffer : Union[bytes, bytearray], width : int, height : int ):
|
||||
|
||||
"""
|
||||
constructs QImage of ARGB32 format from byte buffer with given width,height
|
||||
|
||||
given buffer must live the whole life cycle of QImage.
|
||||
"""
|
||||
if len(buffer) != width*height*4:
|
||||
raise ValueError(f'Size of buffer must be width*height*4 == {width*height*4}')
|
||||
|
||||
return QImage(buffer, width, height, width*3, QImage.Format.Format_ARGB32)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue