我尝试开发对象检测API,在实现过程中发生错误,
import cv2
cap = cv2.VideoCapture(0)
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while(True):
ret,image_np =cap.read()
image_np_expanded = np.expand_dims(image_np,axis=0)
image_tensor =detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
(boxes,scores,classes,num_detections) = sess.run(
[boxes,scores,classes,num_detections],
feed_dict={ image_tensor:image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
cv2.imshow('image',cv2.resize(image_np,(1280,960)))
if cv2.waitkey(25) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
答案 0 :(得分:0)
您是否检查了流是否正确打开。您可以使用下面的行来尝试。
cap.isOpened()
如果上述方法返回true,则表示读取文件没有问题。然后,您需要在下面的行中设置要阅读的帧。
cap.set(cv2.CAP_PROP_POS_MSEC,frameNumber)
在直播的情况下,您只需将frameNumber设置为1000(以毫秒为单位)!