Problems importing Edge Impulse into python code on Raspberry pi 4

Question/Issue:
Good day to everyone. I’m working on a project that, when an object labeled ‘Beetle’ is detected, turns on the relay for 5 seconds. I use a Raspberry pi 4 model B with the Debian Bulseye operating system for this, the relay is connected directly to the Raspberry. When running the code (example below), an error appears from time to time : ‘Failed to handle message’
Unfortunately, I can’t handle it, even though Runner works without problems if you run it separately on the command line. Please tell me how to solve this problem? I use a Logitech c270 720p camera. I am extremely grateful in advance

**Code:

import RPi.GPIO as GPIO
import time
from edge_impulse_linux.runner import ImpulseRunner
import subprocess

Настройка пина реле

relay_pin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)

Инициализация модели Edge Impulse

model_path = “/полный/путь/к/модели/model.eim” # Убедитесь, что путь корректен
runner = ImpulseRunner(model_path)

Разрешение камеры

camera_resolution = “1280x720”

def detect_object_live():
r = runner.init()
try:
for result in runner.classify(capture_image_from_camera()): # Режим классификации
for classification in result[‘result’][‘classification’]:
if classification[‘label’] == ‘Beetle’ and classification[‘value’] > 0.5:
return True
except Exception as e:
print(f"Ошибка при классификации: {e}")
finally:
runner.stop()
return False

def capture_image_from_camera():
# Захват изображения с помощью fswebcam
image_path = “/tmp/captured_image.jpg”
try:
result = subprocess.run([“fswebcam”, “-r”, camera_resolution, image_path], check=True)
if result.returncode != 0:
raise Exception(“fswebcam не удалось захватить изображение.”)
return image_path
except FileNotFoundError:
print(“fswebcam не найден. Установите пакет fswebcam.”)
return None
except Exception as e:
print(f"Ошибка при включении камеры: {e}")
return None

try:
while True:
if detect_object_live():
print(“Объект Beetle обнаружен!”)
GPIO.output(relay_pin, GPIO.HIGH)
time.sleep(5)
GPIO.output(relay_pin, GPIO.LOW)
time.sleep(1) # Задержка перед следующим кадром

finally:
GPIO.cleanup()

Project ID:
[Provide the project ID]
504031

Reproducibility:

  • [×] Always
  • [ ] Sometimes
  • [ ] Rarely

Environment:

  • Platform: Raspberry Pi
  • OS Version: Debian Bulseye
  • Edge Impulse Version (Firmware): 1.0.10