qq_38436848 2018-12-07 13:30 采纳率: 0%
浏览 1138

我的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 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直接遍历的是当前工作目录
    终端切到所在目录应该就行了

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?