瀏覽代碼

公共组件服务提交代码

tags/V2.7.3^2
chenyukun 1 年之前
父節點
當前提交
f70cb7aac9
共有 2 個檔案被更改,包括 80 行新增30 行删除
  1. +3
    -1
      .idea/workspace.xml
  2. +77
    -29
      util/Cv2Utils.py

+ 3
- 1
.idea/workspace.xml 查看文件

@@ -6,6 +6,7 @@
<component name="ChangeListManager">
<list default="true" id="4f7dccd9-8f92-4a6e-90cc-33890d102263" name="Changes" comment="Changes">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/util/Cv2Utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/util/Cv2Utils.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -134,7 +135,7 @@
"WebServerToolWindowPanel.toolwindow.show.date": "false",
"WebServerToolWindowPanel.toolwindow.show.permissions": "false",
"WebServerToolWindowPanel.toolwindow.show.size": "false",
"last_opened_file_path": "D:/tuoheng/code/tuoheng_alg",
"last_opened_file_path": "D:/tuoheng/code/AIlib2",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -422,6 +423,7 @@
<workItem from="1679013228398" duration="17427000" />
<workItem from="1679039229464" duration="9832000" />
<workItem from="1679118299629" duration="17688000" />
<workItem from="1679289612196" duration="5380000" />
</task>
<servers />
</component>

+ 77
- 29
util/Cv2Utils.py 查看文件

@@ -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:

Loading…
取消
儲存