发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!