程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Multiprocessing with tensorflow on Raspberry Pi

发布于2025-01-02 16:53     阅读(934)     评论(0)     点赞(13)     收藏(4)


I've been debugging for days now and I hope you can help me. I'm having trouble running tensorflow outside of my main script, and I'm wondering if there is a workaround.

So to isolate the issue I wrote a couple of test scripts to see what the problem is. This scripts works fine.

main.py

from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
from multiprocessing import Process

def start_ml():
    model = "efficientdet_lite0.tflite"
    numThreads = 4

    dispW = int(1280 / 2)
    dispH = int(720 / 2)

    picam2 = Picamera2()
    picam2.preview_configuration.main.size=(dispW, dispH)
    picam2.preview_configuration.main.format='RGB888'
    picam2.preview_configuration.align()
    picam2.configure("preview")
    picam2.start()

    baseOptions = core.BaseOptions(file_name=model, use_coral=False, num_threads=numThreads)
    detectionOptions = processor.DetectionOptions(max_results=3, score_threshold=0.5)
    options = vision.ObjectDetectorOptions(base_options=baseOptions, detection_options=detectionOptions)
    detector = vision.ObjectDetector.create_from_options(options)

    while True:
        im = picam2.capture_array()
        im = cv2.flip(im, -1)

        imRGB = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) # convert from BGR to RGB image
        imTensor = vision.TensorImage.create_from_array(imRGB) # create a tensor image
        myDetections = detector.detect(imTensor) # get the objects that are detected by tensorflow
        image = utils.visualize(im, myDetections) # create a decorated image with detected objects

        cv2.imshow('Camera', im)
        if cv2.waitKey(1)==ord('q'):
            break

    cv2.destroyAllWindows()

process = Process(target=start_ml)
process.start()

As soon as I have the tensorflow code outside of main.py, it gets stuck at or around the tensorflow method calls. There is no error or any indication that anything went wrong, it just hangs.

main.py

from detector import Detector

detector = Detector()
detector.start_camera_feed(detector)

detector.py

from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
from multiprocessing import Process


class Detector:
    def __init__(self):
        dispW = int(1280 / 2)
        dispH = int(720 / 2)

        self._picam2 = Picamera2()
        self._picam2.preview_configuration.main.size = (dispW, dispH)
        self._picam2.preview_configuration.main.format = 'RGB888'
        self._picam2.preview_configuration.align()
        self._picam2.configure("preview")
        self._picam2.start()

        model = "efficientdet_lite0.tflite"
        numThreads = 4

        baseOptions = core.BaseOptions(file_name=model, use_coral=False, num_threads=numThreads)
        detectionOptions = processor.DetectionOptions(max_results=3, score_threshold=0.5)
        options = vision.ObjectDetectorOptions(base_options=baseOptions, detection_options=detectionOptions)
        self._detector = vision.ObjectDetector.create_from_options(options)

    def start_camera_feed(self):
        process = Process(target=self._show_camera_feed)
        process.start()

    def _show_camera_feed(self):
        iterator = 0
        while True:
            im = self._picam2.capture_array()
            im = cv2.flip(im, -1)

            imRGB = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)  # convert from BGR to RGB image
            imTensor = vision.TensorImage.create_from_array(imRGB)  # create a tensor image
            myDetections = self._detector.detect(imTensor)  # get the objects that are detected by tensorflow
            image = utils.visualize(im, myDetections)  # create a decorated image with detected objects

            cv2.imshow('Camera', im)
            if cv2.waitKey(1) == ord('q'):
                break

            iterator += 1
            if iterator == 100:
                break

        cv2.destroyAllWindows()

This problem actually occured when I tried to implement some tensorflow object detection in a hobby project I'm working on. I really want to keep the structure I have in that project and not have any tensorflow code in my main script, that's why I'm not just moving it all to main.py. I'd also like to keep the subprocesses.


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/2046695/f77a62e793fa9c16a295/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

13 0
收藏该文
已收藏

评论内容:(最多支持255个字符)