|
5 | 5 | # cap = cv2.VideoCapture('NASA_video1.mp4') # video with airplane. |
6 | 6 |
|
7 | 7 | # patch_size = 15 # Size of image patch to extract around featuns points |
8 | | -patch_size = 25 # Size of image patch to extract around feature points |
| 8 | +patch_size = 25 # Size of image patch to extract around feature points |
9 | 9 |
|
10 | 10 |
|
11 | | -count = 0 # Loop counter to control frequency of object recognition |
12 | | -objfreq = 5 # Frequence of object recognition |
| 11 | +count = 0 # Loop counter to control frequency of object recognition |
| 12 | +objfreq = 5 # Frequence of object recognition |
13 | 13 | # NumCorners = 50 # Number of corners to extract in a given frame |
14 | | -NumCorners = 10 # Number of corners to extract in a given frame |
| 14 | +NumCorners = 10 # Number of corners to extract in a given frame |
15 | 15 | ''' |
16 | 16 | # fourcc = cv2.cv.CV_FOURCC(*'XVID') |
17 | 17 | # out = cv2.VideoWriter('result.avi', fourcc, 20.0, (450,170)) |
|
32 | 32 | if 1: |
33 | 33 | if 1: |
34 | 34 | # Convert to gray scale |
35 | | - gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) |
| 35 | + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
36 | 36 |
|
37 | 37 | # Find corners in gray scale image |
38 | | - corners = cv2.goodFeaturesToTrack(gray,NumCorners,0.01,100) |
39 | | - print 'The corners are:',corners |
| 38 | + corners = cv2.goodFeaturesToTrack(gray, NumCorners, 0.01, 100) |
| 39 | + print 'The corners are:', corners |
40 | 40 | corners = np.int0(corners) |
41 | 41 |
|
42 | 42 | # For each corner found, extract a patch and classify patch |
43 | | - for j,i in enumerate(corners): |
44 | | - x,y = i.ravel() |
45 | | - #cv2.circle(frame,(x,y),3,255,-1) |
| 43 | + for j, i in enumerate(corners): |
| 44 | + x, y = i.ravel() |
| 45 | + # cv2.circle(frame,(x,y),3,255,-1) |
46 | 46 | print 'The x pos of the corner is: ', x |
47 | 47 | print 'The y pos of the corner is: ', y |
48 | 48 | print 'The i of the corners is: ', i |
49 | 49 | print 'The j of the corners is: ', j |
50 | 50 | # Define size of patch in image coordinates |
51 | 51 | xstart = x - patch_size |
52 | | - xend = x + patch_size |
| 52 | + xend = x + patch_size |
53 | 53 | ystart = y - patch_size |
54 | | - yend = y + patch_size |
55 | | - |
| 54 | + yend = y + patch_size |
56 | 55 |
|
57 | 56 | # clip image patch based on image size |
58 | | - xlen = frame.shape[1] |
59 | | - ylen = frame.shape[0] |
| 57 | + xlen = frame.shape[1] |
| 58 | + ylen = frame.shape[0] |
60 | 59 |
|
61 | 60 | if xend > xlen: |
62 | 61 | xend = xlen |
|
67 | 66 | yend = ylen |
68 | 67 | if ystart < 0: |
69 | 68 | ystart = 0 |
70 | | - |
71 | | - cv2.rectangle(frame,(xstart,ystart),(xend,yend),(255,0,0),2) |
72 | | - # Extract the image patch from each frame in the video |
73 | | - img_patch = frame[ystart:yend,xstart:xend] |
74 | 69 |
|
| 70 | + cv2.rectangle(frame, (xstart, ystart), |
| 71 | + (xend, yend), (255, 0, 0), 2) |
| 72 | + # Extract the image patch from each frame in the video |
| 73 | + img_patch = frame[ystart:yend, xstart:xend] |
75 | 74 |
|
76 | 75 | # Transform image to use caffe library |
77 | 76 | transformed_image = transformer.preprocess('data', img_patch) |
78 | | - |
| 77 | + |
79 | 78 | # copy the image data into the memory allocated for the net |
80 | | - net.blobs['data'].data[j,:,:,:] = transformed_image |
81 | | - |
82 | | - ### perform classification |
| 79 | + net.blobs['data'].data[j, :, :, :] = transformed_image |
| 80 | + |
| 81 | + # perform classification |
83 | 82 | output = net.forward() |
84 | | - |
| 83 | + |
85 | 84 | # Go through image patch for each corner and find if there are any airplanes |
86 | 85 | Position = [] |
87 | | - for i,j in enumerate(corners): |
88 | | - x,y = j.ravel() |
89 | | - output_prob = output['prob'][i] |
90 | | - |
| 86 | + for i, j in enumerate(corners): |
| 87 | + x, y = j.ravel() |
| 88 | + output_prob = output['prob'][i] |
| 89 | + |
91 | 90 | # sort top five predictions from softmax output |
92 | 91 | # top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items |
93 | | - top_inds = output_prob.argsort()[::-1][:10] # reverse sort and take five largest items |
94 | | - print 'The classes are:', top_inds |
| 92 | + # reverse sort and take five largest items |
| 93 | + top_inds = output_prob.argsort()[::-1][:10] |
| 94 | + print 'The classes are:', top_inds |
95 | 95 | # print 'predicted class is:', output_prob.argmax() |
96 | | - # print 'output label:', labels[output_prob.argmax()] |
| 96 | + # print 'output label:', labels[output_prob.argmax()] |
97 | 97 | # print 'prob', output_prob[top_inds[0]] |
98 | 98 |
|
99 | 99 | # If airlane, record position to draw bounding box |
100 | 100 |
|
101 | | - AirplaneLabels = [895,404,405,812] # Airplane label ids in caffe database |
102 | | - #437,566,556,570,706,735,752,818,830,848 |
103 | | - #VehicleLabels = [867,717,675,757,569,734,751,817,864,656] # Car, truck, van label ids in caffe database |
| 101 | + # Airplane label ids in caffe database |
| 102 | + AirplaneLabels = [895, 404, 405, 812] |
| 103 | + # 437,566,556,570,706,735,752,818,830,848 |
| 104 | + # VehicleLabels = [867,717,675,757,569,734,751,817,864,656] # Car, truck, van label ids in caffe database |
104 | 105 | # for k in range (0,5): |
105 | | - for k in range (0,10): |
106 | | - if (top_inds[k] in AirplaneLabels ): |
| 106 | + for k in range(0, 10): |
| 107 | + if (top_inds[k] in AirplaneLabels): |
107 | 108 | if output_prob[top_inds[0]] > 0.0: |
108 | 109 | print 'Shown class is:', top_inds[k] |
109 | | - print 'output label:', labels[top_inds[k]] |
| 110 | + print 'output label:', labels[top_inds[k]] |
110 | 111 | print 'prob', output_prob[top_inds[k]] |
111 | | - Position.append((x,y)) |
| 112 | + Position.append((x, y)) |
112 | 113 | # carNum = carNum + 1 |
113 | 114 | # break |
114 | 115 | # Draw rectangles around each airplane |
|
117 | 118 | for pos in Position: |
118 | 119 | xpos = pos[0] |
119 | 120 | ypos = pos[1] |
120 | | - cv2.rectangle(frame,(xpos-patch_size,ypos-patch_size),(xpos+patch_size,ypos+patch_size),(0,255,0),2) |
| 121 | + cv2.rectangle(frame, (xpos-patch_size, ypos-patch_size), |
| 122 | + (xpos+patch_size, ypos+patch_size), (0, 255, 0), 2) |
121 | 123 | # break |
122 | 124 | # out.write(frame) |
123 | | - cv2.imshow('frame',frame) |
| 125 | + cv2.imshow('frame', frame) |
124 | 126 | cv2.waitKey() |
125 | 127 | # Show image frame on screen |
126 | 128 | count = count + 1 |
|
133 | 135 | if count > cap.get(7)/2: |
134 | 136 | break |
135 | 137 | ''' |
136 | | -#out.release() |
| 138 | +# out.release() |
137 | 139 | cap.release() |
138 | 140 | cv2.destroyAllWindows() |
0 commit comments