yolov6源码自带了图片目标检测推理功能;
当我们想进行摄像头实时检测识别的时候会有点不方便,根据源码的图片目标检测推理功能,我们进行稍作调整即可进行调用本地摄像头进行目标检测推理功能。

首先在源码文件路径 tools/infer.py 打开infer.py文件,如下图所示:

infer.py文件第22行修改如下内容:

#source 如果是要根据单独图片进行识别可以输入图片的路径或者图片文件夹的路径; 如果是要根据摄像头实时识别则输入 0
parser.add_argument('--source', type=str, default='0', help='the source path, 0, e.g. image-file/dir/camera.')

infer.py文件第100行修改如下内容:

    if source != '0':if save_txt or save_img:LOGGER.info(f"Results saved to {save_dir}")

其次在源码文件路径 yolov6/inferer.py 打开inferer.py文件,新增以及修改内容如下所示:

inferer.py文件第34行增加如下内容:

self.camera = source

inferer.py文件第45行 #Load datadef infer函数之前的内容修改成如下内容:

        # open cameraif self.camera == '0':passelse:# Load dataif os.path.isdir(source):img_paths = sorted(glob.glob(os.path.join(source, '*.*')))  # direlif os.path.isfile(source):img_paths = [source]  # fileselse:raise Exception(f'Invalid path: {source}')self.img_paths = [img_path for img_path in img_paths if img_path.split('.')[-1].lower() in IMG_FORMATS]

inferer.py文件第58行def infer函数整个更改为如下图所示的内容:

    def infer(self, conf_thres, iou_thres, classes, agnostic_nms, max_det, save_dir, save_txt, save_img, hide_labels, hide_conf):''' Model Inference and results visualization '''if self.camera == '0':print("开始调用摄像头...")cap = cv2.VideoCapture(0)while True:f, img_src = cap.read()image = letterbox(img_src, self.img_size, stride=self.stride)[0]txt_path = osp.join(save_dir, 'labels', osp.splitext(osp.basename(img_path))[0])# Convertimage = image.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGBimage = torch.from_numpy(np.ascontiguousarray(image))image = image.half() if self.half else image.float()  # uint8 to fp16/32image /= 255  # 0 - 255 to 0.0 - 1.0img = imageimg = img.to(self.device)if len(img.shape) == 3:img = img[None]# expand for batch dimpred_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 whwhimg_ori = img_src# check image and fontassert 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 classlabel = 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.namedWindow('test', cv2.WINDOW_AUTOSIZE)     # 窗口设置为自动调节大小cv2.imshow('test', img_src)if cv2.waitKey(1) & 0xFF == ord('q'):       # 按q退出breakcap.release()   # 释放摄像头cv2.destroyAllWindows()  # 结束所有窗口                else:for img_path in tqdm(self.img_paths):img, img_src = self.precess_image(img_path, self.img_size, self.stride, self.half)img = img.to(self.device)if len(img.shape) == 3:img = img[None]# expand for batch dimpred_results = self.model(img)det = non_max_suppression(pred_results, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)[0]save_path = osp.join(save_dir, osp.basename(img_path))  # im.jpgtxt_path = osp.join(save_dir, 'labels', osp.splitext(osp.basename(img_path))[0])gn = torch.tensor(img_src.shape)[[1, 0, 1, 0]]  # normalization gain whwhimg_ori = img_src# check image and fontassert 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):if save_txt:  # Write to filexywh = (self.box_convert(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf)with open(txt_path + '.txt', 'a') as f:f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img:class_num = int(cls)  # integer classlabel = 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)# Save results (image with detections)if save_img:cv2.imwrite(save_path, img_src)

做完上述所有的修改后,回到 tools/infer.py 运行infer.py文件,识别结果如下图所示:

