Многопроцессорный процесс Python, не помещающий функции в собственный процесс / ядро - PullRequest
0 голосов
/ 29 мая 2019

Я пытаюсь заставить мой скрипт работать на нескольких ядрах.Когда я запускаю скрипт, он работает, но, кажется, работает только на одном ядре.Я запускаю это на Raspberry Pi 3 b + с 4 ядрами, но при проверке с использованием top используется только одно.

Я пробовал использовать Pool и Process из модуля многопроцессорной обработки.У меня нет опыта программирования, но я уже писал базовые сценарии на Python, хотя впервые пытаюсь использовать многопроцессорность.

Извините, что поделился всем этим кодом, но я думаю, что это лучший способ показать, что япробовал.

import multiprocessing as mp
from multiprocessing import Process

import cv2

import face_pi_helper as fph


def put_frame_on_q(q_vid_cap, vid_cap):
    for i in range(5):
        frame = fph.fetch_frame(vid_cap)
    q_vid_cap.put(frame)


def detect_from_q(q_vid_cap, q_detections, net):
    print(fph.time_stamp(), "[INFO] starting detect from q...")
    while True:
        frame = q_vid_cap.get()
        if frame is None: 
            print(fph.time_stamp(), "[WARNING] No frame")
        else:
            detections = tuple(fph.detect(frame, net))
            d = detections[1]
            for f in d:
                conf = float(f[2])
                if conf > 0.9:
                    print(fph.time_stamp(), "[INFO] face found")

            q_detections.put(detections)


def identify_from_q(q_detections,
        vid_cap,
        known_face_encodings,
        known_face_names,
        q_vid_cap
        ):
    print(fph.time_stamp(), "[INFO] starting identify from q...")
    while True:
        detections = q_detections.get()
        if detections is None: 
            print(fph.time_stamp(), "[WARNING] No detections")
        else:
            frame = detections[0]
            fph.identify(
                frame,
                detections[1],
                known_face_encodings,
                known_face_names
                )

        put_frame_on_q(q_vid_cap, vid_cap)


if __name__ == "__main__":

    topology = "models/intel/face-detection-retail-0004.xml"
    weights = "models/intel/face-detection-retail-0004.bin"
    image = "images/adam.jpg"
    db_file = "data/allstars_faces_plus.json"

    print(fph.time_stamp(), "[INFO] starting app...")

    net = fph.load_model(topology, weights)
    known_face_encodings, known_face_names = fph.load_face_db(db_file)

    q_vid_cap = mp.Queue(1)
    q_detections = mp.Queue(1)

    vid_cap = cv2.VideoCapture(0)
    put_frame_on_q(q_vid_cap, vid_cap)

    p_detect = Process(
        target = detect_from_q,
        args = (q_vid_cap, q_detections, net, ))
    print(fph.time_stamp(), "[INFO] starting detect from q process...")
    p_detect.start()

    p_identify = Process(
        target = identify_from_q,
        args = (
            q_detections,
            vid_cap,
            known_face_encodings,
            known_face_names,
            q_vid_cap,
            )
        )
    print(fph.time_stamp(),
        "[INFO] starting identify from q process..."
        )
    p_identify.start()

    p_detect.join()
    p_identify.join()

Я ожидал, что он будет работать на 3 ядрах, но он работает только на 1.

...