Amazing-Python-Scripts

Форк
0
140 строк · 5.1 Кб
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
            # clip image patch based on image size
57
            xlen = frame.shape[1]
58
            ylen = frame.shape[0]
59

60
            if xend > xlen:
61
                xend = xlen
62
            if xstart < 0:
63
                xstart = 0
64

65
            if yend > ylen:
66
                yend = ylen
67
            if ystart < 0:
68
                ystart = 0
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]
74

75
            # Transform image to use caffe library
76
            transformed_image = transformer.preprocess('data', img_patch)
77

78
            # copy the image data into the memory allocated for the net
79
            net.blobs['data'].data[j, :, :, :] = transformed_image
80

81
        # perform classification
82
        output = net.forward()
83

84
        # Go through image patch for each corner and find if there are any airplanes
85
        Position = []
86
        for i, j in enumerate(corners):
87
            x, y = j.ravel()
88
            output_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
93
            top_inds = output_prob.argsort()[::-1][:10]
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
            # 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
105
            # for k in range (0,5):
106
            for k in range(0, 10):
107
                if (top_inds[k] in AirplaneLabels):
108
                    if output_prob[top_inds[0]] > 0.0:
109
                        print 'Shown class is:', top_inds[k]
110
                        print 'output label:', labels[top_inds[k]]
111
                        print 'prob', output_prob[top_inds[k]]
112
                        Position.append((x, y))
113
#                        carNum = carNum + 1
114
                   #     break
115
        # Draw rectangles around each airplane
116
        # print 'The number of cars detected are:', carNum
117
        print 'The number of frame is:', count+1
118
        for pos in Position:
119
            xpos = pos[0]
120
            ypos = pos[1]
121
            cv2.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)
125
        cv2.imshow('frame', frame)
126
        cv2.waitKey()
127
    # Show image frame on screen
128
    count = count + 1
129
    # out.write(frame)
130
    # cv2.imshow('frame',frame)
131
    # cv2.waitKey()
132
'''
133
    if cv2.waitKey(1) & 0xFF == ord('q'):
134
        break
135
    if count > cap.get(7)/2:
136
        break
137
'''
138
# out.release()
139
cap.release()
140
cv2.destroyAllWindows()
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.