首页 > 解决方案 > 使用 TensorFlow 对象检测 API 遍历目录

问题描述

我正在尝试遍历包含多个图像的目录,并使用 TensorFlow 对它们进行对象分析。我正在尝试修改用于分析单个图像的代码,但无法使其正常工作。它分析第一张图像,显示它然后停止。我希望关闭图像/按键盘上的一个键会提示分析和显示下一个,但事实并非如此。该程序继续运行(我认为),但没有任何反应。

我正在从https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/blob/master/Object_detection_image.py修改代码

# Name of the directory containing the images to be analysed
DIR_NAME = 'testImages'

# Path to directory containing images to be analysed
PATH_TO_DIR = os.path.join(CWD_PATH, DIR_NAME)

# Iterate through the files to be analysed in the directory
for filename in os.listdir(PATH_TO_DIR):
    if filename.endswith(".jpg"):
        image = cv2.imread(filename)
        image = cv2.resize(image, (800,600))
        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=8,
            min_score_thresh=0.60)
        cv2.imshow('Object detector', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

标签: pythonloopstensorflowobject-detection

解决方案


结果一个文件以 .jpg 结尾,其余的都是 .JPG

if filename.lower().endswith(".jpg"):

解决了我的问题。哎呀。


推荐阅读