Showing
42 changed files
with
22 additions
and
10 deletions
No preview for this file type
No preview for this file type
codes/Project/input/example1.mp4
0 → 100644
This file is too large to display.
No preview for this file type
codes/Project/mid/frame0.png
0 → 100644

1.07 MB
codes/Project/mid/frame1.png
0 → 100644

1020 KB
codes/Project/mid/frame10.png
0 → 100644

503 KB
codes/Project/mid/frame11.png
0 → 100644

548 KB
codes/Project/mid/frame12.png
0 → 100644

553 KB
codes/Project/mid/frame13.png
0 → 100644

515 KB
codes/Project/mid/frame14.png
0 → 100644

458 KB
codes/Project/mid/frame15.png
0 → 100644

387 KB
codes/Project/mid/frame16.png
0 → 100644

397 KB
codes/Project/mid/frame17.png
0 → 100644

402 KB
codes/Project/mid/frame18.png
0 → 100644

400 KB
codes/Project/mid/frame19.png
0 → 100644

400 KB
codes/Project/mid/frame2.png
0 → 100644

994 KB
codes/Project/mid/frame20.png
0 → 100644

402 KB
codes/Project/mid/frame21.png
0 → 100644

460 KB
codes/Project/mid/frame22.png
0 → 100644

502 KB
codes/Project/mid/frame3.png
0 → 100644

935 KB
codes/Project/mid/frame4.png
0 → 100644

878 KB
codes/Project/mid/frame5.png
0 → 100644

782 KB
codes/Project/mid/frame6.png
0 → 100644

675 KB
codes/Project/mid/frame7.png
0 → 100644

572 KB
codes/Project/mid/frame8.png
0 → 100644

488 KB
codes/Project/mid/frame9.png
0 → 100644

509 KB

941 KB

