赞
踩
opencv
的摄像头实时目标检测anaconda
环境conda create -n XXX python=3.10
# 查看cuda版本(示例为:11.8)
nvcc -V
# 安装对应版本的pytorch
# 官网:https://pytorch.org/
# pip安装
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# conda安装,建议配置conda国内镜像源
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
pip install -r ./requirements-target-dec.txt
在人工智能和机器学习的浪潮中,YOLO8作为目标检测领域的一颗新星,以其卓越的性能和灵活性,受到了广泛关注。本项目基于YOLO8算法,构建了一个高效、易用的目标检测系统,旨在为用户提供一个强大的本地部署解决方案。通过精心设计的界面和丰富的功能,用户可以轻松实现目标检测任务,无论是在网页端还是本地计算机上。
scan_taskflow
提供可执行对象import cv2
import gradio as gr
from scan_task import ScanTargetDec
scan_model = ScanTargetDec(version='YOLOv8n', use_gpu=False)
def target_scan(frame):
frame, _ = scan_model.run(frame, text_size=50)
'''run方法其他可传参数
text_color: 显示文字颜色 默认:(0, 0, 255)
text_size: 显示文字大小 默认:20
y_pos: y轴位置偏移量 默认:0
'''
return frame
if __name__ == '__main__':
examples = [
[cv2.imread('./examples/image_detection.jpg')]]
with gr.Blocks() as demo:
with gr.Tabs():
# 图片目标检测
with gr.Tab(label='图片目标检测') as tab1:
gr.Markdown(value="# 图片目标检测")
with gr.Row(variant="panel"):
with gr.Column():
img_input1 = gr.Image(label="上传图片输入", mirror_webcam=False)
with gr.Row(variant="panel"):
submit_bn1 = gr.Button(value='上传')
clear_bn1 = gr.ClearButton(value='清除')
img_out1 = gr.Image(label="目标检测输出", mirror_webcam=False)
# 添加演示用例
gr.Examples(label='上传示例图片', examples=examples, fn=target_scan,
inputs=[img_input1],
outputs=[img_out1],
cache_examples=False)
submit_bn1.click(fn=target_scan, inputs=img_input1, outputs=img_out1)
clear_bn1.add([img_input1, img_out1])
# 摄像头实时目标检测
with gr.Tab(label='摄像头实时目标检测') as tab3:
gr.Markdown(value="# 摄像头实时目标检测")
with gr.Column(variant='panel') as demo_scan:
with gr.Row(variant="panel"):
img_input3 = gr.Image(label="实时输入", sources=["webcam"],
mirror_webcam=False, streaming=True)
img_out3 = gr.Image(label="目标检测输出", sources=["webcam"],
mirror_webcam=False, streaming=True)
img_input3.stream(fn=target_scan, inputs=img_input3, outputs=img_out3)
demo.launch()
use_gpu
、text_color
、text_size
、y_pos
use_gpu
: 是否使用gputext_color
:定位的二维码,显示文字颜色 默认:(0, 0, 255)text_size
:定位的二维码,显示文字大小 默认:20y_pos
:y轴位置偏移量 默认:0class ScanTaskflow:
def __init__(self, task: str, video_index=0, win_name='Scan XXX', win_width=800, win_height=600, **kwargs):
..初始化摄像头扫描对象,设置窗口尺寸等属性..
def run(self, **kwargs):
..开启摄像头,进行检测任务..
if __name__ == '__main__':
# 启动默认的目标检测系统
scanTaskflow = ScanTaskflow(task='scan_target_dec',
version='YOLOv8n', use_gpu=True,
video_index=0, win_name='target_dec',
win_width=640, win_height=480)
scanTaskflow.run(text_color=(0, 255, 0), y_pos=0)
__init__
用于预加载项目所需模型run
是检测系统的核心方法,用于将视频的实时帧进行检测Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。