강세희

sehee:[add] 코드

Showing 153 changed files with 120 additions and 0 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
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);
No preview for this file type
No preview for this file type