-
Notifications
You must be signed in to change notification settings - Fork 3
/
interactive.py
89 lines (74 loc) · 2.88 KB
/
interactive.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import sys
from Neural_Network.master_features import feature_images
from Testing_Grounds.helpers import compressJPG
NUM_IMAGES = 22 # max image number
FOLDER_NAME = './Sample_Images/'
RESIZE_FACTOR = 0.5
MAX_WINDOW_SIZE = 200
MIN_WINDOW_SIZE = 20
imgnum = 1 # keeps track of which image we're on; 15 is set as default
img = compressJPG(cv2.imread(FOLDER_NAME+str(imgnum)+'.jpg'), RESIZE_FACTOR)
imgH,imgW,channels = img.shape
win_size = 50 # default size of sliding windows
def onclick(event):
ix, iy = event.xdata, event.ydata
if ix and iy:
ix = int(ix)
iy = int(iy)
print 'x = %d, y = %d'%(ix, iy)
winL = (win_size)*(ix/win_size) # left column coordinate
winT = (win_size)*(iy/win_size) # top row coordinate
winR = min(winL+win_size, imgW) # right column coordinate
winB = min(winT+win_size, imgH) # bottom row coordinate
window = img[winT:winB,winL:winR]
plot_window(window)
plt.show()
def plot_window(window):
fig2 = plt.figure()
color, edge, grayscale = feature_images(window)
plt.subplot(221),plt.imshow(window)
plt.title('Window'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(grayscale,cmap='gray')
plt.title('Grayscale'), plt.xticks([]), plt.yticks([])
plt.subplot(223),plt.imshow(edge)
plt.title('Edges'), plt.xticks([]), plt.yticks([])
plt.subplot(224),plt.imshow(color)
plt.title('Colors'), plt.xticks([]), plt.yticks([])
def keypress(event):
if event.key == 'escape': # exit
plt.close()
sys.exit()
global imgnum, win_size, img, imgH, imgW
sys.stdout.flush()
if event.key == 'x': # scroll forward one image
imgnum = min(imgnum+1, NUM_IMAGES)
if event.key == 'z': # scroll backward one image
imgnum = max(imgnum-1, 1)
if event.key == 'v': # increment window size
win_size = min(win_size+10,MAX_WINDOW_SIZE)
if event.key == 'c': # decrement window size
win_size = max(win_size-10,MIN_WINDOW_SIZE)
img = compressJPG(cv2.imread(FOLDER_NAME+str(imgnum)+'.jpg'), RESIZE_FACTOR)
imgH,imgW,channels = img.shape
img_with_windows = draw_windows(win_size, win_size, img)
plt.imshow(img_with_windows)
plt.title('Image '+str(imgnum)+' of '+str(NUM_IMAGES)), plt.xticks([]), plt.yticks([])
plt.draw()
def draw_windows(winW, winH, img):
imgCopy = np.copy(img)
imgH,imgW,channels = img.shape
for i in range(-1*((-1*imgH)/winH)): # -1*((-1*imgH)/winH) is the ceiling of imgH/winH
imgCopy[winH*i,:] = [[0,0,0]]*imgW
for i in range(-1*((-1*imgW)/winW)): # -1*((-1*imgW)/winW) is the ceiling of imgW/winW
imgCopy[:,winW*i] = [[0,0,0]]*imgH
return imgCopy
fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', keypress)
fig.canvas.mpl_connect('button_press_event', onclick)
img_with_windows = draw_windows(win_size, win_size, img)
plt.imshow(img_with_windows)
plt.title('Image '+str(imgnum)+' of '+str(NUM_IMAGES)), plt.xticks([]), plt.yticks([])
plt.show()