500 KB
1 | import math | 1 | import math |
2 | +import time | ||
2 | import cv2 | 3 | import cv2 |
3 | import numpy as np | 4 | import numpy as np |
4 | from copy import deepcopy | 5 | from copy import deepcopy |
... | @@ -30,19 +31,27 @@ def get_classes(file): | ... | @@ -30,19 +31,27 @@ def get_classes(file): |
30 | if __name__ == '__main__': | 31 | if __name__ == '__main__': |
31 | # 파일 열기 | 32 | # 파일 열기 |
32 | camera = cv2.VideoCapture("input/example.mp4") | 33 | camera = cv2.VideoCapture("input/example.mp4") |
34 | + if camera.isOpened() == False: | ||
35 | + print("Can't open Video.") | ||
33 | 36 | ||
34 | # Yolo 학습 | 37 | # Yolo 학습 |
35 | yolo = YOLO(0.6, 0.5) | 38 | yolo = YOLO(0.6, 0.5) |
36 | file = 'data/coco_classes.txt' | 39 | file = 'data/coco_classes.txt' |
37 | all_classes = get_classes(file) | 40 | all_classes = get_classes(file) |
38 | 41 | ||
39 | - # 1 카운트 할 때마다 frame 얻어서 파일로 저장 | 42 | + # 10 카운트 할 때마다 frame 얻어서 파일로 저장 |
40 | - success,image = camera.read() | 43 | + success, image = camera.read() |
41 | count = 0 | 44 | count = 0 |
45 | + frameCount = 0 | ||
46 | + start_time = time.time() | ||
42 | while success: | 47 | while success: |
43 | - cv2.imwrite("mid/frame%d.png" % count, image) # save frame as JPEG file | 48 | + frameCount += 1 |
49 | + if frameCount % 3 == 0: | ||
50 | + cv2.imwrite("mid/frame%d.png" % count, image) # save frame as JPEG file | ||
51 | + count += 1 | ||
44 | success, image = camera.read() | 52 | success, image = camera.read() |
45 | - count += 1 | 53 | + |
54 | + camera.release() | ||
46 | 55 | ||
47 | # 각 프레임 별로 Image Detection 후 프레임 번호, 객체 이름(name)과 객체의 크기(size), 객체가 얼마나 가운데 있는지(coordinatevalue) 저장 | 56 | # 각 프레임 별로 Image Detection 후 프레임 번호, 객체 이름(name)과 객체의 크기(size), 객체가 얼마나 가운데 있는지(coordinatevalue) 저장 |
48 | detectionInfo = [] | 57 | detectionInfo = [] |
... | @@ -50,7 +59,6 @@ if __name__ == '__main__': | ... | @@ -50,7 +59,6 @@ if __name__ == '__main__': |
50 | #filename = "mid/frame"+str(i)+"." | 59 | #filename = "mid/frame"+str(i)+"." |
51 | image = cv2.imread("mid/frame%d.png" % i) | 60 | image = cv2.imread("mid/frame%d.png" % i) |
52 | pimage = process_image(image) | 61 | pimage = process_image(image) |
53 | - | ||
54 | boxes, classes, scores = yolo.predict(pimage, image.shape) | 62 | boxes, classes, scores = yolo.predict(pimage, image.shape) |
55 | for box, score, cl in zip(boxes, scores, classes): | 63 | for box, score, cl in zip(boxes, scores, classes): |
56 | x, y, w, h = box | 64 | x, y, w, h = box |
... | @@ -61,7 +69,6 @@ if __name__ == '__main__': | ... | @@ -61,7 +69,6 @@ if __name__ == '__main__': |
61 | continue | 69 | continue |
62 | if x <= 0 or x+w >= image.shape[1] or y <= 0 or y+h >= image.shape[0]: # 검출된 객체가 프레임 밖으로 나간 경우 | 70 | if x <= 0 or x+w >= image.shape[1] or y <= 0 or y+h >= image.shape[0]: # 검출된 객체가 프레임 밖으로 나간 경우 |
63 | continue | 71 | continue |
64 | - | ||
65 | # 얼마나 가운데인지 확인하는 알고리즘 | 72 | # 얼마나 가운데인지 확인하는 알고리즘 |
66 | object = Point2D(width= x + w/2, height= y + h/2) | 73 | object = Point2D(width= x + w/2, height= y + h/2) |
67 | a = image.shape[1]/2 - object.width | 74 | a = image.shape[1]/2 - object.width |
... | @@ -174,7 +181,8 @@ if __name__ == '__main__': | ... | @@ -174,7 +181,8 @@ if __name__ == '__main__': |
174 | 181 | ||
175 | # 두 객체가 겹쳤는지 좌표 비교를 통해 확인, 겹쳤다면 겹친 부분의 좌표 값 o_xxx 에 저장 | 182 | # 두 객체가 겹쳤는지 좌표 비교를 통해 확인, 겹쳤다면 겹친 부분의 좌표 값 o_xxx 에 저장 |
176 | isOverlapped = False | 183 | isOverlapped = False |
177 | - if a_top < b_top < a_bottom or b_top < a_top < b_bottom or a_left < b_left < a_right or b_left < a_left < b_right: | 184 | + if a_top < b_top < a_bottom or b_top < a_top < b_bottom \ |
185 | + or a_left < b_left < a_right or b_left < a_left < b_right: | ||
178 | isOverlapped = True | 186 | isOverlapped = True |
179 | o_top = max(a_top, b_top) | 187 | o_top = max(a_top, b_top) |
180 | o_bottom = min(a_bottom, b_bottom) | 188 | o_bottom = min(a_bottom, b_bottom) |
... | @@ -187,11 +195,14 @@ if __name__ == '__main__': | ... | @@ -187,11 +195,14 @@ if __name__ == '__main__': |
187 | if isOverlapped == True: | 195 | if isOverlapped == True: |
188 | a_object = Point2D(width= (a_top + a_bottom)//2, height=(a_left + a_right)//2) | 196 | a_object = Point2D(width= (a_top + a_bottom)//2, height=(a_left + a_right)//2) |
189 | b_object = Point2D(width= (b_top + b_bottom)//2, height=(b_left + b_right)//2) | 197 | b_object = Point2D(width= (b_top + b_bottom)//2, height=(b_left + b_right)//2) |
190 | - o_object = Point2D(width=(a_object.width+b_object.width)//2 , height = (a_object.height + b_object.height)//2) | 198 | + o_object = Point2D(width=(a_object.width+b_object.width)//2 , \ |
199 | + height = (a_object.height + b_object.height)//2) | ||
191 | toTheOrigin_w = image.shape[1] / 2 - o_object.width | 200 | toTheOrigin_w = image.shape[1] / 2 - o_object.width |
192 | toTheOrigin_h = image.shape[0] / 2 - o_object.height | 201 | toTheOrigin_h = image.shape[0] / 2 - o_object.height |
193 | coordiValue_object = int(math.sqrt((toTheOrigin_w ** 2) + (toTheOrigin_h ** 2))) | 202 | coordiValue_object = int(math.sqrt((toTheOrigin_w ** 2) + (toTheOrigin_h ** 2))) |
194 | - bestList[key] = [value[0][0] + ", "+ value[1][0], value[0][1]+value[1][1]-o_size, coordiValue_object, min(a_top, b_top), min(a_left, b_left), max(a_right, b_right), max(a_bottom, b_bottom)] | 203 | + bestList[key] = [value[0][0] + ", "+ value[1][0], value[0][1]+value[1][1]-o_size, \ |
204 | + coordiValue_object, min(a_top, b_top), min(a_left, b_left), \ | ||
205 | + max(a_right, b_right), max(a_bottom, b_bottom)] | ||
195 | else: | 206 | else: |
196 | # 겹치지 않았다면 목록에서 삭제 | 207 | # 겹치지 않았다면 목록에서 삭제 |
197 | deleteList.append(key) | 208 | deleteList.append(key) |
... | @@ -250,7 +261,8 @@ if __name__ == '__main__': | ... | @@ -250,7 +261,8 @@ if __name__ == '__main__': |
250 | ratio = int((image.shape[1]+padNum)/image.shape[1]) | 261 | ratio = int((image.shape[1]+padNum)/image.shape[1]) |
251 | output = cv2.resize(output, dsize=(width, height), interpolation=cv2.INTER_LINEAR) | 262 | output = cv2.resize(output, dsize=(width, height), interpolation=cv2.INTER_LINEAR) |
252 | # 물체가 프레임의 중앙에 있을 경우 | 263 | # 물체가 프레임의 중앙에 있을 경우 |
253 | - elif image.shape[1]//2 - image.shape[1]//20 < (crop_left + crop_right)// 2 <= image.shape[1]//2 + image.shape[1]//20: | 264 | + elif image.shape[1]//2 - image.shape[1]//20 < (crop_left + crop_right)// 2 \ |
265 | + <= image.shape[1]//2 + image.shape[1]//20: | ||
254 | crop_left = (crop_left + crop_right)//2 - width//2 | 266 | crop_left = (crop_left + crop_right)//2 - width//2 |
255 | crop_right = (crop_left + crop_right)//2 + width//2 | 267 | crop_right = (crop_left + crop_right)//2 + width//2 |
256 | output = image[crop_top:crop_bottom, crop_left:crop_right].copy() | 268 | output = image[crop_top:crop_bottom, crop_left:crop_right].copy() | ... | ... |
-
Please register or login to post a comment