コード例 #1
0
 def __init__(self, fmt, maxsize):
     self.fmt = fmt
     self.itemsize = struct.calcsize(fmt)
     self.maxsize = maxsize
     self.values = memoryview(RawArray("b", maxsize * self.itemsize))
     self.start = RawValue(ctypes.c_longlong, 0)
     self.startlock = multiprocessing.Lock()
     self.getsem = multiprocessing.Semaphore(0)
     self.stop = RawValue(ctypes.c_longlong, 0)
     self.stoplock = multiprocessing.Lock()
     self.putsem = multiprocessing.Semaphore(maxsize)
コード例 #2
0
    def __init__(self):
        params = {
            'chords_amp': RawValue(ctypes.c_float),
            'chords_chroma': RawArray(ctypes.c_float, 12 * [0.]),
            'chords_mfcc': RawArray(ctypes.c_float, 64 * [0.]),
            'chords_dissonance': RawValue(ctypes.c_float),
            'bass_amp': RawValue(ctypes.c_float),
            'bass_pitch': RawValue(ctypes.c_float),
            'bass_has_pitch': RawValue(ctypes.c_float),
            'drums_amp': RawValue(ctypes.c_float),
            'drums_onset': RawValue(ctypes.c_float),
            'drums_centroid': RawValue(ctypes.c_float),
        }

        params['chords_amp'].value = 0.
        params['chords_dissonance'].value = 0.
        params['bass_amp'].value = 0.
        params['bass_pitch'].value = 0.
        params['bass_has_pitch'].value = 0.
        params['drums_amp'].value = 0.
        params['drums_onset'].value = 0.
        params['drums_centroid'].value = 0.

        self.generator = Generator(params)
        self.osc = OSCServer(params)

        self.exit = Event()
コード例 #3
0
 def __init__(self, total: int, length: int, precision: int = 0) -> None:
     self.bar_length = length
     # Using synchronized Values allows us to keep the same value for all child processes. No
     # need for locks, since access to __call__ should always be externally synchronized anyway.
     self.first = RawValue(c_bool, True)
     self.loaded = RawValue(c_size_t, 0)
     self.total = total
     if DEBUG:
         self.total_str = f"(<DEBUG>{{}}/{self.total}) "
     # Display new_percentage next to loading bar:
     self.perc = RawValue(c_float, 0.)
     self.precision = precision
     self.format = max(0, self.precision)
     # Number of "full" characters in the loading bar:
     self.fill = RawValue(c_ubyte, 0)
コード例 #4
0
    def __init__(self, _v=None, maxsize=10240, serializing=json, szarg=None):
        class ShareStructure(ctypes.Structure):
            _fields_ = [("len", ctypes.c_long), ("md5", ctypes.c_char * 32),
                        ("data", ctypes.c_char * maxsize)]

        if serializing == 'json':
            serializing = json
            if szarg == None:
                szarg = ({'encoding': 'utf-8'}, {'encoding': 'utf-8'})
        elif serializing == 'pickle':
            serializing = cPickle

        if szarg == None:
            szarg = ({}, {})

        self.maxsize = maxsize
        self.szarg = szarg
        self.sz = serializing

        self.sharestruct = ShareStructure

        s = ShareStructure(0, '', '')

        self.sharespace = RawValue(ShareStructure, 1)

        self.lock = threading.Lock()

        self.value = _v

        property.__init__(self, self.value_getter, self.value_setter)
    def __init__(self, capacity, data_size, data_tree = None, policy = 'random', passes_before_random = 0.):
        if data_tree == None:
            self.capacity = capacity  # for all priority values
        # calculate singular layers where it cannot be fully devided by 2:
            width = 1; self.num_of_nodes = 0
            while width < capacity:
                self.num_of_nodes += width
                width *= 2

            self.tree_buffer = RawArray('d', self.num_of_nodes + capacity)
            self.tree = np.frombuffer(self.tree_buffer,dtype='float64')
        # [--------------Parent nodes-------------][-------leaves to recode priority-------]
        #             size: self.num_of_nodes                   size: capacity
            self.data_buffer = RawArray('f', capacity*data_size)
            self.data = np.frombuffer(self.data_buffer,dtype='float32').reshape((capacity,data_size))  # for all transitions
            self.data_size = data_size
        # [--------------data frame-------------]
        #             size: capacity
            self.len = RawValue('i',0)
            self.passes = - passes_before_random
            assert self.passes <= 0

            #self.childrens = []
            if policy == 'sequential': self.sequential = True
            elif policy == 'random': self.sequential = False

        else:
            self.capacity, self.len, self.passes, self.sequential, self.data_size, self.num_of_nodes = capacity, data_tree.len, data_tree.passes, data_tree.sequential, data_tree.data_size, data_tree.num_of_nodes
            self.data = np.frombuffer(data_tree.data_buffer,dtype='float32').reshape((self.capacity,self.data_size))
            self.tree = np.frombuffer(data_tree.tree_buffer,dtype='float64')
