I’m trying to get the image from the ROI for Plate Number OCR
import hailo
# Importing VideoFrame before importing GST is must
from gsthailo import VideoFrame
from gi.repository import Gst
import cv2
import numpy as np
#from paddleocr import PaddleOCR
# Initialize PaddleOCR
#ocr = PaddleOCR(use_angle_cls=True, lang='en') # Initialize OCR with English language
def process_detection_and_visualize_with_map_buffer(video_frame, bbox, label="object", confidence=0.0):
"""
Process detection, convert normalized bbox to pixel coordinates, and draw on the frame using map_buffer.
Parameters:
- video_frame: The video frame containing the buffer.
- bbox: Bounding box in normalized format (xmin, ymin, width, height).
- label: Detection label (e.g., "license_plate").
- confidence: Detection confidence score.
Returns:
- frame: NumPy array with bounding box and label drawn on it.
"""
# Map the buffer to retrieve the frame data
success, data, video_width, video_height = video_frame.map_buffer()
if not success:
raise RuntimeError("Failed to map the video frame buffer.")
try:
# Convert the mapped buffer to a NumPy array
frame = np.frombuffer(data, dtype=np.uint8).reshape((video_height, video_width, 3))
# Extract normalized bbox values
xmin, ymin, width, height = bbox
# Convert normalized bbox to pixel values
x1 = int(xmin * video_width)
y1 = int(ymin * video_height)
x2 = int((xmin + width) * video_width)
y2 = int((ymin + height) * video_height)
# Draw the bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Prepare the label text
display_label = f"{label} {confidence:.2f}"
# Ensure label text doesn't overflow the frame boundaries
text_x = max(0, x1)
text_y = max(0, y1 - 10)
# Draw the label text
cv2.putText(frame, display_label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return frame
finally:
# Unmap the buffer to release resources
video_frame.unmap_buffer()
def run(video_frame: VideoFrame):
try:
video_info = video_frame.video_info
#print(f"Video Info - Width: {video_info.width}, Height: {video_info.height}, Frame Size: {video_info.size}")
#print(f"ROI Objects: {video_frame.roi.get_objects()}")
video_info_from_caps = video_frame._video_info_from_caps
#print(dir(video_info.from_caps))
#print(f"Video Info - Width: {video_info_from_caps.width}, Height: {video_info_from_caps.height}, Frame Size: {video_info.size}")
#print(f"ROI Objects: {video_frame.roi.get_objects()}")
# caps = Gst.Caps.from_string("video/x-raw, format=RGB, width=640, height=480")
# video_frame.video_info = video_info.from_caps(caps)
objects = video_frame.roi.get_objects()
#print(f"Number of objects in ROI: {len(objects)}")
for obj in objects:
#print(dir(obj))
#print(f"TYPE : {obj.get_type()}")
what = obj.get_type()
#print(f"TYPE What : {what}")
if "hailo_object_t.HAILO_DETECTION" == f"{what}":
#print("Object is a detection")
bbox = obj.get_bbox()
detections = obj.get_objects_typed(hailo.HAILO_DETECTION)
print(dir(detections))
print(video_info.finfo)
#print(dir(video_info.from_caps()))
#print(dir(video_info.new_from_caps()))
# Call the methods to retrieve values
xmin = bbox.xmin()
ymin = bbox.ymin()
width = bbox.width()
height = bbox.height()
label = obj.get_label() # Assuming get_label() exists
confidence = obj.get_confidence() # Assuming get_confidence() exists
print(f"Object detected: Label={label}, Confidence={confidence}")
print(f"Bounding Box: xmin={xmin}, ymin={ymin}, width={width}, height={height}")
# Extract the frame as a NumPy array
#image = video_frame.buffer
#print(dir(image))
#width, height = 640, 480 # Replace with actual frame dimensions
#frame = extract_frame_from_buffer(video_frame, width, height)
video_width = 1536 # Frame width
video_height = 864 # Frame height
#frame_with_detections = process_detection_and_visualize_with_map_buffer(video_frame, video_width, video_height, bbox)
# Define offsets
x_offset = 0.05 # Example offset for x (positive to move right)
y_offset = 0.05 # Example offset for y (positive to move down)
# Apply offsets
offset_xmin = xmin + x_offset
offset_ymin = ymin + y_offset
# Ensure the offsets remain within valid bounds (0.0 to 1.0 for normalized values)
offset_xmin = max(0.0, min(1.0, offset_xmin))
offset_ymin = max(0.0, min(1.0, offset_ymin))
#ocr_results = ocr.ocr(preprocessed_plate, cls=True)
# Create a new bounding box with the offsets
#car_bbox = hailo.HailoBBox(xmin=offset_xmin, ymin=offset_ymin, width=width, height=height)
#car = hailo.HailoDetection(bbox=car_bbox, label='Car', confidence=confidence)
#video_frame.roi.add_object(car)
# Draw on the frame
# cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
# cv2.putText(frame, f"{label} ({confidence:.2f})", (xmin, ymin - 10),
# cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# Ensure the frame is mapped and accessible
# with video_frame.map_buffer() as map_info:
# frame = video_frame.numpy_array_from_buffer(map_info)
# print("Frame received and processed.")
return Gst.FlowReturn.OK
except Exception as e:
print(f"Error in hailopython module: {e}")
return Gst.FlowReturn.ERROR