解决方案:
参考:Intel Realsense D435报错 RuntimeError: MFCreateDeviceSource(_device_attrs, &_source) returned: HResult

完整示例代码:
主要看def dontla_evaluate_detect(self)

# -*- coding: utf-8 -*-
"""
@File    : test-使用locals()函数批量配置摄像头运行识别程序并画框.py
@Time    : 2019/11/26 11:20
@Author  : Dontla
@Email   : sxana@qq.com
@Software: PyCharm
"""import cv2
import numpy as np
import tensorflow as tf
import core.utils as utils
from core.config import cfg
from core.yolov3 import YOLOV3
import pyrealsense2 as rs
import timeclass YoloTest(object):def __init__(self):# D·C 191111:__C.TEST.INPUT_SIZE = 544self.input_size = cfg.TEST.INPUT_SIZEself.anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE# Dontla 191106注释:初始化class.names文件的字典信息属性self.classes = utils.read_class_names(cfg.YOLO.CLASSES)# D·C 191115:类数量属性self.num_classes = len(self.classes)self.anchors = np.array(utils.get_anchors(cfg.YOLO.ANCHORS))# D·C 191111:__C.TEST.SCORE_THRESHOLD = 0.3self.score_threshold = cfg.TEST.SCORE_THRESHOLD# D·C 191120:__C.TEST.IOU_THRESHOLD = 0.45self.iou_threshold = cfg.TEST.IOU_THRESHOLDself.moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY# D·C 191120:__C.TEST.ANNOT_PATH = "./data/dataset/Dontla/20191023_Artificial_Flower/test.txt"self.annotation_path = cfg.TEST.ANNOT_PATH# D·C 191120:__C.TEST.WEIGHT_FILE = "./checkpoint/f_g_c_weights_files/yolov3_test_loss=15.8845.ckpt-47"self.weight_file = cfg.TEST.WEIGHT_FILE# D·C 191115:可写标记(bool类型值)self.write_image = cfg.TEST.WRITE_IMAGE# D·C 191115:__C.TEST.WRITE_IMAGE_PATH = "./data/detection/"(识别图片画框并标注文本后写入的图片路径)self.write_image_path = cfg.TEST.WRITE_IMAGE_PATH# D·C 191116:TEST.SHOW_LABEL设置为Trueself.show_label = cfg.TEST.SHOW_LABEL# D·C 191120:创建命名空间“input”with tf.name_scope('input'):# D·C 191120:建立变量(创建占位符开辟内存空间)self.input_data = tf.placeholder(dtype=tf.float32, name='input_data')self.trainable = tf.placeholder(dtype=tf.bool, name='trainable')model = YOLOV3(self.input_data, self.trainable)self.pred_sbbox, self.pred_mbbox, self.pred_lbbox = model.pred_sbbox, model.pred_mbbox, model.pred_lbbox# D·C 191120:创建命名空间“指数滑动平均”with tf.name_scope('ema'):ema_obj = tf.train.ExponentialMovingAverage(self.moving_ave_decay)# D·C 191120:在允许软设备放置的会话中启动图形并记录放置决策。(不懂啥意思。。。)allow_soft_placement=True表示允许tf自动选择可用的GPU和CPUself.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))# D·C 191120:variables_to_restore()用于加载模型计算滑动平均值时将影子变量直接映射到变量本身self.saver = tf.train.Saver(ema_obj.variables_to_restore())# D·C 191120:用于下次训练时恢复模型self.saver.restore(self.sess, self.weight_file)def predict(self, image):# D·C 191107:复制一份图片的镜像,避免对图片直接操作改变图片的内在属性org_image = np.copy(image)# D·C 191107:获取图片尺寸org_h, org_w, _ = org_image.shape# D·C 191108:该函数将源图结合input_size,将其转换成预投喂的方形图像(作者默认544×544,中间为缩小尺寸的源图,上下空区域为灰图):image_data = utils.image_preprocess(image, [self.input_size, self.input_size])# D·C 191108:打印维度看看:# print(image_data.shape)# (544, 544, 3)# D·C 191108:创建新轴,不懂要创建新轴干嘛?image_data = image_data[np.newaxis, ...]# D·C 191108:打印维度看看:# print(image_data.shape)# (1, 544, 544, 3)# D·C 191110:三个box可能存放了预测框图(可能是N多的框,有用的没用的重叠的都在里面)的信息(但是打印出来的值完全看不懂啊喂?)pred_sbbox, pred_mbbox, pred_lbbox = self.sess.run([self.pred_sbbox, self.pred_mbbox, self.pred_lbbox],feed_dict={self.input_data: image_data,self.trainable: False})# D·C 191110:打印三个box的类型、形状和值看看:# print(type(pred_sbbox))# print(type(pred_mbbox))# print(type(pred_lbbox))# 都是<class 'numpy.ndarray'># print(pred_sbbox.shape)# print(pred_mbbox.shape)# print(pred_lbbox.shape)# (1, 68, 68, 3, 6)# (1, 34, 34, 3, 6)# (1, 17, 17, 3, 6)# print(pred_sbbox)# print(pred_mbbox)# print(pred_lbbox)# D·C 191110:(-1,6)表示不知道有多少行,反正你给我整成6列,然后concatenate又把它们仨给叠起来,最终得到无数个6列数组(后面self.num_classes)个数存放的貌似是这个框属于类的概率)pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + self.num_classes)),np.reshape(pred_mbbox, (-1, 5 + self.num_classes)),np.reshape(pred_lbbox, (-1, 5 + self.num_classes))], axis=0)# D·C 191111:打印pred_bbox和它的维度看看:# print(pred_bbox)# print(pred_bbox.shape)# (18207, 6)# D·C 191111:猜测是第一道过滤,过滤掉score_threshold以下的图片,过滤完之后少了好多:# D·C 191115:bboxes维度为[n,6],前四列是坐标,第五列是得分,第六列是对应类下标bboxes = utils.postprocess_boxes(pred_bbox, (org_h, org_w), self.input_size, self.score_threshold)# D·C 191111:猜测是第二道过滤,过滤掉iou_threshold以下的图片:bboxes = utils.nms(bboxes, self.iou_threshold)return bboxesdef dontla_evaluate_detect(self):ctx = rs.context()devices = ctx.query_devices()# 循环reset摄像头# hardware_reset()后是不是应该延迟一段时间?不延迟就会报错for dev in devices:dev.hardware_reset()print('摄像头{}重置成功'.format(i for i, x in enumerate(devices) if x == dev))time.sleep(5)# Check if the cameras are all connectedcam_num = len(ctx.devices)print('摄像头个数:{}'.format(cam_num))if cam_num != 6:print('The cameras are not all connected!')else:# Print the serial number of the connected camera and its USB interface numbern = 0# 创建需要显示在窗口上的备注信息(窗口名)serial_list = []for i in ctx.devices:n += 1serial_list.append('camera{}; serials number {}; usb port {}'.format(n, i.get_info(rs.camera_info.serial_number),i.get_info(rs.camera_info.usb_type_descriptor)))print('serial number {}:{};usb port:{}'.format(n, i.get_info(rs.camera_info.serial_number),i.get_info(rs.camera_info.usb_type_descriptor)))# print(serial_list)for i in range(cam_num):locals()['pipeline' + str(i)] = rs.pipeline()locals()['config' + str(i)] = rs.config()locals()['serial' + str(i)] = ctx.devices[i].get_info(rs.camera_info.serial_number)locals()['config' + str(i)].enable_device(locals()['serial' + str(i)])locals()['config' + str(i)].enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)locals()['config' + str(i)].enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)locals()['pipeline' + str(i)].start(locals()['config' + str(i)])# 创建对齐对象(深度对齐颜色)locals()['align' + str(i)] = rs.align(rs.stream.color)try:# 设置break标志break2 = Falsewhile True:for i in range(cam_num):locals()['frames' + str(i)] = locals()['pipeline' + str(i)].wait_for_frames()# 获取对齐帧集locals()['aligned_frames' + str(i)] = locals()['align' + str(i)].process(locals()['frames' + str(i)])# 获取对齐后的深度帧和彩色帧locals()['aligned_depth_frame' + str(i)] = locals()['aligned_frames' + str(i)].get_depth_frame()locals()['color_frame' + str(i)] = locals()['aligned_frames' + str(i)].get_color_frame()if not locals()['aligned_depth_frame' + str(i)] or not locals()['color_frame' + str(i)]:continue# 获取颜色帧内参locals()['color_profile' + str(i)] = locals()['color_frame' + str(i)].get_profile()locals()['cvsprofile' + str(i)] = rs.video_stream_profile(locals()['color_profile' + str(i)])locals()['color_intrin' + str(i)] = locals()['cvsprofile' + str(i)].get_intrinsics()locals()['color_intrin_part' + str(i)] = [locals()['color_intrin' + str(i)].ppx,locals()['color_intrin' + str(i)].ppy,locals()['color_intrin' + str(i)].fx,locals()['color_intrin' + str(i)].fy]locals()['color_image' + str(i)] = np.asanyarray(locals()['color_frame' + str(i)].get_data())# D·C 191121:显示帧看看# cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)# cv2.imshow('RealSense', color_frame)# cv2.waitKey(1)locals()['bboxes_pr' + str(i)] = self.predict(locals()['color_image' + str(i)])locals()['image' + str(i)] = utils.draw_bbox(locals()['color_image' + str(i)],locals()['bboxes_pr' + str(i)],locals()['aligned_depth_frame' + str(i)],locals()['color_intrin_part' + str(i)],show_label=self.show_label)# cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)# cv2.imshow('window{}'.format(i), locals()['image' + str(i)])cv2.imshow('{}'.format(serial_list[i]), locals()['image' + str(i)])key = cv2.waitKey(1)# 如果按下ESC,则跳出循环if key == 27:# 貌似直接用return也行# returnbreak2 = Truebreakif break2 == True:breakfinally:# 停止所有流locals()['pipeline' + str(i)].stop()# 销毁所有窗口cv2.destroyAllWindows()if __name__ == '__main__':YoloTest().dontla_evaluate_detect()

