Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example code #17

Closed
mohammdkaraca opened this issue Jul 31, 2024 · 3 comments
Closed

example code #17

mohammdkaraca opened this issue Jul 31, 2024 · 3 comments
Labels
question Further information is requested

Comments

@mohammdkaraca
Copy link

          Below is an example of how you can write code to implement video stream processing and save the final processed video with the results of patch-based instance segmentation inference (as in the example from the GIF of the previous comment):
import cv2
from ultralytics import YOLO
from patched_yolo_infer import MakeCropsDetectThem, CombineDetections, visualize_results

# Load the YOLOv8 model
model = YOLO("yolov8m-seg.pt")  #or yolov8m-seg.engine in case of TensorRT

# Open the video file
cap = cv2.VideoCapture("video.mp4")

# Check if the video file was successfully opened
if not cap.isOpened():
    exit()

# Get the frames per second (fps) of the video
fps = cap.get(cv2.CAP_PROP_FPS)
# Get the width and height of the video frames
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # Codec for MP4
out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))

while True:
    # Read a frame from the video
    ret, frame = cap.read()

    # Break the loop if there are no more frames
    if not ret:
        break

    # Detect elements in the frame using the YOLOv8 model
    element_crops = MakeCropsDetectThem(
        image=frame,
        model=model,
        segment=True,
        shape_x=640,
        shape_y=500,
        overlap_x=35,
        overlap_y=35,
        conf=0.2,
        iou=0.75,
        imgsz=640,
        resize_initial_size=True,
        show_crops=False,
        batch_inference=True,
        classes_list=[0, 1, 2, 3, 4, 5, 6]
    )

    # Combine the detections from the different crops
    result = CombineDetections(element_crops, nms_threshold=0.2, match_metric='IOS')

    # Visualize the results on the frame
    frame = visualize_results(
        img=result.image,
        confidences=result.filtered_confidences,
        boxes=result.filtered_boxes,
        polygons=result.filtered_polygons,
        classes_ids=result.filtered_classes_id,
        classes_names=result.filtered_classes_names,
        segment=True,
        thickness=3,
        show_boxes=False,
        fill_mask=True,
        show_class=False,
        alpha=1,
        return_image_array=True
    )

    # Resize the frame for display
    scale = 0.5
    frame_resized = cv2.resize(frame, (-1, -1), fx=scale, fy=scale)

    # Display the frame
    cv2.imshow('video', frame_resized)

    # Write the frame to the output video file
    out.write(frame)

    # Break the loop if 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the video capture and writer objects
cap.release()
out.release()

# Close all OpenCV windows
cv2.destroyAllWindows()

Originally posted by @Koldim2001 in #8 (comment)

@mohammdkaraca
Copy link
Author

hi I contacted you a couple of months ago about this library but back then it was to slow to be able to use it for my case I saw you telling someone that i got faster i tried the code you gave them but it doesent work it has an error here is the error itself:

"C:\Users\Mohammad karaca\PycharmProjects\yolo_teknofest.venv\Scripts\python.exe" "C:\Users\Mohammad karaca\PycharmProjects\yolo_teknofest.venv\patched_yolo.py"
Traceback (most recent call last):
File "C:\Users\Mohammad karaca\PycharmProjects\yolo_teknofest.venv\patched_yolo.py", line 34, in
element_crops = MakeCropsDetectThem(
TypeError: init() got an unexpected keyword argument 'batch_inference'

Process finished with exit code 1

@Koldim2001
Copy link
Owner

@mohammdkaraca Good afternoon. I suppose you have an older version of the library in this python environment. I recommend updating it ->

pip install --upgrade patched_yolo_infer

@Koldim2001
Copy link
Owner

@mohammdkaraca Good afternoon. Has the library update solved your problem?

@Koldim2001 Koldim2001 added the question Further information is requested label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants