Automated Tagging of Images on Nextcloud with Tensorflow (for Poets 2)

I take lots of pictures on my mobile phone of nearly everything. Whenever I fuel up my car, I take a picture of my dashboard so that I can remember the date and odometer to be able to calculate my fuel consumption. Another case would be each time I go to supermarket, I take a picture of the receipt provided so that I’ll be able to keep track of my expenses. Going Hiking with friends are also great opportunities to take more pictures. How to  sort out everything? Fortunately we have lots of machine learning tools out there. I chose Tensorflow not because I found it to be the best but simply because it has more buzz around it.

Let’s start by viewing our files in Nextcloud which are synced with my Phone.

In the above example, I manually looked for car dashboard pictures and tagged them  as “Car Dashboard”. Let’s do the same with supermarket receipts, business cars and hiking pictures. I found that we need atleast 30 pictures for TensorFlow to not crash. In case I was feeling lazy, I would just copy/paste the same pictures till I get 30. I am lazy.

Now that it’s all done with like a few, lets download TensorFlow for Poets 2. You may follow instructions from here: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0

TensorFlow for Poets require you to put your training images into their respective category folder. So we need to download the images we just tagged in the proper way. Nextcloud Offers a WebDAV API as standard. Unfortunately classic python DAV clients would not suffice for our task. We’ll have to create our own methods and overload some.

 

#git clone https://github.com/CloudPolis/webdav-client-python
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/webdav-client-python')

import webdav.client as wc
from webdav.client import WebDavException

class NCCLient(wc.Client):
  pass

We write a function which will download the pictures for each tags specified by their ID and put them inside /tmp/nc/{categoryid}

def download_training_set():
  try:
    client = NCCLient(options) 
      for tag in ['2','9','1','3','11','12','14']:
        index = 0
        try:
          os.mkdir("%s/%s/" % (options['download_path'],tag))
        except:
          pass
	for filepath in client.listTag("/files/Nayar/",tag):
	index = index + 1
	filepath = filepath.path().replace('remote.php/dav','')
	local_filepath = '/tmp/nc/%s/%s' % (tag,os.path.basename(filepath))
	if(os.path.isfile(local_filepath)):
	  pass
	else:
	  client.download_file(filepath,local_path=local_filepath)
	if(index > 200):
	  break	
	except WebDavException as exception:
		pprint.pprint(exception)

The method listTag is not present in the default DAV classes so we have to write our own in our extended class

Now that we have all our images in their respective folders, we can run TensorFlow for Poets.

IMAGE_SIZE=224
ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"

python3 -m scripts.retrain   --bottleneck_dir=tf_files/bottlenecks   --how_many_training_steps=100   --model_dir=tf_files/models/   --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}"   --output_graph=tf_files/retrained_graph.pb   --output_labels=tf_files/retrained_labels.txt   --architecture="${ARCHITECTURE}"   --image_dir=/tmp/nc/

What’s left now is to download all images from Nextcloud and try to label them with the TensorFlow model. We write the classify_data() function. All images downloaded are renamed as follows /tmp/nc_clasify/{nextcloudfileid}.png.

When the classify function is run on each of the images, they are renamed if the label accuracy is greater then 99.9%. Else they are deleted. The renaming scheme is as follows /tmp/nc_classify/{tagname}-{tagid}-{nextcloudfileid}.jpg

We have to manually check if some images are mislabeled and we simply delete them from the folder. Finally we need to send the labelled images info back to NextCloud. We write the function as follows:

def update_classification():
  client = NCCLient(options) 
  files = os.listdir('/tmp/nc_classify/')
  for f in files:
	match = re.search('(.*)-(.*)-(.*).jpg', f)
	print(match.group(2),match.group(3))
	client.putTag(int(match.group(3)),int(match.group(2)))
	os.remove('/tmp/nc_classify/' + f)

The putTag function is also custom. Please see the gist. And there you go.

Repeat the same process multiple times till your training data get bigger and bigger and it starts to classify more and more images.

Now the next step would be to dig deeper into tensorflow and OCR to automate lists of items I buy from the supermarket and make it generate reports for me.

[Intellectual Property] Part 1: Introduction

Our minds are what drive us everyday to do things. Some tasks we do are  auto-pilot mode and others require us to think. Walking to us seem to be an auto-pilot function but was that always the case? Do you remember the first times you learnt to walk as a baby? It was a very brain intensive process. Our parents taught us part of the solution and our brains adapted the solution to work with our body. Same concept can be said about driving and even talking.