Intel Realsense D435运行报错 RuntimeError: Camera not connected! dev.hardware_reset()函数需加睡眠sleep()相关推荐

  1. Intel Realsense D435报错 RuntimeError: MFCreateDeviceSource(_device_attrs, _source) returned: HResult

    解决方案:hardware_reset()后是不是应该延迟一段时间?不延迟就会报错,加个sleep? 该错误会与RuntimeError: Camera not connected!交替出现,注释掉h ...

  2. Intel Realsense D435 测试摄像头在不同曝光值下的帧生成时间(防止曝光时间过长导致fps下降)auto_exposure_priority(没成功)

    文章目录 不用测了 下面测试auto_exposure_priority参数在自动曝光下的作用 下面测试在自动曝光模式下如何实时获取曝光值 测试摄像头在不同曝光值下的帧生成时间 不用测了 参考文章:I ...

  3. SLAM之camera(Intel RealSense D435)调试第一弹:Win10平台下getting started

    参见官方的getting started文档 https://software.intel.com/en-us/realsense/d400/get-started,这个quick start gui ...

  4. yunyang tensorflow-yolov3 Intel Realsense D435 (并发)调用两个摄像头运行识别程序并画框

    只是一个测试,测试在并发运行下,同时开启两个摄像头获取视频流并调用识别函数的运行结果,以后在摄像头多的情况下,肯定不能这样,需要批量创建各种对象. 并发,指的是不在多线程的情况下,每个摄像头的视频流送 ...

  5. Intel Realsense D435 hardware_reset() 摄像头重置记录 context.query_devices()

    摄像头重置后它自己不能马上恢复,如果我们重置后立即访问它们,就会出错,以下是一些问题记录: 之前是使用device=ctx.query_device(),后面重置后检测摄像头的时候直接用len(dev ...

  6. 20200314 SQ Intel Realsense D435 USB 线长、转接线个数测试数据表

    测试名称 Intel Realsense D435 摄像头USB线支持最大线长.转接线个数测试 测试参数 Platform:win10x64 系统内存:32G 测试程序:tensorflow_yolo ...

  7. tensorflow-yolov3 调试Intel Realsense D435摄像头所遇到的问题(USB自动检测并重置机制)hardware_reset() pyusb libusb devcon

    文章目录 191126 191127 191128 191129 尝试第二种方案 Devcon 191130 191126 连接6摄像头运行,开始运行正常,能够正常识别,但不小心线动了一下,视频窗口卡 ...

  8. Tensorflow_yolov3 Intel Realsense D435奇怪的现象,多摄像头连接时一旦能检测到深度马上就会卡(卡住)

    两个摄像头连接时一旦能检测到深度马上就会卡(小于30公分),,单个摄像头没事,这是使用了多线程传输后的现象,不知咋回事... 后来加了这句验证全局变量是否存在,好像好点了,有待验证 20200401 ...

  9. SQ 小车避障 Intel Realsense D435 基于线性梯度的深度值过滤

    原理图 相关代码 # -*- coding: utf-8 -*- """ @File : 191224_obstacle_detection_建立梯度.py @Time ...

最新文章

  1. iOS--动画demo--Launch Image淡出效果
  2. 成功解决Please use the NLTK Downloader to obtain the resource:
  3. 【机器视觉】 ifelse算子(已废弃)
  4. 【Linux网络编程】原始套接字编程
  5. Java:ThreadPoolExecutor解析续--Executors
  6. Python3.6学习笔记(二)
  7. leetcode 451. 根据字符出现频率排序
  8. Android官方开发文档Training系列课程中文版:Activity测试之创建运行测试
  9. ajax json 封装,Ajax--json(Ajax调用返回json封装代码、格式及注意事项)
  10. android开发之android:padding和android:margin的区别
  11. 第十八篇 JS传参数
  12. redis 值字符串前面部分乱码_Spring-RedisTemplate写入数据乱码问题的复现与解决
  13. ffmpeg拉流设置暂停_ffmpeg+SDL2实现的视频播放器「退出、暂停、播放」
  14. 分布式定时任务开源方案
  15. 计算机 仿真 流体力学剪切应力,基于影像的计算流体力学在冠状动脉疾病中的研究进展...
  16. Hugo博客搭建配置
  17. signature=c7580760a679f082e2d4960e4c2c7772,Signatures of moiré-trapped valley excitons in MoSe
  18. 卡西欧计算机的型号配置,【卡西欧 CASIO fx-82CN X计算器使用总结】菜单|供电|设置|输入|运算_摘要频道_什么值得买...
  19. DB2 SQL消息查询
  20. NLP 实战 (3) | 整体设计之数据集/模型管理

热门文章

  1. ORA-12519: TNS: 没有找到适用的服务处理
  2. abap中利用se95还原程序到原始版本
  3. SAP财务报表不平之分析
  4. VF02 会计凭证过账时间
  5. SAP-ABAP DESCRIBE FIELD 用法
  6. SAPMMC控制台服务消失的解决方法
  7. 关于COPC后台配置的几个关键步骤及其事务代码
  8. 生产订单成本的计划、控制和结算
  9. 创建BAPI程序的步骤
  10. 将矩阵转为一行_LeetCode1253:矩阵重构