qq_38436848
qq_38436848
采纳率0%
2018-12-07 13:30 阅读 1.1k

我的py文件可以在python3.6.2上运行,但是却不能在终端上运行,不知道问题出在哪,请求大佬帮帮忙,谢谢。

#!/usr/bin/python
"""
Created on Fri Dec  7 20:27:44 2018

@author: 10748
"""


import os
import filetype
import sys
import tensorflow as tf

def train(file):

    a_list = list()
    # change this as you see fit
    image_path = file

# Read in the image_data
    image_data = tf.gfile.FastGFile(image_path, 'rb').read()

# Loads label file, strips off carriage return
    label_lines = [line.rstrip() for line
                   in tf.gfile.GFile("output_labels.txt")]

# Unpersists graph from file
    with tf.gfile.FastGFile("output_graph.pb", 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')

    with tf.Session() as sess:
    # Feed the image_data as input to the graph and get first prediction
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

        predictions = sess.run(softmax_tensor, \
                {'DecodeJpeg/contents:0': image_data})

    # Sort to show labels of first prediction in order of confidence
        top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

        for node_id in top_k:
            human_string = label_lines[node_id]
            score = predictions[0][node_id]
            a_list.append(human_string+'_score = '+str(score))
    return a_list



def file_type(file):
     try:
        b = filetype.guess(file)
        c = b.extension
        if c == 'jpg':
            return file
     except:
         return

file_list = list()

for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        file_list.append(os.path.join(name))

for i in range(len(file_list)):
    file_name = file_type(file_list[i])
    if file_name == None:
        print('')
    else:
        a =train(file_name)

for q in range (len(a)):
    print(a[q])

input('按下回车键退出')

终端上显示:
Traceback (most recent call last):
File "D:\Anaconda3\envs\tensorflow-gpu\flow\data\train\TS.py", line 35, in
for q in range (len(a)):
NameError: name 'a' is not defined

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • crf_moonlight crf_moonlight 2018-12-07 14:10
    for i in range(len(file_list)):
        file_name = file_type(file_list[i])
        if file_name == None:
            print('')
        else:
            a =train(file_name)
    
    

    a在这定义, 出错问题就在这.
    "在python3.6.2运行"是什么意思, "在终端运行"又是什么意思....麻烦把这些都叙述明白....
    根据你这模糊的说法, 应该是工作目录的问题, 你os.walk直接遍历的是当前工作目录
    终端切到所在目录应该就行了

    点赞 评论 复制链接分享

相关推荐