当前位置:网站首页>[Yolo] v5s version 6.1 loadstreams class source code interpretation
[Yolo] v5s version 6.1 loadstreams class source code interpretation
2022-07-18 12:01:00 【Orange civet cat】
Because there is a problem reading the network stream , Have to modify LoadStreams class , I really gain a lot from reading the source code , Here is a rough record , For general reference .
# Take receiving network video stream as an example
class LoadStreams:
# Pass in the parameter sources
def __init__(self, sources='streams.txt', img_size=640, stride=32, auto=True):
# Some parameters about prediction
self.mode = 'stream'
self.img_size = img_size
self.stride = stride
# Determine whether the source is a file , Network video streaming is certainly not a file
if os.path.isfile(sources):
with open(sources) as f:
sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
else:
# Convert to list
sources = [sources]
# The length of the list
n = len(sources)
# Some information of video stream
# self.imgs: picture
# self.fps: frames
# self.frames: How many frames
# self.threads: There are several threads for a few streams
self.imgs, self.fps, self.frames, self.threads = [None] * n, [0] * n, [0] * n, [None] * n
# Remove the messy symbols in the source name
self.sources = [clean_str(x) for x in sources] # clean source names for later
self.auto = auto
# enumerate loop , The index number will be displayed
for i, s in enumerate(sources): # index, source
# Start thread to read frames from video stream
# Use f String to splice strings
st = f'{
i + 1}/{
n}: {
s}... '
# If it is youtube Video on , How, how
if urlparse(s).hostname in ('www.youtube.com', 'youtube.com', 'youtu.be'): # if source is YouTube video
check_requirements(('pafy', 'youtube_dl==2020.12.2'))
import pafy
s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL
# eval() Function to execute a string expression , And return the value of the expression , Video streaming s = s
s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam
if s == 0:
assert not is_colab(), '--source 0 webcam unsupported on Colab. Rerun command in a local environment.'
assert not is_kaggle(), '--source 0 webcam unsupported on Kaggle. Rerun command in a local environment.'
# use opencv establish cap object , It can be regarded as pre fetching stream information
cap = cv2.VideoCapture(s)
# assert( Assertion ) Used to determine an expression , In the expression, the condition is false Trigger exception when
# Judge cap Whether to open
assert cap.isOpened(), f'{
st}Failed to open {
s}'
# Start extracting stream information
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan
# Store information ,float('inf') It's infinity , Second.
self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback
# Officially read the first frame
_, self.imgs[i] = cap.read() # guarantee first frame
# Initialize a thread , Specially responsible for reading screen information , If there are multiple cameras , Whether the data of two camera pictures can be collected at the same time by using multithreading
self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)
# Logging information
LOGGER.info(f"{
st} Success ({
self.frames[i]} frames {
w}x{
h} at {
self.fps[i]:.2f} FPS)")
self.threads[i].start()
LOGGER.info('') # newline
# check for common shapes
s = np.stack([letterbox(x, self.img_size, stride=self.stride, auto=self.auto)[0].shape for x in self.imgs])
# np.unique Remove duplicate numbers from the array , And output after sorting .
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
LOGGER.warning('WARNING: Stream shapes differ. For optimal performance supply similarly-shaped streams.')
def update(self, i, cap, stream):
# Read stream `i` frames in daemon thread
n, f, read = 0, self.frames[i], 1 # frame number, frame array, inference every 'read' frame
while cap.isOpened() and n < f:
n += 1
# _, self.imgs[index] = cap.read()
# cap.grab() Used to point to the next frame , Its grammatical form is :
# retval = cv2.VideoCapture.grab()
# If the function successfully points to the next frame , Return value retval by True.
cap.grab()
if n % read == 0:
# cap.retrieve() To decode , And return the letter cv2.VideoCapture.grab() Captured video frames . The syntax format of this function is :
# retval, image = cv2.VideoCapture.retrieve()
# image For the returned video frame , If it doesn't work , An empty image is returned .
#retval Is Boolean type , If unsuccessful , return False; Otherwise return to True.
success, im = cap.retrieve()
if success:
self.imgs[i] = im
else:
LOGGER.warning('WARNING: Video stream unresponsive, please check your IP camera connection.')
self.imgs[i] = np.zeros_like(self.imgs[i])
cap.open(stream) # re-open stream if signal was lost
time.sleep(0.0) # wait time
# following __iter__ and __next__ There are two magic methods , Update information when iterating this class
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord('q'): # q to quit
cv2.destroyAllWindows()
raise StopIteration
# Letterbox
img0 = self.imgs.copy()
img = [letterbox(x, self.img_size, stride=self.stride, auto=self.rect and self.auto)[0] for x in img0]
# Stack
img = np.stack(img, 0)
# Convert
img = img[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW
img = np.ascontiguousarray(img)
return self.sources, img, img0, None, ''
def __len__(self):
return len(self.sources) # 1E12 frames = 32 streams at 30 FPS for 30 years
边栏推荐
- 程序员头疼的 4 种原因 | 每日趣闻
- Deep learning ----- verification code
- C语言力扣第206题之反转链表。双指针法,迭代递归(三种方法)。图文保姆教程
- A limit question
- 为什么说大公司不是天堂,里面有哪些坑?
- 隐马尔科夫模型(hidden Markov model, HMM)
- 【VSCode】切换文件与编辑器的快捷键
- MATLAB绘图_1绘制衰减震荡曲线
- Ue5 fonctions simples de détection des collisions de rôles
- The distant savior obeys the objective law (IV) -- cultural attribute
猜你喜欢

Redis data type

一道极限题目

Development of upper computer -- a series of database problems are solved in a net

读书笔记:程序员的自我修养---第一章

How to calculate the running time of terminal equipment (such as water pump) during TIA Bo (attached with FB library file)?

Gao fushuai in unit testing, pytest framework (end) test report

数据库系统概述--数据模型概述

单元测试界的高富帅,Pytest框架 (完) 测试报告篇

Using mysql+excel to process data

【C语言】字符串函数和内存操作函数
随机推荐
Hidden Markov model (HMM)
【C语言】字符串函数和内存操作函数
【YOLO】v5s 6.1版本LoadStreams类源码解读
免费SSL证书申请及部署实践
[image fusion] multi focus image fusion in DCT domain based on MATLAB [including Matlab source code, 1973]
解决 : ReferenceError: PubSub is not defined
GDB常用指令
What effective measures can be taken to improve the efficiency of discussing research issues?
DDD Domain Driven Design
力扣(LeetCode)196. 删除重复的电子邮箱(2022.07.15)
The Sandbox Alpha 第三季首支预告片
[load balancer does not contain an instance for the service mall coupling] and the project start normally but cannot register with Nacos
重磅:Mobileye官宣推迟IPO,营收增速放缓、市场竞争加剧
Mobile Robotics (II) posture solution
限制ASML给中国供应光刻机?其实它已经离不开中国市场了
解决TS中“Cannot find module ‘path‘ or its corresponding type declarations.”
Redis data type
汇编语言指令大全
redis数据类型
为什么说大公司不是天堂,里面有哪些坑?