Number recognition from Image with python and tensorflow
<p><strong>Details: Ubuntu 14.04(LTS), OpenCV 2.4.13, Spyder 2.3.9(Python 2.7), Tensorflow r0.10</strong></p>

<p>I want to recognize Number from 
<a href="http://i.stack.imgur.com/deOJ3.jpg" rel="noreferrer">the image</a> with <em>Python</em> and <em>Tensorflow</em>(optional <em>OpenCV</em>).</p>

<p>Additionally, I want to use MNIST data training with tensorflow</p>

<p>Like this(the code is referred to <a href="https://www.youtube.com/watch?v=1vCOoBwYQVU&amp;feature=youtu.be" rel="noreferrer">this page</a>'s video),</p>

<p><strong>Code:</strong></p>

<pre><code>import tensorflow as tf
import random

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

### modeling ###

activation = tf.nn.softmax(tf.matmul(x, W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(activation), reduction_indices=1))

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

### training ###

for epoch in range(training_epochs) :

    avg_cost = 0
    total_batch = int(mnist.train.num_examples/batch_size)

    for i in range(total_batch) :

        batch_xs, batch_ys =mnist.train.next_batch(batch_size)
        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
        avg_cost += sess.run(cross_entropy, feed_dict = {x: batch_xs, y: batch_ys}) / total_batch

    if epoch % display_step == 0 :
        print "Epoch : ", "%04d" % (epoch+1), "cost=", "{:.9f}".format(avg_cost)

print "Optimization Finished"

### predict number ###

r = random.randint(0, mnist.test.num_examples - 1)
print "Prediction: ", sess.run(tf.argmax(activation,1), {x: mnist.test.images[r:r+1]})
print "Correct Answer: ", sess.run(tf.argmax(mnist.test.labels[r:r+1], 1))
</code></pre>

<p>But, the problem is how can I make numpy array like</p>

<p><strong>Code addition:</strong></p>

<pre><code>mnist.test.images[r:r+1]
</code></pre>

<blockquote>
  <p>[[ 0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.50196081  0.50196081
     0.50196081  0.50196081  0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.50196081  1.          1.          1.
     1.          1.          1.          0.50196081  0.25098041  0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.50196081  1.          1.          1.          1.
     1.          1.          1.          1.          1.          1.
     0.25098041  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.74901962  1.          1.          1.
     1.          0.50196081  0.50196081  0.50196081  0.74901962  1.          1.
     1.          0.74901962  0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.50196081  1.          1.          1.
     0.74901962  0.          0.          0.          0.          0.          0.
     0.50196081  1.          1.          0.74901962  0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          1.          1.          1.
     0.50196081  0.          0.          0.          0.          0.          0.
     0.          0.          0.25098041  1.          1.          0.74901962
     0.25098041  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.74901962  1.          1.
     0.74901962  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.25098041  1.          1.
     0.74901962  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.50196081  1.          1.
     0.74901962  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.
     0.25098041  1.          1.          0.50196081  0.          0.          0.
     0.          0.          0.          0.          0.          0.50196081
     1.          1.          0.25098041  0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          1.          1.          0.50196081  0.          0.
     0.          0.          0.          0.          0.          0.          1.
     1.          1.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.25098041  1.          1.          1.          0.          0.
     0.          0.          0.          0.          0.          0.          1.
     1.          0.50196081  0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.25098041  1.          1.          1.          1.          0.          0.
     0.          0.          0.          0.          0.          0.
     0.74901962  1.          0.50196081  0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.74901962  1.          1.          1.          0.25098041
     0.          0.          0.          0.          0.          0.          0.
     0.          0.50196081  1.          1.          0.          0.          0.
     0.          0.          0.          0.          0.          0.
     0.25098041  0.74901962  1.          1.          1.          1.
     0.74901962  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.50196081  1.          1.
     0.74901962  0.          0.          0.          0.          0.
     0.25098041  0.50196081  1.          1.          1.          1.          1.
     1.          0.50196081  0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.
     0.74901962  1.          1.          1.          1.          0.50196081
     0.50196081  0.74901962  1.          1.          1.          1.          1.
     1.          1.          0.50196081  0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.74901962  1.          1.          1.          1.
     1.          1.          1.          1.          1.          1.          1.
     0.50196081  0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.25098041  1.          1.          1.
     1.          1.          1.          1.          0.50196081  0.25098041
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.
     0.50196081  0.50196081  0.50196081  0.50196081  0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.          0.          0.          0.
     0.          0.          0.          0.        ]]</p>
</blockquote>

<p>When I use OpenCV to solve the probelm, I can make numpy array about the Image but little bit strange.
(I want to make array into a vector of 28x28)</p>

<p><strong>Code addition:</strong></p>

<pre><code>image = cv2.imread("img_easy.jpg")
resized_image = cv2.resize(image, (28, 28))
</code></pre>

<blockquote>
  <p>[[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]</p>
  
  <p>[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]</p>
  
  <p>[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]</p>
  
  <p>..., </p>
  
  <p>[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]</p>
  
  <p>[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]</p>
  
  <p>[[255 255 255]  [255 255 255]  [255 255 255]  ...,   [255 255 255]  [255 255 255]  [255 255 255]]]</p>
</blockquote>

<p>And then, I put the value('resized_image') into the Tensorflow code.
Like this,</p>

<p><strong>Code modification:</strong></p>

<pre><code>### predict number ###

print "Prediction: ", sess.run(tf.argmax(activation,1), {x: resized_image})
print "Correct Answer: 9"
</code></pre>

<p>As a result, the error is occured at this line.</p>

<blockquote>
  <p>ValueError: Cannot feed value of shape (28, 28, 3) for Tensor u'Placeholder_2:0', which has shape '(?, 784)'</p>
</blockquote>

<p><strong>Finally,</strong></p>

<p>1) I want to know how can I make a data which can be input the tensorflow code(maybe numpy array [784])</p>

<p>2) Do you know about the number recognition examples that use tensorflow?</p>

<p>I'm a beginner in machine-learning.</p>

<p>Please Tell me in detail what should I do.</p>