Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Generate image dataset from one folder, label each image one by one, for machine learning via tensorflow

This is an update for previous post.
http://lengerrong.blogspot.com/2017/04/create-your-own-dataset-for-machine.html

It is hard to collect enough images for your machine traning.
So I update this post to append images once you find some to your existed dataset.
All you need to do is copy your images to a folder and run the python script.

Of cause, you have to label each image while running the python script.
Below python script just have two label, 0 means cat, 1 means dog.
You can add more labels by just modify blue codes.

How to fix tf.nn.in_top_k out of range issue: tensorflow.python.framework.errors_impl.InvalidArgumentError: targets[0] is out of range

tensorflow.python.framework.errors_impl.InvalidArgumentError: targets[0] is out of range
         [[Node: InTopK = InTopK[T=DT_INT32, k=1, _device="/job:localhost/replica:0/task:0/cpu:0"](softmax_linear/softmax_linear, Reshape_1)]]

Caused by op u'InTopK', defined at:


python show an image via subprocess.Popen

import tensorflow as tf
import Image
import os
import subprocess

def main(argv): 
  folder = argv[0]
  if (len(argv) > 1):
    if tf.gfile.Exists(argv[1]):
      folder = argv[1]
    else:
      print ('%s not existed' % argv[1])
      return
  else:
    print ("please input training image data folder path")
    return
  for f in tf.gfile.ListDirectory(folder):
    filepath = os.path.join(folder, f)
    try:
      im = Image.open(filepath)
      p = subprocess.Popen(["display", filepath])
      label = raw_input("please label the image, 0 means cat, 1 means dog:")
      p.kill()
    except Exception, e:
      print (e)

python Thread.join & setDaemon sample

prototype:

join([timeout]) # wait the child thread complete

setDaemon(True) # once the daemon thread complete, the child thread complete too.

from threading import Thread
from PIL import Image

    class ImageThread(Thread):
      def __init__(self, filepath):
        Thread.__init__(self)
        self.filepath = filepath
        self.openresult = 0

      def run(self):
        try:
          im = Image.open(self.filepath)
          im.show()
          self.openresult = 1
        except Exception, e:
          print (e)

    imt = ImageThread('image.gif')
    imt.setDaemon(True) # should set before start
    imt.start()
    imt.join(10) # wait 10 seconds

Solution for no image display after call Image.show via python

simple python code like:
 
from PIL import Image   
image = Image.open('image.gif' 
image.show() 

no image display while running on ubuntu system.

The easy solution is:
sudo apt-get install imagemagick

fixed: embedded-redis: Unable to run on macOS Sonoma

Issue you might see below error while trying to run embedded-redis for your testing on your macOS after you upgrade to Sonoma. java.la...