我有一个对象识别脚本,可以对从多个视频中提取的帧进行识别。我想将读取的文件名,预测的类名称和每个视频的准确性得分保存到具有标题的csv文件中,该标题将保存到各自的视频文件夹中。我也想将读取的文件名保存到单独的json文件中,并将其保存到相应的视频文件夹中
当前,脚本显示准确性得分和检测到的对象名称,并将其写入单个csv,每次处理新视频中的帧以进行识别时,该csv会将数据附加到同一文件中。
def recognize_object(model_name,ckpt_path,label_path,test_img_path):
count=0
sys.path.append("..")
MODEL_NAME = model_name
#CWD_PATH = os.getcwd()
#PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
PATH_TO_CKPT = ckpt_path
#PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')
PATH_TO_LABELS = label_path
#PATH_TO_IMAGE = list(glob.glob("C:\\new_multi_cat\\models\\research\\object_detection\\img_test\\*jpg"))
PATH_TO_IMAGE = list(glob(test_img_path))
NUM_CLASSES = 3
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for paths in range(len(PATH_TO_IMAGE)):
image = cv2.imread(PATH_TO_IMAGE[paths])
image_expanded = np.expand_dims(image, axis=0)
(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80,
skip_scores=True)
coordinates=vis_util.return_coordinates(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80)
threshold=0.80
**objects = []
with open('metadata.csv','a') as csv_file:
writer = csv.writer(csv_file)
for index, value in enumerate(classes[0]):
object_dict = {}
if scores[0, index] > threshold:
object_dict[(category_index.get(value)).get('name').encode('utf8')] = scores[0, index]
objects.append(object_dict)
writer.writerow(objects)
print (objects)**
假设代码处理video1。因此,我想将video1帧的检测结果(如文件名,准确性得分和预测类)保存到提取video1帧的文件夹内的单独的csv文件中。 video2的csv文件需要保存在包含video2帧的文件夹中,依此类推。突出显示的部分是csv脚本。另外,我还需要将每个视频的读取文件名保存到一个单独的json文件中