-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFrame.py
More file actions
77 lines (57 loc) · 2.41 KB
/
getFrame.py
File metadata and controls
77 lines (57 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import re
import numpy as np
def get_cctv_data(lat, lng, key):
# 국가교통정보센터 API
# coordx: 경도 좌표, coordy: 위도 좌표
# lat: 위도, lng: 경도
# CCTV 탐색 범위 지정
minX = str(lng-1) # 최소 경도 영역
maxX = str(lng+1) # 최대 경도 영역
minY = str(lat-1) # 최소 위도 영역
maxY = str(lat+1) # 최대 위도 영역
api_call = 'https://openapi.its.go.kr:9443/cctvInfo?' \
'apiKey='+ key + \
'&type=ex&cctvType=1' \
'&minX=' + minX + \
'&maxX=' + maxX + \
'&minY=' + minY + \
'&maxY=' + maxY + \
'&getType=json'
dataset = requests.get(api_call).json()
cctv_data = dataset['response']['data']
coordx_list = []
for index, data in enumerate(cctv_data):
xy_couple = (float(cctv_data[index]['coordy']),float(cctv_data[index]['coordx']))
coordx_list.append(xy_couple)
# 입력한 위경도 좌표에서 가장 가까운 위치에 있는 CCTV를 찾는 과정
coordx_list = np.array(coordx_list)
leftbottom = np.array((lat, lng))
distances = np.linalg.norm(coordx_list - leftbottom, axis=1)
min_index = np.argmin(distances)
cctv_url = cctv_data[min_index]['cctvurl']
cctv_name = cctv_data[min_index]['cctvname']
print('CCTV:', cctv_name)
processed_name = re.sub(r'\[.*?\]', '', cctv_name).strip()
return cctv_url, processed_name
def process_frame(video, model):
retval, frame = video.read() # retval: 성공하면 True
if not retval:
return None, None
# conf: 최소 신뢰도 임계값 (이 임계값 미만으로 감지된 객체는 무시)
# iou: 값이 낮을수록 겹치는 상자가 제거되어 중복을 줄이는 데 유용 (default: 0.7)
tracks = model.track(frame, persist=True, classes=[2, 3, 5, 7], conf=0.3, iou=0.7)
frame = tracks[0].plot() # track 이미지
'''
bounding_boxes:
[[525.9249, 228.6959, 22.8433, 18.2890],
[327.0034, 222.9131, 19.8440, 19.6347]]
track_ids: [1, 4, 5, 3, 7]
'''
bounding_boxes = tracks[0].boxes.xywh.cpu() # return x, y, w, h
track_ids = tracks[0].boxes.id
if track_ids is None:
track_ids = []
else:
track_ids = track_ids.int().cpu().tolist()
return frame, bounding_boxes, track_ids