コード例 #6
0
    def __init__(self, cellular_automaton, process_count: int = 2):
        multiprocessing.freeze_support()
        if process_count < 1:
            raise ValueError

        super().__init__(cellular_automaton)

        self.evolve_range = range(len(self._ca.cells))
        self._ca.current_evolution_step = RawValue(
            c_int, self._ca.current_evolution_step)
        self.__init_processes(process_count)
 def __init__(self, train, num_of_processes, others=''):
     self.train = train
     self.processes = []
     self.actor_update_time = 10.
     self.lr_step = 0
     self.pending_training_updates = Value('d', 0., lock=True)
     # somehow RawValue also needs us to call ".value", otherwise it says the type is c_double or c_int
     self.episode = RawValue('i', 0)
     self.t_done = Value('d', 0., lock=True)
     self.last_achieved_time = RawValue('d', 0.)
     # set the data going to subprocesses:
     self.train.memory.start_proxy_process(
         (self.pending_training_updates, self.episode, self.t_done,
          self.last_achieved_time), self.train.transitions_storage,
         (self.train.batch_size, self.train.memory.tree.data_size))
     # the following will create threads, which not end and cause error (not exiting)
     spawn = mp.get_context('spawn')
     self.manager = spawn.Manager()
     self.MemoryInputQueue = self.manager.Queue()
     self.end_event = self.manager.Event()
     self.pause_event = self.manager.Event()
     self.learning_in_progress_event = self.manager.Event()
     # actors
     self.ActorReceivePipe, self.ActorUpdatePipe = spawn.Pipe(
         False
     )  # unidirectional pipe that send message from conn2 to conn1
     seed = random.randrange(0, 9999999)
     self.worker_manager = spawn.Process(
         target=worker_manager,
         args=(copy.deepcopy(train.net).cpu(),
               (self.MemoryInputQueue, self.ActorReceivePipe,
                self.end_event, self.pause_event), num_of_processes,
               seed, others))
     # store and manage experience (including updating priority and potentially sampling out replays)
     # all the arguments passed into it are used (**by fork initialization in RL module**).
     # somehow RawValue also needs us to call ".value" ? Otherwise it says the type is c_double / c_int
     self.train.memory.set_memory_source(
         self.MemoryInputQueue, (self.pause_event, self.end_event,
                                 self.learning_in_progress_event))
     self.backup_period = self.train.backup_period
     self.train.backup_period = 100
コード例 #8
0
    def __init__(self, cfg):
        super().__init__(cfg)

        self.processes = []
        self.terminate = RawValue(ctypes.c_bool, False)

        self.start_event = multiprocessing.Event()
        self.start_event.clear()

        self.report_queue = Queue()
        self.report_every_sec = 1.0
        self.last_report = 0

        self.avg_stats_intervals = (1, 10, 60, 300, 600)
        self.fps_stats = deque([], maxlen=max(self.avg_stats_intervals))
コード例 #9
0
def test_futex_handshake():
    print()

    print(os.uname().sysname)

    runner_class = threading.Thread
#     runner_class = multiprocessing.Process

    resource = RawValue(ctypes.c_int)
    assert resource.value == 0

    trace_list = RawArray(ctypes.c_float, 4)
    for index in range(4):
        assert trace_list[index] == 0

    def reader_code():
        trace_list[0] = time.time()
        print("reader#1")
        invoke_futex_wait(resource)  # @UndefinedVariable
        print("reader#2")
        time.sleep(0.1)
        print("reader#3")
        trace_list[1] = time.time()

    def writer_code():
        print("writer#1")
        time.sleep(0.1)
        trace_list[2] = time.time()
        print("writer#2")
        invoke_futex_wake(resource)  # @UndefinedVariable
        print("writer#3")
        trace_list[3] = time.time()

    reader_proc = runner_class(target=reader_code, daemon=True)
    writer_proc = runner_class(target=writer_code, daemon=True)

    reader_proc.start()
    writer_proc.start()

#     reader_proc.join()
#     writer_proc.join()

    for index in range(3):
        print(f"{index} trace_list={trace_list}")
        time.sleep(1)
コード例 #10
0
    def __init__(self, fn, *args, **kwargs):
        multiprocessing.Process.__init__(self)
        self.fn = fn
        self.args = args
        self.kwargs = kwargs

        # Create return pipe for return value
        self.return_pipe = Pipe(duplex=False)

        # Create 64 byte unique id based on network address, sequence number and time sample.
        self.id = uuid.uuid1().hex + "." + fn.func_name[:31]

        # Channel request state
        self.cond = None
        self.state = FAIL
        self.result_ch_idx = None
        self.result_msg = None

        # Used to wait for acknowledgements from the RemoteLock
        self.ack = False

        # Used to ensure the validity of the remote answers
        self.sequence_number = 1

        # Protect against early termination of mother-processes leavings childs in an invalid state
        self.spawned = []

        # Protect against early termination of channelhomes leaving processes in an invalid state
        self.registeredChanHomeList = []
        self.registeredChanConnectList = []

        # Protect against early termination of processes leaving channelhomes in an invalid state
        self.activeChanList = []
        self.closedChanList = []

        # Identify this as a wrapped pycsp process, which must not be terminated by shutdown
        self.maintained = True

        # report execution error
        self._error = RawValue('i', 0)
コード例 #11
0
 def get_shared_queue(size):
     queue = Array(Message, size)
     init_queue(queue, RawValue('i', 0), Condition(queue.get_lock()),
                Condition(queue.get_lock()))
     return queue
コード例 #12
0
 def make(cls) -> ctypes.Structure:
     return RawValue(StatusStore)
コード例 #13
0
from multiprocessing import Process
from multiprocessing.sharedctypes import RawValue
import ctypes


