Amazing-Python-Scripts
140 строк · 5.1 Кб
1import cv2
2from 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
8patch_size = 25 # Size of image patch to extract around feature points
9
10
11count = 0 # Loop counter to control frequency of object recognition
12objfreq = 5 # Frequence of object recognition
13# NumCorners = 50 # Number of corners to extract in a given frame
14NumCorners = 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
19while(cap.isOpened()):
20carNum = 0 # Number of cars detected
21# Read frame
22ret, frame = cap.read()
23# Resize each frame to a smaller size for speed
24frame = cv2.resize(frame,(500, 300), interpolation = cv2.INTER_CUBIC)
25#frame = frame[260:450,200:700]
26# Implement object recognition at specified frequency
27if count%objfreq == 0:
28'''
29# frame = cv2.imread('patch8901.png')
30frame = cv2.imread('frame184.png')
31# frame = cv2.imread('patch1.png')
32if 1:
33if 1:
34# Convert to gray scale
35gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
36
37# Find corners in gray scale image
38corners = cv2.goodFeaturesToTrack(gray, NumCorners, 0.01, 100)
39print 'The corners are:', corners
40corners = np.int0(corners)
41
42# For each corner found, extract a patch and classify patch
43for j, i in enumerate(corners):
44x, y = i.ravel()
45# cv2.circle(frame,(x,y),3,255,-1)
46print 'The x pos of the corner is: ', x
47print 'The y pos of the corner is: ', y
48print 'The i of the corners is: ', i
49print 'The j of the corners is: ', j
50# Define size of patch in image coordinates
51xstart = x - patch_size
52xend = x + patch_size
53ystart = y - patch_size
54yend = y + patch_size
55
56# clip image patch based on image size
57xlen = frame.shape[1]
58ylen = frame.shape[0]
59
60if xend > xlen:
61xend = xlen
62if xstart < 0:
63xstart = 0
64
65if yend > ylen:
66yend = ylen
67if ystart < 0:
68ystart = 0
69
70cv2.rectangle(frame, (xstart, ystart),
71(xend, yend), (255, 0, 0), 2)
72# Extract the image patch from each frame in the video
73img_patch = frame[ystart:yend, xstart:xend]
74
75# Transform image to use caffe library
76transformed_image = transformer.preprocess('data', img_patch)
77
78# copy the image data into the memory allocated for the net
79net.blobs['data'].data[j, :, :, :] = transformed_image
80
81# perform classification
82output = net.forward()
83
84# Go through image patch for each corner and find if there are any airplanes
85Position = []
86for i, j in enumerate(corners):
87x, y = j.ravel()
88output_prob = output['prob'][i]
89
90# sort top five predictions from softmax output
91# top_inds = output_prob.argsort()[::-1][:5] # reverse sort and take five largest items
92# reverse sort and take five largest items
93top_inds = output_prob.argsort()[::-1][:10]
94print '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# Airplane label ids in caffe database
102AirplaneLabels = [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
105# for k in range (0,5):
106for k in range(0, 10):
107if (top_inds[k] in AirplaneLabels):
108if output_prob[top_inds[0]] > 0.0:
109print 'Shown class is:', top_inds[k]
110print 'output label:', labels[top_inds[k]]
111print 'prob', output_prob[top_inds[k]]
112Position.append((x, y))
113# carNum = carNum + 1
114# break
115# Draw rectangles around each airplane
116# print 'The number of cars detected are:', carNum
117print 'The number of frame is:', count+1
118for pos in Position:
119xpos = pos[0]
120ypos = pos[1]
121cv2.rectangle(frame, (xpos-patch_size, ypos-patch_size),
122(xpos+patch_size, ypos+patch_size), (0, 255, 0), 2)
123# break
124# out.write(frame)
125cv2.imshow('frame', frame)
126cv2.waitKey()
127# Show image frame on screen
128count = count + 1
129# out.write(frame)
130# cv2.imshow('frame',frame)
131# cv2.waitKey()
132'''
133if cv2.waitKey(1) & 0xFF == ord('q'):
134break
135if count > cap.get(7)/2:
136break
137'''
138# out.release()
139cap.release()
140cv2.destroyAllWindows()
141