目标检测——使用yolov6调用本地摄像头进行实时检测相关推荐

  1. 人工智能Java SDK:读取本地摄像头,实时检测人脸

    人脸检测 人脸识别技术目前已经广泛应用于包括人脸门禁系统.刷脸支付等各行各业.随着人脸识别技术的提升,应用越来越广泛.目前中国的人脸识 别技术已经在世界水平上处于领先地位,在安防行业,国内主流安防厂家 ...

  2. Web网页调用本地摄像头、实时获取图片

    PC端Web网页跳用本地摄像头,实时获取图片.(注意事项:如果是本地localhost可直接调用,挂到服务器必须使用https访问) 仅测试了google浏览器,运行代码,会跳出摄像头授权,请按&qu ...

  3. opencv图片倾斜度检测(二)利用摄像头进行实时检测图片中物体并画出坐标轴和倾斜度

    是在检测图片的基础上进行加工的 详情可看opencv图片倾斜度检(一)对图片进行检测 打开摄像头进行实时检测矩形轮廓,实时画出坐标轴坐标点和倾斜度,并且具有保存图片和利用plot单纯画出矩形的功能 直 ...

  4. ROS调用本地摄像头数据并在rviz里显示

    ROS调用本地摄像头数据并在rviz里显示: 1 通过usb_cam驱动包启动摄像头 1.1 创建并编译usb_cam驱动程序 1.2 启动usb_cam_node节点 1.3 在rviz中显示本地摄 ...

  5. vue调用本地摄像头实现拍照

    前言: vue调用本地摄像头实现拍照功能,由于调用摄像头有使用权限,只能在本地运行,线上需用https域名才可以使用. 实现效果: 1.摄像头效果: 2.拍照效果: 实现代码: <templat ...

  6. OpenCV调用手机摄像头与人脸检测

    文章目录 一.OpenCV机器视觉环境搭建 1. OpenCV下载 2. OpenCV安装 3. 安装测试 二.OpenCV调用摄像头及人脸检测 1. 调用电脑摄像头 2. 调用手机摄像头及人脸检测 ...

  7. js调用本地摄像头拍照截图,提交后台

    今天有个需求,需要在前端界面调用本地摄像头,然后拍照结束后可以截取预览,最后将结果提交到后台.查了网上很多的插件,发现适合的非常少,于是决定自己修改一个. 这里我修改了一个jquery插件,把摄像头拍 ...

  8. JS调用本地摄像头拍照(兼容各大浏览器及IE8+)

    最近做的项目遇到了个难题,使用video+canvas+getUserMedia()写的调用本地摄像头拍照不兼容IE. 原因:IE8及以下不支持HTML5标签:video和canvas:IE11及以下 ...

  9. 摄像头 虚拟服务器页面,虚拟云服务器能调用本地摄像头

    虚拟云服务器能调用本地摄像头 内容精选 换一换 虚拟IP(Virtual IP Address,简称VIP)是一个未分配给真实弹性云服务器网卡的IP地址.弹性云服务器除了拥有私有IP地址外,还可以拥有 ...

最新文章

  1. 贾跃亭画了一个8500亿的大饼
  2. 草图检索和识别[开源]
  3. 从LASSO回归到结构性稀疏:线性回归的正则项都带来了什么?
  4. utf8编码转换脚本
  5. 前端学习(3079):vue+element今日头条管理-数据筛选处理
  6. oracle12 快照保存时间,【AWR】调整AWR数据采样时间间隔及历史快照保留时间
  7. 洛谷P2351 [SDOi2012]吊灯 【数学】
  8. 最适合物联网的开源数据库
  9. Fault Tolerance 要求、限制和许可
  10. 客户端访问Web Service--参数类型的序列化与反序列化(一)
  11. 输入起止坐标,返回途径网格。
  12. android hdmi拔插广播,Android_8.1插拔hdmi后,音量会变到最大
  13. 3000字作文 小孩调皮
  14. 从0开始学AI-DAY1
  15. 使用echarts完成中国省市区县镇地图展示
  16. 串口发送数据,只接收到00的原因之一
  17. 《Android游戏编程入门经典》——1.1节Android 4简介
  18. 电销行业竞争与日俱增,西安外呼系统如何改善?
  19. do while循环
  20. Agile EC3010 MCAD UserGuide-1

热门文章

  1. RoboMaster视觉教程(6)目标位置解算(PnP求解目标与摄像头间的相对位置)
  2. 怎么让上下两排对齐_《excel表中怎么使同一格内的上下两行对齐》 excel两表格数据对齐...
  3. 【人工智能项目】深度学习实现白葡萄酒品质预测
  4. ZJCTF-Triple Language Write up
  5. 你是真的“C”——宏与函数的英雄本色
  6. 小米机器人履带双轮平衡_小米米兔机器人评测:一个站在平衡车上的机器人
  7. 谱半径一定大于0_S11大于0怎么回事
  8. SQL Server-判断日期是否为周六 周日
  9. 嵌入式技术的前沿应用领域
  10. js 将阿拉伯数字翻译成中文的大写数字