当前位置:网站首页>目标检测——使用yolov6进行视频推理
目标检测——使用yolov6进行视频推理
2022-06-29 06:32:00 【不变强不改名】
yolov6源码里自带了图片的推理,推理完成后结果图片保存再runs/inference文件夹下;
但当我们想对视频进行推理的时候不是很方便,但v6里面封装了对图片的推理函数,我们只需要稍微改一下即可。
默认的图片推理代码如下:
inferer = Inferer(source, weights, device, yaml, img_size, half)
inferer.infer(conf_thres, iou_thres, classes, agnostic_nms, max_det, save_dir, save_txt, save_img, hide_labels, hide_conf)我们只需要基于图片的infer代码,修改出一个infer_video接口即可:
找到inferer.py,定义两个新函数,其实就是复制infer和process_image函数并略微修改
1. infer_video
def infer_video(self, cap, conf_thres, iou_thres, classes, agnostic_nms, max_det, save_dir, save_txt, save_img, hide_labels, hide_conf):
cv2.namedWindow("test", cv2.WINDOW_NORMAL)
while True:
r, frame = cap.read()
if not r:
break
else:
img, img_src = self.precess_video(frame, self.img_size, self.stride, self.half)
img = img.to(self.device)
if len(img.shape) == 3:
img = img[None]
# expand for batch dim
pred_results = self.model(img)
det = non_max_suppression(pred_results, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)[0]
gn = torch.tensor(img_src.shape)[[1, 0, 1, 0]] # normalization gain whwh
img_ori = img_src
# check image and font
assert img_ori.data.contiguous, 'Image needs to be contiguous. Please apply to input images with np.ascontiguousarray(im).'
self.font_check()
if len(det):
det[:, :4] = self.rescale(img.shape[2:], det[:, :4], img_src.shape).round()
for *xyxy, conf, cls in reversed(det):
class_num = int(cls) # integer class
label = None if hide_labels else (self.class_names[
class_num] if hide_conf else f'{self.class_names[class_num]} {conf:.2f}')
self.plot_box_and_label(img_ori, max(round(sum(img_ori.shape) / 2 * 0.003), 2), xyxy, label,
color=self.generate_colors(class_num, True))
img_src = np.asarray(img_ori)
cv2.imshow("test", img_src)
if cv2.waitKey(33) == ord("q"):
break2. precess_video
@staticmethod
def precess_video(img, img_size, stride, half):
'''Process image before image inference.'''
try:
img_src = img
assert img_src is not None, f'Invalid image: {img}'
except Exception as e:
LOGGER.Warning(e)
image = letterbox(img_src, img_size, stride=stride)[0]
# Convert
image = image.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
image = torch.from_numpy(np.ascontiguousarray(image))
image = image.half() if half else image.float() # uint8 to fp16/32
image /= 255 # 0 - 255 to 0.0 - 1.0
return image, img_src然后回到infer.py,我们把推理图片的代码注掉,使用下面代码进行推理即可
# Inference
inferer = Inferer(source, weights, device, yaml, img_size, half)
# infer imgs
#inferer.infer(conf_thres, iou_thres, classes, agnostic_nms, max_det, save_dir, save_txt, save_img, hide_labels, hide_conf)
# infer video
videolist = ["../data/videos/test.MOV",
"../data/videos/test1.MOV",
"../data/videos/test2.mp4",
"../data/videos/test3.mp4",
"../data/videos/genvideo.avi"]
cap = cv2.VideoCapture(videolist[4])
inferer.infer_video(cap, conf_thres, iou_thres, classes, agnostic_nms, max_det, save_dir, save_txt, save_img, hide_labels, hide_conf)边栏推荐
- P5 DS - component and document Association
- QT (x): innosetup for software packaging
- Regular expressions for shell script values
- Week 12 - task 2- shoulder to shoulder cadres
- There are two ways for golang to develop mobile applications
- Convert data frame with date column to timeseries
- Introduction to Ceres Quartet
- 百度小程序自动提交搜索
- Review of MySQL knowledge points
- [MySQL technology topic] technical analysis and guide for analyzing the high availability architecture of MySQL
猜你喜欢

Part 63 - interpreter and compiler adaptation (II)

Hyperledger Fabric 2. X custom smart contract

Conditional test, if and case conditional test statements of shell script

How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)

Pytest (7) -yield and termination function
![ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience](/img/fd/4c24e10fc91a7ce7e709a0874ba675.jpg)
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience

Clickhouse data type

Rich material libraries make modeling easy and efficient for developers

VLAN experiment
![[high concurrency] deeply analyze the callable interface](/img/17/93056547aa1a2b342e1b159b7c41c2.jpg)
[high concurrency] deeply analyze the callable interface
随机推荐
Design of leetcode simple problem goal parser
[C language series] - initial C language (4)
[chromium] win10 vs2019 environment chromium configuration and compilation.
Why is there a packaging type?
How to hand over complex legacy systems?
QT (x): packaging and deployment
What is MES? What does it do?
Summary of redis basic knowledge points
3 frequently tested SQL data analysis questions (including data and code)
Leetcode simple question: judging the color of a grid on a chess board
[high concurrency] deeply analyze the callable interface
QT writing map comprehensive application 58 compatible with multi browser kernel
It turns out that the joys and sorrows of programmers are not interlinked
Week 12 - task 2- shoulder to shoulder cadres
想请教一下,究竟有哪些劵商推荐?在线开户是安全么?
How does MySQL implement distributed locks?
Client and server working modes of JVM
After “Go to Definition”, is there a command to return to where you came from?
Ghost in the Log4Shell
The most complete machine learning model training process