(video)
| 594 | |
| 595 | |
| 596 | def draw_bbox(video): |
| 597 | import matplotlib.pyplot as plt |
| 598 | from matplotlib.widgets import Button, RectangleSelector |
| 599 | |
| 600 | clip = VideoWriter(video) |
| 601 | frame = None |
| 602 | # Read the video until a frame is successfully read |
| 603 | while frame is None: |
| 604 | frame = clip.read_frame() |
| 605 | |
| 606 | bbox = [0, 0, frame.shape[1], frame.shape[0]] |
| 607 | |
| 608 | def line_select_callback(eclick, erelease): |
| 609 | bbox[:2] = int(eclick.xdata), int(eclick.ydata) # x1, y1 |
| 610 | bbox[2:] = int(erelease.xdata), int(erelease.ydata) # x2, y2 |
| 611 | |
| 612 | def validate_crop(*args): |
| 613 | fig.canvas.stop_event_loop() |
| 614 | |
| 615 | def display_help(*args): |
| 616 | print( |
| 617 | "1. Use left click to select the region of interest. " |
| 618 | "A red box will be drawn around the selected region. \n\n" |
| 619 | "2. Use the corner points to expand the box and center to move the box around the image. \n\n" |
| 620 | "3. Click " |
| 621 | ) |
| 622 | |
| 623 | fig = plt.figure() |
| 624 | ax = fig.add_subplot(111) |
| 625 | ax.imshow(frame) |
| 626 | ax_help = fig.add_axes([0.9, 0.2, 0.1, 0.1]) |
| 627 | ax_save = fig.add_axes([0.9, 0.1, 0.1, 0.1]) |
| 628 | crop_button = Button(ax_save, "Crop") |
| 629 | crop_button.on_clicked(validate_crop) |
| 630 | help_button = Button(ax_help, "Help") |
| 631 | help_button.on_clicked(display_help) |
| 632 | |
| 633 | RectangleSelector( |
| 634 | ax, |
| 635 | line_select_callback, |
| 636 | minspanx=5, |
| 637 | minspany=5, |
| 638 | interactive=True, |
| 639 | spancoords="pixels", |
| 640 | ) |
| 641 | plt.show(block=False) |
| 642 | |
| 643 | # import platform |
| 644 | # if platform.system() == "Darwin": # for OSX use WXAgg |
| 645 | # fig.canvas.start_event_loop(timeout=-1) |
| 646 | # else: |
| 647 | fig.canvas.start_event_loop(timeout=-1) # just tested on Ubuntu I also need this. |
| 648 | # #fig.canvas.stop_event_loop() |
| 649 | |
| 650 | plt.close(fig) |
| 651 | return bbox |
| 652 | |
| 653 |
no test coverage detected