Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"classify_webcam.py" line 40 image_data = graph_def.get_tensor_by_name('final_result:0')AttributeError:get_tensor_by_name #22

Open
vipulsomaiah1296 opened this issue May 19, 2019 · 1 comment

Comments

@vipulsomaiah1296
Copy link

`import sys
import modulegraph
import os
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import copy
import cv2

Disable tensorflow compilation warnings

os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf

def predict(image_data):

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]
max_score = 0.0
res = ''
for node_id in top_k:
    human_string = label_lines[node_id]
    score = predictions[0][node_id]
    if score > max_score:
        max_score = score
        res = human_string
return res, max_score

Loads label file, strips off carriage return

label_lines = [line.rstrip() for line
in tf.gfile.GFile("logs/output_labels.txt")]

Unpersists graph from file

with tf.gfile.FastGFile("logs/output_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = 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
image_data = graph_def.get_tensor_by_name('final_result:0')
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
c = 0
cap = cv2.VideoCapture(0)
res, score = '', 0.0
i = 0
mem = ''
consecutive = 0
sequence = ''

while True:
    ret, img = cap.read()
    img = cv2.flip(img, 1)
    
    if ret:
        x1, y1, x2, y2 = 100, 100, 300, 300
        img_cropped = img[y1:y2, x1:x2]
        c += 1
        image_data = cv2.imencode('.jpg', img_cropped)[1].tostring()            
        a = cv2.waitKey(1) # waits to see if `esc` is pressed           
        if i == 4:
            res_tmp, score = predict(image_data)
            res = res_tmp
            i = 0
            if mem == res:
                consecutive += 1
            else:
                consecutive = 0
            if consecutive == 2 and res not in ['nothing']:
                if res == 'space':
                    sequence += ' '
                elif res == 'del':
                    sequence = sequence[:-1]
                else:
                    sequence += res
                consecutive = 0
        i += 1
        cv2.putText(img, '%s' % (res.upper()), (100,400), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 4)
        cv2.putText(img, '(score = %.5f)' % (float(score)), (100,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))
        mem = res
        cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
        cv2.imshow("img", img)
        img_sequence = np.zeros((200,1200,3), np.uint8)
        cv2.putText(img_sequence, '%s' % (sequence.upper()), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
        cv2.imshow('sequence', img_sequence)
        
        if a == 27: # when `esc` is pressed
            break

Following line should... <-- This should work fine now

cv2.destroyAllWindows()
cv2.VideoCapture(0).release()`

@someshium
Copy link

hi did you were successful in running this project?
mine is taking too much time in training it's more than 8 hours till now?
can you please help. i am taking this repo as a reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants