
170 KB

838 KB

258 KB

1.06 MB

241 KB

961 KB

240 KB

935 KB

247 KB

919 KB

243 KB

901 KB

241 KB

878 KB

239 KB

848 KB

235 KB

822 KB

226 KB

782 KB

213 KB

738 KB

212 KB

701 KB

263 KB

1.07 MB

208 KB

675 KB

200 KB

638 KB

203 KB

622 KB

190 KB

572 KB

174 KB

530 KB

167 KB

509 KB

154 KB

488 KB

159 KB

487 KB

169 KB

501 KB

177 KB

509 KB

264 KB

1.07 MB

71.1 KB

410 KB

142 KB

457 KB

160 KB

503 KB

169 KB

533 KB

175 KB

545 KB

176 KB

548 KB

176 KB

553 KB

181 KB

548 KB

183 KB

553 KB

186 KB

555 KB

246 KB

1.02 MB

185 KB

551 KB

146 KB

515 KB

127 KB

481 KB

122 KB

469 KB

118 KB

458 KB

117 KB

442 KB

102 KB

402 KB

98.1 KB

387 KB

105 KB

399 KB

105 KB

402 KB

243 KB

1020 KB

112 KB

397 KB

106 KB

390 KB

110 KB

400 KB

107 KB

402 KB

108 KB

403 KB

111 KB

402 KB

111 KB

400 KB

117 KB

399 KB

119 KB

399 KB

120 KB

400 KB

241 KB

1000 KB

56.8 KB

357 KB

108 KB

383 KB

117 KB

402 KB

124 KB

426 KB

131 KB

440 KB

140 KB

460 KB

146 KB

477 KB

149 KB

487 KB

156 KB

502 KB

243 KB

1000 KB

242 KB

994 KB

242 KB

981 KB

599 KB

726 KB

585 KB