white and green sheetwork on brown surface

Ideas

Our brains generate ideas to our everyday problems in every aspect of our life. But sometimes we let our auto-pilot take control of our selves and we stop being creative. For example, it’s fine to walk from your chair to your bathroom but to walk from your home to your work 15km apart is still possible but it is not practical. Can you come up with some ideas for it?

  • Put shoes instead of waking naked feet. Much more comfort.
  • Walk early in the morning to avoid high sun temperatures.
  • Find a shorter route on the map. Less tiredness and quicker.
  • Take energy drinks on the way. (I really do not recommend this one)
  • Take a bicycle or a bus instead of using our feet. Less tiredness and quicker.

woman walking on the desert

Now let’s imagine you were the first human to come up with those ideas. Would you prevent others from copying you or would you let them copy you but only if they remunerated you in some ways first?

This is where Intellectual Property (IP) comes in place. You will not be able to protect each and every kind of things which your mind can create. The IP system differs in every country and “by striking the right balance between the interests of innovators and the wider public interest, the IP system aims to foster an environment in which creativity and innovation can flourish” [1].

In the next post, we shall see what the different “Creations of the mind” and what are their legal qualifications. In the meanwhile, can you write down few creations of the mind?

[1] https://www.wipo.int/about-ip/en/

 

Goodbye Volkswagen Polo

After 39 months of being at my service, I finally sold my VW Polo. It was a car which I had intended to keep for only 1 year just to learn how to drive after getting my driving license. Yep, having a driving license doesn’t mean you’ve automatically been qualified to be a semi-professional rally driver.

The Polo never left my family stranded around Mauritius. Each year we decided to keep it one more year until I said Stop! We have to get something more decent in terms of looks, comfort and safety.

As with all cars, it wasn’t 100% problem-free. It did have some issues such as the cooling system started leaking and it costed me quite some money to fix. Other minor issues occurred such as the indicator relay got damaged thus my flashers were being ON or OFF state continuously.

Oh yes, one of the front shock absorber was damaged when I was trying to park my car in the dark at Curepipe not noticing that the curb was like really high. I went full on. I looked for shock absorbers on the secondhand market. It was like Rs 2000. One garage was going to install one but when they tested it, they found it was already faulty. I then decided to put 2 new shocks in front instead which cut me back by Rs 6000 (2 pairs of shocks + its cover). The performance and comfort was great with the new shocks.

My dad was very reluctant to sell the car but I had to convince him to. Older cars lack lots of safety features such as ABS, Airbags and also comfort features such as power steering and Air Conditioning. Parallel parking was really really tough to perform with the car.

The car’s engine is still vibrant despite having 241,XXX km on it. Lots of my friends said it feels like sitting in a rally car. Old cars can be really fun. Next I’ll be posting a detailed article on the Total Cost of Ownership of the vehicle. It takes lots of money to maintain a relatively good car. Stay tuned for more.

 

 

Using old Android Phones as Surveillance Cameras in Home Assistant

Phones get obsoleted very fast nowadays. Meaning you must have phones lying around in your house which are too slow to be used daily. I had an LG E612 which I thought to use as an IP Camera.

I installed “IP Webcam” app from Google Playstore. We need to note down the IP address and Port of the webservice which it starts. Ideally we should fix the IP address on the DHCP of our routers.

Next we configure Home Assistant to fetch data from our phone.

We then restart Home Assistant and voila! We have our livefeed.

 

Problems encountered:

  • The Android phone seems to be freezing after 5 hours. Maybe it is overheating or something but it doesn’t reboot. It gets stuck on the booting animation. Is it a bug in Cyanogenmod or hardware related?

 

 

Playing PUBG Mobile on the move in Mauritius

It takes me 2 hours to get home at Curepipe (Robinson) from Reduit. It’s scandalous. Plus the bus is crammed with people when it reaches Ebene. We need more 173 and 191 Curepipe buses!!! But anyways, to kill some time, I play this game called PUBG Mobile (Player Unknown Battleground).

I know it’s not the most productive thing to do but yeah, it’s relaxing after a day of work.

It’s very much playable on mobile data. The game doesn’t lag at all while the bus is moving. But my friends were curious about how much data does the game consumes. After 2 matches today. I saw that it took about 18MB.

I think we can safely say that a match is about 10MB max. I use the Zeness Pack from Chili in which I get 750MB for Rs 75 + Vat. That means you will be able to have 2.5 winning matches per day for 30 days. I think online playing on mobile is very accessible now.

Do you also play online games in the bus? Please share with us.