|
| 1 | +import cv2 |
| 2 | +from InitCaffe import * |
| 3 | + |
| 4 | +# Open video |
| 5 | +# cap = cv2.VideoCapture('NASA_video1.mp4') # video with airplane. |
| 6 | + |
| 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 |
| 9 | + |
| 10 | + |
| 11 | +count = 0 # Loop counter to control frequency of object recognition |
| 12 | +objfreq = 5 # Frequence of object recognition |
| 13 | +# NumCorners = 50 # Number of corners to extract in a given frame |
| 14 | +NumCorners = 10 # Number of corners to extract in a given frame |
| 15 | +''' |
| 16 | +# fourcc = cv2.cv.CV_FOURCC(*'XVID') |
| 17 | +# out = cv2.VideoWriter('result.avi', fourcc, 20.0, (450,170)) |
| 18 | +# Read each frame of video and do object recognition at specified frequency |
| 19 | +while(cap.isOpened()): |
| 20 | + carNum = 0 # Number of cars detected |
| 21 | + # Read frame |
| 22 | + ret, frame = cap.read() |
| 23 | + # Resize each frame to a smaller size for speed |
| 24 | + frame = cv2.resize(frame,(500, 300), interpolation = cv2.INTER_CUBIC) |
| 25 | + #frame = frame[260:450,200:700] |
| 26 | + # Implement object recognition at specified frequency |
| 27 | + if count%objfreq == 0: |
| 28 | +''' |
| 29 | +# frame = cv2.imread('patch8901.png') |
| 30 | +frame = cv2.imread('frame184.png') |
| 31 | +# frame = cv2.imread('patch1.png') |
| 32 | +if 1: |
| 33 | + if 1: |
| 34 | + # Convert to gray scale |
| 35 | + gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) |
| 36 | + |
| 37 | + # Find corners in gray scale image |
| 38 | + corners = cv2.goodFeaturesToTrack(gray,NumCorners,0.01,100) |
| 39 | + print 'The corners are:',corners |
| 40 | + corners = np.int0(corners) |
| 41 | + |
| 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) |
| 46 | + print 'The x pos of the corner is: ', x |
| 47 | + print 'The y pos of the corner is: ', y |
| 48 | + print 'The i of the corners is: ', i |
| 49 | + print 'The j of the corners is: ', j |
| 50 | + # Define size of patch in image coordinates |
| 51 | + xstart = x - patch_size |
| 52 | + xend = x + patch_size |
| 53 | + ystart = y - patch_size |
| 54 | + yend = y + patch_size |
| 55 | + |
| 56 | + |
| 57 | + # clip image patch based on image size |
| 58 | + xlen = frame.shape[1] |
| 59 | + ylen = frame.shape[0] |
| 60 | + |
| 61 | + if xend > xlen: |
| 62 | + xend = xlen |
| 63 | + if xstart < 0: |
| 64 | + xstart = 0 |
| 65 | + |
| 66 | + if yend > ylen: |
| 67 | + yend = ylen |
| 68 | + if ystart < 0: |
| 69 | + 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 | + |
| 75 | + |
| 76 | + # Transform image to use caffe library |
| 77 | + transformed_image = transformer.preprocess('data', img_patch) |
| 78 | + |
| 79 | + # copy the image data into the memory allocated for the net |
| 80 | + net.blobs['data'].data[j,:,:,:] = transformed_image |
| 81 | + |
| 82 | + ### perform classification |
| 83 | + output = net.forward() |
| 84 | + |
| 85 | + # Go through image patch for each corner and find if there are any airplanes |
| 86 | + Position = [] |
| 87 | + for i,j in enumerate(corners): |
| 88 | + x,y = j.ravel() |
| 89 | + output_prob = output['prob'][i] |
| 90 | + |
| 91 | + # sort top five predictions from softmax output |
| 92 | + # 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 |
| 95 | + # print 'predicted class is:', output_prob.argmax() |
| 96 | + # print 'output label:', labels[output_prob.argmax()] |
| 97 | + # print 'prob', output_prob[top_inds[0]] |
| 98 | + |
| 99 | + # If airlane, record position to draw bounding box |
| 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 |
| 104 | + # for k in range (0,5): |
| 105 | + for k in range (0,10): |
| 106 | + if (top_inds[k] in AirplaneLabels ): |
| 107 | + if output_prob[top_inds[0]] > 0.0: |
| 108 | + print 'Shown class is:', top_inds[k] |
| 109 | + print 'output label:', labels[top_inds[k]] |
| 110 | + print 'prob', output_prob[top_inds[k]] |
| 111 | + Position.append((x,y)) |
| 112 | +# carNum = carNum + 1 |
| 113 | + # break |
| 114 | + # Draw rectangles around each airplane |
| 115 | + # print 'The number of cars detected are:', carNum |
| 116 | + print 'The number of frame is:', count+1 |
| 117 | + for pos in Position: |
| 118 | + xpos = pos[0] |
| 119 | + ypos = pos[1] |
| 120 | + cv2.rectangle(frame,(xpos-patch_size,ypos-patch_size),(xpos+patch_size,ypos+patch_size),(0,255,0),2) |
| 121 | + # break |
| 122 | + # out.write(frame) |
| 123 | + cv2.imshow('frame',frame) |
| 124 | + cv2.waitKey() |
| 125 | + # Show image frame on screen |
| 126 | + count = count + 1 |
| 127 | + # out.write(frame) |
| 128 | + # cv2.imshow('frame',frame) |
| 129 | + # cv2.waitKey() |
| 130 | +''' |
| 131 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 132 | + break |
| 133 | + if count > cap.get(7)/2: |
| 134 | + break |
| 135 | +''' |
| 136 | +#out.release() |
| 137 | +cap.release() |
| 138 | +cv2.destroyAllWindows() |
0 commit comments