|
326 | 326 | "---" |
327 | 327 | ] |
328 | 328 | }, |
| 329 | + { |
| 330 | + "cell_type": "markdown", |
| 331 | + "metadata": {}, |
| 332 | + "source": [ |
| 333 | + "# Video stream processing:" |
| 334 | + ] |
| 335 | + }, |
| 336 | + { |
| 337 | + "cell_type": "markdown", |
| 338 | + "metadata": {}, |
| 339 | + "source": [ |
| 340 | + "Webcam stream segmentation:" |
| 341 | + ] |
| 342 | + }, |
| 343 | + { |
| 344 | + "cell_type": "code", |
| 345 | + "execution_count": null, |
| 346 | + "metadata": {}, |
| 347 | + "outputs": [], |
| 348 | + "source": [ |
| 349 | + "import cv2\n", |
| 350 | + "from ultralytics import YOLO\n", |
| 351 | + "from patched_yolo_infer import visualize_results_usual_yolo_inference\n", |
| 352 | + "\n", |
| 353 | + "def process_stream(model, camera_id=0):\n", |
| 354 | + " # Open the input video file\n", |
| 355 | + " cap = cv2.VideoCapture(camera_id)\n", |
| 356 | + "\n", |
| 357 | + " while True:\n", |
| 358 | + " ret, frame = cap.read()\n", |
| 359 | + " if not ret:\n", |
| 360 | + " break\n", |
| 361 | + "\n", |
| 362 | + " # Apply YOLO model to the frame\n", |
| 363 | + " result = visualize_results_usual_yolo_inference(\n", |
| 364 | + " frame,\n", |
| 365 | + " model,\n", |
| 366 | + " imgsz=640,\n", |
| 367 | + " conf=0.5,\n", |
| 368 | + " iou=0.7,\n", |
| 369 | + " segment=True,\n", |
| 370 | + " thickness=2,\n", |
| 371 | + " show_boxes=False,\n", |
| 372 | + " show_class=False,\n", |
| 373 | + " show_confidences=False,\n", |
| 374 | + " fill_mask=True,\n", |
| 375 | + " return_image_array=True,\n", |
| 376 | + " )\n", |
| 377 | + "\n", |
| 378 | + " # Display the resulting frame\n", |
| 379 | + " cv2.imshow('Processed Frame', result)\n", |
| 380 | + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", |
| 381 | + " break\n", |
| 382 | + "\n", |
| 383 | + " cap.release()\n", |
| 384 | + " cv2.destroyAllWindows()\n", |
| 385 | + "\n", |
| 386 | + "# Example usage\n", |
| 387 | + "model = YOLO(\"yolov8m-seg.pt\") \n", |
| 388 | + "process_stream(model)" |
| 389 | + ] |
| 390 | + }, |
| 391 | + { |
| 392 | + "cell_type": "markdown", |
| 393 | + "metadata": {}, |
| 394 | + "source": [ |
| 395 | + "Video file stream detection:" |
| 396 | + ] |
| 397 | + }, |
| 398 | + { |
| 399 | + "cell_type": "code", |
| 400 | + "execution_count": null, |
| 401 | + "metadata": {}, |
| 402 | + "outputs": [], |
| 403 | + "source": [ |
| 404 | + "import cv2\n", |
| 405 | + "from ultralytics import YOLO\n", |
| 406 | + "from patched_yolo_infer import visualize_results_usual_yolo_inference\n", |
| 407 | + "\n", |
| 408 | + "# Function to process a video file\n", |
| 409 | + "def process_video(input_video_path, output_video_path, model):\n", |
| 410 | + " # Open the input video file\n", |
| 411 | + " cap = cv2.VideoCapture(input_video_path)\n", |
| 412 | + "\n", |
| 413 | + " # Get the video's frame width and height\n", |
| 414 | + " frame_width = int(cap.get(3))\n", |
| 415 | + " frame_height = int(cap.get(4))\n", |
| 416 | + "\n", |
| 417 | + " # Define the codec and create VideoWriter object\n", |
| 418 | + " fourcc = cv2.VideoWriter_fourcc(*'mp4v')\n", |
| 419 | + " out = cv2.VideoWriter(output_video_path, fourcc, 25.0, (frame_width, frame_height))\n", |
| 420 | + "\n", |
| 421 | + " while True:\n", |
| 422 | + " ret, frame = cap.read()\n", |
| 423 | + " if not ret:\n", |
| 424 | + " break\n", |
| 425 | + "\n", |
| 426 | + " # Apply YOLO model to the frame\n", |
| 427 | + " result = visualize_results_usual_yolo_inference(\n", |
| 428 | + " frame,\n", |
| 429 | + " model,\n", |
| 430 | + " imgsz=640,\n", |
| 431 | + " conf=0.5,\n", |
| 432 | + " iou=0.7,\n", |
| 433 | + " thickness=2,\n", |
| 434 | + " font_scale=1.0,\n", |
| 435 | + " show_boxes=True,\n", |
| 436 | + " show_confidences=False,\n", |
| 437 | + " return_image_array=True\n", |
| 438 | + " )\n", |
| 439 | + "\n", |
| 440 | + " # Write the resulting frame to the output video\n", |
| 441 | + " out.write(result)\n", |
| 442 | + "\n", |
| 443 | + " # Display the resulting frame (optional)\n", |
| 444 | + " cv2.imshow('Processed Frame', result)\n", |
| 445 | + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", |
| 446 | + " break\n", |
| 447 | + "\n", |
| 448 | + " # Release everything when done\n", |
| 449 | + " cap.release()\n", |
| 450 | + " out.release()\n", |
| 451 | + " cv2.destroyAllWindows()\n", |
| 452 | + "\n", |
| 453 | + "# Example usage\n", |
| 454 | + "model = YOLO(\"yolov8m.pt\")\n", |
| 455 | + "input_video_path = 'input.mp4'\n", |
| 456 | + "output_video_path = 'output.mp4'\n", |
| 457 | + "process_video(input_video_path, output_video_path, model)" |
| 458 | + ] |
| 459 | + }, |
| 460 | + { |
| 461 | + "cell_type": "markdown", |
| 462 | + "metadata": {}, |
| 463 | + "source": [ |
| 464 | + "---" |
| 465 | + ] |
| 466 | + }, |
329 | 467 | { |
330 | 468 | "cell_type": "markdown", |
331 | 469 | "metadata": { |
|
751 | 889 | "name": "python", |
752 | 890 | "nbconvert_exporter": "python", |
753 | 891 | "pygments_lexer": "ipython3", |
754 | | - "version": "3.10.13" |
| 892 | + "version": "3.11.8" |
755 | 893 | } |
756 | 894 | }, |
757 | 895 | "nbformat": 4, |
|
0 commit comments