How to pass frames (numpy arrays) as video_reference into Roboflow Inference Pipeline

30 Views Asked by At
# Initialize the inference pipeline
pipeline = InferencePipeline.init(
    model_id="crap-object-detection/1",
    api_key="VCkdCb9yChgHCrexOwvX",
    video_reference=0,
    on_prediction=my_custom_sink,
)

I would like to modify the camera feed from device 0, to be cropped and straightened, and then passed as the video reference for the Roboflow Inference Pipeline.

This is my script which crops the video feed. As you can see it displays the camerafeed and the resultant video - boardImg. I would like to use boardImg as my video_reference in the code above.

However, video_reference only accepts a video path, a device id or an RTSP stream url. How can I go about doing this? Thanks.

import cv2
import os
import time
import numpy as np

from Cropping import ExtractAndStraightenFromImage
from LocateGrid import DetectGrid

IMAGE_FILE_PATH = os.path.join("Capture", "BoardPictures")

# Create directory to save pictures if it doesn't exist
if not os.path.exists(IMAGE_FILE_PATH):  
    os.makedirs(IMAGE_FILE_PATH)

# Start the video capture
vid = cv2.VideoCapture(0)  # ID 1 assumes a second camera (like your Orbbec Astra). Use 0 for default camera

is_automatic = False

def apply_morphological_ops(img):
    # Define a kernel size
    kernel = np.ones((5,5),np.uint8)
    # Apply morphological opening
    opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    return opening

try:
    while True:
        ret, frame = vid.read()
        if not ret:
            print("Failed to grab frame")
            break

        key = cv2.waitKey(1)

        if key == 32:  # Spacebar pressed
            unique_filename = time.strftime("%Y%m%d_%H%M%S") + ".png"  # Unique filename using current timestamp
            screenshot_path = os.path.join(IMAGE_FILE_PATH, unique_filename)
            boardImg = ExtractAndStraightenFromImage(frame)
            cv2.imwrite(screenshot_path, boardImg)  # Save the screenshot
            print(f"Screenshot saved as {screenshot_path}")

        boardImg = ExtractAndStraightenFromImage(frame)
        #checkBoard = DetectGrid(boardImg)
        
        #checkBoard2 = apply_morphological_ops(checkBoard)

        cv2.imshow("Frame", frame)
        #cv2.imshow("Checkboard filter", checkBoard2)
        cv2.imshow("Board img", boardImg)
        #cv2.imshow("Check board", checkBoard)
            
finally:
    vid.release()
    cv2.destroyAllWindows()
0

There are 0 best solutions below