-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
171 lines (126 loc) · 4.43 KB
/
test.py
File metadata and controls
171 lines (126 loc) · 4.43 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import cv2
from ultralytics import YOLO
import numpy as np
from io import BytesIO
from PIL import Image
def compress_image(image, quality=10):
# OpenCV 이미지를 Pillow 이미지로 변환
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# 이미지를 BytesIO에 저장하고 JPEG 형식으로 압축
output_buffer = BytesIO()
pil_image.save(output_buffer, format="JPEG", quality=quality)
# 압축된 이미지 바이너리를 반환
return output_buffer.getvalue()
def getVideoStreaming():
# Load the YOLOv8 model
# model = YOLO('C:/Users/USER/Downloads/ultralytics-main/runs/detect/train35/weights/best.pt')
model = YOLO('/app/best.pt')
# 동영상 파일 사용시
video_path = "/app/as.mp4"
# 비디오 캡처 객체 생성
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
# YOLOv8 모델을 사용하여 프레임에 추론 수행
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# 결과가 발생하면 'event' 출력
# if results[0].speed:
# print('event')
# yield "event: alarm\ndata: {}\n\n"
# 이미지를 바이너리로 인코딩
ret, buffer = cv2.imencode('.jpg', annotated_frame)
frame_bytes = bytearray(buffer.tobytes())
# 이미지를 압축
compressed_frame = compress_image(annotated_frame)
# 바이너리 이미지를 전송
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + compressed_frame + b'\r\n')
def getVideoStreaming1():
video_path1 = "/app/n1.mp4"
cap = cv2.VideoCapture(video_path1)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Encode the image into binary
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n'
)
def getVideoStreaming2():
video_path1 = "/app/n2.mp4"
cap = cv2.VideoCapture(video_path1)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Encode the image into binary
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n'
)
def getVideoStreaming3():
video_path1 = "/app/n3.mp4"
cap = cv2.VideoCapture(video_path1)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Encode the image into binary
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n'
)
def getVideoStreaming4():
video_path1 = "/app/n4.mp4"
cap = cv2.VideoCapture(video_path1)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Encode the image into binary
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n'
)
def getVideoStreaming5():
video_path1 = "/app/n5.mp4"
cap = cv2.VideoCapture(video_path1)
while cap.isOpened():
success, frame = cap.read()
if not success:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
# Encode the image into binary
ret, buffer = cv2.imencode('.jpg', frame)
if not ret:
continue
frame_bytes = buffer.tobytes()
yield (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n'
)