opt import module and cupy

This commit is contained in:
flandre 2025-05-13 14:40:17 +08:00
parent b187e36aa4
commit c926bc0072
4 changed files with 39 additions and 85 deletions

View File

@ -1,10 +1,11 @@
import importlib
import logging
import multiprocessing
import os
import shutil
import subprocess
import tomllib
from enum import Enum
from enum import Enum, auto
from pathlib import Path
import click
@ -14,39 +15,24 @@ from flandre import C
from flandre import P
from flandre.BusClient import BusClient
from flandre.kde_pyqt6_mainui import kde_pyqt6_mainui
from flandre.nodes.Beamformer import Beamformer
from flandre.nodes.Broker import Broker
from flandre.nodes.Device import Device
from flandre.nodes.ImageCV import ImageCV
from flandre.nodes.ImageFFMPEG import ImageFFMPEG
from flandre.nodes.ImageQt import ImageQt
from flandre.nodes.Loader import Loader
from flandre.nodes.MainUI import MainUI
from flandre.nodes.Mi import Mi
from flandre.nodes.Midi import Midi
from flandre.nodes.Muxer import Muxer
from flandre.nodes.Recorder import Recorder
from flandre.nodes.Robot import Robot
from flandre.nodes.VideoQt import VideoQt
from flandre.nodes.Web import Web
from flandre.utils.Msg import KillMsg, NodeOnlineMsg, Msg1, Msg2
class LaunchComponent(Enum):
MainUI = MainUI
ImageCV = ImageCV
Muxer = Muxer
Robot = Robot
Loader = Loader
Device = Device
Recorder = Recorder
Beamformer = Beamformer
ImageFFMPEG = ImageFFMPEG
ImageQt = ImageQt
Midi = Midi
Mi = Mi
Web = Web
VideoQt = VideoQt
MainUI = auto()
ImageCV = auto()
Muxer = auto()
Robot = auto()
Loader = auto()
Device = auto()
Recorder = auto()
Beamformer = auto()
ImageFFMPEG = auto()
ImageQt = auto()
Midi = auto()
Mi = auto()
Web = auto()
VideoQt = auto()
def launch(arg: dict[LaunchComponent, dict]):
@ -59,7 +45,9 @@ def launch(arg: dict[LaunchComponent, dict]):
if k == LaunchComponent.MainUI and os.environ.get('XDG_CURRENT_DESKTOP', None) == 'KDE':
ps.append(kde_pyqt6_mainui)
continue
ps.append(k.value(**v))
m = importlib.import_module(f"flandre.nodes.{k.name}")
c = getattr(m, k.name)
ps.append(c(**v))
pps = []
for p in ps:

View File