def f(n):
    n.value = 'hello!!'


if __name__ == '__main__':
    num = RawValue(ctypes.c_wchar_p, 'abc')

    p = Process(target=f, args=(num, ))
    p.start()
    p.join()

    print(num.value)
コード例 #14
0
"""

import pigpio
from multiprocessing import Process
from multiprocessing.sharedctypes import RawValue
from collections import deque
from time import sleep

# set up
left_sensor_gpio = 27
right_sensor_gpio = 17
cache_width = 3
time_out = 135
time_width_restr = 20000
# shared value
left_speed = RawValue('f', 0)
right_speed = RawValue('f', 0)
# shared lock
#ls_lock = Lock()
#rs_lock = Lock()


def init_monitor(ls, rs):
    gpio = pigpio.pi()
    gpio.set_mode(left_sensor_gpio, pigpio.INPUT)
    gpio.set_mode(right_sensor_gpio, pigpio.INPUT)
    left_edge_time_tick = gpio.get_current_tick()
    right_edge_time_tick = gpio.get_current_tick()
    left_speed_queue = deque(maxlen=cache_width)
    right_speed_queue = deque(maxlen=cache_width)
コード例 #15
0
 def __init__(self, value=0):
     # RawValue because we don't need it to create a Lock:
     self.val = RawValue('i', value)
     self._val = value
     self.lock = Lock()
コード例 #16
0
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        # 图像大小
        self.img_shape = (480, 720)

        # 初始化界面
        self.label_imgshow.setScaledContents(True)  # 图片自适应显示
        self.label_imgshow_res.setScaledContents(True)  # 检测结果图片自适应显示
        self.img_none = np.ones((480, 720, 3), dtype=np.uint8) * 255
        self.show_img(self.img_none)

        # SSD检测初始化
        self.weight_path = './ssd/weights/weights_SSD300.hdf5'
        self.weight_path = os.path.normpath(os.path.abspath(self.weight_path))
        self.obj_names = [
            'Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle', 'Bus', 'Car',
            'Cat', 'Chair', 'Cow', 'Diningtable', 'Dog', 'Horse', 'Motorbike',
            'Person', 'Pottedplant', 'Sheep', 'Sofa', 'Train', 'Tvmonitor'
        ]
        # 需要显示的目标list, 用于过滤
        self.include_class = self.obj_names

        # 检测子进程
        self.queue = Queue()
        # 多进程之间的共享图片内存
        self.img_share = RawArray('I',
                                  self.img_shape[0] * self.img_shape[1] * 3)
        # 标识当前进程的状态,非0:保持检测;0:停止检测
        self.process_flg = RawValue('I', 1)
        # 当前图像共享内存 img_share 的状态,非0:主进程使用中;0:子进程使用中
        self.img_get_flg = RawValue('I', 1)
        # 创建检测子进程
        self.detector_process = Process(target=detector_process,
                                        args=(self.img_share, self.img_shape,
                                              self.process_flg,
                                              self.img_get_flg, self.queue))
        self.detector_process.start()

        # 接收检测结果的线程
        self.recv_thread = Recv_res(parent=self, queue=self.queue)
        # 连接信号
        self.recv_thread.res_signal.connect(self.show_res)
        self.recv_thread.start()

        # 视频流URL
        self.camera_index = 0
        self.FPS = None

        # 初始化计时器
        self.timer = QTimer(self)  # 更新计时器
        self.timer.timeout.connect(self.timer_update)  # 超时信号连接对应的槽函数

        # 等待加载模型
        self.textEdit.setText('正在加载模型,请稍后......')
        self.pushButton_start.setEnabled(False)
        self.pushButton_open.setEnabled(False)
        self.pushButton_pause.setEnabled(False)
        self.lineEdit_cameraIndex.setEnabled(False)

        # 暂停初始化为不暂停
        self.pause = False
コード例 #17
0
def RawValue(typecode_or_type, *args):
    from multiprocessing.sharedctypes import RawValue
    return RawValue(typecode_or_type, *args)
コード例 #18
0
def shared_value(default, dtype="float"):
    if dtype in typemap.keys():
        return RawValue(typemap[dtype], default)
    else:
        raise ValueError
コード例 #19
0
ファイル: BQ_CamMP.py プロジェクト: hanhiver/welding_seam
    def __init__(self,
                 logger_manager,
                 filename=None,
                 cam_ip=None,
                 width=1920,
                 height=1200,
                 auto_expose=True,
                 auto_balance=True):

        self.logger = logger_manager.get_logger("BQ_Cam")
        self.sched_run = None
        if filename is None:
            # 设置相机模式标识。
            self.mode = 0
            # 相机模式分辨率自设,默认1920*1200
            (self.w, self.h) = (width, height)
            self.cam_ip = cam_ip
            self.auto_expose = auto_expose
            self.auto_balance = auto_balance
            self.logger.debug("网络相机模式,IP: {}, (w, h): ({}, {}).".format(
                cam_ip, self.w, self.h))
        else:
            # 设置文件模式标识。
            self.mode = 1
            # 文件模式获取分辨率
            (self.w, self.h) = gige.get_resolution(filename)
            self.logger.debug("文件读取模式: {}, (w, h): ({}, {}).".format(
                filename, self.w, self.h))

        self.width = self.w
        self.height = self.h

        # 准备进程锁和进程间共享空间。
        self.process_lock = multiprocessing.Lock()
        self.array_temp = np.ones(shape=(self.h * self.w * 3), dtype=np.ubyte)
        self.shared_array = RawArray(ctypes.c_ubyte, self.array_temp)
        self.shared_value = RawValue(ctypes.c_uint, 0)
        self.logger.debug("进程共享内存准备完毕。")

        if self.mode == 0:
            self.sched_run = schedrun.SchedRun(
                func=gige.get_frame_from_camera,
                args=(
                    self.shared_array,
                    self.shared_value,
                    self.process_lock,
                    False,
                ),
                init_func=gige.init_camera,
                init_args=(self.cam_ip, self.w, self.h, self.auto_expose,
                           self.auto_balance),
                clean_func=gige.close_camera,
                clean_args={},
                interval=0.0,
                init_interval=0.0)
        else:
            self.sched_run = schedrun.SchedRun(func=gige.get_frame_from_file,
                                               args=(
                                                   self.shared_array,
                                                   self.shared_value,
                                                   self.process_lock,
                                               ),
                                               init_func=gige.init_file,
                                               init_args=(filename, ),
                                               clean_func=gige.close_file,
                                               clean_args={},
                                               interval=0.025,
                                               init_interval=0.0)

        self.logger.debug("帧读取进程启动。")

        #while self.shared_value != 0:

        for i in range(10):
            if self.shared_value == 0:
                self.logger.debug("帧读取进程没准备好,等待100ms。")
                # 相机进程还没有准备好。
                time.sleep(0.1)

        if self.shared_value == 0:
            return None
コード例 #20
0
ファイル: benchmark.py プロジェクト: tibordp/pyncette
    run_option = subparsers.add_parser("run", help="Run the Pyncette app")
    run_option.add_argument(
        "--processes", type=int, default=1, help="Number of processes to run"
    )
    run_option.add_argument(
        "--partition-count",
        type=int,
        default=PARTITION_COUNT,
        help="How many partitions each process should poll",
    )

    options = parser.parse_args()
    setup(options.log_level)

    if options.command == "run":
        hit_count = [RawValue("l", 0) for _ in range(options.processes)]
        staleness = [RawValue("f", 0) for _ in range(options.processes)]

        if options.partition_count * options.processes < PARTITION_COUNT:
            logger.warning(
                f"partition_count * processes < {PARTITION_COUNT}. Not all partitions will be processed."
            )

        for i in range(options.processes):
            enabled_partitions = sorted(
                (i * options.partition_count + j) % PARTITION_COUNT
                for j in range(options.partition_count)
            )

            job = Process(
                target=_run,
コード例 #21
0
ファイル: GigE_Daheng.py プロジェクト: hanhiver/welding_seam
def main1():
    process_lock = multiprocessing.Lock()
    array_temp = np.ones(shape=(HEIGHT * WIDTH * 3), dtype=np.ubyte)
    shared_array = RawArray(ctypes.c_ubyte, array_temp)
    shared_value = RawValue(ctypes.c_uint, 0)

    sched_run = schedrun.SchedRun(func=get_frame_from_camera,
                                  args=(
                                      shared_array,
                                      shared_value,
                                      process_lock,
                                      False,
                                  ),
                                  init_func=init_camera,
                                  init_args=(
                                      WIDTH,
                                      HEIGHT,
                                  ),
                                  clean_func=close_camera,
                                  clean_args={},
                                  interval=0.0,
                                  init_interval=0.0)

    cv2.namedWindow("result", cv2.WINDOW_NORMAL)
    #cv2.resizeWindow("result", 800, 500)
    #cv2.moveWindow("result", 100, 100)

    accum_time = 0
    curr_fps = 0
    prev_time = time.time()
    show_fps = "Show FPS: ??"
    gige_fps = "GigE FPS: ??"
    font_scale = WIDTH // 800 + 1

    while True:
        process_lock.acquire()
        frame = np.array(shared_array, dtype=np.uint8)
        process_lock.release()
        frame = frame.reshape((HEIGHT, WIDTH, 3))

        curr_time = time.time()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1
        if accum_time > 1:
            accum_time = accum_time - 1
            show_fps = "Show FPS: " + str(curr_fps)
            curr_fps = 0

        if shared_value.value > 0:
            gige_fps = "GigE FPS: " + str(shared_value.value)

        #fps = gige_fps + " VS " + show_fps
        cv2.putText(frame,
                    text=gige_fps,
                    org=(30, 80),
                    fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                    fontScale=font_scale,
                    color=(0, 0, 255),
                    thickness=2)
        cv2.putText(frame,
                    text=show_fps,
                    org=(30, 160),
                    fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                    fontScale=font_scale,
                    color=(0, 0, 255),
                    thickness=2)

        cv2.imshow('result', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            sched_run.stop()
            cv2.destroyAllWindows()
            return True

    # Normally, should not be here.
    sched_run.stop()
    cv2.destroyAllWindows()
    print('Oops, something wrong')
    return False
コード例 #22
0
def master_parallel(recording=True, to_video=False):
    # init variables
    wait_time = 5

    #output settings
    frame_gap = 1 / G.FPS  #0.0002 add this line when print pace and perf_counter() are used
    init_buffer = -int(G.FPS * wait_time)

    #shared variables
    count = RawValue('i', init_buffer)
    control = RawValue(
        'i', 1
    )  # start at 1 so that control depended process start only when count >= 0
    title = RawValue(
        'i', 0)  # file will start to save after the first array is completed
    switch = RawValue('i', 1)
    lock = Lock()

    sync_processes = []
    arrays = []
    data = []

    for ID in range(G.cam_num):
        array_0 = shared_array(G.Tshape, np.uint8)
        array_1 = shared_array(G.Tshape, np.uint8)

        img_0 = shared_array(G.Ishape, np.uint8)
        img_1 = shared_array(G.Ishape, np.uint8)
        data_0 = shared_array(G.kp_shape, np.int16)
        data_1 = shared_array(G.kp_shape, np.int16)

        process_read = Process(target=cam_read,
                               args=(ID, count, switch, array_0, array_1))
        process_infer = Process(target=array_inference,
                                args=(ID, count, control, switch, array_0,
                                      array_1, img_0, img_1, data_0, data_1))

        sync_processes = sync_processes + [process_read] + [process_infer]
        arrays = arrays + [array_0] + [array_1]
        data = data + [img_0] + [img_1] + [data_0] + [data_1]

    if recording:
        central_0 = shared_array(G.Fshape, np.uint8)
        central_1 = shared_array(G.Fshape, np.uint8)
        process = Process(target=fill_array,
                          args=(count, control, title, switch, central_0,
                                central_1, *arrays))
        sync_processes.append(process)
        process = Process(target=save_array,
                          args=(count, title, switch, central_0, central_1))
        sync_processes.append(process)

    process_post = Process(target=post_process,
                           args=(count, control, switch, *data))
    process_pace = Process(target=pace_maker,
                           args=(frame_gap, count, control, title, lock,
                                 switch))
    sync_processes = sync_processes + [process_post] + [process_pace]

    for i in range(len(sync_processes)):
        sync_processes[i].start()

    for i in range(len(sync_processes)):
        sync_processes[i].join()

    if to_video:
        read_h5()
コード例 #23
0
def wsVideoPhaseMP(input, output, local_view = True, arduino = False, time_debug = False, simple_show = True):
    import gxipy as gx 
    import schedrun
    import multiprocessing
    import ctypes
    from multiprocessing.sharedctypes import RawArray, RawValue
    from mptest import init_camera, close_camera, get_frame_from_camera
    from mptest import init_file, close_file, get_frame_from_file

    print("Multiprocess Mode. ")
    time.sleep(0.05)

    process_lock = multiprocessing.Lock()
    array_temp = np.ones(shape = (1200 * 1920 * 3), dtype = np.ubyte)
    shared_array = RawArray(ctypes.c_ubyte, array_temp)
    shared_value = RawValue(ctypes.c_uint, 0)

    if input[0] == '0':
        sched_run = schedrun.SchedRun(func = get_frame_from_camera, args = (shared_array, shared_value, process_lock, False, ), 
                                      init_func = init_camera, init_args = (1920, 1200, False, False),
                                      clean_func = close_camera, clean_args = {}, 
                                      interval = 0.0, 
                                      init_interval = 0.0)
    else:
        sched_run = schedrun.SchedRun(func = get_frame_from_file, args = (shared_array, shared_value, process_lock, ), 
                                      init_func = init_file, init_args = (input[0], ),
                                      clean_func = close_file, clean_args = {}, 
                                      interval = 0.0, 
                                      init_interval = 0.0)
    
    W = 1920//2
    H = 720//2
    RESOLUTION = (W*2, H*2)
    HALF_RESOLUTION = (W, H)
    
    center_list = []

    if time_debug:
        time_stamp = time.time()
        print(time_stamp, ': time debug enabled.')

    # Initialize the arduino serial communication. 
    if arduino is True:
        AS_device = AS.arduino_serial('/dev/ttyUSB0')
        ret = AS_device.openPort()
        if ret is False:
            print('Failed to open the Arduino Serial for communication. ')
            return

    isOutput = True if output != "" else False
    if isOutput:
        if simple_show: 
            output_res = (1920, 720)
        else:
            output_res = (600, 750)

        video_FourCC = cv2.VideoWriter_fourcc(*'DIVX')
        #video_FourCC = -1
        video_FourCC = cv2.VideoWriter_fourcc("m", "p", "4", "v")
        out = cv2.VideoWriter(output, video_FourCC, 50, output_res)
        out_opened = out.isOpened()
        if out_opened:
            print('OUT Opened: isOpened(): {}. '.format(out_opened))
        else:
            print('OUT Closed: isOpened(): {}. '.format(out_opened))
            return

    print("=== Start the WS detecting ===")

    print('Load C lib. ')
    so_file = './libws_c.so'
    lib = ctypes.cdll.LoadLibrary(so_file)

    lib.testlib()

    kernel = np.ones((5,5),np.uint8)

    # 为normalizeCenter准备数据数组。
    center_array = []
    dropped_array = []

    if local_view:
        #cv2.namedWindow("result", cv2.WINDOW_NORMAL)
        cv2.namedWindow("result")
        #cv2.resizeWindow("result", 1600, 800)
        #cv2.moveWindow("result", 100, 100)

    if time_debug:
        time_cur = time.time()
        print('{:1.6f}: 初始化完成. '.format((time_cur - time_stamp) * 1000));
        time_stamp = time_cur
        time_frame = time_cur
        time_dur_accum = 0

    while True:
        
        if time_debug:
            #print('[{:3.3f} ms]: 帧间时间. '.format((time.time() - time_stamp)*1000));
            print('[{:3.3f} - {:3.3f} ms]: 开始处理一帧画面. '.format((time.time() - time_frame)*1000, time_dur_accum))
            time_dur_accum = 0
    
        time_frame = time.time()

        if time_debug:
            time_stamp = time.time()

        process_lock.acquire()
        frame = np.array(shared_array, dtype = np.uint8)
        process_lock.release()
        if frame.max() < 5:
            continue

        frame = frame.reshape((1200, 1920, 3))

        if time_debug:
            time_dur = time.time() - time_stamp
            time_stamp = time.time()
            time_dur *= 1000
            time_dur_accum += time_dur
            print('\t[{:3.3f} ms]: 从输出源得到一帧画面. '.format(time_dur))
                
        # 根据图像特殊处理
        # ===========================
        frame = imgRotate(frame, ADJUST_ANGLE)
        # ===========================
        
        if type(frame) != type(None):
            
            # 根据摄像头摆放位置确定是否需要旋转图像。
            # 目前的处理逻辑是处理凸字形的焊缝折线。
            frame = np.rot90(frame, k = 0)

            # 根据摄像头摆放位置切除多余的干扰画面。
            # 目前这个设置是基于7块样板的图像进行设置。
            # 未来这里会在GUI界面中可以设置,排除不必要的干扰区域。
            (h, w) = frame.shape[:2]

            # 对应12mm镜头,切除左右各1/4,切除下方1/4的画面
            # ===========================
            #frame = frame[0:3*h//4, w//4:3*w//4]
            # ===========================

            # 对应16mm镜头,暂时不切除。
            # ===========================
            #frame = frame[2*h//5:h, 0:w]
            frame = frame[0:h, 0:w]
            # ===========================

            #frame = frame[4*h//9:5*h//9, 5*w//13:7*w//12]

            if len(frame.shape) > 2:
                color_input = True
            else:
                color_input = False

            #frame = cv2.resize(frame, RESOLUTION, interpolation = cv2.INTER_LINEAR)
            (h, w) = frame.shape[:2]

            #frame = cv2.medianBlur(frame, 5)
            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 图像输入预处理完成. '.format(time_dur))
                time_stamp = time.time()

            if color_input:
                # Get the blue image. 
                #b, r, g = cv2.split(frame)
                #n = 50
                #filt = (g.clip(n, n+1) - n) * 255 
                #filt = r//3 + g//3 + b//3
                filt = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
                #filt = r
            else:
                filt = frame

            mean = filt.mean()
            #black_limit = (int)(mean * 8)
            black_limit = (int)(mean * 5)
            if black_limit > 245:
                black_limit = 245

            if black_limit < 3:
                black_limit = 3

            #print('MEAN: ', filt.mean(), ' BLACK_LIMIT: ', black_limit)

            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 分色和黑场检测完成. '.format(time_dur))

            coreline = getLineImage(lib, filt, black_limit = black_limit, correct_angle = False)

            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 基准线检测完成. '.format(time_dur))
            
            #coreline = cutLowPixels(lib, coreline, low_level_limit = 10)
            gaps = fillLineGaps(lib, coreline, start_pixel = 5)
            result = gaps + coreline
            #result = coreline

            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 缺损检测以及填充完成. '.format(time_dur))

           
            b_center, b_level, bound = getBottomCenter(lib, result, bottom_thick = BOTTOM_THICK, noisy_pixels = NOISY_PIXELS)
                        
            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 焊缝中心识别完成. '.format(time_dur))

            # 将center的输出值进行normalize处理,消除尖峰噪音干扰。
            b_center, center_array, dropped_array = normalizeCenter(center_array, b_center, thres_drop = 100, thres_normal = 50, move_limit = 3, skip = False, dropped_array = dropped_array)

            # 因为目前采用的分辨率是模拟屏幕的5倍,为了对应当前逻辑和减少抖动,输出值除以3取整。
            real_center = int(b_center / 3)
            center_list.append(real_center)

            if time_debug:
                time_dur = time.time() - time_stamp
                time_stamp = time.time()
                time_dur *= 1000
                time_dur_accum += time_dur
                print('\t[{:3.3f} ms]: 焊缝中心尖峰降噪完成. '.format(time_dur))

            # 如果我们开启了arduino serial通讯,这里拼凑坐标传送给机器人。
            if arduino is True:

                # 这个地方的b_center就是焊缝识别得到的中点坐标。
                # 我们在这里用传统380线分辨率的做一个规一划处理。
                # real_center = b_center * 380 // w
            
                ####################################
                # 这个地方请修改代码,是否应该将real_center的值转化为16进制的数字,
                # 通信的高地位分别怎么设置,我这里就只是用了你示例代码中的通信标志。
                ####################################
                AS_device.writePort('E7E701450124005A0008004EFE')
                if time_debug:
                    time_dur = time.time() - time_stamp
                    time_stamp = time.time()
                    time_dur *= 1000
                    time_dur_accum += time_dur
                    print('\t[{:3.3f} ms]: 坐标写入串口完成. '.format(time_dur))

            if not color_input:
                frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)


            if simple_show:
                mix_image = fill2ColorImage(lib, frame, result, fill_color = (255, 0, 0))
                if not DRAW_BOUND:
                    bound = None

                if not DRAW_BOTTOM:
                    bottom_thick = None
                else: 
                    bottom_thick = BOTTOM_THICK

                drawTag(frame, b_center, b_level, bottom_thick = bottom_thick, bound = bound)
                #drawTag(frame, b_center, b_level)
                images = frame
                #images = cv2.resize(frame, HALF_RESOLUTION, interpolation = cv2.INTER_LINEAR)

                if time_debug:
                    time_dur = time.time() - time_stamp
                    time_stamp = time.time()
                    time_dur *= 1000
                    time_dur_accum += time_dur
                    print('\t[{:3.3f} ms]: 图像输出标记完成. '.format(time_dur))

                
            else: #simple_show  
                mix_image = fill2ColorImage(lib, frame//2, result, fill_color = (255, 0, 0))
                mix_image = fill2ColorImage(lib, mix_image, gaps, fill_color = (0, 255, 0))

                drawTag(mix_image, b_center, b_level)

                result = cv2.cvtColor(result, cv2.COLOR_GRAY2RGB)
                
                drawTag(result, b_center, b_level)
                drawTag(frame, b_center, b_level)
                
                if time_debug:
                    time_dur = time.time() - time_stamp
                    time_stamp = time.time()
                    time_dur *= 1000
                    time_dur_accum += time_dur
                    print('\t[{:3.3f} ms]: 图像输出标记完成. '.format(time_dur))
                
                fill_black = np.zeros(shape = (RESOLUTION[1], RESOLUTION[0], 3))

                #frame = cv2.resize(frame, HALF_RESOLUTION, interpolation = cv2.INTER_LINEAR)
                result = cv2.resize(result, HALF_RESOLUTION, interpolation = cv2.INTER_LINEAR)
                mix_image = cv2.resize(mix_image, HALF_RESOLUTION, interpolation = cv2.INTER_LINEAR)

                image2 = np.hstack([mix_image, result])
                images = np.vstack([frame, image2])
                images = cv2.resize(images, (600, 750), interpolation = cv2.INTER_LINEAR_EXACT)

            center_string = "Center: " + str(real_center)
            center_string += " --  TOF: {:3.3f}ms".format((time.time() - time_frame) * 1000)

            cv2.putText(images, text=center_string, org=(30, 30), fontFace=cv2.FONT_HERSHEY_TRIPLEX, 
                    fontScale=0.5, color=(255, 255, 255), thickness=1)
            #print(images.shape)
            
            if local_view:
                #images = coreline
                cv2.imshow("result", images)
                
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

                if time_debug:
                    time_dur = time.time() - time_stamp
                    time_stamp = time.time()
                    time_dur *= 1000
                    time_dur_accum += time_dur
                    print('\t[{:3.3f} ms]: 图像屏幕输出完成. '.format(time_dur))
             
            if isOutput:
                out.write(images)

                if time_debug:
                    time_dur = time.time() - time_stamp
                    time_stamp = time.time()
                    time_dur *= 1000
                    time_dur_accum += time_dur
                    print('\t[{:3.3f} ms]: 图像文件输出完成. '.format(time_dur))

        else:
            break

    sched_run.stop()
    
    plt.figure()
    x = range(len(center_list))
    plt.plot(x, center_list)
    plt.ylim(0, max(center_list) + 10)
    plt.show()

    if local_view:
        cv2.destroyAllWindows()
コード例 #24
0
ファイル: __init__.py プロジェクト: webiumsk/WOT-0.9.17-CT
def RawValue(typecode_or_type, *args):
    """
    Returns a shared object
    """
    from multiprocessing.sharedctypes import RawValue
    return RawValue(typecode_or_type, *args)
コード例 #25
0
 def __init__(self, start_loc, length, width):
     self._pos = RawArray('d', start_loc)
     self._vel = RawArray('d', 2)
     self._acc = RawArray('d', 2)
     self._rot = RawValue('d')
     self._sz = RawArray('d', (length, width))
コード例 #26
0
ファイル: GigE_Daheng.py プロジェクト: hanhiver/welding_seam
def main(filename):
    process_lock = multiprocessing.Lock()
    array_temp = np.ones(shape=(HEIGHT * WIDTH * 3), dtype=np.ubyte)
    shared_array = RawArray(ctypes.c_ubyte, array_temp)
    shared_value = RawValue(ctypes.c_uint, 0)

    sched_run = schedrun.SchedRun(func=get_frame_from_file,
                                  args=(
                                      shared_array,
                                      shared_value,
                                      process_lock,
                                  ),
                                  init_func=init_file,
                                  init_args=(filename, ),
                                  clean_func=close_file,
                                  clean_args={},
                                  interval=0.0,
                                  init_interval=0.0)

    cv2.namedWindow("result", cv2.WINDOW_NORMAL)
    #cv2.resizeWindow("result", 800, 500)
    #cv2.moveWindow("result", 100, 100)

    accum_time = 0
    curr_fps = 0
    prev_time = time.time()
    show_fps = "Show FPS: ??"
    gige_fps = "GigE FPS: ??"
    font_scale = WIDTH // 800 + 1

    while True:
        if shared_value.value > 10000:
            # Something wrong in the frame aquirement.
            break
        elif shared_value.value == 0:
            # Fram aquirement process is not ready.
            # sleep for 50ms.
            sleep(0.05)
            continue

        gige_fps = "GigE FPS: " + str(shared_value.value)

        process_lock.acquire()
        frame = np.array(shared_array, dtype=np.uint8)
        process_lock.release()
        frame = frame.reshape((HEIGHT, WIDTH, 3))

        curr_time = time.time()
        exec_time = curr_time - prev_time
        prev_time = curr_time
        accum_time = accum_time + exec_time
        curr_fps = curr_fps + 1
        if accum_time > 1:
            accum_time = accum_time - 1
            show_fps = "Show FPS: " + str(curr_fps)
            curr_fps = 0

        #fps = gige_fps + " VS " + show_fps
        cv2.putText(frame,
                    text=gige_fps,
                    org=(30, 80),
                    fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                    fontScale=font_scale,
                    color=(0, 0, 255),
                    thickness=2)
        cv2.putText(frame,
                    text=show_fps,
                    org=(30, 160),
                    fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                    fontScale=font_scale,
                    color=(0, 0, 255),
                    thickness=2)

        cv2.imshow('result', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    sched_run.stop()
    cv2.destroyAllWindows()
コード例 #27
0
ファイル: BufferMan.py プロジェクト: koshoid/picoDAQ
  def __init__(self, BMdict, DevConf):
    '''Args:  configuration dictionary
              Device Class             
    '''
 
    # read configuration dictionary
    if "NBuffers" in BMdict: 
      self.NBuffers = BMdict["NBuffers"] # number of buffers
    else:
      self.NBuffers= 16
    if "BMmodules" in BMdict: 
      self.BMmodules = BMdict["BMmodules"] # display modules to start
    else:
      self.BMmodules = []
    if "LogFile" in BMdict:
     self.LogFile = BMdict["LogFile"]
    else:
      self.LogFile = None
    self.flog = None    # file not yet open
    if "verbose" in BMdict: 
      self.verbose = BMdict["verbose"]
    else:
      self.verbose=1   # print (detailed) info if >0 
    if "logTime" in BMdict: 
      self.logTime = BMdict["logTime"] # display modules to start
    else:
      self.logTime = 60 # logging information once per 60 sec

# read device congiguration and set up Buffer space
    self.DevConf = DevConf  
    self.NChannels = DevConf.NChannels # number of channels in use
    self.NSamples = DevConf.NSamples   # number of samples 
    self.TSampling = DevConf.TSampling # sampling interval
    # function collecting data from hardware device
    self.rawDAQproducer = DevConf.acquireData 

# data structure for BufferManager in shared c-type memory ...
    self.CBMbuf = RawArray('f', 
                  self.NBuffers * self.NChannels * self.NSamples) 
    self.CtimeStamp = RawArray('f', self.NBuffers )
    self.CtrigStamp = RawArray('i', self.NBuffers )
#  ... and map to numpy arrays
    self.BMbuf = np.frombuffer(self.CBMbuf, 'f').reshape(self.NBuffers, 
        self.NChannels, self.NSamples)
    self.timeStamp = np.frombuffer(self.CtimeStamp, 'f')
    self.trigStamp = np.frombuffer(self.CtrigStamp, 'f')

    self.ibufr = RawValue('i', -1) # read index, synchronization with producer 

# global variables for producer statistics
    self.Ntrig = RawValue('i', 0)    # count number of readings
    self.Ttrig = RawValue('f', 0.)   # time of last event
    self.Tlife = RawValue('f', 0.)   # DAQ lifetime
    self.readrate = RawValue('f', 0) # current rate                
    self.lifefrac = RawValue('f', 0) # current life-time

# set up variables for Buffer Manager status and accounting  
    self.BMT0 = 0.
    self.tPause = 0.  # time when last paused
    self.dTPause = 0. # total time spent in paused state
    self.ACTIVE = RawValue('b', 0) 
    self.RUNNING = RawValue('b', 0)
    self.STOPPED = False

  # queues ( multiprocessing Queues for communication with sub-processes)
    self.prod_Que = Queue(self.NBuffers) # acquireData <-> manageDataBuffer
    self.request_Ques=[] # consumer request to manageDataBuffer
                # 0:  request event pointer, obligatory consumer
                # 1:  request event data, random consumer 
                # 2:  request event data, obligatoray consumer
    self.consumer_Ques=[] # data from manageDataBuffer to consumer

  # multiprocessing Queues for data transfer to subprocesses
    self.mpQues = []
    self.BMInfoQue = None

    self.BMlock = threading.Lock() 
    self.logQ = None

 # keep track of sub-processes started by BufferManager   
    self.procs=[] # list of sub-processes started by BufferMan
    self.thrds=[] # list of sub-processes started by BufferMan
コード例 #28
0
 def __init__(self, bam_bits):
     self.bam_bits = bam_bits
     self.buf = RawArray('B', Driver.MEM_SIZE * bam_bits)
     self.sp = None
     self.quit = RawValue('B', 0)
コード例 #29
0
 def __init__(self, initial_state=(0., ), draw_first_state=True):
     super().__init__(initial_state, draw_first_state)
     self._state_slots = [RawArray(c_float, initial_state) for i in range(self.__class__._state_save_slot_count)]
     self._active = [RawValue(c_bool, False) for i in range(self.__class__._state_save_slot_count)]
     self._active[0].value = True
     self._dirty = RawValue(c_bool, draw_first_state)