解决方案:
参考:Intel Realsense D435报错 RuntimeError: MFCreateDeviceSource(_device_attrs, &_source) returned: HResult
完整示例代码:
主要看def dontla_evaluate_detect(self)
"""
@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 time
class YoloTest(object):def __init__(self
):self
.input_size
= cfg
.TEST
.INPUT_SIZEself
.anchor_per_scale
= cfg
.YOLO
.ANCHOR_PER_SCALEself
.classes
= utils
.read_class_names
(cfg
.YOLO
.CLASSES
)self
.num_classes
= len(self
.classes
)self
.anchors
= np
.array
(utils
.get_anchors
(cfg
.YOLO
.ANCHORS
))self
.score_threshold
= cfg
.TEST
.SCORE_THRESHOLDself
.iou_threshold
= cfg
.TEST
.IOU_THRESHOLDself
.moving_ave_decay
= cfg
.YOLO
.MOVING_AVE_DECAYself
.annotation_path
= cfg
.TEST
.ANNOT_PATHself
.weight_file
= cfg
.TEST
.WEIGHT_FILEself
.write_image
= cfg
.TEST
.WRITE_IMAGEself
.write_image_path
= cfg
.TEST
.WRITE_IMAGE_PATHself
.show_label
= cfg
.TEST
.SHOW_LABEL
with tf
.name_scope
('input'):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
with tf
.name_scope
('ema'):ema_obj
= tf
.train
.ExponentialMovingAverage
(self
.moving_ave_decay
)self
.sess
= tf
.Session
(config
=tf
.ConfigProto
(allow_soft_placement
=True))self
.saver
= tf
.train
.Saver
(ema_obj
.variables_to_restore
())self
.saver
.restore
(self
.sess
, self
.weight_file
)def predict(self
, image
):org_image
= np
.copy
(image
)org_h
, org_w
, _
= org_image
.shapeimage_data
= utils
.image_preprocess
(image
, [self
.input_size
, self
.input_size
])image_data
= image_data
[np
.newaxis
, ...]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})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)bboxes
= utils
.postprocess_boxes
(pred_bbox
, (org_h
, org_w
), self
.input_size
, self
.score_threshold
)bboxes
= utils
.nms
(bboxes
, self
.iou_threshold
)return bboxes
def dontla_evaluate_detect(self
):ctx
= rs
.context
()devices
= ctx
.query_devices
()for dev
in devices
:dev
.hardware_reset
()print('摄像头{}重置成功'.format(i
for i
, x
in enumerate(devices
) if x
== dev
))time
.sleep
(5)cam_num
= len(ctx
.devices
)print('摄像头个数:{}'.format(cam_num
))if cam_num
!= 6:print('The cameras are not all connected!')else:n
= 0serial_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
)))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: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
)]:continuelocals()['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
())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
.imshow
('{}'.format(serial_list
[i
]), locals()['image' + str(i
)])key
= cv2
.waitKey
(1)if key
== 27:break2
= 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()的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。