Ask Your Question
0

I am not able to put text in my frames of the video it is not giving any error but still i can't see the text

asked 2017-10-22 16:23:56 -0600

shorav gravatar image
import tensorflow as tf
import cv2

fn_list=[]

# Capture video from file

cap = cv2.VideoCapture('test_lego.wmv')
image_path = '/home/shorav/tfClassifier/new_capture.jpg'

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

with tf.gfile.FastGFile("./final_graph.pb", 'rb') as f:
   graph_def = tf.GraphDef()                
   graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
Frame_number=0

while True:
   ret, frame = cap.read()

if ret == True:
    Frame_number+=1

    fn_list.append(Frame_number)

    print "frame number is :", Frame_number


    gray = cv2.cvtColor(frame,cv2.COLOR_RGB2GRAY)
    ret,thresh= cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    c = max(contours, key = cv2.contourArea)
    x,y,w,h = cv2.boundingRect(c)
    font = cv2.FONT_HERSHEY_SIMPLEX


    #cv2.drawContours(gray,contours,-1, (0,255,255), 2)
    for contour in contours:
        [x,y,w,h] = cv2.boundingRect(contour)
        cv2.rectangle(gray,(x,y),(x+w,y+h),(255,0,255),2)


        cv2.putText(frame,'Show the text',(100,50), font, 1, (0,255,0), 4,cv2.CV_AA)    
        cv2.imshow('frame',gray)
    if (Frame_number%10==0):
        cv2.imwrite('new_capture.jpg',frame)
        image_data = tf.gfile.FastGFile(image_path, 'rb').read()
        with tf.Session() as sess:
                softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
                predictions = sess.run(softmax_tensor, \
                         {'DecodeJpeg/contents:0': image_data})
                top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
                score_results=[]
                final_label=[]
                for node_id in top_k:
                    human_string = label_lines[node_id]
                    score = predictions[0][node_id]
                    print('%s (score = %.5f)' % (human_string, score))
                    score_results.append(score)
                    final_label.append(human_string)

        if cv2.waitKey(50) & 0xFF == ord('q'):
            break


else:
     break

cap.release() cv2.destroyAllWindows()

edit retag flag offensive close merge delete

Comments

indentation problem ? the if cv2.waitKey part must be executed on each frame, not on each 10'th

berak gravatar imageberak ( 2017-10-22 17:12:03 -0600 )edit

i did that but i am not able to put the text correctly in the image. The text appears to be scattered not staying at right position...??

shorav gravatar imageshorav ( 2017-10-22 19:16:20 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-10-22 21:08:41 -0600

supra56 gravatar image
for contour in contours:
    [x,y,w,h] = cv2.boundingRect(contour)
    cv2.rectangle(gray,(x,y),(x+w,y+h),(255,0,255),2)
    cv2.putText(frame,'Show the text',(x-10, y-10), font, 1, (0,255,0), 4,cv2.CV_AA)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-22 16:23:28 -0600

Seen: 3,742 times

Last updated: Oct 22 '17