793 KB
1 | +"""Demo for use yolo v3 | ||
2 | +""" | ||
3 | +import os | ||
4 | +import time | ||
5 | +import cv2 | ||
6 | +import numpy as np | ||
7 | +from model.yolo_model import YOLO | ||
8 | + | ||
9 | + | ||
10 | +def process_image(img): | ||
11 | + """Resize, reduce and expand image. | ||
12 | + | ||
13 | + # Argument: | ||
14 | + img: original image. | ||
15 | + | ||
16 | + # Returns | ||
17 | + image: ndarray(64, 64, 3), processed image. | ||
18 | + """ | ||
19 | + image = cv2.resize(img, (416, 416), | ||
20 | + interpolation=cv2.INTER_CUBIC) | ||
21 | + image = np.array(image, dtype='float32') | ||
22 | + image /= 255. | ||
23 | + image = np.expand_dims(image, axis=0) | ||
24 | + | ||
25 | + return image | ||
26 | + | ||
27 | + | ||
28 | +def get_classes(file): | ||
29 | + """Get classes name. | ||
30 | + | ||
31 | + # Argument: | ||
32 | + file: classes name for database. | ||
33 | + | ||
34 | + # Returns | ||
35 | + class_names: List, classes name. | ||
36 | + | ||
37 | + """ | ||
38 | + with open(file) as f: | ||
39 | + class_names = f.readlines() | ||
40 | + class_names = [c.strip() for c in class_names] | ||
41 | + | ||
42 | + return class_names | ||
43 | + | ||
44 | + | ||
45 | +if __name__ == '__main__': | ||
46 | +# 파일 열기 | ||
47 | + camera = cv2.VideoCapture("input/example.mp4") | ||
48 | + | ||
49 | +# Yolo 학습 | ||
50 | + yolo = YOLO(0.6, 0.5) | ||
51 | + file = 'data/coco_classes.txt' | ||
52 | + all_classes = get_classes(file) | ||
53 | + | ||
54 | +# 1 카운트 할 때마다 frame 얻어서 파일로 저장 | ||
55 | + success,image = camera.read() | ||
56 | + count = 0 | ||
57 | + while success: | ||
58 | + cv2.imwrite("mid/frame%d.png" % count, image) # save frame as JPEG file | ||
59 | + success,image = camera.read() | ||
60 | + count += 1 | ||
61 | + | ||
62 | +# 각 프레임 별로 Image Detection 후 프레임 번호, 객체 이름(name)과 객체의 크기(size), 객체가 얼마나 가운데 있는지(coordinatevalue) 저장 | ||
63 | + detectionInfo = [] | ||
64 | + for i in range(count): | ||
65 | + #filename = "mid/frame"+str(i)+"." | ||
66 | + image = cv2.imread("mid/frame%d.png" % i) | ||
67 | + pimage = process_image(image) | ||
68 | + | ||
69 | + boxes, classes, scores = yolo.predict(pimage, image.shape) | ||
70 | + for box, score, cl in zip(boxes, scores, classes): | ||
71 | + x, y, w, h = box | ||
72 | + name = all_classes[cl] | ||
73 | + size = w*h | ||
74 | + # 얼마나 가운데인지 확인하는 알고리즘 | ||
75 | + coordinatevalue = abs(x-image.shape[1]/2)+ abs(y-image.shape[0]/2)/image.shape[0]*image.shape[1] | ||
76 | + detectionInfo.append([i, name, size, coordinatevalue]) | ||
77 | + | ||
78 | +# name 별로 크기가 가장 크거나 물체가 프레임의 가운데 있는 프레임 번호 목록 얻어오기 | ||
79 | + | ||
80 | + indexlist = [] | ||
81 | + # 검출된 물체 리스트(중복 없이) | ||
82 | + namelist = set() | ||
83 | + for i in range(count): | ||
84 | + namelist.add(detectionInfo[i][1]) | ||
85 | + namelist = list(namelist) | ||
86 | + | ||
87 | + | ||
88 | + # 검출된 물체 중 가장 크기가 큰 것을 불러옴 | ||
89 | + for i in range(len(namelist)): | ||
90 | + maxindex = 0 | ||
91 | + maxvalue = 0 | ||
92 | + for j in range(count): | ||
93 | + if detectionInfo[j][1] == namelist[i]: # 물체 리스트 중에서 | ||
94 | + if detectionInfo[j][2] > maxvalue: # 가장 큰 값을 갱신한다면 | ||
95 | + maxvalue = detectionInfo[j][2] | ||
96 | + maxindex = j | ||
97 | + indexlist.append(maxindex) | ||
98 | + | ||
99 | + | ||
100 | + # 검출된 물체 중 가장 가운데 있는 것을 불러 | ||
101 | + for i in range(len(namelist)): | ||
102 | + minindex = 0 | ||
103 | + minvalue = 2000 | ||
104 | + for j in range(count): | ||
105 | + if detectionInfo[j][1] == namelist[i]: # 물체 리스트 중에서 | ||
106 | + if detectionInfo[j][3] < minvalue: # 가장 큰 값을 갱신한다면 | ||
107 | + minvalue = detectionInfo[j][3] | ||
108 | + minindex = j | ||
109 | + indexlist.append(minindex) | ||
110 | + | ||
111 | + | ||
112 | +#얻어온 프레임 목록 결과값으로 저장, 선명도 높이는 작업 수행 | ||
113 | + for i in range(len(indexlist)): | ||
114 | + image = cv2.imread("mid/frame%d.png" % indexlist[i]) | ||
115 | + output1 = cv2.GaussianBlur(image,(5,5),0) | ||
116 | + #kernel_sharpen_1 = np.array([[-1,-1,-1],[-1,9,-1],[-1,-1,-1]]) | ||
117 | + #output2 = cv2.filter2D(output1,-1,kernel_sharpen_1) | ||
118 | + | ||
119 | + | ||
120 | + cv2.imwrite("output/output%d.png"% (i+1), output1); |
-
Please register or login to post a comment