I can’t process frames from the web camera via hailo 8 on raspberry pi 5. The web camera itself works, I checked it via qv4l2. I get a segmentation fault error when calling the get_roi_from_buffer function
if name == “main”:
user_data = user_app_callback_class()
app = GStreamerDetectionApp(app_callback, user_data) #app.run()
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = frame.astype('uint8')
roi = hailo.get_roi_from_buffer(frame) # error: segmentation fault
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
Welcome to the Hailo Community! Great to have you here.
Regarding the segmentation fault you’re encountering, it’s important to note that the Hailo Raspberry Pi 5 examples are specifically designed to work with GStreamer pipelines for handling video streams. When you use OpenCV’s cv2.VideoCapture, the frames are processed as NumPy arrays, which are not compatible with Hailo’s hailo.get_roi_from_buffer() function. On the other hand, GStreamer pipelines provide Gst.Buffer objects that integrate seamlessly with the Hailo Raspberry Pi 5 examples.
To address this issue, you have two potential solutions:
Use our GStreamer-based app and extract the frames from GStreamer. You can find an example of this approach in our Hailo Raspberry Pi 5 examples repository: GitHub - hailo-ai/hailo-rpi5-examples
Both of these options should help you resolve the segmentation fault and enable you to successfully process video streams using the Hailo Raspberry Pi 5.
Let me know if you have any further questions or need additional assistance!
Hi @sherinkonstantiv,
We developed a python SDK that makes working with Hailo8/Hailo8L easy. You find instructions and tutorials at: DeGirum/hailo_examples.
For your specific issue, our PySDK code looks as below. The code runs a yolov8n model on WebCam with index 0. If you have HAILO8 device instead of HAILO8L, just change model_name=yolov8n_relu6_coco--640x640_quant_hailort_hailo8_1
import degirum as dg, degirum_tools
model_name = "yolov8n_relu6_coco--640x640_quant_hailort_hailo8l_1"
video_source = 0
# load AI model
model = dg.load_model(
model_name=model_name,
inference_host_address=inference_host_address,
zoo_url=zoo_url,
token=token
)
# Run inference on video_source
with degirum_tools.Display("AI Camera") as output_display:
for inference_result in degirum_tools.predict_stream(model, video_source):
output_display.show(inference_result)
Please let u sknow if this helps or if you encounter any difficulties in running the above code.
Hi @shashi,
I have tried these solutions and they work great locally with image and video sources, but I can’t make it work with Picamera. I have a Raspberry Pi 5 with an AI Kit (Hailo8L) and a connected Arducam. I can run a simple Picamera2 script to capture and display images, so I am certain that the camera index is 0.
I tried to run the following code:
import degirum as dg
import degirum_tools
import cv2
from picamera2 import Picamera2
import time
face_det_model_name = "scrfd_500m--640x640_quant_hailort_hailo8l_1"
inference_host_address = "@local"
zoo_url = "/home/pi/degirum/scrfd_500m--640x640_quant_hailort_hailo8l_1"
image_source = "Friends1.jpg"
video_source = "faces_and_gender.mp4"
camera_source = 0
token = ''
model = dg.load_model(
model_name=face_det_model_name,
inference_host_address=inference_host_address,
zoo_url=zoo_url,
token=token,
overlay_color=(0, 255, 0) # Green color for bounding boxes
)
with degirum_tools.Display("display") as display:
for res in degirum_tools.predict_stream(model, camera_source):
display.show(res)
I am getting the following error:
degirum.exceptions.DegirumException: Fail to capture camera frame. May be camera was opened by another notebook?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/pi/degirum/face_detection.py", line 28, in <module>
for res in degirum_tools.predict_stream(model, camera_source):
File "/home/pi/degirum/degirum_env/lib/python3.11/site-packages/degirum_tools/inference_support.py", line 224, in predict_stream
for res in model.predict_batch(video_source(stream, fps=fps)):
File "/home/pi/degirum/degirum_env/lib/python3.11/site-packages/degirum/model.py", line 288, in predict_batch
for res in self._predict_impl(source):
File "/home/pi/degirum/degirum_env/lib/python3.11/site-packages/degirum/model.py", line 1249, in _predict_impl
raise DegirumException(
degirum.exceptions.DegirumException: Failed to perform model 'scrfd_500m--640x640_quant_hailort_hailo8l_1' inference: Fail to capture camera frame. May be camera was opened by another notebook?
Hi @Szymon_Parol
camera_source=0 means a web camera attached and not a raspberry pi camera. You can define a generator function that gets frames from picamera and pass it to predict function. Please let me know if you need further help.
@Szymon_Parol
Can you please try this code and let me know if it works?
import degirum as dg
import cv2
from picamera2 import Picamera2
import numpy as np
# Define a frame generator: a function that yields frames from the Picamera2
def frame_generator():
picam2 = Picamera2()
# Configure the camera (optional: set the resolution or other settings)
picam2.configure(picam2.create_preview_configuration())
# Start the camera
picam2.start()
try:
while True:
# Capture a frame as a numpy array
frame = picam2.capture_array()
# Yield the frame
yield frame
finally:
picam2.stop() # Stop the camera when the generator is closed
# Define model parameters (replace these with your own values)
face_det_model_name = "scrfd_500m--640x640_quant_hailort_hailo8l_1"
inference_host_address = "@local"
zoo_url = "/home/pi/degirum/scrfd_500m--640x640_quant_hailort_hailo8l_1"
token = ''
# Load the object detection AI model from the model zoo
model = dg.load_model(
model_name=face_det_model_name,
inference_host_address=inference_host_address,
zoo_url=zoo_url,
token=token,
overlay_color=(0, 255, 0) # Green color for bounding boxes
)
# Process the video stream by AI model using model.predict_batch():
for result in model.predict_batch(frame_generator()):
# Display the frame with AI annotations in a window named 'AI Inference'
cv2.imshow("AI Inference", result.image_overlay)
# Process GUI events and break the loop if 'q' key was pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Destroy any remaining OpenCV windows after the loop finishes
cv2.destroyAllWindows()
@shashi
Thank you very much. It worked! I only added {‘format’: ‘RGB888’} as an argument to picam2.create_preview_configuration because Picamera2 returns 4-channel images by default.
I will add the whole code in case someone needs it.
import degirum as dg
import cv2
from picamera2 import Picamera2
import numpy as np
# Define a frame generator: a function that yields frames from the Picamera2
def frame_generator():
picam2 = Picamera2()
# Configure the camera (optional: set the resolution or other settings)
picam2.configure(picam2.create_preview_configuration({'format': 'RGB888'}))
# Start the camera
picam2.start()
try:
while True:
# Capture a frame as a numpy array
frame = picam2.capture_array()
# Yield the frame
yield frame
finally:
picam2.stop() # Stop the camera when the generator is closed
# Define model parameters (replace these with your own values)
face_det_model_name = "scrfd_500m--640x640_quant_hailort_hailo8l_1"
inference_host_address = "@local"
zoo_url = "/home/pi/degirum/scrfd_500m--640x640_quant_hailort_hailo8l_1"
token = ''
# Load the object detection AI model from the model zoo
model = dg.load_model(
model_name=face_det_model_name,
inference_host_address=inference_host_address,
zoo_url=zoo_url,
token=token,
overlay_color=(0, 255, 0) # Green color for bounding boxes
)
# Process the video stream by AI model using model.predict_batch():
for result in model.predict_batch(frame_generator()):
# Display the frame with AI annotations in a window named 'AI Inference'
cv2.imshow("AI Inference", result.image_overlay)
# Process GUI events and break the loop if 'q' key was pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Destroy any remaining OpenCV windows after the loop finishes
cv2.destroyAllWindows()