+++ ITS/api.py
... | ... | @@ -0,0 +1,82 @@ |
1 | +import json | |
2 | +import time | |
3 | +import cv2 | |
4 | +import requests | |
5 | +import pandas as pd | |
6 | +import os | |
7 | +import numpy as np | |
8 | +from dotenv import load_dotenv | |
9 | + | |
10 | + | |
11 | +def create_url(apiKey, type, cctvType, minX, maxX, minY, maxY, getType="json", | |
12 | + baseurl="https://openapi.its.go.kr:9443/cctvInfo"): | |
13 | + ''' | |
14 | + 국가교통정보센터 예제 실행 | |
15 | + https://www.its.go.kr/opendata/openApiEx?service=cctv 참고 | |
16 | + :param apiKey: ``str`` 국가교통정보센터에서 발급받은 api 키 | |
17 | + :param type: ``str`` 도로 유형 ('ex' : 고속도로, 'its' : 국도) | |
18 | + :param cctvType: ``int`` CCTV 유형 (1. 실시간 스트리밍(HLS) / 2. 동영상 파일(m3u8) / 3. 정지 영상(JPEG)) | |
19 | + :param minX: 최소 경도 영역 | |
20 | + :param maxX: 최대 경도 영역 | |
21 | + :param minY: 최소 위도 영역 | |
22 | + :param maxY: 최대 경도 영역 | |
23 | + :param getType: 출력 결과 형식 ("xml" or "json") | |
24 | + :return: api 요청 url | |
25 | + ''' | |
26 | + assert type != "ex" or "its", 'Error! type should be either "ex" or "its"' | |
27 | + assert cctvType != 1 or 2 or 3, 'Error! cctvType should be one of 1, 2, 3!' | |
28 | + assert getType != "json" or "xml", 'Error! gettype should be either "json" or "xml"!' | |
29 | + | |
30 | + return ( | |
31 | + f"{baseurl}?" | |
32 | + f"apiKey={apiKey}&" | |
33 | + f"type={type}&" | |
34 | + f"cctvType={cctvType}&" | |
35 | + f"minX={minX}&maxX={maxX}&minY={minY}&maxY={maxY}&" | |
36 | + f"getType={getType}" | |
37 | + ) | |
38 | + | |
39 | +def gather_cctv_list(xmin, xmax, ymin, ymax, intervals, cctvtype): | |
40 | + dotenv = load_dotenv() | |
41 | + apiKey= os.getenv("ITS_API") | |
42 | + x_values = np.linspace(xmin, xmax, intervals+1) | |
43 | + y_values = np.linspace(ymin, ymax, intervals+1) | |
44 | + | |
45 | + all_data_df = pd.DataFrame() | |
46 | + | |
47 | + for i in range(len(x_values) - 1): | |
48 | + for j in range(len(y_values) - 1): | |
49 | + x = x_values[i] | |
50 | + y = y_values[j] | |
51 | + | |
52 | + x_next = x_values[i + 1] | |
53 | + y_next = y_values[j + 1] | |
54 | + | |
55 | + url = create_url(apiKey, 'its', cctvtype, | |
56 | + x, x_next, y, y_next) | |
57 | + response = requests.get(url) | |
58 | + response_json = json.loads(response.text) | |
59 | + if response_json['response']['datacount'] == 1: | |
60 | + new_df = pd.json_normalize(response_json['response']['data']) | |
61 | + all_data_df = pd.concat([all_data_df, new_df], ignore_index=True) | |
62 | + # df.to_csv(f"result/pohang/listofcctv_포항_{x}_{y}.csv", index=False) | |
63 | + elif response_json['response']['datacount'] != 0: | |
64 | + new_df = pd.DataFrame(response_json['response']['data']) | |
65 | + all_data_df = pd.concat([all_data_df, new_df], ignore_index=True) | |
66 | + # df.to_csv(f"result/pohang/listofcctv_포항_{x}_{y}.csv", index=False) | |
67 | + time.sleep(1) | |
68 | + print(f"{i}, {j}") | |
69 | + return all_data_df | |
70 | + | |
71 | +def get_jpeg(url): | |
72 | + response = requests.get(url) | |
73 | + encoded_img = np.frombuffer(response.content, dtype=np.uint8) | |
74 | + img = cv2.imdecode(encoded_img, cv2.IMREAD_COLOR) | |
75 | + return img | |
76 | + | |
77 | + | |
78 | + | |
79 | +if __name__ == "__main__": | |
80 | + df = gather_cctv_list(129.28, 129.35, 35.999, 36.07, 1, 1) | |
81 | + pass | |
82 | + # get_jpeg("http://cctvsec.ktict.co.kr:8090/74236/IM2NQs4/uHZcgnvJo3V/mjo3tswwgUj87kpcYZfR/BPxaQ4lk9agnl8ARIB9lhlgOD87VBx6RDHFl423kLkqHQ==")(파일 끝에 줄바꿈 문자 없음) |
+++ README.md
... | ... | @@ -0,0 +1,1 @@ |
1 | +# 2024-솔팩SW시제품 |
+++ hls_streaming/captured_frame_/2024-05-07 18:32:40.497320_captured_frame_0.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 18:37:40.502782_captured_frame_1.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 18:42:40.579021_captured_frame_2.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 18:47:40.579907_captured_frame_3.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 18:52:40.616010_captured_frame_4.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 18:57:40.653891_captured_frame_5.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:02:40.687858_captured_frame_6.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:07:40.695026_captured_frame_7.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:12:40.792749_captured_frame_8.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:17:40.823494_captured_frame_9.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:22:40.909893_captured_frame_10.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:27:41.001104_captured_frame_11.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:32:41.007518_captured_frame_12.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:37:41.030042_captured_frame_13.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:42:41.039646_captured_frame_14.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:47:41.051450_captured_frame_15.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:52:41.096156_captured_frame_16.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 19:57:41.124661_captured_frame_17.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:02:41.171300_captured_frame_18.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:07:41.269192_captured_frame_19.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:12:41.364544_captured_frame_20.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:17:41.425624_captured_frame_21.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:22:41.472469_captured_frame_22.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:27:41.501834_captured_frame_23.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:32:41.538025_captured_frame_24.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:37:41.550344_captured_frame_25.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:42:41.565371_captured_frame_26.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:47:41.660797_captured_frame_27.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:52:41.667282_captured_frame_28.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 20:57:41.693692_captured_frame_29.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:02:41.721790_captured_frame_30.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:07:41.754760_captured_frame_31.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:12:41.791607_captured_frame_32.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:17:41.828643_captured_frame_33.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:22:41.834543_captured_frame_34.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:27:41.927953_captured_frame_35.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:32:41.993226_captured_frame_36.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:37:42.030863_captured_frame_37.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:42:42.043692_captured_frame_38.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:47:42.124020_captured_frame_39.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:52:42.159839_captured_frame_40.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 21:57:42.196815_captured_frame_41.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:02:42.207873_captured_frame_42.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:07:42.255001_captured_frame_43.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:12:42.304034_captured_frame_44.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:17:42.338696_captured_frame_45.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:22:42.349684_captured_frame_46.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:27:42.382503_captured_frame_47.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:32:42.391337_captured_frame_48.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:37:42.424517_captured_frame_49.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:42:42.448217_captured_frame_50.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:47:42.493646_captured_frame_51.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:52:42.523427_captured_frame_52.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 22:57:42.557813_captured_frame_53.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:02:42.601307_captured_frame_54.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:07:42.635126_captured_frame_55.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:12:42.682218_captured_frame_56.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:17:42.724449_captured_frame_57.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:22:42.767956_captured_frame_58.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:27:42.848590_captured_frame_59.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:32:42.893293_captured_frame_60.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:37:42.922747_captured_frame_61.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:42:42.954281_captured_frame_62.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:47:42.971019_captured_frame_63.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:52:43.062536_captured_frame_64.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-07 23:57:43.066043_captured_frame_65.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:02:43.153025_captured_frame_66.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:07:43.173694_captured_frame_67.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:12:43.199560_captured_frame_68.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:17:43.300925_captured_frame_69.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:22:43.347876_captured_frame_70.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:27:43.365754_captured_frame_71.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:32:43.440897_captured_frame_72.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:37:43.540462_captured_frame_73.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:42:43.638664_captured_frame_74.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:47:43.690263_captured_frame_75.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:52:43.735337_captured_frame_76.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 00:57:43.735970_captured_frame_77.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:02:43.779411_captured_frame_78.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:07:43.846346_captured_frame_79.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:12:43.883210_captured_frame_80.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:17:43.891351_captured_frame_81.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:22:43.931253_captured_frame_82.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:27:43.970761_captured_frame_83.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:32:44.006982_captured_frame_84.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:37:44.103114_captured_frame_85.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:42:44.144241_captured_frame_86.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:47:44.178473_captured_frame_87.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:52:44.201132_captured_frame_88.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 01:57:44.216987_captured_frame_89.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:02:44.227948_captured_frame_90.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:07:44.260602_captured_frame_91.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:12:44.319142_captured_frame_92.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:17:44.381586_captured_frame_93.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:22:44.443666_captured_frame_94.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:27:44.492667_captured_frame_95.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:32:44.591100_captured_frame_96.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:37:44.643152_captured_frame_97.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:42:44.664627_captured_frame_98.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:47:44.707388_captured_frame_99.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:52:44.726383_captured_frame_100.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 02:57:44.737433_captured_frame_101.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:02:44.814908_captured_frame_102.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:07:44.819226_captured_frame_103.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:12:44.827575_captured_frame_104.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:17:44.899651_captured_frame_105.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:22:44.934952_captured_frame_106.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:27:44.966791_captured_frame_107.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:32:45.008211_captured_frame_108.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:37:45.018493_captured_frame_109.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:42:45.030083_captured_frame_110.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:47:45.057323_captured_frame_111.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:52:45.107915_captured_frame_112.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 03:57:45.157591_captured_frame_113.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:02:45.193810_captured_frame_114.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:07:45.220361_captured_frame_115.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:12:45.221789_captured_frame_116.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:17:45.239388_captured_frame_117.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:22:45.330362_captured_frame_118.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:27:45.374669_captured_frame_119.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:32:45.401053_captured_frame_120.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:37:45.437917_captured_frame_121.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:42:45.461922_captured_frame_122.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:47:45.503682_captured_frame_123.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:52:45.580192_captured_frame_124.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 04:57:45.625241_captured_frame_125.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:02:45.659804_captured_frame_126.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:07:45.714172_captured_frame_127.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:12:45.720629_captured_frame_128.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:17:45.742179_captured_frame_129.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:22:45.782340_captured_frame_130.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:27:45.804583_captured_frame_131.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:32:45.902838_captured_frame_132.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:37:45.913247_captured_frame_133.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:42:45.918754_captured_frame_134.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:47:45.956584_captured_frame_135.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:52:46.005816_captured_frame_136.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 05:57:46.042982_captured_frame_137.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:02:46.082034_captured_frame_138.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:07:46.161327_captured_frame_139.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:12:46.203575_captured_frame_140.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:17:46.299139_captured_frame_141.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:22:46.386825_captured_frame_142.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:27:46.481340_captured_frame_143.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:32:46.570643_captured_frame_144.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:37:46.576270_captured_frame_145.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:42:46.593852_captured_frame_146.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:47:46.631535_captured_frame_147.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:52:46.672242_captured_frame_148.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 06:57:46.719295_captured_frame_149.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:02:46.765380_captured_frame_150.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:07:46.788089_captured_frame_151.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:12:46.838490_captured_frame_152.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:17:46.886790_captured_frame_153.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:22:46.969893_captured_frame_154.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:27:47.000199_captured_frame_155.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:32:47.037296_captured_frame_156.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:37:47.044202_captured_frame_157.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:42:47.087622_captured_frame_158.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:47:47.131433_captured_frame_159.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:52:47.160197_captured_frame_160.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 07:57:47.244315_captured_frame_161.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:02:47.308492_captured_frame_162.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:07:47.356100_captured_frame_163.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:12:47.409948_captured_frame_164.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:17:47.451823_captured_frame_165.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:22:47.498511_captured_frame_166.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:27:47.522951_captured_frame_167.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:32:47.537032_captured_frame_168.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:37:47.554573_captured_frame_169.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:42:47.557926_captured_frame_170.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:47:47.593329_captured_frame_171.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:52:47.646175_captured_frame_172.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 08:57:47.693021_captured_frame_173.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 09:02:47.699658_captured_frame_174.jpg
Binary file is not shown |
+++ hls_streaming/captured_frame_/2024-05-08 09:07:47.756147_captured_frame_175.jpg
Binary file is not shown |
+++ hls_streaming/hls.py
... | ... | @@ -0,0 +1,115 @@ |
1 | +import time | |
2 | +import av | |
3 | +import cv2 | |
4 | +import numpy as np | |
5 | +import requests | |
6 | +from zoneinfo import ZoneInfo | |
7 | +from datetime import datetime | |
8 | +from threading import Lock, Thread, Event | |
9 | + | |
10 | + | |
11 | +class FrameCapturer: | |
12 | + def __init__(self, hls_url, cctv_id, interval=5, buffer_duration=15, buffer_size=600, time_zone="Asia/Seoul", endpoint="localhost:12345"): | |
13 | + ''' | |
14 | + :param hls_url: hls address | |
15 | + :param cctv_id: cctv_id number(whatever it is, this exists to distinguish from where. Further disscusion is needed with frontend developers.) | |
16 | + :param interval: interval of sampling in seconds | |
17 | + :param buffer_duration: video buffer, 15 seconds is default for ITS video streaming | |
18 | + :param time_zone: default Asia/Seoul | |
19 | + ''' | |
20 | + self.hls_url = hls_url | |
21 | + self.interval = interval | |
22 | + self.buffer_duration = buffer_duration | |
23 | + self.buffer_size = 600 | |
24 | + self.frame_buffer = [] | |
25 | + self.frame_buffer_lock = Lock() | |
26 | + self.captured_frame_count = 0 | |
27 | + self.last_capture_time = 0 | |
28 | + self.start_time = time.time() | |
29 | + self.stop_event = Event() | |
30 | + self.setup_stream() | |
31 | + self.cctvid = cctv_id | |
32 | + self.time_zone = ZoneInfo(time_zone) | |
33 | + self.endpoint = endpoint | |
34 | + | |
35 | + def setup_stream(self): | |
36 | + self.input_stream = av.open(self.hls_url) | |
37 | + self.video_stream = next(s for s in self.input_stream.streams if s.type == 'video') | |
38 | + self.fps = self.video_stream.guessed_rate.numerator | |
39 | + self.capture_interval = 1 / self.fps | |
40 | + | |
41 | + # ```capture_frames``` and ```process_frames``` work asynchronously (called with Thread) | |
42 | + # so that it always run as intended (for every '''interval''' sec, send a photo) | |
43 | + # regardless of what buffer frames are now. | |
44 | + # They are triggered by ```start``` and halts by ```stop``` | |
45 | + def capture_frames(self): | |
46 | + for packet in self.input_stream.demux(self.video_stream): | |
47 | + for frame in packet.decode(): | |
48 | + with self.frame_buffer_lock: | |
49 | + self.frame_buffer.append(frame) | |
50 | + time.sleep(self.capture_interval) | |
51 | + | |
52 | + def process_frames(self): | |
53 | + while not self.stop_event.is_set(): | |
54 | + current_time = time.time() | |
55 | + if current_time - self.start_time >= self.buffer_duration: | |
56 | + if self.last_capture_time == 0 or current_time - self.last_capture_time >= self.interval: | |
57 | + with self.frame_buffer_lock: | |
58 | + if self.frame_buffer: | |
59 | + if len(self.frame_buffer) > self.buffer_size: | |
60 | + self.frame_buffer = self.frame_buffer[-self.buffer_size:] | |
61 | + buffered_frame = self.frame_buffer[-1] | |
62 | + print(len(self.frame_buffer)) | |
63 | + img = buffered_frame.to_image() | |
64 | + img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) | |
65 | + frame_name = f"captured_frame_{self.captured_frame_count}.jpg" | |
66 | + img_binary = cv2.imencode('.png', img) | |
67 | + self.send_image_to_server(img_binary, self.endpoint) | |
68 | + cv2.imwrite(f'hls_streaming/captured_frame_/{datetime.now()}_{frame_name}', img) | |
69 | + self.last_capture_time = current_time | |
70 | + print(f"Captured {frame_name} at time: {current_time - self.start_time:.2f}s") | |
71 | + self.captured_frame_count +=1 | |
72 | + | |
73 | + time.sleep(0.1) | |
74 | + | |
75 | + def send_image_to_server(self, image, endpoint, image_type="png"): | |
76 | + time_sent = datetime.now(self.time_zone).strftime("yyyy-MM-dd'T'HH:mm:ss'Z'") | |
77 | + header = { | |
78 | + 'Content-Type': f'image/{image_type}', | |
79 | + 'x-time-sent': time_sent, | |
80 | + 'x-cctv-info': str(self.cctvid), | |
81 | + } | |
82 | + try: | |
83 | + requests.post(endpoint, headers=header, files=image) | |
84 | + except: | |
85 | + print("Can not connect to the analyzer server. Check the endpoint address or connection.") | |
86 | + | |
87 | + def start(self): | |
88 | + self.capture_thread = Thread(target=self.capture_frames) | |
89 | + self.process_thread = Thread(target=self.process_frames) | |
90 | + self.capture_thread.start() | |
91 | + self.process_thread.start() | |
92 | + | |
93 | + def stop(self): | |
94 | + self.stop_event.set() | |
95 | + self.capture_thread.join() | |
96 | + self.process_thread.join() | |
97 | + self.input_stream.close() | |
98 | + | |
99 | + | |
100 | +# Example usage | |
101 | +if __name__ == "__main__": | |
102 | + capturer = FrameCapturer( | |
103 | + 'http://cctvsec.ktict.co.kr/73496/' | |
104 | + '7xhDlyfDPK1AtaOUkAUDUJgZvfqvRXYYZUmRLxgPgKXk+eEtIJIfGkiC/gcQmysaz7zhDW2Jd8qhPCxgpo7cn5VqArnowyKjUePjdAmuQQ8=', | |
105 | + 101, 300 | |
106 | + ) | |
107 | + t1 = time.time() | |
108 | + try: | |
109 | + capturer.start() | |
110 | + time.sleep(600000) | |
111 | + finally: | |
112 | + capturer.stop() | |
113 | + t2 = time.time() | |
114 | + with open("result.txt", "w") as file: | |
115 | + file.write(f'{t2-t1} seconds before terminating') |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?