Saturday, December 28, 2019

A simple classifier to classify Cars and aeroplanes with CNN(Part 2: inference)


Hello there, hope you are doing well. This is a sequential post of classifier with CNN. In our earlier post we learned how collect the data, organize them and train a model for classification. In this post we will learn how we can use the trained model and actually classify the Cars and Planes. When I was starting to train a CNN and learn, I had a difficult time to learn how to use the model and actually see the result. All the article or blogs I was following only talks about how to train the network but no one was actually talking about how we can see the classification results. Enough talking lets start :

If you followed my previous post, the model file(model.h5) was created with 96% accuracy and save in the models folder. Now we will use that model for inference. 

Step1: We will start by importing the required libraries as we did for the training.
Step2: In the test.py code we will specify where the model and the test images are. We will load the model and the weights. Specify the image size we dealing with.

Step3: Now we will define a function for prediction which will take the test image as input and return the prediction output accordingly. As we have only two classes(cars and areoplanes), we will get the probability of two classes as output. We will read that probability and show the output result.
You can clone the whole project from github here. Do let me know if you have any feedback or suggestions. Hope you enjoyed coding with me. Wish you all a very happy new year 2020 in advace.


Friday, December 20, 2019

Reading Image frame by frame from Saved videoes or Camera Using opencv Python

One of my friend was asking about reading image frames from videoes, so I thought a quick block may be very helpful for beginners. It is actually very easy, just follow the bellow steps:


Step 1. Installations



a. Install python




If you still do not have python in your system , please install python


For linux: sudo apt-get update $ sudo apt-get install python3.6

For windows: download the installation file from python website and follow the instructions



b. Install Opencv



For linux:

sudo pip3 install opencv-python
 
For Windows:

pip3 install opencv-python






Step2: Reading saved or camera video



- At first need to import the opencv



- Read the video either from camera or saved video


- While frame is available show the frame and save it. Finally release the camera and window which we used to show.


Hope you liked this post. I am posting the script below so that you can just copy paste. leave your feedback below.

import cv2

#if reading from saved video, need to specify where the file is saved
#cap = cv2.VideoCapture('D:\project\spoof\classification\test_video\test.avi')

#if reading from camera, camera id is 0 here
cap = cv2.VideoCapture(0)

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

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    cv2.imwrite('savedImage.jpg', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()