-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathset_regions.py
107 lines (87 loc) · 3.21 KB
/
set_regions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import numpy as np
import cv2
import pickle
import argparse
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.widgets import PolygonSelector
from matplotlib.collections import PatchCollection
from shapely.geometry import box
from shapely.geometry import Polygon as shapely_poly
points = []
prev_points = []
patches = []
total_points = []
breaker = False
class SelectFromCollection(object):
def __init__(self, ax):
self.canvas = ax.figure.canvas
self.poly = PolygonSelector(ax, self.onselect)
self.ind = []
def onselect(self, verts):
global points
points = verts
self.canvas.draw_idle()
def disconnect(self):
self.poly.disconnect_events()
self.canvas.draw_idle()
def break_loop(event):
global breaker
global globSelect
global savePath
if event.key == 'b':
globSelect.disconnect()
if os.path.exists(savePath):
os.remove(savePath)
print("data saved in "+ savePath + " file")
with open(savePath, 'wb') as f:
pickle.dump(total_points, f, protocol=pickle.HIGHEST_PROTOCOL)
exit()
def onkeypress(event):
global points, prev_points, total_points
if event.key == 'n':
pts = np.array(points, dtype=np.int32)
if points != prev_points and len(set(points)) == 4:
print("Points : "+str(pts))
patches.append(Polygon(pts))
total_points.append(pts)
prev_points = points
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('video_path', help="Path of video file")
parser.add_argument('--out_file', help="Name of the output file", default="regions.p")
args = parser.parse_args()
global globSelect
global savePath
savePath = args.out_file if args.out_file.endswith(".p") else args.out_file+".p"
print("\n> Select a region in the figure by enclosing them within a quadrilateral.")
print("> Press the 'f' key to go full screen.")
print("> Press the 'esc' key to discard current quadrilateral.")
print("> Try holding the 'shift' key to move all of the vertices.")
print("> Try holding the 'ctrl' key to move a single vertex.")
print("> After marking a quadrilateral press 'n' to save current quadrilateral and then press 'q' to start marking a new quadrilateral")
print("> When you are done press 'b' to Exit the program\n")
video_capture = cv2.VideoCapture(args.video_path)
cnt=0
rgb_image = None
while video_capture.isOpened():
success, frame = video_capture.read()
if not success:
break
if cnt == 5:
rgb_image = frame[:, :, ::-1]
cnt += 1
video_capture.release()
while True:
fig, ax = plt.subplots()
image = rgb_image
ax.imshow(image)
p = PatchCollection(patches, alpha=0.7)
p.set_array(10*np.ones(len(patches)))
ax.add_collection(p)
globSelect = SelectFromCollection(ax)
bbox = plt.connect('key_press_event', onkeypress)
break_event = plt.connect('key_press_event', break_loop)
plt.show()
globSelect.disconnect()