|
|
@@ -2,7 +2,6 @@ |
|
|
|
import json |
|
|
|
import time |
|
|
|
|
|
|
|
import GPUtil |
|
|
|
import cv2 |
|
|
|
import subprocess as sp |
|
|
|
|
|
|
@@ -50,28 +49,49 @@ class Cv2Util(): |
|
|
|
self.width = width |
|
|
|
self.height = height |
|
|
|
if width > 1600: |
|
|
|
self.wh = int(width * height * 3 // 8) |
|
|
|
self.wh = int(width * height * 3 // 4) |
|
|
|
self.wah = '%sx%s' % (int(self.width / 2), int(self.height / 2)) |
|
|
|
self.h = int(self.height * 3 // 4) |
|
|
|
self.h = int(self.height // 2) |
|
|
|
self.w = int(self.width // 2) |
|
|
|
self.hn = int(self.height // 2) |
|
|
|
self.wn = int(self.width // 2) |
|
|
|
w_f = self.wh != width * height * 3 / 8 |
|
|
|
h_f = self.h != self.height * 3 / 4 |
|
|
|
w_f = self.wh != width * height * 3 / 4 |
|
|
|
h_f = self.h != self.height / 2 |
|
|
|
wd_f = self.w != self.width / 2 |
|
|
|
if w_f or h_f or wd_f: |
|
|
|
self.resize_status = True |
|
|
|
self.wh = int(width * height * 3 // 2) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
self.h = int(self.height * 3 // 2) |
|
|
|
self.h = int(self.height) |
|
|
|
self.w = int(self.width) |
|
|
|
# self.wh = int(width * height * 3 // 8) |
|
|
|
# self.wah = '%sx%s' % (int(self.width / 2), int(self.height / 2)) |
|
|
|
# self.h = int(self.height * 3 // 4) |
|
|
|
# self.w = int(self.width // 2) |
|
|
|
# self.hn = int(self.height // 2) |
|
|
|
# self.wn = int(self.width // 2) |
|
|
|
# w_f = self.wh != width * height * 3 / 8 |
|
|
|
# h_f = self.h != self.height * 3 / 4 |
|
|
|
# wd_f = self.w != self.width / 2 |
|
|
|
# if w_f or h_f or wd_f: |
|
|
|
# self.resize_status = True |
|
|
|
# self.wh = int(width * height * 3 // 2) |
|
|
|
# self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
# self.h = int(self.height * 3 // 2) |
|
|
|
# self.w = int(self.width) |
|
|
|
else: |
|
|
|
self.wh = int(width * height * 3 // 2) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
self.h = int(self.height * 3 // 2) |
|
|
|
self.h = int(self.height) |
|
|
|
self.w = int(self.width) |
|
|
|
self.hn = int(self.height) |
|
|
|
self.wn = int(self.width) |
|
|
|
# self.wh = int(width * height * 3 // 2) |
|
|
|
# self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
# self.h = int(self.height * 3 // 2) |
|
|
|
# self.w = int(self.width) |
|
|
|
# self.hn = int(self.height) |
|
|
|
# self.wn = int(self.width) |
|
|
|
|
|
|
|
def clear_video_info(self): |
|
|
|
self.fps = None |
|
|
@@ -111,28 +131,49 @@ class Cv2Util(): |
|
|
|
self.width = int(width) |
|
|
|
self.height = int(height) |
|
|
|
if width > 1600: |
|
|
|
self.wh = int(width * height * 3 // 8) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.wah = '%sx%s' % (int(self.width / 2), int(self.height / 2)) |
|
|
|
self.h = int(self.height * 3 // 4) |
|
|
|
self.w = int(self.width / 2) |
|
|
|
self.hn = int(self.height / 2) |
|
|
|
self.h = int(self.height // 2) |
|
|
|
self.w = int(self.width // 2) |
|
|
|
self.hn = int(self.height // 2) |
|
|
|
self.wn = int(self.width // 2) |
|
|
|
w_f = self.wh != width * height * 3 / 8 |
|
|
|
h_f = self.h != self.height * 3 / 4 |
|
|
|
w_f = self.wh != width * height * 3 / 4 |
|
|
|
h_f = self.h != self.height / 2 |
|
|
|
wd_f = self.w != self.width / 2 |
|
|
|
if w_f or h_f or wd_f: |
|
|
|
self.resize_status = True |
|
|
|
self.wh = int(width * height * 3 // 2) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
self.h = int(self.height * 3 // 2) |
|
|
|
self.h = int(self.height) |
|
|
|
self.w = int(self.width) |
|
|
|
# self.wh = int(width * height * 3 // 8) |
|
|
|
# self.wah = '%sx%s' % (int(self.width / 2), int(self.height / 2)) |
|
|
|
# self.h = int(self.height * 3 // 4) |
|
|
|
# self.w = int(self.width / 2) |
|
|
|
# self.hn = int(self.height / 2) |
|
|
|
# self.wn = int(self.width // 2) |
|
|
|
# w_f = self.wh != width * height * 3 / 8 |
|
|
|
# h_f = self.h != self.height * 3 / 4 |
|
|
|
# wd_f = self.w != self.width / 2 |
|
|
|
# if w_f or h_f or wd_f: |
|
|
|
# self.resize_status = True |
|
|
|
# self.wh = int(width * height * 3 // 2) |
|
|
|
# self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
# self.h = int(self.height * 3 // 2) |
|
|
|
# self.w = int(self.width) |
|
|
|
else: |
|
|
|
self.wh = int(width * height * 3 // 2) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
self.h = int(self.height * 3 // 2) |
|
|
|
self.h = int(self.height) |
|
|
|
self.w = int(self.width) |
|
|
|
self.hn = int(self.height) |
|
|
|
self.wn = int(self.width) |
|
|
|
# self.wh = int(width * height * 3 // 2) |
|
|
|
# self.wah = '%sx%s' % (int(self.width), int(self.height)) |
|
|
|
# self.h = int(self.height * 3 // 2) |
|
|
|
# self.w = int(self.width) |
|
|
|
# self.hn = int(self.height) |
|
|
|
# self.wn = int(self.width) |
|
|
|
if nb_frames: |
|
|
|
self.all_frames = int(nb_frames) |
|
|
|
up, down = str(fps).split('/') |
|
|
@@ -182,8 +223,9 @@ class Cv2Util(): |
|
|
|
self.height = int(height) |
|
|
|
if self.width and self.height: |
|
|
|
if self.isGpu: |
|
|
|
self.wh = int(width * height * 3 // 2) |
|
|
|
self.read_w_h = (self.height * 3 // 2, self.width) |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.read_w_h = ([self.height, self.width, 3]) |
|
|
|
# self.read_w_h = (self.height, self.width) |
|
|
|
else: |
|
|
|
self.wh = int(width * height * 3) |
|
|
|
self.read_w_h = ([self.height, self.width, 3]) |
|
|
@@ -232,6 +274,7 @@ class Cv2Util(): |
|
|
|
command.extend(['-i', self.pullUrl, |
|
|
|
# '-vf', f'scale=iw*{scale_percent//100}:ih*{scale_percent//100}', |
|
|
|
'-f', 'rawvideo', |
|
|
|
'-f' |
|
|
|
'-an', |
|
|
|
'-']) |
|
|
|
else: |
|
|
@@ -243,6 +286,7 @@ class Cv2Util(): |
|
|
|
'-hwaccel', 'cuda', |
|
|
|
'-i', self.pullUrl, |
|
|
|
'-f', 'rawvideo', |
|
|
|
'-pix_fmt', 'bgr24', |
|
|
|
'-an', |
|
|
|
'-']) |
|
|
|
self.pull_p = sp.Popen(command, stdout=sp.PIPE) |
|
|
@@ -277,10 +321,11 @@ class Cv2Util(): |
|
|
|
logger.exception("视频格式异常:{}, requestId:{}", ei, self.requestId) |
|
|
|
raise ServiceException(ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[0], |
|
|
|
ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[1]) |
|
|
|
if self.isGpu: |
|
|
|
result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) |
|
|
|
else: |
|
|
|
result = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
|
|
|
result = img |
|
|
|
# if self.isGpu: |
|
|
|
# result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) |
|
|
|
# else: |
|
|
|
# result = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
|
|
|
self.current_frame += 1 |
|
|
|
except ServiceException as s: |
|
|
|
raise s |
|
|
@@ -328,6 +373,7 @@ class Cv2Util(): |
|
|
|
'-resize', self.wah, |
|
|
|
'-i', self.pullUrl, |
|
|
|
'-f', 'rawvideo', |
|
|
|
'-pix_fmt', 'bgr24', |
|
|
|
'-an', |
|
|
|
'-']) |
|
|
|
self.pull_p = sp.Popen(command, stdout=sp.PIPE) |
|
|
@@ -363,21 +409,23 @@ class Cv2Util(): |
|
|
|
# logger.error("拉流管道为空, requestId:{}", self.requestId) |
|
|
|
# raise ServiceException(ExceptionType.PULL_PIPELINE_INIT_EXCEPTION.value[0], |
|
|
|
# ExceptionType.PULL_PIPELINE_INIT_EXCEPTION.value[1]) |
|
|
|
# in_bytes = self.pull_p.stdout.read(self.wh) |
|
|
|
in_bytes = self.pull_p.stdout.read(self.wh) |
|
|
|
self.current_frame += 1 |
|
|
|
if in_bytes is not None and len(in_bytes) > 0: |
|
|
|
# result = (np.frombuffer(in_bytes, np.uint8).reshape([int(self.height), int(self.width), 3])) |
|
|
|
try: |
|
|
|
img = (np.frombuffer(in_bytes, np.uint8)).reshape((self.h, self.w)) |
|
|
|
result = (np.frombuffer(in_bytes, np.uint8).reshape([int(self.height), int(self.width), 3])) |
|
|
|
# img = (np.frombuffer(in_bytes, np.uint8)).reshape((self.h, self.w)) |
|
|
|
except Exception as ei: |
|
|
|
logger.exception("视频格式异常:{}, requestId:{}", ei, self.requestId) |
|
|
|
raise ServiceException(ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[0], |
|
|
|
ExceptionType.VIDEO_RESOLUTION_EXCEPTION.value[1]) |
|
|
|
result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) |
|
|
|
# result = cv2.cvtColor(img, cv2.COLOR_YUV2BGR_NV12) |
|
|
|
# result = cv2.cvtColor(result, cv2.COLOR_RGB2BGR) |
|
|
|
if self.resize_status: |
|
|
|
if self.width > 1600: |
|
|
|
result = cv2.resize(result, (int(self.width / 2), int(self.height / 2)), |
|
|
|
interpolation=cv2.INTER_LINEAR) |
|
|
|
self.current_frame += 1 |
|
|
|
except ServiceException as s: |
|
|
|
raise s |
|
|
|
except Exception as e: |