@ -1,17 +1,13 @@
import sys
import time
import cv2
import numpy as np
from PyQt6.QtCore import QByteArray, Qt
from PyQt6.QtGui import QImage, QPixmap, QKeyEvent, QWheelEvent
from PyQt6.QtWidgets import QMainWindow, QApplication, QGraphicsPixmapItem, QGraphicsScene
from flandre import C
from flandre.nodes.Node import Node
from flandre.pyqt.Image import Ui_MainWindow
from flandre.pyqt.ZMQReceiver import ZMQReceiver
from flandre.utils.Msg import KillMsg, Msg, BMMsg, RfMatMsg, KeyPressMsg, RGB888Msg
from flandre.utils.Msg import KillMsg, Msg, BMMsg, RfMatMsg, KeyPressMsg
from flandre.utils.RfMat import RfMat
@ -79,38 +75,6 @@ class Adv(QMainWindow, Ui_MainWindow):
if self.need_fit:
self.graphicsView.fitInView(self.s.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
self.need_fit = False
# elif isinstance(msg, RGB888Msg):
# w = msg.w
# h = msg.h
# d: RfMat = msg.rfmat
# d2 = (d
# .resize((int(w * self.zoom), int(h * self.zoom)))
# .watermark(cond=self.watermark)
# )
# w = d2.w
# h = d2.h
# if self.grey:
# qImg = QImage(
# d2.__bytes__(),
# w, h, w,
# QImage.Format.Format_Grayscale8
# )
# else:
# qImg = QImage(
# d2.__bytes__(),
# w, h, 3 * w,
# QImage.Format.Format_BGR888
# )
# self.g.setPixmap(QPixmap(qImg))
# self.s.setSceneRect(0.0, 0.0, w, h)
# if self.scale:
# self.graphicsView.fitInView(self.s.sceneRect())
# else:
# if self.need_fit:
# self.graphicsView.fitInView(self.s.sceneRect(), Qt.AspectRatioMode.KeepAspectRatio)
# self.need_fit = False
#
# RGB888Msg
class ImageQt(Node):

View File

@ -1,20 +1,12 @@
import inspect
from pathlib import Path
import cupyx
import cv2
import matplotlib
import numpy as np
import cupy as cp
import cupyx
import cupyx.scipy.fft
import cupyx.scipy.ndimage
import cupyx.scipy.signal
import cv2
import scipy
import scipy.signal
from cupyx.scipy.fft import dctn, idctn
# from cupyx.scipy.fft import dctn, idctn
from scipy.stats import norm as norms
from flandre.utils.RfFrame import RfFrame, RfFrameFile, RfFrameMemory
@ -136,11 +128,12 @@ class RfMat:
return None
m = np.frombuffer(frame.__bytes__(), dtype=np.int16).reshape(seq_meta.shape).copy()
if device == 'gpu':
import cupy as cp
m = cp.asarray(m)
return RfMat(m, frame.meta, seq_meta)
def __init__(self,
data: cp.ndarray,
data: np.ndarray,
frame_meta: RfFrameMeta = None,
seq_meta: RfSequenceMeta = None,
):
@ -150,7 +143,7 @@ class RfMat:
self.seq_meta = seq_meta
if isinstance(data, np.ndarray):
self.device = 'cpu'
elif isinstance(data, cp.ndarray):
elif type(data).__module__ == 'cupy' and type(data).__name__ == 'ndarray':
self.device = 'gpu'
else:
raise NotImplementedError
@ -183,6 +176,7 @@ class RfMat:
def p(self):
if self.device == 'cpu':
return np
import cupy as cp
return cp
def __bytes__(self):
@ -212,7 +206,7 @@ class RfMat:
s = p.zeros_like(h) + 1
v = p.zeros_like(h) + 1
hsv = p.stack((h, s, v), axis=2)
if p == cp:
if self.device == 'gpu':
rgb = hsv_to_rgb(hsv.get())
else:
rgb = hsv_to_rgb(hsv)
@ -304,7 +298,8 @@ class RfMat:
def dct(self, mmin, mmax):
dct_ = scipy.fft.dct
idct = scipy.fft.idct
if self.p == cp:
if self.device == 'gpu':
import cupyx.scipy.fft
dct_ = cupyx.scipy.fft.dct
idct = cupyx.scipy.fft.idct
m_dct = dct_(self.m)
@ -327,7 +322,8 @@ class RfMat:
arg = scipy.signal.argrelextrema
m = self.m
p = self.p
if p == cp:
if self.device == 'gpu':
import cupyx.scipy.signal
arg = cupyx.scipy.signal.argrelextrema
rm = p.zeros_like(m)
indies1 = arg(m, p.greater, axis=axis)
@ -346,13 +342,14 @@ class RfMat:
cv = scipy.ndimage.convolve1d
m = self.m
p = self.p
if p == cp:
if self.device == 'gpu':
import cupyx.scipy.ndimage
cv = cupyx.scipy.ndimage.convolve1d
rv = norms(loc=0, scale=b)
x2 = np.arange(-1, 1.1, 0.1)
w = rv.pdf(x2)
if p == cp:
w = cp.asarray(w)
if self.device == 'gpu':
w = p.asarray(w)
rm = cv(m, w, axis=axis)
return self.copy(rm)

5
test/type_test.py Normal file
View File

@ -0,0 +1,5 @@
import cupy as cp
if __name__ == '__main__':
p = cp.ndarray([1,2,3])
print(p.__class__.__name__)
print(type(p).__qualname__)