<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lakshay Kumar]]></title><description><![CDATA[An aspiring data scientist, having worked on languages like Java, C++, Python, now exploring this fascinating world of AI/ML.]]></description><link>https://blogs.lakshaykumar.tech</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 11:04:09 GMT</lastBuildDate><atom:link href="https://blogs.lakshaykumar.tech/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Contributing to Open Source in Python]]></title><description><![CDATA[Open-source software development is a collaborative effort where developers from around the world work together to build and improve software that is freely available for anyone to use. Python, a versatile and widely used programming language, has a ...]]></description><link>https://blogs.lakshaykumar.tech/contributing-to-open-source-in-python</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/contributing-to-open-source-in-python</guid><category><![CDATA[Python]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[mediapipe]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Sun, 01 Oct 2023 05:55:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/BfrQnKBulYQ/upload/e3c796c76e675bd7a965905a932376f1.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Open-source software development is a collaborative effort where developers from around the world work together to build and improve software that is freely available for anyone to use. Python, a versatile and widely used programming language, has a vibrant open-source community that offers numerous opportunities for individuals to contribute. In this blog post, we will explore the world of open-source contributions in Python, including forking repositories on GitHub, setting up a virtual environment, editing code, and creating pull requests.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#introduction">Introduction</a></p>
</li>
<li><p><a class="post-section-overview" href="#understanding-open-source-contributions">Understanding Open Source Contributions</a></p>
</li>
<li><p><a class="post-section-overview" href="#getting-started">Getting Started</a></p>
<ul>
<li><p><a class="post-section-overview" href="#creating-a-github-account">Creating a GitHub Account</a></p>
</li>
<li><p><a class="post-section-overview" href="#forking-a-github-repository">Forking a GitHub Repository</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#setting-up-your-development-environment">Setting Up Your Development Environment</a></p>
<ul>
<li><p><a class="post-section-overview" href="#installing-python">Installing Python</a></p>
</li>
<li><p><a class="post-section-overview" href="#using-virtual-environments">Using Virtual Environments</a></p>
</li>
<li><p><a class="post-section-overview" href="#installing-dependencies">Installing Dependencies</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#making-your-first-contribution">Making Your First Contribution</a></p>
<ul>
<li><p><a class="post-section-overview" href="#editing-code">Editing Code</a></p>
</li>
<li><p><a class="post-section-overview" href="#testing-your-changes">Testing Your Changes</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#sharing-your-contribution">Sharing Your Contribution</a></p>
<ul>
<li><p><a class="post-section-overview" href="#pushing-changes-to-github">Pushing Changes to GitHub</a></p>
</li>
<li><p><a class="post-section-overview" href="#creating-a-pull-request">Creating a Pull Request</a></p>
</li>
</ul>
</li>
<li><p><a class="post-section-overview" href="#conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-1-introduction">1. Introduction</h2>
<p>Contributing to open-source projects is not only a way to give back to the community but also an excellent opportunity to learn, collaborate, and build your skills as a developer. Python, with its extensive library of open-source projects, provides an ideal platform for aspiring contributors.</p>
<h2 id="heading-2-understanding-open-source-contributions">2. Understanding Open Source Contributions</h2>
<h3 id="heading-can-i-contribute-to-open-source-using-python">Can I contribute to open source using Python?</h3>
<p>Absolutely! Python is one of the most popular programming languages for open-source development. Whether you are interested in web development, data science, machine learning, or any other domain, there are numerous Python-based open-source projects awaiting contributions.</p>
<h3 id="heading-what-are-open-source-contributions">What are open-source contributions?</h3>
<p>Open-source contributions refer to actively participating in the development of open-source software. This can include writing code, fixing bugs, creating documentation, providing support to users, or even helping with design and testing.</p>
<h3 id="heading-what-is-open-source-in-python">What is open source in Python?</h3>
<p>Open source in Python refers to Python-based projects or libraries whose source code is publicly available, allowing anyone to view, modify, and contribute to it. These projects often have repositories hosted on platforms like GitHub, where collaboration takes place.</p>
<h3 id="heading-how-do-you-write-an-open-source-contribution">How do you write an open-source contribution?</h3>
<p>Writing an open-source contribution involves several steps, including forking a repository, setting up a development environment, making code changes, testing those changes, pushing them to the repository, and finally creating a pull request (PR) to submit your contribution.</p>
<h2 id="heading-3-getting-started">3. Getting Started</h2>
<h3 id="heading-creating-a-github-account">Creating a GitHub Account</h3>
<p>To contribute to open-source projects, you'll need a GitHub account. GitHub is a popular platform for hosting and collaborating on code repositories. Go to <a target="_blank" href="https://github.com/">GitHub</a> and sign up for an account if you don't already have one.</p>
<h3 id="heading-forking-a-github-repository">Forking a GitHub Repository</h3>
<p>Once you have a GitHub account, you can start contributing by forking a repository. Forking creates a copy of the original repository under your account, allowing you to work on it independently.</p>
<ol>
<li><p>Visit the repository you want to contribute to. In this example, we'll use the <a target="_blank" href="https://github.com/laksh-2193/lkcomputervision.git">lkcomputervision</a> repository by Laksh Agarwal.</p>
</li>
<li><p>Click the "Fork" button in the top right corner of the repository's page. This action will create a copy of the repository under your GitHub account.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696139646300/e85dd3a4-4e35-421c-bfaf-ba5574453c00.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-4-setting-up-your-development-environment">4. Setting Up Your Development Environment</h2>
<h3 id="heading-installing-python">Installing Python</h3>
<p>Before you can start contributing to Python open source projects, you need to have Python installed on your computer. You can download the latest version of Python from the <a target="_blank" href="https://www.python.org/downloads/">official Python website</a> and follow the installation instructions for your operating system.</p>
<h3 id="heading-using-virtual-environments">Using Virtual Environments</h3>
<p>It's a best practice to work in a virtual environment when contributing to open source projects. Virtual environments isolate project-specific dependencies and prevent conflicts with other Python projects.</p>
<p>To create a virtual environment, open your terminal or command prompt and run the following commands:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Create a virtual environment (replace 'myenv' with your preferred name)</span>
python -m venv myenv

<span class="hljs-comment"># Activate the virtual environment</span>
<span class="hljs-comment"># On Windows:</span>
myenv\Scripts\activate
<span class="hljs-comment"># On macOS and Linux:</span>
<span class="hljs-built_in">source</span> myenv/bin/activate
</code></pre>
<h3 id="heading-installing-dependencies">Installing Dependencies</h3>
<p>Projects often have specific dependencies required for development. To install these dependencies, navigate to your project's directory and use the following command:</p>
<pre><code class="lang-bash">pip install -r requirements.txt
</code></pre>
<h2 id="heading-5-making-your-first-contribution">5. Making Your First Contribution</h2>
<h3 id="heading-editing-code">Editing Code</h3>
<p>Now that your environment is set up, you can start making code contributions. Open the code files you want to work on using your preferred code editor.</p>
<p>Make sure to follow the project's contribution guidelines and coding style. These guidelines can usually be found in the project's README or <a target="_blank" href="http://CONTRIBUTING.md">CONTRIBUTING.md</a> file.</p>
<p>Edit the code to implement your contribution, fix a bug, or add a new feature. Be sure to document your changes within the code and write clear commit messages to describe what you've done.</p>
<h3 id="heading-testing-your-changes">Testing Your Changes</h3>
<p>Before sharing your contribution, it's crucial to test your changes to ensure they work as intended. Run any tests provided by the project and conduct additional testing if necessary. Make sure your code doesn't introduce new issues or break existing functionality.</p>
<h2 id="heading-6-sharing-your-contribution">6. Sharing Your Contribution</h2>
<h3 id="heading-pushing-changes-to-github">Pushing Changes to GitHub</h3>
<p>Once you are satisfied with your code changes and tests have passed, it's time to push your changes to your forked repository on GitHub. Use the following commands:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Add your changes to the staging area</span>
git add .

<span class="hljs-comment"># Commit your changes with a descriptive message</span>
git commit -m <span class="hljs-string">"Your commit message here"</span>

<span class="hljs-comment"># Push your changes to your GitHub repository</span>
git push origin master
</code></pre>
<p>Replace <code>master</code> with the branch name you are working on if it's not the default branch.</p>
<h3 id="heading-creating-a-pull-request">Creating a Pull Request</h3>
<p>After pushing your changes, you can create a pull request (PR) to submit your contribution to the original repository. Follow these steps:</p>
<ol>
<li><p>Visit your forked repository on GitHub.</p>
</li>
<li><p>Click the "Pull Request" tab.</p>
</li>
<li><p>Click the "New Pull Request" button.</p>
</li>
<li><p>Select the base repository (the original repository you forked from) and the base branch (usually "master" or "main").</p>
</li>
<li><p>Select your fork as the head repository and your branch with the changes.</p>
</li>
<li><p>Add a title and description for your pull request, explaining the purpose and details of your contribution.</p>
</li>
<li><p>Click "Create Pull Request."</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1696139687646/6b4dc1d7-d4b4-4072-b62f-dcd09486e44d.png" alt class="image--center mx-auto" /></p>
<p>Your pull request is now submitted for review by the project maintainers. Be patient, as it may take some time for them to review your code and provide feedback.</p>
<h2 id="heading-7-conclusion">7. Conclusion</h2>
<p>Contributing to open source in Python is a rewarding journey that allows you to collaborate with the global developer community, improve your coding skills, and give back to the projects you rely on. In this blog post, we've covered the essential steps to get started with open-source contributions in Python, from forking repositories on GitHub to creating pull requests. Remember to always follow the project's guidelines, communicate with maintainers and other contributors, and enjoy the process of making meaningful contributions to the world of open-source software.</p>
<p>Start your open-source journey today, and let your Python skills shine in the world of collaborative development!</p>
<p>Contributing to open-source projects in Python is an excellent way to grow as a developer and give back to the community. By following the steps outlined in this guide, you can embark on your open-source journey with confidence and make a positive impact on the Python ecosystem.</p>
]]></content:encoded></item><item><title><![CDATA[Activation Functions in Deep Learning]]></title><description><![CDATA[In the vast realm of deep learning, where intricate neural networks simulate the complexities of human cognition, lies a pivotal element that empowers these models to learn and make predictions with astonishing accuracy: activation functions. Often o...]]></description><link>https://blogs.lakshaykumar.tech/activation-functions-in-deep-learning</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/activation-functions-in-deep-learning</guid><category><![CDATA[Deep Learning]]></category><category><![CDATA[Python]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Thu, 11 May 2023 07:29:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/BQLw0OrA6F4/upload/835932f71565ef9895eb944537e3bb7d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the vast realm of deep learning, where intricate neural networks simulate the complexities of human cognition, lies a pivotal element that empowers these models to learn and make predictions with astonishing accuracy: activation functions. Often overshadowed by the glitz and glamour of advanced architectures and massive datasets, these unassuming mathematical formulas play a critical role in shaping the behavior and expressiveness of neural networks.</p>
<p>Imagine a world where neural networks are merely passive observers, incapable of distinguishing signal from noise, unable to unlock the hidden patterns buried within vast amounts of data. Activation functions hold the key to breathing life into these networks, empowering them to transform inputs into meaningful outputs, and facilitating the non-linearity required to tackle real-world challenges.</p>
<p>In this blog, we embark on a journey to explore the inner workings of activation functions in deep learning. We'll dive into their significance, understand the diverse range of functions available, and uncover the impact they have on model performance. By the end of this exploration, you'll gain a deeper understanding of how these seemingly inconspicuous formulas shape the very essence of deep learning and unleash its true potential.</p>
<h1 id="heading-activation-functions">Activation Functions</h1>
<p>Activation functions are mathematical functions applied to the outputs of individual neurons or nodes within a neural network. They introduce non-linearity into the network, enabling it to learn and model complex relationships between inputs and outputs.</p>
<p>Activation functions play a vital role in determining the activation levels or "firing" of neurons. They define the output values based on the weighted sum of inputs received by a neuron and introduce non-linear transformations that allow neural networks to approximate highly nonlinear functions.</p>
<p>Activation functions serve two primary purposes:</p>
<ol>
<li><p>Introducing non-linearity: Linear operations, such as simple weighted sums, are limited in their ability to model complex relationships. Activation functions provide the non-linear element required for neural networks to learn and represent intricate patterns and mappings in the data.</p>
</li>
<li><p>Enabling decision-making: Activation functions determine the level of activation or firing of a neuron, thereby deciding whether the information it carries is relevant or not. This enables the network to make decisions and classify inputs into different categories.</p>
</li>
</ol>
<p>There are various types of activation functions commonly used in deep learning, each with its own characteristics and suitability for different tasks. Examples include the sigmoid function, hyperbolic tangent (tanh), rectified linear unit (ReLU), and variants like leaky ReLU and exponential linear unit (ELU).</p>
<p>Choosing an appropriate activation function is crucial as it impacts the network's ability to learn, convergence speed, and generalization capabilities. Deep learning researchers and practitioners often experiment with different activation functions to find the most suitable one for a specific task or architecture.</p>
<h2 id="heading-sigmoid-function">Sigmoid function:</h2>
<p>The sigmoid function is a mathematical function commonly used in machine learning and neural networks. It maps any real-valued number to a value between 0 and 1, providing a smooth and continuous "S"-shaped curve. The function is defined as:</p>
<p>$$\sigma(x) = \frac{1}{1 + e^{-x}}$$</p><p>In this equation, "e" represents the base of the natural logarithm, and "x" is the input value. The sigmoid function has a range between 0 and 1, with an output of approximately 0.5 when the input is zero. As the input becomes more positive, the output approaches 1, and as the input becomes more negative, the output approaches 0.</p>
<p>The sigmoid function is particularly useful in machine learning because it allows us to convert an input into a probability. The output of the sigmoid function can be interpreted as the probability of a binary event occurring, where values close to 1 indicate a high probability, and values close to 0 indicate a low probability. Additionally, the smooth and differentiable nature of the sigmoid function facilitates efficient training of neural networks using gradient-based optimization algorithms.</p>
<h2 id="heading-hyperbolic-tangent-tanh-function">Hyperbolic Tangent (tanh) Function:</h2>
<p>The hyperbolic tangent function, denoted as tanh, is a mathematical function commonly used in various fields, including mathematics, physics, and machine learning. It is an activation function that maps real numbers to a range between -1 and 1. The tanh function is derived from the regular tangent function but adjusted to fit within the range of -1 to 1.</p>
<p>Mathematically, the tanh function can be expressed as:</p>
<p>$$\tanh(x) = \frac{{e^x - e^{-x}}}{{e^x + e^{-x}}}$$</p><p>Here, 'e' represents Euler's number, a mathematical constant approximately equal to 2.71828, and 'x' is the input value.</p>
<p>The tanh function has several key properties. Firstly, it is an odd function, meaning tanh(-x) = -tanh(x), which results in symmetry around the origin. Additionally, the tanh function is bounded, with its range limited between -1 and 1. As 'x' approaches positive or negative infinity, tanh(x) approaches 1 and -1, respectively.</p>
<p>As an activation function. It can introduce non-linearity into the network, allowing it to model complex relationships between input and output. The tanh function is advantageous over other activation functions, such as the sigmoid function, as it produces values centred around zero, which can aid in faster convergence during training.</p>
<h2 id="heading-relu-function">ReLU Function:</h2>
<p>The Rectified Linear Unit (ReLU) is a widely used activation function in artificial neural networks. It introduces non-linearity by outputting the input value if it is positive, and zero otherwise. The ReLU function is defined as:</p>
<p>$$f(x) = max(0,x)$$</p><p>Mathematically, this means that for any input value 'x', the ReLU function returns 'x' if it is positive, and zero otherwise. This simple thresholding behavior allows the ReLU function to model complex relationships between the inputs and outputs of a neural network.</p>
<p>The ReLU activation function offers several advantages. Firstly, it is computationally efficient to evaluate compared to other activation functions like the sigmoid or tanh. The ReLU function also helps to mitigate the vanishing gradient problem, which can occur during backpropagation in deep neural networks. Additionally, ReLU activations produce sparse representations, as many neurons can be activated while others remain inactive.</p>
<p>However, one limitation of ReLU is that it can cause dead neurons when the input is negative, leading to a zero gradient and no learning. To address this, various modifications have been proposed, such as Leaky ReLU, Parametric ReLU, and Exponential ReLU, which introduce small non-zero slopes for negative inputs to alleviate the dead neuron issue.</p>
<h2 id="heading-leaky-relu">Leaky ReLU:</h2>
<p>Leaky ReLU (Rectified Linear Unit) is an activation function commonly used in artificial neural networks. It is an improvement over the standard ReLU function, addressing one of its limitations. In the Leaky ReLU function, for inputs less than zero, instead of simply outputting zero as in ReLU, a small linear component is introduced. This linear component helps overcome the "dying ReLU" problem where neurons that have negative inputs during training become inactive, resulting in dead neurons that do not contribute to learning.</p>
<p>The mathematical representation of the Leaky ReLU function is as follows:</p>
<p>$$f(x) = max(ax, x)$$</p><p>where x is the input to the function, and a is a small positive constant known as the leakage coefficient. Typically, the value of a is set to a small value such as 0.01. If x is positive, the output is equal to x. If x is negative, the output is equal to <code>ax</code>, where <code>ax</code> is a small fraction of x.</p>
<p>By introducing this small linear component, the Leaky ReLU function ensures that even neurons with negative inputs can contribute to the network's learning process. This helps prevent the dying ReLU problem and improves the overall performance and robustness of the neural network.</p>
<h2 id="heading-exponential-linear-unit-elu">Exponential Linear Unit (ELU):</h2>
<p>The Exponential Linear Unit (ELU) is an activation function commonly used in deep learning models. It addresses the limitations of other activation functions, such as the vanishing gradient problem, and helps improve the performance of neural networks.</p>
<p>ELU is defined by a piecewise function that consists of two parts: the linear region for negative inputs and the exponential region for non-negative inputs. Mathematically, ELU is expressed as follows:</p>
<p>$$f(x) = \begin{cases} x, &amp; \text{if } x \geq 0 \\ \alpha \cdot (\exp(x) - 1), &amp; \text{if } x &lt; 0 \end{cases}$$</p><h2 id="heading-softmax-function">Softmax Function:</h2>
<p>The softmax function is commonly used as an activation function in machine learning and neural networks. It is particularly useful in multi-class classification problems, where the goal is to assign an input to one of several possible categories. The softmax function takes a vector of real-valued numbers as input and transforms them into a probability distribution over the classes.</p>
<p>Mathematically, given an input vector z = [z₁, z₂, ..., zn], the softmax function computes the exponential of each element, yielding exp(z₁), exp(z₂), ..., exp(zn). These values are then normalized by dividing each element by the sum of all exponentiated values, exp(z₁) + exp(z₂) + ... + exp(zn). The result is a vector of values between 0 and 1 that add up to 1, representing probabilities.</p>
<p>The softmax function's mathematical formula can be expressed as follows:</p>
<p>$$\operatorname{softmax}(z_i) = \frac{\exp(z_i)}{\sum_{j=1}^n \exp(z_j)}, \qquad i=1,2,\dots,n$$</p><p>For above in matrix form</p>
<p>$$\operatorname{softmax}(\mathbf{z}) = \frac{\exp(\mathbf{z})}{\sum_{j=1}^n \exp(z_j)}, \qquad \mathbf{z}=[z_1, z_2, \dots, z_n]^\top$$</p><p>By applying the softmax function, the largest value in the input vector is amplified, while smaller values are suppressed. This makes the output vector suitable for interpreting as class probabilities, allowing us to choose the class with the highest probability as the predicted class during classification tasks.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, we have explored several popular activation functions used in machine learning and neural networks. Each activation function offers unique properties and advantages that can impact the performance and behavior of the model.</p>
<p>The sigmoid function, with its S-shaped curve, is commonly used in binary classification problems. It squashes the input into a range between 0 and 1, representing probabilities. However, it suffers from the vanishing gradient problem, which can hinder training in deep networks.</p>
<p>The hyperbolic tangent (tanh) function also squashes the input between -1 and 1, but it is symmetric around the origin. It addresses the vanishing gradient problem to some extent and is often used in recurrent neural networks (RNNs) and certain types of architectures.</p>
<p>The rectified linear unit (ReLU) function is widely popular due to its simplicity and effectiveness. It sets negative inputs to zero, providing faster convergence during training. However, ReLU suffers from the dying ReLU problem, where neurons can get stuck in a state of inactivity.</p>
<p>Leaky ReLU and Parametric ReLU (PReLU) are variants of ReLU that address the dying ReLU problem by introducing a small slope for negative inputs. This helps prevent the complete "death" of neurons and improves the performance of deep networks.</p>
<p>The softmax function is commonly used in multi-class classification problems. It transforms a vector of real values into a probability distribution over classes, allowing us to select the class with the highest probability as the predicted class.</p>
<p>Choosing the appropriate activation function depends on the specific problem, network architecture, and desired behaviour. Experimentation and understanding the characteristics of each activation function are crucial for achieving optimal performance in machine learning tasks.</p>
<p>That's all for this blog, For any queries, feel free to write in the comments or reach out to me over different social media platforms. Know more at <a target="_blank" href="http://lakshaykumar.tech"><strong>lakshaykumar.tech</strong></a></p>
<p>Happy Learning!</p>
]]></content:encoded></item><item><title><![CDATA[Generative Artificial Intelligence]]></title><description><![CDATA[Artificial Intelligence (AI) has been one of the most revolutionary advancements in the field of technology in recent years. The ability of machines to perform complex tasks previously only possible for humans has transformed the way we live, work, a...]]></description><link>https://blogs.lakshaykumar.tech/generative-artificial-intelligence</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/generative-artificial-intelligence</guid><category><![CDATA[generative ai]]></category><category><![CDATA[Artificial Intelligence]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Deep Learning]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Mon, 08 May 2023 07:08:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/zbLW0FG8XU8/upload/848a1428a5e43b556adf39d59a2acbfd.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Artificial Intelligence (AI) has been one of the most revolutionary advancements in the field of technology in recent years. The ability of machines to perform complex tasks previously only possible for humans has transformed the way we live, work, and interact with each other. One of the latest and most exciting advancements in the field of AI is Generative AI. This technology allows machines to create new and unique content such as images, videos, and even entire stories without human intervention. In this blog post, we will explore the maths behind Generative AI. The images in this blog are taken from 'youtube'.</p>
<h3 id="heading-what-is-this">What is this?</h3>
<p>Generative AI is a rapidly evolving field of Artificial Intelligence that is focused on developing algorithms capable of generating new content autonomously. This is achieved through the use of generative models that are trained on a dataset of existing content. The models then use this data to generate new examples that are similar in style, structure, and content to the original dataset.</p>
<p>There are various types of generative models used in AI, such as the Naive Bayes Classifier, Gaussian Mixture Model, and Generative Adversarial Networks (GANs). These models are designed to create new examples that mimic the features of the training data by learning patterns, relationships, and distributions within the dataset.</p>
<h3 id="heading-gans">GANs</h3>
<p>GAN stands for Generative Adversarial Networks. It is Deep learning-based generative models are used for unsupervised learning. Two neural networks compete with each other to generate variations in the data.</p>
<p>This concept was first published in "Alec Radford Paper in the year 2016 - DCGAN (Deep Convolutional General Adversarial Networks)".</p>
<p>It uses two sub-models</p>
<ul>
<li><p><strong>Generator</strong>: The generator network takes a sample and generates a sample of data. Used to generate fake data for negative sampling.</p>
</li>
<li><p><strong>Discriminator</strong>: Discriminator network decides whether the data is generated or taken from the real sample using binary classification using the sigmoid function giving output 0 or 1.</p>
</li>
</ul>
<p><strong>How does GAN work</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683528266486/de16a7fb-c607-4e6c-a4c4-2a9a9bb3e68e.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>First, the GAN model is trained on a dataset of real samples. This dataset serves as a reference for the generator network to generate new samples that resemble the real data.</p>
</li>
<li><p>During the training process, the generator network takes random noise as input and generates a sample. This generated sample is then passed on to the discriminator network.</p>
</li>
<li><p>The discriminator network is designed to differentiate between real and fake samples. It takes both the real and generated samples as input and provides an output that indicates whether the sample is real or fake.</p>
</li>
<li><p>The generator network then adjusts its parameters based on the feedback from the discriminator network. If the generated sample is classified as fake, the generator network modifies its parameters to generate a sample that is more similar to the real data. This process continues until the generator network can generate samples that are indistinguishable from the real data.</p>
</li>
<li><p>The discriminator network is also simultaneously updated during the training process. Its goal is to accurately distinguish between real and fake samples. The updates to the discriminator network are based on the accuracy of its classification of the generated samples.</p>
</li>
<li><p>This process of training and updating both the generator and discriminator networks continues until the generator network can generate samples that are virtually indistinguishable from the real data.</p>
</li>
</ul>
<h3 id="heading-training-a-gan-model">Training a GAN Model</h3>
<ol>
<li><p>Train the discriminator and freeze the generator, which means the training set for the generator is turned as False and the network will only do the forward pass and no back-propagation will be applied.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683528587041/2ef45293-afd4-4f28-a476-22792b8257d4.png" alt class="image--center mx-auto" /></p>
</li>
</ol>
<p>Train the generator and freeze the discriminator. In this phase, we get the results from the first phase and can use them to make better from the previous state to try and fool the discriminator better.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683528651173/a1896b3f-642c-4921-96db-173d023fb45f.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-mathematical-formulation-of-gan">Mathematical Formulation of GAN</h3>
<p>$$\min_G \max_D V(D,G) = \mathbb{E}_{x \sim p_{\text{data}}(x)}[\log D(x)] + \mathbb{E}_{z \sim p_z(z)}[\log(1 - D(G(z)))]$$</p><p>The function is the objective function of a generative adversarial network (GAN) and is used to train the generator and discriminator networks in the GAN.</p>
<p>The objective of the GAN is to generate synthetic data that closely resembles the real data. The function represents a minimax game between the generator network (G) and the discriminator network (D), where G tries to minimize the function and D tries to maximize it.</p>
<p>The function takes as input two probability distributions: the distribution of the real data samples (p_data(x)) and the distribution of the noise vector (p_z(z)). It has two terms, each representing an expected value.</p>
<p>The first term represents the expected value of the logarithm of the discriminator's output when fed real data samples. The discriminator network tries to maximize this term by correctly classifying real data samples as real.</p>
<p>The second term represents the expected value of the logarithm of 1 minus the discriminator's output when fed generated samples from the generator network. The generator network tries to minimize this term by generating samples that the discriminator network classifies as real.</p>
<p>By minimizing this function, the generator network learns to generate synthetic data that is similar to the real data, while the discriminator network learns to distinguish between real and fake samples. The training process continues until the generator network can generate synthetic data that is indistinguishable from the real data.</p>
<p><strong>For Fake Data</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683529323855/67f0b00c-28ce-4790-a407-7f85e9d68cb5.png" alt class="image--center mx-auto" /></p>
<p><strong>For Real Data</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683529345572/3851bed2-e422-40e2-85dc-c3638df9a5b5.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1683529368878/27504060-36a8-4314-873d-798558694311.png" alt class="image--center mx-auto" /></p>
<p>Here the unsupervised model (non-labelled data, real and fake) is being converted to a supervised model through Adversarial Framework</p>
<ul>
<li><p>E(x~p_data(x))[log D(x)] - Discriminative prediction on real data. The expectation of log when the input is from the real data distribution. Basically, the average of the discriminator's predictions when data is real. Discriminator wants a high D(x) value</p>
</li>
<li><p>E(z~p_data(z))[log D(G(x))] - Discriminative prediction on fake data. The expectation of the log is when we pull a lot of input noise from the generator. Discriminator wants a low D(G(z)) value showing the confidence</p>
</li>
</ul>
<h3 id="heading-types-of-gan">Types of GAN</h3>
<p>There are several types of generative adversarial networks (GANs) that have been developed, each with its own unique architecture and training procedure. Here are some of the most common types of GANs:</p>
<ol>
<li><p>Vanilla GANs: These are the standard GANs that were introduced by Ian Goodfellow in 2014. They consist of a generator network that generates fake samples and a discriminator network that distinguishes between real and fake samples.</p>
</li>
<li><p>Conditional GANs: These are GANs that are conditioned on additional information, such as class labels or images. The additional information is typically fed into both the generator and discriminator networks to help them generate and distinguish between samples that belong to different categories.</p>
</li>
<li><p>Deep Convolutional GANs (DCGANs): These are GANs that use deep convolutional neural networks (CNNs) as both the generator and discriminator networks. They are particularly effective for generating high-resolution images.</p>
</li>
<li><p>Wasserstein GANs (WGANs): These are GANs that use a different loss function based on the Wasserstein distance between the real and fake data distributions. This loss function is more stable than the one used in vanilla GANs and can lead to better results.</p>
</li>
<li><p>CycleGANs: These are GANs that learn to translate between two different domains, such as turning a photo into a painting. They consist of two GANs, one for each domain, that are trained in an adversarial manner to generate samples that can be translated between the two domains.</p>
</li>
<li><p>Progressive GANs: These are GANs that generate high-resolution images by gradually increasing the resolution of the generated images during training. They are able to generate images that are much higher in resolution than other types of GANs.</p>
</li>
</ol>
<p>These are just some of the many types of GANs that have been developed. Each type has its own advantages and disadvantages, and the choice of which type to use depends on the specific application and the desired outcome.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>To sum up, generative AI has the potential to revolutionize many fields by enabling machines to create new data and generate novel solutions to complex problems. The development of generative models such as GANs, VAEs, and autoregressive models has opened up new avenues for creativity, innovation, and problem-solving. From generating art, music, and literature to synthesizing new materials, drugs, and molecules, generative AI is unlocking new possibilities in many domains. While there are still many challenges to be overcome, such as improving the stability and diversity of generated samples, the future of generative AI looks bright, and we can expect to see many exciting advances in this field in the years to come.</p>
<p>That's all for this blog, For any queries, feel free to write in the comments or reach out to me over different social media platforms. Know more at <a target="_blank" href="https://www.lakshaykumar.tech/"><strong>https://www.lakshaykumar.tech/</strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Edge computing and Tensorflow Lite]]></title><description><![CDATA[We all know that ML requires a lot of computing speed and even cloud services are required on large scale. This blog covers details about how to integrate ML and run TensorFlow on a microcontroller that will help in saving a lot of costs. This blog e...]]></description><link>https://blogs.lakshaykumar.tech/edge-computing-and-tensorflow-lite</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/edge-computing-and-tensorflow-lite</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[TensorFlow]]></category><category><![CDATA[Artificial Intelligence]]></category><category><![CDATA[Python]]></category><category><![CDATA[arduino]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Wed, 01 Feb 2023 15:25:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1675264837855/0450d4cf-e5fa-473d-9a71-71e3a7d32460.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We all know that ML requires a lot of computing speed and even cloud services are required on large scale. This blog covers details about how to integrate ML and run TensorFlow on a microcontroller that will help in saving a lot of costs. This blog emphasises that this doesn’t need high computing power if we learn to use Tiny ML effectively.</p>
<p>So, all this started with a problem, I wanted to control the environmental conditions of a plant using Machine Learning, but surprisingly the Microcontroller i.e. ESP32 have just 512 kB of space and my model itself was 1.23 MB. So the question is, how can I use that model in my ESP32 board?</p>
<p>After some research, I got to know about <strong>Edge Computing which</strong> <strong>refers to the process of performing data processing and storage at the edge of a network, rather than in a centralized location such as a data centre. This allows for faster and more efficient data processing, as well as reduced latency and bandwidth usage.</strong></p>
<p>For example, Self-driving cars rely on edge computing to process sensor data in real-time, allowing them to make quick decisions and navigate safely.</p>
<p>Talking about the advantages of Edge computing, here are a few -</p>
<ol>
<li><p>Reduced Latency: Edge computing minimizes the distance data must travel, reducing latency and enabling real-time decision-making.</p>
</li>
<li><p>Improved Performance: Edge computing can handle large amounts of data generated by IoT devices and other sources, improving the overall performance of the system.</p>
</li>
<li><p>Increased Security: Edge computing can improve security by reducing the amount of sensitive data that needs to be transmitted over the network.</p>
</li>
<li><p>Cost Savings: Edge computing has the potential to reduce costs and increase efficiency by reducing the amount of data that needs to be transmitted and stored in the cloud.</p>
</li>
<li><p>Offline Operation: Edge computing enables devices to continue to operate even when disconnected from the network, providing continuity of operations in remote or offline locations.</p>
</li>
<li><p>Increased Reliability: Edge computing can provide a more reliable solution by reducing the risk of data loss due to network outages or other disruptions.</p>
</li>
<li><p>Scalability: Edge computing can be scaled easily as the number of devices and data sources grows, without requiring significant changes to the central infrastructure.</p>
</li>
<li><p>Increased Flexibility: Edge computing allows for the deployment of machine learning models and other advanced technologies at the edge, enabling more flexible and innovative solutions.</p>
</li>
</ol>
<p>Delving more into the problem, Another concept that can help me reach the solution is Tiny ML. It is a <strong>subset of the broader field of machine learning (ML)</strong> that focuses on implementing ML algorithms on small, low-power devices such as microcontrollers, sensors, and embedded systems.</p>
<p>So, my approach to the solution</p>
<ul>
<li><p><strong>Deploy Model to cloud</strong>: This requires internet that might not be available always to the ESP32.</p>
</li>
<li><p><strong>Using Model Directly:</strong> Again space issues?</p>
</li>
</ul>
<h3 id="heading-tflite">Tflite</h3>
<p>TensorFlow Lite (TFLite) is a <strong>lightweight version of TensorFlow</strong>, an open-source machine learning framework developed by Google. It is specifically designed to run on resource-constrained devices such as mobile phones, embedded systems, and microcontrollers.</p>
<p>Refer to this colab notebook- <a target="_blank" href="https://colab.research.google.com/drive/1P3KrhuXFPjKb8Ub9lLcnSek2KZH1xQ4k?usp=sharing">https://colab.research.google.com/drive/1P3KrhuXFPjKb8Ub9lLcnSek2KZH1xQ4k?usp=sharing</a></p>
<p>This colab shows how to reduce the size of our model.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675263082306/f8966bd8-a62f-4af0-b377-efa034801e5f.png" alt class="image--center mx-auto" /></p>
<p>The repository uses the process of <strong>Quantization</strong></p>
<p>Quantization is the process of converting a continuous, high-precision representation of a signal or data into a lower-precision, discrete form. The goal of quantization is to reduce the memory and computational requirements of a machine-learning model, without significantly affecting its accuracy. This is achieved by reducing the number of bits used to represent the data, thus reducing the number of possible values that can be represented. Quantization can be applied to weights, activations, and gradients in a neural network, and is a common technique used in hardware acceleration and deployment of deep learning models on embedded devices with limited resources.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675263334632/2a1b1638-3fae-47e6-bad9-3d97a4c851a6.png" alt /></p>
<h3 id="heading-model-accuracy"><strong>Model Accuracy</strong></h3>
<p>There was a slight change in the accuracy of the model.</p>
<p>The goal here is to show how to compress the size of the model, so as not to focus much on the accuracy part.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1675263389830/10172034-8c02-44bf-ae96-86bfac86e918.png" alt /></p>
<p>Now we have a model, how can we use this model in Arduino code. `model.tflite` is the file generated from the python code.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;TensorFlowLite.h&gt;</span></span>

<span class="hljs-comment">// Load the model into a TensorFlow Lite interpreter</span>
<span class="hljs-function">tflite::FlatBufferModel <span class="hljs-title">model</span><span class="hljs-params">(<span class="hljs-string">"model.tflite"</span>)</span></span>;
tflite::ops::builtin::BuiltinOpResolver resolver;
<span class="hljs-function">tflite::InterpreterBuilder <span class="hljs-title">builder</span><span class="hljs-params">(model, resolver)</span></span>;
<span class="hljs-built_in">std</span>::<span class="hljs-built_in">unique_ptr</span>&lt;tflite::Interpreter&gt; interpreter;
builder(&amp;interpreter);
interpreter-&gt;AllocateTensors();

<span class="hljs-comment">// Get pointers to the input and output tensors</span>
TfLiteTensor* input = interpreter-&gt;input(<span class="hljs-number">0</span>);
TfLiteTensor* output = interpreter-&gt;output(<span class="hljs-number">0</span>);

<span class="hljs-comment">// Create a buffer to hold the input data</span>
<span class="hljs-keyword">float</span> input_data[<span class="hljs-number">3</span>] = { <span class="hljs-number">45</span>,<span class="hljs-number">40</span>,<span class="hljs-number">8</span> };

<span class="hljs-comment">// Fill the input tensor with data</span>
<span class="hljs-built_in">memcpy</span>(input-&gt;data.f, input_data, <span class="hljs-keyword">sizeof</span>(input_data));

<span class="hljs-comment">// Run the model</span>
interpreter-&gt;Invoke();

<span class="hljs-comment">// Get the results from the output tensor</span>
<span class="hljs-keyword">float</span>* result = output-&gt;data.f;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">3</span>; i++) {
    <span class="hljs-keyword">int</span> prediction = round(result[i]);
    Serial.print(<span class="hljs-string">"Prediction for input "</span>);
    Serial.print(i);
    Serial.print(<span class="hljs-string">" is "</span>);
    Serial.println(prediction);
}
</code></pre>
<p>To process the image, use the following piece of code. Make sure you have a camera connected to ESP32. This code converts the image to a matrix form that can be used as an input for your model.</p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;Adafruit_GFX.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;Adafruit_SSD1306.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;Wire.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;SPI.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;Adafruit_VC0706.h&gt;</span></span>

Adafruit_VC0706 cam = Adafruit_VC0706(&amp;Serial1);

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">setup</span><span class="hljs-params">()</span> </span>{
  Serial.begin(<span class="hljs-number">115200</span>);

  <span class="hljs-keyword">if</span> (!cam.begin()) {
    Serial.println(<span class="hljs-string">"Couldn't find camera"</span>);
    <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>);
  }

  <span class="hljs-comment">// Take a photo</span>
  <span class="hljs-keyword">if</span> (!cam.takePicture()) {
    Serial.println(<span class="hljs-string">"Failed to take picture"</span>);
    <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>);
  }

  <span class="hljs-comment">// Read the image data</span>
  <span class="hljs-keyword">uint8_t</span> *image;
  <span class="hljs-keyword">uint16_t</span> jpglen = cam.frameLength();
  image = <span class="hljs-keyword">new</span> <span class="hljs-keyword">uint8_t</span>[jpglen];
  <span class="hljs-keyword">if</span> (!cam.readPicture(image, jpglen)) {
    Serial.println(<span class="hljs-string">"Failed to read picture"</span>);
    <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>);
  }

  <span class="hljs-comment">// Convert the image to a matrix</span>
  <span class="hljs-keyword">int</span> rows = cam.getSize().height;
  <span class="hljs-keyword">int</span> cols = cam.getSize().width;
  <span class="hljs-keyword">uint8_t</span> matrix[rows][cols];
  <span class="hljs-keyword">int</span> index = <span class="hljs-number">0</span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; rows; i++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; cols; j++) {
      matrix[i][j] = image[index];
      index++;
    }
  }

  <span class="hljs-comment">// Use the matrix as desired</span>
  <span class="hljs-comment">// ...</span>

  <span class="hljs-keyword">delete</span>[] image;
}

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">loop</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-comment">// nothing to do here</span>
}
</code></pre>
<h3 id="heading-challenges">Challenges</h3>
<ul>
<li><p><strong>Finding the working code available:</strong> Have to work with lots of hit and trial codes</p>
</li>
<li><p><strong>Model compatibility with .tflite library version in Arduino:</strong> Since .tflite is still in the development process, its version compatibility is a major problem</p>
</li>
<li><p><strong>Handling tensors in C++ is a bit complex:</strong> Working with tensors and matrices in C++ is challenging, as C++ is closer to CPU than Python.</p>
</li>
</ul>
<p>In conclusion, TFLite and Edge computing are revolutionizing the way we approach machine learning, making it possible to deploy complex models on low-power devices. This opens up exciting new possibilities for creating intelligent applications that can run on a wide range of devices, from smartphones and wearables to industrial equipment and IoT devices. The future of TFLite and Edge computing is bright, and it will be exciting to see how developers leverage this technology to build the next generation of smart and connected devices. Whether you're a seasoned machine learning practitioner or just starting to explore this field, the benefits of TFLite and Edge computing are undeniable. Get ready to witness a world of limitless possibilities, where devices are no longer just smart, but truly intelligent.</p>
<p>For any queries, feel free to write in the comments or reach out to me over different social media platforms. Know more at <a target="_blank" href="https://www.lakshaykumar.tech/">https://www.lakshaykumar.tech/</a></p>
]]></content:encoded></item><item><title><![CDATA[Machine Learning Simplified!]]></title><description><![CDATA[We have been hearing about machine learning for a long time. IBM defines Machine Learning as a "Branch of artificial intelligence (AI) and computer science which focuses on the use of data and algorithms to imitate the way that humans learn, graduall...]]></description><link>https://blogs.lakshaykumar.tech/machine-learning-simplified</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/machine-learning-simplified</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Tue, 17 Jan 2023 04:49:45 GMT</pubDate><content:encoded><![CDATA[<p><img src="https://i.ytimg.com/vi/Rt6beTKDtqY/maxresdefault.jpg" alt="The Mathematics of Machine Learning - YouTube" /></p>
<p>We have been hearing about machine learning for a long time. IBM defines Machine Learning as a "Branch of artificial intelligence (AI) and computer science which focuses on the use of data and algorithms to imitate the way that humans learn, gradually improving its accuracy"</p>
<p>We categorize ML as Supervised, Unsupervised and Reinforcement Learning. All these types have many Algorithms under them. Every algorithm has its own maths behind its work.</p>
<p>I am exploring this world of applied Mathematics for the last 2 years as an absolute beginner. Starting from concepts of Regression, and exploring algorithms like SVM, decision tree and advanced algorithms such as Random forest, there is rigorous mathematics involved. But there is a very basic equation being followed in most of the algorithms, i.e.</p>
<p>$$y = mx + c$$</p>
<p>You might have studied this in your high school under the topic <em>Equation of Straight Line.</em> This is the very basic equation to determine the equation of the straight line. In this blog, we'll see how this equation is modified in different algorithms and how they work to provide accurate output.</p>
<p>x - Independent Variable</p>
<p>y - Dependent Variable</p>
<p><strong>Simple Linear Regression -</strong></p>
<p>It is the type of regression that shows how two variables (x and y) are changing. We have points on the XY plane and we fit a line across all the points such that the distance between the points and the line is minimum. Here is an example.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673928943579/0aa44db1-a721-4257-9f10-560df49b4aec.png" alt class="image--center mx-auto" /></p>
<p>We see the y and x are increasing linearly, so we can fit a line like this and find the values of <strong>m</strong> and <strong>c</strong> defining the slope and intercept respectively.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673929077324/2b70300e-c2f4-4417-9050-845624fabed5.png" alt class="image--center mx-auto" /></p>
<p>This is a very simple example. That can predict the value of y as per the value of x.</p>
<p><strong>Multiple Linear Regression -</strong></p>
<p>The above simple example gave us the idea of how to find a relationship between two variables. Instead of a single independent variable x, what if we have multiple values of x i.e. my output depends on multiple variables? In that case, we will have a messy graph like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673929727905/4762bf68-ccf7-4055-9212-69aa72350afb.png" alt class="image--center mx-auto" /></p>
<p>We see three variables here with 3 different lines fit. Here we have multiple values of x and an intercept. Here the equation is like this.</p>
<p>$$y = x<em>{1}m</em>{1} + x<em>{2}m</em>{2} + x<em>{3}m</em>{3} + c$$</p>
<p>Note here that for every independent variable, we have a different slope. We can generalize the equation here as-</p>
<p>$$y = \sum<em>{i=1}^{n}x</em>{i}m_{i} + c$$</p>
<p><strong>Neural Networks</strong></p>
<p>You might be surprised, but actually, the neural network works on this slope-intercept equation. But the twist is, we have to work with different equations at different times.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673930701178/e278a535-38ce-4b26-8fbe-e8aa7217127d.png" alt class="image--center mx-auto" /></p>
<p>We see that at the end, we have an equation something similar to the above in Multiple Linear Regression. In a complex neural network, we have multiple biases, these biases will continue till we reach the output layer.</p>
<p>Here I have discussed three major use cases of this equation. But there are numerous use cases. For any queries, feel free to comment or contact me over email. Don't forget to subscribe on my website - <a target="_blank" href="http://lakshaykumar.tech"><strong>lakshaykumar.tech</strong></a></p>
]]></content:encoded></item><item><title><![CDATA[Let numbers speak!]]></title><description><![CDATA[Machine Learning and Data Science are all about playing with numbers. In this blog, I'll be covering some basic mathematical functions/terms that can help you in data analysis.
Logarithm
Imagine you have to visualize the following table
CompanyRevenu...]]></description><link>https://blogs.lakshaykumar.tech/let-numbers-speak</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/let-numbers-speak</guid><category><![CDATA[Data Science]]></category><category><![CDATA[Mathematics]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Thu, 12 Jan 2023 14:58:40 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/Wpnoqo2plFA/upload/563f9755dbb90887344828da305e3d42.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Machine Learning and Data Science are all about playing with numbers. In this blog, I'll be covering some basic mathematical functions/terms that can help you in data analysis.</p>
<h2 id="heading-logarithm">Logarithm</h2>
<p>Imagine you have to visualize the following table</p>
<table><tbody><tr><td><p><strong>Company</strong></p></td><td><p><strong>Revenue</strong></p></td></tr><tr><td><p>Tesla</p></td><td><p>31</p></td></tr><tr><td><p>Uber</p></td><td><p>11</p></td></tr><tr><td><p>Amazon</p></td><td><p>386</p></td></tr><tr><td><p>Jindal Steel</p></td><td><p>4.7</p></td></tr><tr><td><p>Axis Bank</p></td><td><p>5.6</p></td></tr><tr><td><p>Vedanta</p></td><td><p>11.3</p></td></tr></tbody></table>

<p>Plotting this through a bar plot, this is what we'll get</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673078863835/e3334100-59aa-4b70-8b71-742828e31fad.png" alt class="image--center mx-auto" /></p>
<p>We can see that Amazon have such a high revenue that it is making it difficult to read the revenue for Jindal Steel and Axis Bank. So what we do now, here comes the concept of <em>log.</em> The Log is the inverse of an exponent. In the given equation -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673079143486/85c92d27-cb49-4812-9147-4ee7cadb50f2.png" alt class="image--center mx-auto" /></p>
<p>this means that <strong><em>b raised to the power c gives us a</em></strong>*,* where b is the base. The common base used is a base of 10. So in the above data, if I add a column for log base 10, this is what we'll get</p>
<table><tbody><tr><td><p><strong>Company</strong></p></td><td><p><strong>Revenue</strong></p></td><td><p><strong>Log (base 10)</strong></p></td></tr><tr><td><p>Tesla</p></td><td><p>31</p></td><td><p>1.491361694</p></td></tr><tr><td><p>Uber</p></td><td><p>11</p></td><td><p>1.041392685</p></td></tr><tr><td><p>Amazon</p></td><td><p>386</p></td><td><p>2.586587305</p></td></tr><tr><td><p>Jindal Steel</p></td><td><p>4.7</p></td><td><p>0.6720978579</p></td></tr><tr><td><p>Axis Bank</p></td><td><p>5.6</p></td><td><p>0.748188027</p></td></tr><tr><td><p>Vedanta</p></td><td><p>11.3</p></td><td><p>1.053078443</p></td></tr></tbody></table>

<p>We can see that in the log column, the numbers are very much comparable so as its plot, we can clearly see the difference in revenue of the companies in much better way than the above.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1673079612999/f4b4ed6d-0345-4cc4-a886-7d2db9e356fb.png" alt class="image--center mx-auto" /></p>
<p>In the log, every consecutive number is base times greater than the preceding one, for example, log<sub>5</sub>125 is 10 times greater than log<sub>5</sub>25.</p>
<p>Practical examples include Earthquakes, where on a Richter Scale 5 is 10 times more powerful than 4.</p>
<h2 id="heading-mean-and-deviations">Mean and Deviations</h2>
<p>Consider the following data on salaries for employees -</p>
<table><tbody><tr><td><p><strong>Employee Name</strong></p></td><td><p><strong>Salary (in USD)</strong></p></td></tr><tr><td><p>Jack</p></td><td><p>1678.3</p></td></tr><tr><td><p>Alan</p></td><td><p>2242.9</p></td></tr><tr><td><p>Kelvin</p></td><td><p>998.8</p></td></tr><tr><td><p>Jake</p></td><td><p>1938.6</p></td></tr><tr><td><p>Elly</p></td><td><p>1200.4</p></td></tr></tbody></table>

<p>We can say that the average salary of a company is 1611.8 USD, but there is a huge difference between the salary of Kelvin and Alan when compared to the average. Imagine we have a new employee Addie with a salary of 25000 USD and then our new mean salary would be 5509.83 USD, but this isn't the case. We see except 1, everyone has their salary less than 2500 USD. So we can calculate the deviation from mean -</p>
<table><tbody><tr><td><p><strong>Employee Name</strong></p></td><td><p><strong>Salary (in USD)</strong></p></td><td><p><strong>Deviation (Abs)</strong></p></td></tr><tr><td><p>Jack</p></td><td><p>1678.3</p></td><td><p>3831.533333</p></td></tr><tr><td><p>Alan</p></td><td><p>2242.9</p></td><td><p>3266.933333</p></td></tr><tr><td><p>Kelvin</p></td><td><p>998.8</p></td><td><p>4511.033333</p></td></tr><tr><td><p>Jake</p></td><td><p>1938.6</p></td><td><p>3571.233333</p></td></tr><tr><td><p>Elly</p></td><td><p>1200.4</p></td><td><p>4309.433333</p></td></tr><tr><td><p>Addie</p></td><td><p>25000</p></td><td><p>-19490.16667</p></td></tr></tbody></table>

<p>We see there is a lot of deviation from the mean salary. Now how to sum this up in a single number?</p>
<p>Average won't work, because we have negative values here, so instead of normal averaging, we can square the numbers, add them, divide by the total number of samples, and then square root. This is called <strong>Standard Deviation.</strong></p>
<table><tbody><tr><td><p><strong>Employee Name</strong></p></td><td><p><strong>Salary (in USD)</strong></p></td><td><p><strong>Deviation (Abs)</strong></p></td><td><p>Deviation Sq.</p></td></tr><tr><td><p>Jack</p></td><td><p>1678.3</p></td><td><p>3831.533333</p></td><td><p>14680647.68</p></td></tr><tr><td><p>Alan</p></td><td><p>2242.9</p></td><td><p>3266.933333</p></td><td><p>10672853.4</p></td></tr><tr><td><p>Kelvin</p></td><td><p>998.8</p></td><td><p>4511.033333</p></td><td><p>20349421.73</p></td></tr><tr><td><p>Jake</p></td><td><p>1938.6</p></td><td><p>3571.233333</p></td><td><p>12753707.52</p></td></tr><tr><td><p>Elly</p></td><td><p>1200.4</p></td><td><p>4309.433333</p></td><td><p>18571215.65</p></td></tr><tr><td><p>Addie</p></td><td><p>25000</p></td><td><p>-19490.16667</p></td><td><p>379866596.7</p></td></tr><tr><td><p>Mean</p></td><td><p></p></td><td><p></p></td><td><p><strong>8726.343666</strong></p></td></tr></tbody></table>

<p>We can see here that there is so much deviation from the mean salary here..</p>
]]></content:encoded></item><item><title><![CDATA[Dev Retro 2022]]></title><description><![CDATA[The year 2015, when I got my independent computer. It was an HP desktop with 2GB ram, 128GB storage and a Pentium processor PC. At that time I wasn't aware much of these configurations, so I kept on downloading as many software, IDEs, languages etc. ...]]></description><link>https://blogs.lakshaykumar.tech/dev-retro-2022</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/dev-retro-2022</guid><category><![CDATA[#DevRetro2022]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[React]]></category><category><![CDATA[Programming Blogs]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Tue, 13 Dec 2022 09:21:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1670512470408/CX9IDzXrr.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The year 2015, when I got my independent computer. It was an HP desktop with 2GB ram, 128GB storage and a Pentium processor PC. At that time I wasn't aware much of these configurations, so I kept on downloading as many software, IDEs, languages etc. in that system. Not even this, I installed many games too. Due to a lack of proper guidance about how to build a career in Computer. I kept doing whatever comes to my mind.</p>
<p>Then one day, during my computer class, we were given a demo on how to build a basic webpage using the HTML programming language in the class. Initially, I was planning to bunk the class, but thankfully I didn't. That day made me curious about how a very basic tool like notepad can be so helpful in generating results like a basic webpage. I was so fascinated by this simple output that made me explore more on my own. From a basic website, I completed all the chapters related to HTML on my own from the school textbook (lack of internet access). I'll say, that the demo class was the major reason why I liked coding. Slowly during my vacations, I completed the major coding chapters on my own like BASIC (Visual Basic) and C++.</p>
<p>I developed a strong interest in coding till then. I was able to solve questions using C++ and explore many tech stuff on my own. After grade 8, I continued with Java for the next four years along with Python simultaneously. Towards the end of my class 12, I created several apps and brought laurels from various reputed organisations.</p>
<p><strong>Neorky :</strong> App for giving short-term employment to people. (IIT Roorkee App Innovation Challenge, 2nd Runner up).</p>
<p><strong>AntiBully :</strong> Anti-bullying app (IIT Guwhati Alcheringa Code for Change, 2nd Runner Up).</p>
<p><strong>covid19helps:</strong> A system with ML Symptom-based predictor, important medications for covid-19 infected patients and emergency services. Helped a lot during the covid-19 crisis. (Times NIE, App Innovation Challenge).</p>
<p>More of my projects are here - <a target="_blank" href="https://lakshaykumar.tech/#sec5">https://lakshaykumar.tech/#sec5</a></p>
<p>After completing High School, I started my Undergraduate at India's First Liberal Science University - Atria University in Bangalore.</p>
<p>Bangalore as a Tech City (Silicon Valley of India) gave me numerous opportunities to grow. During the initial days of my college, I was more focussed on building solutions and industry-level projects. After working on several projects individually, I realised the importance of teamwork with the community.</p>
<p>On April 5 2022, I attended my first networking tech event - Github Tech Tab. I made many useful connections here that helped me later in several projects. Following this, on April 28, at Microsoft Reactor, it was the first tech conference I attended. Later I kept on attending tech conferences like Global Azure day, Dev Nation, etc.</p>
<p>During my college hours, I was working on the integration of Computer Vision with Drones, this gave me many real insights and a rough idea of the solutions to new-age problems. This was one of my most successful projects, I built autonomous surveillance drones.</p>
<p>Later on August 6, 2022. I was invited for the first time to give a talk at Microsoft Reactor and here my journey as a Community speaker started. Then I was more involved in the community space. I engaged myself in giving talks, helping members of the discord community, writing blogs, building packages for the developer community and hosting competitions/hackathons.</p>
<p>This year, I also got the opportunity to demonstrate some of my projects in front of the Finance Minister of India ~ Nirmala Sitaraman and the Chief Minister of Karnataka State ~ Basvaraj Bommai.</p>
<p>Overall, I'll say that this year 2022 was full of opportunities for me in terms of personal development and growth.</p>
<p>Here are some of my learnings throughout my journey as a developer :</p>
<ul>
<li><p>Initial guidance is very important for a developer. It gives you the kick to start learning new things.</p>
</li>
<li><p>Mentors are important to guide you, but initiation to learn new things has to grow from the inner side.</p>
</li>
<li><p>Never work because of opportunities, they will come at the right time.</p>
</li>
<li><p>The best way to learn something is to build on your own instead of solely following a series of theoretical videos or documentation.</p>
</li>
</ul>
<p>I am always looking for new people in my team to work on any project, if you wish to collaborate, feel free to contact me. Details are available on my <a target="_blank" href="https://www.lakshaykumar.tech/">website</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Publish your own Python Package]]></title><description><![CDATA[Hello folks, 
You might have seen some of my packages in the previous blog
While working on some project / solving any programming problem, have you developed a new algorithm or utility that can help developers in a more efficient way?
So this blog i...]]></description><link>https://blogs.lakshaykumar.tech/publish-your-own-python-package</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/publish-your-own-python-package</guid><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[python libraries]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python projects]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Thu, 03 Nov 2022 17:36:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1667496956214/7h3X1D_YG.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello folks, 
You might have seen some of my packages in the previous <a target="_blank" href="https://blogs.lakshaykumar.tech/computer-vision-made-easy">blog</a>
While working on some project / solving any programming problem, have you developed a new algorithm or utility that can help developers in a more efficient way?
So this blog is for you! This blog will discuss <strong>How can you publish your own python package?</strong>
So Let's get started.</p>
<p>There are few terms you should be familiar with -</p>
<ul>
<li><a target="_blank" href="https://www.geeksforgeeks.org/python-classes-and-objects/"><code>class</code></a> : Blueprint of object. Object is simply a collection of variables and functions.</li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/self-in-python-class/"><code>self</code></a> : Python keyword, a parameter refers to the current instance of the class.</li>
<li><a target="_blank" href="https://www.geeksforgeeks.org/__init__-in-python/"><code>__init__()</code></a> : A function that runs by default whenever a class is executed.</li>
<li><a target="_blank" href="https://docs.python.org/3/reference/import.html"><code>import</code></a> : Keyword to include package in our program</li>
</ul>
<p>The links mentioned along with the keywords will help you know more, I have added a summary for your reference above.</p>
<p>In this blog, we will <strong>create a simple package that can add numbers from any data structure</strong>. I'll be discussing the following ways of doing it.</p>
<ul>
<li>Direct Function Implementation</li>
<li>Class Usage</li>
<li>Through Function of Class</li>
<li>Through the external class of package</li>
</ul>
<p>We are going to publish our package on <a target="_blank" href="https://pypi.org/">pypi.org</a> - A popular collection of all the libraries. First, create an account here and save the login credentials.</p>
<p>Now let's set up a project. Make sure your project name matches the name of the package you intend to publish. I'll be using the different package name for each way I mentioned above.</p>
<h3 id="heading-direct-function-implementation">Direct Function Implementation</h3>
<p>Project Name: <strong>addition_by_direct_function_implementation</strong>
Inside this project, create one more subfolder with the same name and one python file <code>main.py</code>. Inside the subfolder create one python file <code>__init__.py</code></p>
<p><strong>Explanation</strong>: We will test our code in <code>main.py</code>, <code>__init__.py</code> is the python script that will run by default when we call our package from the project folder.</p>
<p>Your file structure should look something like this.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667487588133/oJSIJ8HR7.png" alt="image.png" /></p>
<p><strong><code>__init__.py</code></strong>
Here we have simply defined a function <code>addnumbers</code> with the numbers as parameter. The function returns the sum. Here's the code</p>
<pre><code>def addnumbers(numbersdatastructure):
    add=<span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> numbersdatastructure:
        add+=i
    <span class="hljs-keyword">return</span> add
</code></pre><p>Now our function-based package is ready, let's test on our local system.
<code>main.py</code></p>
<pre><code><span class="hljs-keyword">from</span> addition_by_direct_function_implementation <span class="hljs-keyword">import</span> addnumbers

sum = addnumbers([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>,<span class="hljs-number">9</span>])

print(sum)
</code></pre><p>Since we have a subfolder with the name <strong>addition_by_direct_function_implementation</strong> its calling that folder and by default <code>__init__.py</code> gets called, its referring that code. Hence its calling function <code>addnumbers()</code> from <code>__init__.py</code> of <code>addition_by_direct_function_implementation</code> sub folder.</p>
<h3 id="heading-class-usage">Class Usage</h3>
<p>Project Name: <strong>addition_by_class_usage</strong>
Inside this project, create one more subfolder with the same name and one python file <code>main.py</code>. Inside the subfolder create one python file <code>__init__.py</code></p>
<p><strong>Explanation</strong>: We will test our code in <code>main.py</code>, <code>__init__.py</code> is the python script that will run by default when we call our package from the project folder.</p>
<p>Your file structure should look something like this.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667488558059/mQ6x9s1mI.png" alt="image.png" /></p>
<p><strong><code>__init__.py</code></strong>: Writing a class and within that class, we are creating 2 functions for multiplying and adding the numbers in the data structure.</p>
<pre><code><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">addition</span>():

    <span class="hljs-title">def</span> <span class="hljs-title">addthenumbers</span>(<span class="hljs-title">self</span>,<span class="hljs-title">numbersdatastructure</span>):
        <span class="hljs-title">self</span>.<span class="hljs-title">numbersdatastructure</span> </span>= numbersdatastructure
        add=<span class="hljs-number">0</span>
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> self.numbersdatastructure:
            add+=i
        <span class="hljs-keyword">return</span> add

    def multiplythenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        mul=<span class="hljs-number">1</span>
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> self.numbersdatastructure:
            mul*=i
        <span class="hljs-keyword">return</span> mul
</code></pre><p>Here the self keyword is referring to the variable of current function.</p>
<p>Now our function-based package is ready, let's test on our local system.
<code>main.py</code></p>
<pre><code><span class="hljs-keyword">import</span> addition_by_class_usage <span class="hljs-keyword">as</span> acu
objectOfClass = acu.addition()
addition = objectOfClass.addthenumbers([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
multiply = objectOfClass.multiplythenumbers([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
print(addition,<span class="hljs-string">"\n"</span>,multiply)
</code></pre><p>First I am importing the packing (here package name is project folder name with <code>__init__.py</code> and to make its usage easy in the code, I'll call it as <code>acu</code>.
Then I am creating object of class <code>acu</code> and then calling then simply functions</p>
<h3 id="heading-through-function-of-class">Through Function of Class</h3>
<p>Project Name: <strong>functions_in_class</strong>
Inside this project, create one more subfolder with the same name and one python file <code>main.py</code>. Inside the subfolder create one python file <code>__init__.py</code></p>
<p><strong>Explanation</strong>: We will test our code in <code>main.py</code>, <code>__init__.py</code> is the python script that will run by default when we call our package from the project folder.</p>
<p>Your file structure should look something like this.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667493012116/e-Y9E1WQh.png" alt="image.png" /></p>
<p><code>__init__.py</code></p>
<pre><code>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">addition</span>():

    <span class="hljs-title">def</span> <span class="hljs-title">addthenumbers</span>(<span class="hljs-title">self</span>,<span class="hljs-title">numbersdatastructure</span>):
        <span class="hljs-title">self</span>.<span class="hljs-title">numbersdatastructure</span> </span>= numbersdatastructure
        add=<span class="hljs-number">0</span>
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> self.numbersdatastructure:
            add+=i
        <span class="hljs-keyword">return</span> add

    def multiplythenumbers(self,numbersdatastructure):
        self.numbersdatastructure = numbersdatastructure
        mul=<span class="hljs-number">1</span>
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> self.numbersdatastructure:
            mul*=i
        <span class="hljs-keyword">return</span> mul


def main():
    print(<span class="hljs-string">"Main function called"</span>)


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    main()
</code></pre><p>Here we have defined additional function <code>main()</code> and in that function I am just printing a statement.</p>
<p><code>main.py</code></p>
<pre><code><span class="hljs-keyword">import</span> functions_in_class <span class="hljs-keyword">as</span> acu
<span class="hljs-keyword">from</span> functions_in_class <span class="hljs-keyword">import</span> main

objectOfClass = acu.addition()

addition = objectOfClass.addthenumbers([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
multiply = objectOfClass.multiplythenumbers([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>])
print(addition,<span class="hljs-string">"\n"</span>,multiply)
main()
</code></pre><p>Now we are calling the function <code>main()</code> from the class <code>functions_in_class</code>. Here it will ignore the class and directly call the function.</p>
<h3 id="heading-through-the-external-class-of-package">Through the external class of package</h3>
<p>Here the import syntax is just like direct function implementation, but instead of function, we are calling a python file that have the code.
For example </p>
<pre><code><span class="hljs-keyword">from</span> packagename <span class="hljs-keyword">import</span> classname
</code></pre><p>Here packagename is the name of folder and classname is another python file in the folder other than <code>__init__.py</code></p>
<p>For any queries, feel free to ask in comments or contact me over <a target="_blank" href="mailto:contact@lakshaykumar.tech">email</a></p>
<h3 id="heading-publishing-python-package">Publishing Python Package</h3>
<p>So now, we are ready with our package, lets publish. We'll be publishing on test pypi.org because this is only for testing purposes.</p>
<p><strong>Create</strong> Liscence.txt file : Refrence : <a target="_blank" href="https://choosealicense.com/">https://choosealicense.com/</a><br />
<strong>Create</strong> README.md : Reference : <a target="_blank" href="https://dillinger.io/">https://dillinger.io/</a>
<br />
<strong>Create setup.py</strong> and add the following code. Replace with your own details
Reference : geekforgeeks.org</p>
<pre><code><span class="hljs-keyword">import</span> setuptools

<span class="hljs-keyword">with</span> open(<span class="hljs-string">"README.md"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> fh:
    long_description = fh.read()

setuptools.setup(
    # Here is the <span class="hljs-built_in">module</span> name.
    name=<span class="hljs-string">"addition_by_class_usage"</span>,

    # version <span class="hljs-keyword">of</span> the <span class="hljs-built_in">module</span>
    version=<span class="hljs-string">"0.0.1"</span>,

    # Name <span class="hljs-keyword">of</span> Author
    author=<span class="hljs-string">"Lakshay Kumar"</span>,

    # your Email address
    author_email=<span class="hljs-string">"contact@lakshaykumar.tech"</span>,

    # #Small Description about <span class="hljs-built_in">module</span>
    # description=<span class="hljs-string">"adding number"</span>,

    # long_description=long_description,

    # Specifying that we are using markdown file <span class="hljs-keyword">for</span> description
    long_description=long_description,
    long_description_content_type=<span class="hljs-string">"text/markdown"</span>,




    # <span class="hljs-keyword">if</span> <span class="hljs-built_in">module</span> has dependencies i.e. if your package rely on other package at pypi.org
    # then you must add there, <span class="hljs-keyword">in</span> order to download every requirement <span class="hljs-keyword">of</span> package
     install_requires=[ ],
#Write packages that needs to be preinstalled <span class="hljs-keyword">for</span> using <span class="hljs-built_in">this</span> package.


    license=<span class="hljs-string">"MIT"</span>,

    # classifiers like program is suitable <span class="hljs-keyword">for</span> python3, just leave <span class="hljs-keyword">as</span> it is.
    classifiers=[
        <span class="hljs-string">"Programming Language :: Python :: 3"</span>,
        <span class="hljs-string">"License :: OSI Approved :: MIT License"</span>,
        <span class="hljs-string">"Operating System :: OS Independent"</span>,
    ],
)
</code></pre><p>Your folder structure should look like this.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667495962959/SMPzQLij8.png" alt="image.png" /></p>
<p>Now to install a package called twine using this line -</p>
<pre><code>pip install twine
</code></pre><p>Then</p>
<pre><code>python setup.py bdist_wheel
</code></pre><p>You'll see more folders like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667496056540/etZViihMb.png" alt="image.png" /></p>
<p>Upload to test env.</p>
<pre><code>twine upload -r testpypi dist<span class="hljs-comment">/*</span>
</code></pre><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1667496153718/TtM-GjccW.png" alt="image.png" /></p>
<p>Woohoo!
You finally uploaded it to the test environment. To upload in production environment, use this command</p>
<pre><code>twine upload dist<span class="hljs-comment">/*</span>
</code></pre><p>You'll see your project link at the bottom</p>
<h3 id="heading-test">Test</h3>
<p>To test, go to project link and copy the pip command. In my case its - <br />
<code>pip install -i https://test.pypi.org/simple/ addition-by-class-usage</code></p>
<p>And install in your project.</p>
<h3 id="heading-update">Update</h3>
<p>To update make changes in your code and <strong>It is important to make changes in the version number</strong>. Then you can use the build and upload again to make changes.</p>
<p>That's it. For any queries, feel free to comment or contact me over email.
Don't forget to subscribe on my website - https://www.lakshaykumar.tech/</p>
]]></content:encoded></item><item><title><![CDATA[Computer Vision On Web]]></title><description><![CDATA[Have you ever wondered about running your computer vision program on your website? Maybe you are working with Django or flask to create a web app and now you want to deploy it on your website.
Well, it's straightforward to do it. You just need to hav...]]></description><link>https://blogs.lakshaykumar.tech/computer-vision-on-web</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/computer-vision-on-web</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[Computer Science]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[Python]]></category><category><![CDATA[Flask Framework]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Mon, 17 Oct 2022 13:07:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666011918946/XHux0aTPy.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever wondered about running your computer vision program on your website? Maybe you are working with Django or flask to create a web app and now you want to deploy it on your website.
Well, it's straightforward to do it. You just need to have a basic understanding of flask or Django. Don't worry, even if you don't have one, we will build a simple <strong>hand-tracking application using flask</strong>. So let's get started.</p>
<h3 id="heading-project-setup">Project setup</h3>
<p>I am using <a target="_blank" href="https://www.jetbrains.com/pycharm/download/">Pycharm Community Edition</a> here, personally my favourite for all my Python Projects. You can use any IDE for the same.</p>
<ol>
<li>Open Pycharm and create a project <code>flaskWebApp</code> (You can use any name here).</li>
<li>After the project indexing is done, you will see one folder <code>venv</code> and <code>main.py</code> with some code written in it.</li>
<li>Delete all the code in <code>main.py</code> and rename it to <code>app.py</code> (This is the basic flask project naming convention, let's follow this only).</li>
<li>Create two more folders <code>templates</code> and <code>static</code> in the same project <code>flaskWebApp</code>.</li>
<li>Finally we have set up a basic flask web app structure here. Your project should look something like this.</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666008724584/jg3gjyqrc.png" alt="image.png" /></p>
<p><strong>Explanation</strong><br />
<code>static</code>: This folder will have external static files like your CSS, images, JavaScript files  <br />
<code>templates</code>: This folder contains your main HTML files where they have to be changed.<br />
<code>app.py</code>: Python file to generate templates (dynamic html files in templates folder).</p>
<h3 id="heading-install-packages">Install packages</h3>
<p>We need some of the packages to be installed in order to get the desired output. Here are the packages you need <a target="_blank" href="https://pypi.org/project/Flask/"><code>flask</code></a> (web development framework with python), <a target="_blank" href="https://pypi.org/project/opencv-python/"><code>opencv-python</code></a> (Computer vision library for python), <a target="_blank" href="https://pypi.org/project/lkhandmapping/"><code>lkhandmapping</code></a> (For tracking hand without writing tones of code). Use the syntax given below in the Pycharm terminal.</p>
<pre><code>pip install flask
pip install opencv-python
pip install lkhandmapping
</code></pre><p>###Let's Code</p>
<ol>
<li>Create an <code>index.html</code> file in <code>templates</code> folder and write the following code.</li>
</ol>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="container"&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-md-12"&gt;
            &lt;img src="{{ url_for('video_feed') }}" width="70%" height="600px"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre><ol>
<li>Open <code>app.py</code> and add the following code</li>
</ol>
<pre><code><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template, Response

app = Flask(__name__)


@app.route(<span class="hljs-string">'/'</span>)
def index():
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'index.html'</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=True)
</code></pre><ol>
<li>When you run <code>app.py</code> you will see a local IP address in the terminal like this.</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666009881738/1bGfoWOkC.png" alt="image.png" />
Click on the IP address.
You will see a blank webpage. </p>
<p><strong>What's happening here....</strong>
The function <code>@app.route('/')</code> is defining the home URL of your project. So whenever you are at the home of your project, the function <code>index()</code> will run. In this function, we are just rendering the <code>index.html</code> file that has nothing as of now.<br />
By default running the <code>index()</code> function.</p>
<ol>
<li>Now import the necessary packages and add some additional functions to make our code work.</li>
</ol>
<pre><code><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template, Response
<span class="hljs-keyword">import</span> cv2
<span class="hljs-keyword">from</span> lkhandmapping <span class="hljs-keyword">import</span> handTracker

app = Flask(__name__)

camera = cv2.VideoCapture(<span class="hljs-number">0</span>)

@app.route(<span class="hljs-string">'/'</span>)
def index():
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'index.html'</span>)

@app.route(<span class="hljs-string">'/video_feed'</span>)
def video_feed():
    <span class="hljs-keyword">return</span> Response(gen_frames(), mimetype=<span class="hljs-string">'multipart/x-mixed-replace; boundary=frame'</span>)

def gen_frames():
    <span class="hljs-keyword">while</span> True:
        success, frame = camera.read()  # read the camera frame
        <span class="hljs-keyword">if</span> not success:
            <span class="hljs-keyword">break</span>
        <span class="hljs-attr">else</span>:
            frame = handTracker(frame)
            ret, buffer = cv2.imencode(<span class="hljs-string">'.jpg'</span>, frame[<span class="hljs-number">0</span>])
            frame = buffer.tobytes()
            <span class="hljs-keyword">yield</span> (b<span class="hljs-string">'--frame\r\n'</span>
                   b<span class="hljs-string">'Content-Type: image/jpeg\r\n\r\n'</span> + frame + b<span class="hljs-string">'\r\n'</span>)


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    app.run(debug=True)
</code></pre><p><strong>Explanation</strong><br />
Here we are getting video by changing the image continuously in image tag in our HTML file.The line <code>&lt;img src="{{ url_for('video_feed') }}" width="70%" height="600px"&gt;</code> in <code>index.html</code>. 
<br />
Here the line <code>{{ url_for('video_feed') }}</code> is specifying the URL of the video feed that we are sending as the response from our python file by triggering the function <code>gen_frames()</code> in our video_feed. Let's see what this function is doing. Here is the code with comments as the explanation.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1666011249663/Qb5kaDdMu.png" alt="image.png" /></p>
<p>I hope the syntax is clear. If you have any queries, feel free to ask in the comments, or you can contact me <a target="_blank" href="https://lakshaykumar.tech/#contact">here</a></p>
<p>Don't forget to subscribe on my website - <a target="_blank" href="https://www.lakshaykumar.tech/">https://www.lakshaykumar.tech/</a></p>
]]></content:encoded></item><item><title><![CDATA[Flights Delay Prediction]]></title><description><![CDATA[You must have faced flight delays ever in your life if you are a frequent flight traveller. This might have caused you a lot of trouble, especially when you are running on a tight or busy schedule.
To address the same problem my team - Aryan Bakle, A...]]></description><link>https://blogs.lakshaykumar.tech/flights-delay-prediction</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/flights-delay-prediction</guid><category><![CDATA[Data Science]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Sat, 01 Oct 2022 07:08:11 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/YbtHsy0g_To/upload/v1664607999179/MDb9b8qjZ.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You must have faced flight delays ever in your life if you are a frequent flight traveller. This might have caused you a lot of trouble, especially when you are running on a tight or busy schedule.
To address the same problem my team - <a href="https://www.calmcanfly.com/">Aryan Bakle</a>, <a href="mailto:saxena.a@icloud.com">Aryan Saxena</a> and <a href="https://www.lakshaykumar.tech/">me</a> have prepared this project for prediction of flight delays on <strong>India's Busiest air Route: DEL to BOM</strong></p>
<p>We started off with some research. We observed that on this route 5 major airlines - SpiceJet, Vistara, Indigo, Go First and AirIndia are operating approx. 36 flights daily.
Data Source - <a href="https://www.skyscanner.co.in/">SkyScanner</a></p>
<p>Using Selenium and Python we scraped the data for all the flights and their delay history for the past 100 days. Source : <a href="https://www.flightradar24.com/22.73,75.8/17">FlightRadar24</a>
The above method gave us very <a href="https://docs.google.com/spreadsheets/d/1UmHkMcir-to_cZgy_ne49deK2KojwjytXWwTcSGYs7Q/edit?usp=sharing">raw data</a> with tones of missing values and non-segregated data.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664605639670/aNjuyOjmj.png" alt="image.png" /></p>
<p>After cleaning manually, here is what we came up with - </p>
<p><a href="https://docs.google.com/spreadsheets/d/1DjyhyKEqXRcgd_XmmgDM4ZFaCDbLDF1BzZwq-YsTW6o/edit?usp=sharing">Cleaned data -</a> 
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664605788421/LmFDCeiTX.png" alt="image.png" /></p>
<p>After doing some basic measures of central tendency, here's what we came up with -</p>
<ul>
<li>On average, 20% of total flights were delayed with an average delay time of 27 minutes. (not much)</li>
<li>The maximum delay was 327 minutes ~ 5 hours for Flight number SG8169 (SpiceJet)</li>
<li>From the past data, we saw that the maximum flights were delayed on Thursdays and Fridays. Maybe because people travel more on weekends than weekdays, flights were less booked.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664606568440/rK6HK3nKY.png" alt="image.png" /></p>
<p>Probability Distribution for flights delay</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664607329772/zXSQWcjdY.png" alt="image.png" /></p>
<p>We segregated the cleaned sheet into multiple sheets, specific for each airline and flight number. We got a total of <strong>53 individual excel sheets for analysis</strong>. [Cheers to our team]</p>
<p>Later we tried Logistic regression on our dataset to check if a flight (based on the number and past history) is delayed or not. Here's the confusion Matrix of our model</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664606658349/TKzrwTIcj.png" alt="image.png" /></p>
<p>We see that there were no predicted true values. This might be because we did the analysis on past data i.e. identifying patterns, not the actual factors causing flight delays i.e. weather conditions, delays due to passenger's arrival time, technical issues, airline's carelessness etc. Our <strong>logistic regression model score was 0.7747</strong> which I think is quite enough. Here's our google colab sheet - 
%[https://colab.research.google.com/drive/17eTmUZG65MuoLQkz8Jq3B5W-DaQCRGd4?usp=sharing]</p>
<p>In the end, we created a dashboard in google sheet where based on flight and airline history, The system was able to display the probability of that particular flight being delayed more than a threshold time input by the user.
For example - Analysis for <strong>Go-First Flight No. G8336</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1664607502474/lgywFBwD5.png" alt="image.png" /></p>
<p>We will get all the analysis of airline and flight numbers. In this picture, the Probability of this flight for 30 minutes delay is 58.33% on Friday.</p>
<p>Thanks to our mentors <a href="https://www.linkedin.com/in/saurabh-mahajan-b6583315/">Saurabh Mahajan</a>, <a href="https://www.linkedin.com/in/mathew-george-826a8186">Mathew George</a> and <a href="https://www.linkedin.com/in/vishrut-y-patel/">Vishrut Patel</a> from <a href="https://www.atriauniversity.edu.in">Atria University</a> for their guidance throughout the project.</p>
<p>Do let me know in the comments for the scope of further improvements. </p>
]]></content:encoded></item><item><title><![CDATA[TripGo DALV]]></title><description><![CDATA[Did you ever get confused while choosing a destination for a crazy outing with your squad?
There might be a situation when someone wants to go shopping, some like natural places and what about the spiritual peeps. Finding it challenging to select a l...]]></description><link>https://blogs.lakshaykumar.tech/tripgo-dalv</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/tripgo-dalv</guid><category><![CDATA[Python]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Mon, 19 Sep 2022 15:32:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663601297828/fMl3dy4_L.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Did you ever get confused while choosing a destination for a crazy outing with your squad?
There might be a situation when someone wants to go shopping, some like natural places and what about the spiritual peeps. Finding it challenging to select a location that fulfils each and every one's preference. I was facing the same problem, so thought of solving this problem using python.</p>
<h3 id="heading-problem">Problem</h3>
<p>How to find a location for a casual outing with your group, in Bangalore City.</p>
<h3 id="heading-approach">Approach</h3>
<p>This is a kind of simple recommendation system, where based on preference, it will recommend places. Here we have different preferences like <strong>Nature, Adventure, Fun &amp; Thrill, Shopping and Cultural</strong>
We started by collecting data from the web and manually rating each place based on these preferences out of 5. Here's an example below - </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663599708949/MPhitIjhA.png" alt="image.png" /></p>
<p>We collected the data of around 60 top places within Bangalore city.</p>
<p>On the frontend form, we are collecting values of these 5 parameters through a frontend form developed in basic HTML/CSS</p>
<h3 id="heading-tech-solution">Tech Solution</h3>
<p>The frontend form was connected with python using Flask Framework. The data received from the form was converted into a list. </p>
<pre><code>nature <span class="hljs-operator">=</span> <span class="hljs-keyword">int</span>(request.form[<span class="hljs-string">'nature'</span>])
adventure <span class="hljs-operator">=</span> <span class="hljs-keyword">int</span>(request.form[<span class="hljs-string">'adventure'</span>])
fun <span class="hljs-operator">=</span> <span class="hljs-keyword">int</span>(request.form[<span class="hljs-string">'fun'</span>])
shopping <span class="hljs-operator">=</span> <span class="hljs-keyword">int</span>(request.form[<span class="hljs-string">'shopping'</span>])
cultural <span class="hljs-operator">=</span> <span class="hljs-keyword">int</span>(request.form[<span class="hljs-string">'cultural'</span>])

userPref <span class="hljs-operator">=</span> [nature, adventure, fun, shopping, cultural]
</code></pre><p>Now we are creating a nested list of travel places</p>
<pre><code>df = pd.read<span class="hljs-emphasis">_csv('URL_</span>OF<span class="hljs-emphasis">_OUR SHEET')
travelplaces = []
for i in df.index:
        rows = [<span class="hljs-string">df['place'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'nature'</span>][<span class="hljs-symbol">i</span>],df[<span class="hljs-string">'adventure'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'funAndThrill'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'shopping'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'cultural'</span>][<span class="hljs-symbol">i</span>],
                df[<span class="hljs-string">'budget'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'Distance'</span>][<span class="hljs-symbol">i</span>], df[<span class="hljs-string">'ratings'</span>][<span class="hljs-symbol">i</span>]]
        travelplaces.append(rows)</span>
</code></pre><p>Now finding the <a target="_blank" href="https://itsmycode.com/calculate-euclidean-distance-in-python/">vector distance</a> for each place and finding vector distance using <code>np.linalg.norm</code> for each place and storing into a dictionary.</p>
<pre><code><span class="hljs-keyword">for</span> i in travelplaces:
        user <span class="hljs-operator">=</span> np.array(userInput)
        dbData <span class="hljs-operator">=</span> np.array(i[<span class="hljs-number">1</span>:<span class="hljs-number">8</span>])

        dis <span class="hljs-operator">=</span> np.linalg.norm(user <span class="hljs-operator">-</span> dbData)
        placedict[i[<span class="hljs-number">0</span>]] <span class="hljs-operator">=</span> dis
sorted_dict <span class="hljs-operator">=</span> {}
sorted_keys <span class="hljs-operator">=</span> sorted(placedict, key<span class="hljs-operator">=</span>placedict.get)
</code></pre><p>Finally sorting the dictionary in ascending order of vector distance and finding the top three recommendations. Displaying the same to user.
This system also sends an email to the user, in case they want to save for future reference.
Here's the complete code :
%[https://github.com/laksh-2193/TripgoDalv]</p>
<p>Cheers to my team <a target="_blank" href="https://www.calmcanfly.com">Aryan Bakle</a>, <a target="_blank" href="mailto:diyahafiz098@gmail.com">Diya hafiz</a> and <a target="_blank" href="https://www.linkedin.com/in/vishwa-shah-ab2a2620b/">Vishwa Shah</a> for their effort.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663601441469/KCAcPwdxr.png" alt="image.png" /></p>
<p>Thanks to our mentor <a target="_blank" href="https://www.linkedin.com/in/saurabh-mahajan-b6583315/">Saurabh Mahajan</a> from <a target="_blank" href="https://www.atriauniversity.edu.in/">Atria University</a> for his guidance throughout the project.</p>
<p>Do let me know in comments for the scope of further improvements.</p>
]]></content:encoded></item><item><title><![CDATA[Sentiment Analysis using Python]]></title><description><![CDATA[Deep learning is the first choice when you want to train neural networks for high-end Machine Level projects. It is the subset of machine learning where the neural networks learn by observing intricate structures in the data that they experience, thi...]]></description><link>https://blogs.lakshaykumar.tech/sentiment-analysis-using-python</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/sentiment-analysis-using-python</guid><category><![CDATA[Python]]></category><category><![CDATA[neural networks]]></category><category><![CDATA[Deep Learning]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Sat, 17 Sep 2022 17:05:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663430703768/q8L5sV9X9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Deep learning is the first choice when you want to train neural networks for high-end Machine Level projects. It is the subset of machine learning where the neural networks learn by observing intricate structures in the data that they experience, this includes statistics and predictive modelling. Deep learning model comprises computational models consisting of multiple neural layers that build up the networks at multiple layers of abstraction to represent the data.
In this blog, we are going to learn how can we build a model for predicting human sentiments. We will train models on hundreds of images available online. So lets get started.</p>
<h2 id="heading-file-structure">File Structure</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663431769340/KXw80NroG.png" alt="image.png" /></p>
<p>Download this <a target="_blank" href="https://github.com/laksh-2193/SentimentsAnalysis/tree/master/data">data folder</a> that have images for training and put it in the main project.</p>
<p>Make sure you download this <a target="_blank" href="https://github.com/laksh-2193/SentimentsAnalysis/blob/master/haarcascade_frontalface_default.xml">haarcascade_frontalface_default.xml</a> file that will help in detecting face.</p>
<p>These files will be generated as our code compiles, so don't worry about them</p>
<ol>
<li>Code.ipynb (Not required)</li>
<li>model.h5</li>
<li>sentimentAnalyser.h5</li>
</ol>
<h2 id="heading-lets-code">Lets Code</h2>
<p>Create a python file <a target="_blank" href="https://github.com/laksh-2193/SentimentsAnalysis/blob/master/main.py">main.py</a> and install the following libraries :</p>
<ol>
<li>OpenCV</li>
<li>Numpy</li>
<li>Keras</li>
</ol>
<p>Import all the required libraries </p>
<pre><code><span class="hljs-keyword">from</span> keras.preprocessing.image <span class="hljs-keyword">import</span> <span class="hljs-title">ImageDataGenerator</span>
<span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title">keras</span>.<span class="hljs-title">models</span> <span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">Sequential</span>
<span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title">keras</span>.<span class="hljs-title">layers</span> <span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">Dense</span>,<span class="hljs-title">Dropout</span>,<span class="hljs-title">Flatten</span>
<span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title">keras</span>.<span class="hljs-title">layers</span> <span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">Conv2D</span>,<span class="hljs-title">MaxPool2D</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">os</span>
</code></pre><p>Declare the location for test and training data folders</p>
<pre><code><span class="hljs-attr">train_data_dir</span> = <span class="hljs-string">'data/train'</span>
<span class="hljs-attr">validation_data_dir</span> = <span class="hljs-string">'data/test'</span>
</code></pre><p>Since all the images are coloured, we need to scale them with respect to the 255 RGB value. In addition to that, we are adding some more parameters like rotation, fixing a dimension for a better training dataset</p>
<pre><code>train_datagen <span class="hljs-operator">=</span> ImageDataGenerator(rescale<span class="hljs-operator">=</span><span class="hljs-number">1.</span><span class="hljs-operator">/</span><span class="hljs-number">255</span>,rotation_range<span class="hljs-operator">=</span><span class="hljs-number">30</span>,shear_range<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-number">.3</span>,zoom_range<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-number">.3</span>, horizontal_flip<span class="hljs-operator">=</span>True,fill_mode<span class="hljs-operator">=</span><span class="hljs-string">'nearest'</span>)
validation_datagen <span class="hljs-operator">=</span> ImageDataGenerator(rescale<span class="hljs-operator">=</span><span class="hljs-number">1.</span><span class="hljs-operator">/</span><span class="hljs-number">255</span>)
</code></pre><p>Now we are creating two objects different for training and testing datasets. It takes the image directory e.g. <code>train_datagen</code> as input, converts to <code>grayscale</code>, fixes the dimension <code>(48,48)</code>, since we are categorising image into angry, disgust, fear etc. so we our operation on all these classes are going to be <code>categorical</code>. We will <code>shuffle</code> images for better training</p>
<pre><code>train_genrator <span class="hljs-operator">=</span> train_datagen.flow_from_directory(
    train_data_dir,
    color_mode<span class="hljs-operator">=</span><span class="hljs-string">'grayscale'</span>,
    target_size<span class="hljs-operator">=</span>(<span class="hljs-number">48</span>,<span class="hljs-number">48</span>),
    batch_size<span class="hljs-operator">=</span><span class="hljs-number">32</span>,
    class_mode<span class="hljs-operator">=</span><span class="hljs-string">'categorical'</span>,
    shuffle<span class="hljs-operator">=</span>True
)

validation_genrator <span class="hljs-operator">=</span> validation_datagen.flow_from_directory(
    validation_data_dir,
    color_mode<span class="hljs-operator">=</span><span class="hljs-string">'grayscale'</span>,
    target_size<span class="hljs-operator">=</span>(<span class="hljs-number">48</span>, <span class="hljs-number">48</span>),
    batch_size<span class="hljs-operator">=</span><span class="hljs-number">32</span>,
    class_mode<span class="hljs-operator">=</span><span class="hljs-string">'categorical'</span>,
    shuffle<span class="hljs-operator">=</span>True
)
</code></pre><p>Now we will declare the list of labels in which we will classify images</p>
<pre><code><span class="hljs-attr">class_labels</span> = [<span class="hljs-string">'Angry'</span>,<span class="hljs-string">'Disgust'</span>,<span class="hljs-string">'Fear'</span>,<span class="hljs-string">'Happy'</span>,<span class="hljs-string">'Neutral'</span>,<span class="hljs-string">'Sad'</span>,<span class="hljs-string">'Surprise'</span>]
</code></pre><p>Creating a neural network of 5 layers. The last layer will result in the probability of all 7 classification classes and the max probability will be the final sentiment. Since we want a linear neural network i.e. a stack of layers we will use <code>Sequential()</code> model. <a target="_blank" href="https://keras.io/api/layers/convolution_layers/convolution2d/"><code>Convo2D</code></a> produces a matrix based on <code>kernal_size</code> and <a target="_blank" href="https://keras.io/api/layers/activations/#relu-function"><code>relu</code></a> <code>activation</code> function. <a target="_blank" href="https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D"><code>MaxPool2D</code></a> finds the feature with maximum value for each matrix referenced as <code>pool_size</code> . Finally <code>Dropout</code> is the number of layer that nullifies the node of current layer before doing to next.</p>
<pre><code>model <span class="hljs-operator">=</span> Sequential()
model.add(Conv2D(<span class="hljs-number">32</span>,kernel_size<span class="hljs-operator">=</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),activation<span class="hljs-operator">=</span><span class="hljs-string">'relu'</span>,input_shape<span class="hljs-operator">=</span>(<span class="hljs-number">48</span>,<span class="hljs-number">48</span>,<span class="hljs-number">1</span>)))

model.add(Conv2D(<span class="hljs-number">64</span>,kernel_size<span class="hljs-operator">=</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),activation<span class="hljs-operator">=</span><span class="hljs-string">'relu'</span>))
model.add(MaxPool2D(pool_size<span class="hljs-operator">=</span>(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)))
model.add(Dropout(<span class="hljs-number">0</span><span class="hljs-number">.1</span>))

model.add(Conv2D(<span class="hljs-number">128</span>,kernel_size<span class="hljs-operator">=</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),activation<span class="hljs-operator">=</span><span class="hljs-string">'relu'</span>))
model.add(MaxPool2D(pool_size<span class="hljs-operator">=</span>(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)))
model.add(Dropout(<span class="hljs-number">0</span><span class="hljs-number">.1</span>))

model.add(Conv2D(<span class="hljs-number">256</span>,kernel_size<span class="hljs-operator">=</span>(<span class="hljs-number">3</span>,<span class="hljs-number">3</span>),activation<span class="hljs-operator">=</span><span class="hljs-string">'relu'</span>))
model.add(MaxPool2D(pool_size<span class="hljs-operator">=</span>(<span class="hljs-number">2</span>,<span class="hljs-number">2</span>)))
model.add(Dropout(<span class="hljs-number">0</span><span class="hljs-number">.1</span>))

model.add(Flatten())
model.add(Dense(<span class="hljs-number">512</span>,activation<span class="hljs-operator">=</span><span class="hljs-string">'relu'</span>))
model.add(Dropout(<span class="hljs-number">0</span><span class="hljs-number">.2</span>))

model.add(Dense(<span class="hljs-number">7</span>,activation<span class="hljs-operator">=</span><span class="hljs-string">'softmax'</span>))
</code></pre><p>Here <code>Dense</code> layer classifies images based on output from convolutional layers. <code>Flatten</code> reshapes the layer into a single matrix.</p>
<p>Finally we will compile the model with certain parameters and see the summary for how it worked</p>
<pre><code>model.compile(optimizer<span class="hljs-operator">=</span><span class="hljs-string">'adam'</span>,loss<span class="hljs-operator">=</span><span class="hljs-string">'categorical_crossentropy'</span>,metrics<span class="hljs-operator">=</span>[<span class="hljs-string">'accuracy'</span>])
print(model.summary())
</code></pre><p>Now we are going to train the images on our neural network and save the mode as <code>sentimentanalyser.h5</code></p>
<pre><code>num_train_images<span class="hljs-operator">=</span><span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> root, dirs, files in os.walk(train_path):
    num_train_images<span class="hljs-operator">+</span><span class="hljs-operator">=</span>len(files)

num_test_images<span class="hljs-operator">=</span><span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> root, dirs, files in os.walk(test_path):
    num_test_images<span class="hljs-operator">+</span><span class="hljs-operator">=</span>len(files)


epochs<span class="hljs-operator">=</span><span class="hljs-number">100</span>
history <span class="hljs-operator">=</span> model.fit(train_genrator, steps_per_epoch<span class="hljs-operator">=</span>num_train_images<span class="hljs-comment">//32,</span>
                    epochs<span class="hljs-operator">=</span>epochs,
                    validation_data<span class="hljs-operator">=</span>validation_genrator,
                    validation_steps<span class="hljs-operator">=</span>num_test_images<span class="hljs-comment">//32)</span>

model.save(<span class="hljs-string">'sentimentanalyser.h5'</span>)
</code></pre><p>This step will take time, based on the number of <code>epochs</code>, it is the number of cycles for training the images.</p>
<p>So yay! you have finally created a CNN for detection of sentiments. Find the full code below.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://github.com/laksh-2193/SentimentsAnalysis">https://github.com/laksh-2193/SentimentsAnalysis</a></div>
]]></content:encoded></item><item><title><![CDATA[Python + Drones]]></title><description><![CDATA[You might have flown a Drone or a toy helicopter, it's super easy, isn't it? Use a controller, move joysticks and done. But as a coder, do you want to integrate this with your python code? Imagine you are making autonomous drones, How exciting, isn't...]]></description><link>https://blogs.lakshaykumar.tech/python-drones</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/python-drones</guid><category><![CDATA[Python]]></category><category><![CDATA[mediapipe]]></category><category><![CDATA[Computer Vision]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Fri, 16 Sep 2022 07:03:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663311936831/6LAfhnnaG.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>You might have flown a Drone or a toy helicopter, it's super easy, isn't it? Use a controller, move joysticks and done. But as a coder, do you want to integrate this with your python code? Imagine you are making autonomous drones, How exciting, isn't it?
Let's learn how can you integrate python and drones.<br /><br />
<strong>Pre-requisites</strong></p>
<ol>
<li>DJI Tello (A mini pocket-friendly drone perfect for beginners)  <div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.amazon.in/Renewed-DJI-Camera-Quadcopter-Professional/dp/B08VJ9PLFP/">https://www.amazon.in/Renewed-DJI-Camera-Quadcopter-Professional/dp/B08VJ9PLFP/</a></div>
</li>
<li>Prior Experience with Python programming (Basic)</li>
<li>Basics of motion (Newton's laws of motion &amp; Momentum)</li>
</ol>
<p>You can browse online for these pre-reqs if you want.
So let's get started...</p>
<p>Firstly you need to install a library <code>djitellopy</code> and <code>opencv-python</code> that has the support for this type of drones. Use the following syntax</p>
<blockquote>
<p>pip install djitellotpy<br />
pip install opencv</p>
</blockquote>
<p>NOTE: Every drone has its own library, but coding is more or less the same for every drone.</p>
<p>Let's start by importing the library</p>
<pre><code>from djitellopy <span class="hljs-keyword">import</span> tello
<span class="hljs-keyword">import</span> cv2
<span class="hljs-keyword">import</span> time
</code></pre><p>Create object of tello and make connection connect</p>
<pre><code>me <span class="hljs-operator">=</span> tello.Tello()
me.connect()
print(me.get_battery())
</code></pre><p>The above code will print the battery % of the drone. <code>me</code> is the object of drone</p>
<p>Put stream on using the following code</p>
<pre><code>me.streamon()
</code></pre><p>Controlling the movement of drone.
Function : <code>send_rc_control(left_right_velocity: int, forward_backward_velocity: int, up_down_velocity: int,
                        yaw_velocity: int)</code></p>
<p>Code Snippet for controlling motion of drone</p>
<pre><code><span class="hljs-attribute">speed</span> = <span class="hljs-number">30</span> # this is in cm/s
<span class="hljs-attribute">me</span>.takeoff() #Take <span class="hljs-literal">off</span>
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,speed,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm up
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,-speed,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm down
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,speed,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm forward
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,-speed,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm backward
<span class="hljs-attribute">me</span>.send_rc_control(speed,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm left
<span class="hljs-attribute">me</span>.send_rc_control(-speed,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>) #move <span class="hljs-number">30</span>cm right
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,speed) #rotate <span class="hljs-number">30</span>cm right
<span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,-speed) #rotate <span class="hljs-number">30</span>cm left
<span class="hljs-attribute">me</span>.land() #land the drone
</code></pre><p>Visualise the drone camera stream in your PC</p>
<pre><code><span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
        img = me.get_frame_read().frame <span class="hljs-comment">#Capture each frame</span>
        img = cv2.resize(img, (<span class="hljs-number">360</span>, <span class="hljs-number">240</span>)) <span class="hljs-comment">#Optional but recommended</span>
        cv2.imshow(<span class="hljs-string">"Image"</span>, img) <span class="hljs-comment">#Display video</span>
        cv2.waitKey(<span class="hljs-number">1</span>)
</code></pre><p>That's super duper easy, try it out and let me know in the comments...</p>
<p>Any queries, feel free to type in comments or connect me through <a target="_blank" href="mailto:contact@lakshaykumar.tech">mail</a></p>
]]></content:encoded></item><item><title><![CDATA[Control drone using finger gesture]]></title><description><![CDATA[Mediapipe has a lot of amazing computer vision modules. Among all, my personal favourite is Hand Tracking Module. You can do a lot of customization using this module and play around, it's literally fun.
So in this blog, let's see how we can control t...]]></description><link>https://blogs.lakshaykumar.tech/control-drone-using-finger-gesture</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/control-drone-using-finger-gesture</guid><category><![CDATA[Computer Vision]]></category><category><![CDATA[drone]]></category><category><![CDATA[media queries]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Fri, 16 Sep 2022 06:25:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/unsplash/72AYEEBJpz4/upload/v1663306808183/9SB0gZLYv.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Mediapipe has a lot of amazing computer vision modules. Among all, my personal favourite is <strong>Hand Tracking Module</strong>. You can do a lot of customization using this module and play around, it's literally fun.
So in this blog, let's see how we can control the drone using finger count. Here are the gestures and commands I am going to add to the drone - </p>
<blockquote>
<p>Zero Fingers (Fist) : <strong>Land the drone</strong><br />
One Finger up : <strong>Move Forward</strong><br />
Two Fingers up : <strong>Move Backward</strong><br />
Three Fingers up : <strong>Move Left</strong><br />
Four Fingers up : <strong>Move Right</strong><br />
Five Fingers up : <strong>Take off</strong></p>
</blockquote>
<p>So let's start</p>
<p>Firstly we'll import all the required libraries - <strong>opencv, mediapipe, djitellopy</strong></p>
<pre><code><span class="hljs-keyword">import</span> cv2
<span class="hljs-keyword">import</span> mediapipe <span class="hljs-keyword">as</span> mp
<span class="hljs-title">from</span> djitellopy <span class="hljs-keyword">import</span> tello
</code></pre><p>Making constructor for media pipe and hand tracking modules, here we are using only one hand for the gestures, multiple hands can affect the accuracy of the output</p>
<pre><code>mp_drawing <span class="hljs-operator">=</span> mp.solutions.drawing_utils
mp_drawing_styles <span class="hljs-operator">=</span> mp.solutions.drawing_styles
mp_hands <span class="hljs-operator">=</span> mp.solutions.hands
hands <span class="hljs-operator">=</span> mp_hands.Hands(model_complexity<span class="hljs-operator">=</span><span class="hljs-number">0</span>,min_detection_confidence<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-number">.5</span>,min_tracking_confidence<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-number">.5</span>,max_num_hands<span class="hljs-operator">=</span><span class="hljs-number">1</span>)
</code></pre><p>Then we are using webcam in <code>cap</code> variable and setting a good height / width according to the frame</p>
<pre><code>cap <span class="hljs-operator">=</span> cv2.VideoCapture(<span class="hljs-number">0</span>)
width <span class="hljs-operator">=</span> <span class="hljs-number">720</span>
height <span class="hljs-operator">=</span> <span class="hljs-number">280</span>
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
</code></pre><p>This is the time to connect to our dji tello drone and use its camera. Make sure your drone is switched on and connected to your system's wifi. Before this kindly install <code>djitellopy</code> library using this method</p>
<blockquote>
<p>pip install <code>djitellopy</code></p>
</blockquote>
<pre><code>me = tello.Tello()
me.<span class="hljs-keyword">connect</span>()
me.streamoff()
me.streamon()
isDroneFlying = <span class="hljs-keyword">False</span> #Intialising the variable <span class="hljs-keyword">to</span> <span class="hljs-keyword">check</span> <span class="hljs-keyword">if</span> drone <span class="hljs-keyword">is</span> flying <span class="hljs-keyword">or</span> <span class="hljs-keyword">not</span>
</code></pre><p>Defining a function which takes the parameter as input and count the number of fingers up in that frame if hands are detected. Here is an image for tracking the hand landmark</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663308308791/CLuahIUqw.png" alt="2410344[1].png" /></p>
<p>For each point, we have x and y values on the hand. Now here's a question <code>how will you say that all the fingers are closed in a fist? What's the condition or criteria?</code>
The tip must be lower than the middle ring of the finger. For e.g. here for the index finger, the 8th point must be lower than the 6th point. Since it's a vertical movement, if the y coordinate of the 8th point is greater than the y coordinate of the 6th point, then we can say that the index finger is closed. For all the fingers, we can apply the same logic and find the finger count. Then we can define our actions accordingly.
Here is the function, kindly drop your doubts in comments if you think something isn't working</p>
<pre><code>def droneGestureController(image):
    image.flags.writeable <span class="hljs-operator">=</span> False
    image <span class="hljs-operator">=</span> cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    results <span class="hljs-operator">=</span> hands.process(image)
    image.flags.writeable <span class="hljs-operator">=</span> True
    image <span class="hljs-operator">=</span> cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    <span class="hljs-keyword">if</span> results.multi_hand_landmarks:

        <span class="hljs-keyword">for</span> hand_landmarks in results.multi_hand_landmarks:
            mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
                                      mp_drawing_styles.get_default_hand_landmarks_style(),
                                      mp_drawing_styles.get_default_hand_connections_style())
            handlms <span class="hljs-operator">=</span> []

            c <span class="hljs-operator">=</span> <span class="hljs-number">0</span>
            <span class="hljs-keyword">for</span> i in hand_landmarks.landmark:
                height, width, fc <span class="hljs-operator">=</span> image.shape
                x <span class="hljs-operator">=</span> (i.x) <span class="hljs-operator">*</span> width
                y <span class="hljs-operator">=</span> (i.y) <span class="hljs-operator">*</span> height
                handlms.append([c, <span class="hljs-keyword">int</span>(x), <span class="hljs-keyword">int</span>(y)])
                c <span class="hljs-operator">=</span> c <span class="hljs-operator">+</span> <span class="hljs-number">1</span>
            totalFingers <span class="hljs-operator">=</span> <span class="hljs-number">0</span>

            <span class="hljs-keyword">if</span> (len(handlms) <span class="hljs-operator">!</span><span class="hljs-operator">=</span> <span class="hljs-number">0</span>):
                fingerTips <span class="hljs-operator">=</span> [<span class="hljs-number">8</span>, <span class="hljs-number">12</span>, <span class="hljs-number">16</span>, <span class="hljs-number">20</span>]
                <span class="hljs-keyword">if</span>(handlms[<span class="hljs-number">4</span>][<span class="hljs-number">1</span>]<span class="hljs-operator">&gt;</span>handlms[<span class="hljs-number">3</span>][<span class="hljs-number">1</span>]):
                    totalFingers<span class="hljs-operator">+</span><span class="hljs-operator">=</span><span class="hljs-number">1</span>

                <span class="hljs-keyword">for</span> i in fingerTips:
                    <span class="hljs-keyword">if</span> (handlms[i][<span class="hljs-number">2</span>] <span class="hljs-operator">&lt;</span> handlms[i <span class="hljs-operator">-</span> <span class="hljs-number">2</span>][<span class="hljs-number">2</span>]):
                        totalFingers <span class="hljs-operator">+</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>

            droneAction <span class="hljs-operator">=</span> <span class="hljs-string">""</span>

            <span class="hljs-keyword">if</span> (totalFingers<span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-number">0</span>):
                droneAction<span class="hljs-operator">=</span><span class="hljs-string">"Land"</span>
                me.land()

            elif (totalFingers <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">1</span>):
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"Move forward"</span>
                me.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">30</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)

            elif (totalFingers <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">2</span>):
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"Move backward"</span>
                me.send_rc_control(<span class="hljs-number">0</span>, <span class="hljs-number">-30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            elif (totalFingers <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">3</span>):
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"Left"</span>
                me.send_rc_control(<span class="hljs-number">-30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            elif(totalFingers <span class="hljs-operator">=</span><span class="hljs-operator">=</span> <span class="hljs-number">4</span>):
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"Right"</span>
                me.send_rc_control(<span class="hljs-number">30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            elif(totalFingers<span class="hljs-operator">=</span><span class="hljs-operator">=</span><span class="hljs-number">5</span>):
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"Takeoff"</span>
                me.takeoff()
                me.send_rc_control(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">50</span>, <span class="hljs-number">0</span>)

            <span class="hljs-keyword">else</span>:
                droneAction <span class="hljs-operator">=</span> <span class="hljs-string">"No Action"</span>

            cv2.putText(image, droneAction<span class="hljs-operator">+</span><span class="hljs-string">" "</span><span class="hljs-operator">+</span>str(totalFingers), (<span class="hljs-number">10</span>, <span class="hljs-number">25</span>), cv2.FONT_HERSHEY_SIMPLEX, <span class="hljs-number">1</span>, (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-number">2</span>, cv2.LINE_AA)
            <span class="hljs-keyword">return</span> [image,handlms]
        <span class="hljs-keyword">return</span> [image,[<span class="hljs-number">0</span>]]
    <span class="hljs-keyword">return</span> [image,[<span class="hljs-number">0</span>]]
</code></pre><p>For the syntax <code>me.send_rc_control(0,0,0,0)</code>, it is an inbuilt function from <em>djitellopy</em> library that controls the drone movement after takeoff. Here's is how it accepts the input <code>send_rc_control(self, left_right_velocity: int, forward_backward_velocity: int, up_down_velocity: int, yaw_velocity: int)</code>
The term <code>left_right_velocity</code> defined the speed at which drone should move from left to right, say 10 cm, so to move from <strong>right to left, we will input the value -10</strong>. Default unit for this is cm.</p>
<p>At the end we are printing the action &amp; finger count on the frame, and returning the value. Here's the complete code for the same.</p>
<pre><code><span class="hljs-comment">#YOUTUBE LINK : https://www.youtube.com/shorts/SuQzK4p_Mnw</span>


<span class="hljs-attribute">import</span> cv<span class="hljs-number">2</span>
<span class="hljs-attribute">import</span> mediapipe as mp
<span class="hljs-attribute">from</span> djitellopy import tello


<span class="hljs-attribute">mp_drawing</span> = mp.solutions.drawing_utils
<span class="hljs-attribute">mp_drawing_styles</span> = mp.solutions.drawing_styles
<span class="hljs-attribute">mp_hands</span> = mp.solutions.hands
<span class="hljs-attribute">hands</span> = mp_hands.Hands(model_complexity=<span class="hljs-number">0</span>,min_detection_confidence=<span class="hljs-number">0</span>.<span class="hljs-number">5</span>,min_tracking_confidence=<span class="hljs-number">0</span>.<span class="hljs-number">5</span>,max_num_hands=<span class="hljs-number">1</span>)
<span class="hljs-attribute">cap</span> = cv<span class="hljs-number">2</span>.VideoCapture(<span class="hljs-number">0</span>)
<span class="hljs-attribute">width</span> = <span class="hljs-number">720</span>
<span class="hljs-attribute">height</span> = <span class="hljs-number">280</span>
<span class="hljs-attribute">cap</span>.set(cv<span class="hljs-number">2</span>.CAP_PROP_FRAME_WIDTH, width)
<span class="hljs-attribute">cap</span>.set(cv<span class="hljs-number">2</span>.CAP_PROP_FRAME_HEIGHT, height)
<span class="hljs-attribute">me</span> = tello.Tello()
<span class="hljs-attribute">me</span>.connect()
<span class="hljs-attribute">me</span>.streamoff()
<span class="hljs-attribute">me</span>.streamon()
<span class="hljs-attribute">isDroneFlying</span> = False #Intialising the variable to check if drone is flying or not

<span class="hljs-attribute">def</span> droneGestureController(image):
    <span class="hljs-attribute">image</span>.flags.writeable = False
    <span class="hljs-attribute">image</span> = cv<span class="hljs-number">2</span>.cvtColor(image, cv<span class="hljs-number">2</span>.COLOR_BGR<span class="hljs-number">2</span>RGB)
    <span class="hljs-attribute">results</span> = hands.process(image)
    <span class="hljs-attribute">image</span>.flags.writeable = True
    <span class="hljs-attribute">image</span> = cv<span class="hljs-number">2</span>.cvtColor(image, cv<span class="hljs-number">2</span>.COLOR_RGB<span class="hljs-number">2</span>BGR)
    <span class="hljs-attribute">if</span> results.multi_hand_landmarks:

        <span class="hljs-attribute">for</span> hand_landmarks in results.multi_hand_landmarks:
            <span class="hljs-attribute">mp_drawing</span>.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
                                      <span class="hljs-attribute">mp_drawing_styles</span>.get_default_hand_landmarks_style(),
                                      <span class="hljs-attribute">mp_drawing_styles</span>.get_default_hand_connections_style())
            <span class="hljs-attribute">handlms</span> =<span class="hljs-meta"> []</span>

            <span class="hljs-attribute">c</span> = <span class="hljs-number">0</span>
            <span class="hljs-attribute">for</span> i in hand_landmarks.landmark:
                <span class="hljs-attribute">height</span>, width, fc = image.shape
                <span class="hljs-attribute">x</span> = (i.x) * width
                <span class="hljs-attribute">y</span> = (i.y) * height
                <span class="hljs-attribute">handlms</span>.append([c, int(x), int(y)])
                <span class="hljs-attribute">c</span> = c + <span class="hljs-number">1</span>
            <span class="hljs-attribute">totalFingers</span> = <span class="hljs-number">0</span>

            <span class="hljs-attribute">if</span> (len(handlms) != <span class="hljs-number">0</span>):
                <span class="hljs-attribute">fingerTips</span> =<span class="hljs-meta"> [8, 12, 16, 20]</span>
                <span class="hljs-attribute">if</span>(handlms[<span class="hljs-number">4</span>][<span class="hljs-number">1</span>]&gt;handlms[<span class="hljs-number">3</span>][<span class="hljs-number">1</span>]):
                    <span class="hljs-attribute">totalFingers</span>+=<span class="hljs-number">1</span>

                <span class="hljs-attribute">for</span> i in fingerTips:
                    <span class="hljs-attribute">if</span> (handlms[i][<span class="hljs-number">2</span>] &lt; handlms[i - <span class="hljs-number">2</span>][<span class="hljs-number">2</span>]):
                        <span class="hljs-attribute">totalFingers</span> += <span class="hljs-number">1</span>

            <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">""</span>

            <span class="hljs-attribute">if</span> (totalFingers==<span class="hljs-number">0</span>):
                <span class="hljs-attribute">droneAction</span>=<span class="hljs-string">"Land"</span>
                <span class="hljs-attribute">me</span>.land()

            <span class="hljs-attribute">elif</span> (totalFingers == <span class="hljs-number">1</span>):
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"Move forward"</span>
                <span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>,<span class="hljs-number">30</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>)

            <span class="hljs-attribute">elif</span> (totalFingers == <span class="hljs-number">2</span>):
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"Move backward"</span>
                <span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>, -<span class="hljs-number">30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            <span class="hljs-attribute">elif</span> (totalFingers == <span class="hljs-number">3</span>):
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"Left"</span>
                <span class="hljs-attribute">me</span>.send_rc_control(-<span class="hljs-number">30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            <span class="hljs-attribute">elif</span>(totalFingers == <span class="hljs-number">4</span>):
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"Right"</span>
                <span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">30</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>)

            <span class="hljs-attribute">elif</span>(totalFingers==<span class="hljs-number">5</span>):
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"Takeoff"</span>
                <span class="hljs-attribute">me</span>.takeoff()
                <span class="hljs-attribute">me</span>.send_rc_control(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">50</span>, <span class="hljs-number">0</span>)

            <span class="hljs-attribute">else</span>:
                <span class="hljs-attribute">droneAction</span> = <span class="hljs-string">"No Action"</span>

            <span class="hljs-attribute">cv2</span>.putText(image, droneAction+<span class="hljs-string">" "</span>+str(totalFingers), (<span class="hljs-number">10</span>, <span class="hljs-number">25</span>), cv<span class="hljs-number">2</span>.FONT_HERSHEY_SIMPLEX, <span class="hljs-number">1</span>, (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-number">2</span>, cv<span class="hljs-number">2</span>.LINE_AA)
            <span class="hljs-attribute">return</span><span class="hljs-meta"> [image,handlms]</span>
        <span class="hljs-attribute">return</span><span class="hljs-meta"> [image,[0]]</span>
    <span class="hljs-attribute">return</span><span class="hljs-meta"> [image,[0]]</span>

<span class="hljs-attribute">while</span> True:
    <span class="hljs-attribute">try</span>:
        <span class="hljs-attribute">success</span>, image = cap.read()
        <span class="hljs-attribute">droneImage</span> = me.get_frame_read().frame
        <span class="hljs-attribute">droneImage</span> = cv<span class="hljs-number">2</span>.resize(droneImage, (<span class="hljs-number">360</span>, <span class="hljs-number">240</span>))
        <span class="hljs-attribute">image</span> = droneGestureController(image)[<span class="hljs-number">0</span>]
        <span class="hljs-attribute">isDroneFlying</span> = True
        <span class="hljs-attribute">cv2</span>.imshow('YourPC', image)
        <span class="hljs-attribute">cv2</span>.imshow('Drone', droneImage)
        <span class="hljs-attribute">k</span> = cv<span class="hljs-number">2</span>.waitKey(<span class="hljs-number">1</span>) &amp; <span class="hljs-number">0</span>xFF
        <span class="hljs-attribute">if</span> k == <span class="hljs-number">27</span>:
            <span class="hljs-attribute">cv2</span>.destroyAllWindows()
            <span class="hljs-attribute">break</span>
    <span class="hljs-attribute">except</span>:
        <span class="hljs-attribute">continue</span>
<span class="hljs-attribute">cap</span>.release()
</code></pre><p>Any queries, feel free to type in comment or connect me through <a target="_blank" href="mailto:contact@lakshaykumar.tech">mail</a></p>
<p>Here's the video tutorial for the same. Video credits : <a target="_blank" href="https://www.calmcanfly.com/">Aryan Bakle</a></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/shorts/SuQzK4p_Mnw">https://www.youtube.com/shorts/SuQzK4p_Mnw</a></div>
]]></content:encoded></item><item><title><![CDATA[Computer Vision Made Easy!]]></title><description><![CDATA[Computer vision fascinates me a lot, these days I am exploring the world of Computer Vision. While working I realised that we have to write hundreds of lines of code just for basic computer vision techniques like face detection, hand detection, pose ...]]></description><link>https://blogs.lakshaykumar.tech/computer-vision-made-easy</link><guid isPermaLink="true">https://blogs.lakshaykumar.tech/computer-vision-made-easy</guid><category><![CDATA[Computer Vision]]></category><category><![CDATA[opencv]]></category><category><![CDATA[mediapipe]]></category><category><![CDATA[Artificial Intelligence]]></category><category><![CDATA[Machine Learning]]></category><dc:creator><![CDATA[Lakshay Kumar]]></dc:creator><pubDate>Thu, 15 Sep 2022 17:47:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663264353483/s8VkFUAvZ.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Computer vision fascinates me a lot, these days I am exploring the world of Computer Vision. While working I realised that we have to write hundreds of lines of code just for basic computer vision techniques like face detection, hand detection, pose classification etc.
I explored opencv and mediapipe library which helped me gain a lot of insight into computer vision. I have developed some packages that might help the developer community to focus more on the implementation rather than on coding from scratch. Here are three packages that I developed recently.</p>
<h3 id="heading-1-face-detection"><strong>1. Face Detection</strong></h3>
<p><a target="_blank" href="https://pypi.org/project/lkfacedetection/">Try it out</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663263753498/v7FVuqhFc.png" alt="Screenshot (994).png" /></p>
<h1 id="heading-function">Function</h1>
<p><code>faceDetector(image, draw=False)</code></p>
<ul>
<li><strong>Function Parameter</strong> : This function takes <code>image</code> (a single frame) as input and a variable <code>draw</code> with default value <em>False</em>. You can change the value of parameter <code>draw</code> to <em>True</em> if you want to draw the rectangular box over the face on the <code>image</code> frame.</li>
<li><strong>Output</strong> : This function returns a nested list of length 2. The element at index 1 is the <code>frame</code> and a list of <code>[x,y,w,h]</code>. <code>x</code> is the minimum x co-ordinates of the face, <code>y</code> is the minimum y co-ordinate of face, <code>w</code> is the width and <code>h</code> is the height of the face. NOTE that the <code>frame</code> will have <em>rectangular box over the face if value of <code>draw</code> is set to True in the function</em>.</li>
</ul>
<h1 id="heading-usage">Usage</h1>
<blockquote>
<p>faceDetector(image, draw=False)</p>
</blockquote>
<p>With detection over the face directly through function</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkfacedetection <span class="hljs-keyword">import</span> <span class="hljs-title">faceDetector</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">cv2</span>
<span class="hljs-title">cap</span> <span class="hljs-operator">=</span> <span class="hljs-title">cv2</span>.<span class="hljs-title">VideoCapture</span>(0)
<span class="hljs-title"><span class="hljs-keyword">while</span></span> <span class="hljs-title">True</span>:
    <span class="hljs-title">success</span>, <span class="hljs-title">image</span> <span class="hljs-operator">=</span> <span class="hljs-title">cap</span>.<span class="hljs-title">read</span>()
    <span class="hljs-title">functionValues</span> <span class="hljs-operator">=</span> <span class="hljs-title">faceDetector</span>(<span class="hljs-title">image</span>,<span class="hljs-title">draw</span><span class="hljs-operator">=</span><span class="hljs-title">True</span>) #<span class="hljs-title">draw</span> <span class="hljs-title">over</span> <span class="hljs-title">the</span> <span class="hljs-title">frame</span> <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-title"><span class="hljs-keyword">function</span></span>
    <span class="hljs-title">frame</span> <span class="hljs-operator">=</span> <span class="hljs-title">functionValues</span>[0]
    <span class="hljs-title">cv2</span>.<span class="hljs-title">imshow</span>(<span class="hljs-string">'Face'</span>, <span class="hljs-title">frame</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">waitKey</span>(1)
<span class="hljs-title">cap</span>.<span class="hljs-title">release</span>()
</code></pre><p>With detection externally using the values from function</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkfacedetection <span class="hljs-keyword">import</span> faceDetector
<span class="hljs-keyword">import</span> cv2
cap = cv2.VideoCapture(<span class="hljs-number">0</span>)
<span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
    success, image = cap.read()
    functionValues = faceDetector(image) <span class="hljs-comment">#doesn't draw over the frame</span>
    frame = functionValues[<span class="hljs-number">0</span>]
    x,y,w,h = functionValues[<span class="hljs-number">1</span>]
    cv2.rectangle(frame, (x, y), (x + w, y + h), (<span class="hljs-number">255</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>), <span class="hljs-number">2</span>) <span class="hljs-comment">#Draws a rectangle over the face</span>
    cv2.imshow(<span class="hljs-string">'Face'</span>, frame)
    cv2.waitKey(<span class="hljs-number">1</span>)
cap.release()
</code></pre><h3 id="heading-2-hand-tracking-package"><strong>2. Hand Tracking Package</strong></h3>
<p><a target="_blank" href="https://pypi.org/project/lkhandmapping/">Try it out</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663263862149/wkdarJJIr.png" alt="Screenshot (995).png" /></p>
<h1 id="heading-function">Function</h1>
<p><code>handTracker(image,draw=True)</code></p>
<ul>
<li><strong>Function Parameter</strong> : This function takes <code>image</code> (a single frame) as input and a variable <code>draw</code> with default value <em>False</em>. You can change the value of parameter <code>draw</code> to <em>True</em> if you want to the mapping of hands over the <code>image</code> frame.</li>
<li><strong>Output</strong> : This function returns a nested list of length 2. The element at index 1 is the <code>frame</code> and a list of <code>handLandmarks</code>. Know more about these <code>handLandmarks</code> on this <a target="_blank" href="https://google.github.io/mediapipe/solutions/hands.html#hand-landmark-model">link</a>. NOTE that the <em>function will return [0] at index 1 in the list if no hands are detected</em>.</li>
</ul>
<h1 id="heading-usage">Usage</h1>
<blockquote>
<p>handTracker(image,draw=False)</p>
</blockquote>
<p>Mapping over the hands directly from function.</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkhandtracking <span class="hljs-keyword">import</span> <span class="hljs-title">handTracker</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">cv2</span>

<span class="hljs-title">cap</span> <span class="hljs-operator">=</span> <span class="hljs-title">cv2</span>.<span class="hljs-title">VideoCapture</span>(0)
<span class="hljs-title"><span class="hljs-keyword">while</span></span> <span class="hljs-title">True</span>:
    <span class="hljs-title">success</span>, <span class="hljs-title">image</span> <span class="hljs-operator">=</span> <span class="hljs-title">cap</span>.<span class="hljs-title">read</span>()
    <span class="hljs-title">functionValues</span> <span class="hljs-operator">=</span> <span class="hljs-title">handTracker</span>(<span class="hljs-title">image</span>,<span class="hljs-title">draw</span><span class="hljs-operator">=</span><span class="hljs-title">True</span>)
    <span class="hljs-title">image</span> <span class="hljs-operator">=</span> <span class="hljs-title">functionValues</span>[0]
    <span class="hljs-title">handLms</span> <span class="hljs-operator">=</span> <span class="hljs-title">functionValues</span>[1]
    <span class="hljs-title">print</span>(<span class="hljs-title">handLms</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">imshow</span>(<span class="hljs-string">'Hands'</span>, <span class="hljs-title">image</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">waitKey</span>(1)
<span class="hljs-title">cap</span>.<span class="hljs-title">release</span>()
</code></pre><p>Track hands without mapping.</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkhandtracking <span class="hljs-keyword">import</span> <span class="hljs-title">handTracker</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">cv2</span>

<span class="hljs-title">cap</span> <span class="hljs-operator">=</span> <span class="hljs-title">cv2</span>.<span class="hljs-title">VideoCapture</span>(0)
<span class="hljs-title"><span class="hljs-keyword">while</span></span> <span class="hljs-title">True</span>:
    <span class="hljs-title">success</span>, <span class="hljs-title">image</span> <span class="hljs-operator">=</span> <span class="hljs-title">cap</span>.<span class="hljs-title">read</span>()
    <span class="hljs-title">functionValues</span> <span class="hljs-operator">=</span> <span class="hljs-title">handTracker</span>(<span class="hljs-title">image</span>,<span class="hljs-title">draw</span><span class="hljs-operator">=</span><span class="hljs-title">False</span>)
    <span class="hljs-title">image</span> <span class="hljs-operator">=</span> <span class="hljs-title">functionValues</span>[0]
    <span class="hljs-title">handLms</span> <span class="hljs-operator">=</span> <span class="hljs-title">functionValues</span>[1]
    <span class="hljs-title">print</span>(<span class="hljs-title">handLms</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">imshow</span>(<span class="hljs-string">'Hands'</span>, <span class="hljs-title">image</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">waitKey</span>(1)
<span class="hljs-title">cap</span>.<span class="hljs-title">release</span>()
</code></pre><h3 id="heading-3-body-segmentation"><strong>3. Body Segmentation</strong></h3>
<p><a target="_blank" href="https://pypi.org/project/lkbodysegmentation/">Try it out</a></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1663263919655/wAzKYrXX2.png" alt="Screenshot (996).png" /></p>
<h1 id="heading-function">Function</h1>
<p><code>bodySegmentation(orignalImg,backgroundImg=(255,255,255),threshold=0.3)</code></p>
<ul>
<li><strong>Function Parameter</strong> : This function takes <code>orignalImg</code> i.e. the image on which the segmentation has to happen. Other parameters i.e. <code>backgroundImg</code> sets the background color, by default it is white, but it can be changed as per user's need. threshold defines the level at which background has to be removed.</li>
<li><strong>Output</strong> : The functions returns a frame as output where the background is segmented.</li>
</ul>
<h1 id="heading-usage">Usage</h1>
<blockquote>
<p>bodySegmentation(img)</p>
</blockquote>
<p>Default Segmentation (White Background)</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkbodysegmentation <span class="hljs-keyword">import</span> <span class="hljs-title">bodySegmentation</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">cv2</span>

<span class="hljs-title">cap</span> <span class="hljs-operator">=</span> <span class="hljs-title">cv2</span>.<span class="hljs-title">VideoCapture</span>(0)


<span class="hljs-title"><span class="hljs-keyword">while</span></span> <span class="hljs-title">True</span>:
    <span class="hljs-title">success</span>, <span class="hljs-title">img</span> <span class="hljs-operator">=</span> <span class="hljs-title">cap</span>.<span class="hljs-title">read</span>()
    <span class="hljs-title">img</span> <span class="hljs-operator">=</span> <span class="hljs-title">bodySegmentation</span>(<span class="hljs-title">img</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">imshow</span>(<span class="hljs-string">'Image'</span>,<span class="hljs-title">img</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">waitKey</span>(1)
</code></pre><blockquote>
<p>bodySegmentation(img, backgroundColor, threshold)</p>
</blockquote>
<p>Customized Segmentation</p>
<hr />
<pre><code><span class="hljs-keyword">from</span> lkbodysegmentation <span class="hljs-keyword">import</span> <span class="hljs-title">bodySegmentation</span>
<span class="hljs-title"><span class="hljs-keyword">import</span></span> <span class="hljs-title">cv2</span>

<span class="hljs-title">cap</span> <span class="hljs-operator">=</span> <span class="hljs-title">cv2</span>.<span class="hljs-title">VideoCapture</span>(0)


<span class="hljs-title"><span class="hljs-keyword">while</span></span> <span class="hljs-title">True</span>:
    <span class="hljs-title">success</span>, <span class="hljs-title">img</span> <span class="hljs-operator">=</span> <span class="hljs-title">cap</span>.<span class="hljs-title">read</span>()
    <span class="hljs-title">backgroundColor</span> <span class="hljs-operator">=</span> (255,0,255) #<span class="hljs-title">You</span> <span class="hljs-title">can</span> <span class="hljs-title">replace</span> <span class="hljs-title">with</span> <span class="hljs-title">an</span> <span class="hljs-title">image</span> <span class="hljs-title">too</span>
    <span class="hljs-title">threshold</span> <span class="hljs-operator">=</span> 0.45 #<span class="hljs-title">Level</span> <span class="hljs-title">of</span> <span class="hljs-title">background</span> <span class="hljs-title">to</span> <span class="hljs-title">be</span> <span class="hljs-title">erased</span>
    <span class="hljs-title">img</span> <span class="hljs-operator">=</span> <span class="hljs-title">bodySegmentation</span>(<span class="hljs-title">img</span>,<span class="hljs-title">backgroundColor</span>,<span class="hljs-title">threshold</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">imshow</span>(<span class="hljs-string">'Image'</span>,<span class="hljs-title">img</span>)
    <span class="hljs-title">cv2</span>.<span class="hljs-title">waitKey</span>(1)
</code></pre><h1 id="heading-developer">Developer</h1>
<p>This package is developed by <a target="_blank" href="https://www.lakshaykumar.tech/">Lakshay Kumar</a> an enthusiastic AI Researcher. This is developed keeping in mind the pain to write lengthy lines of code just to detect faces. This will enable other developers to focus more on implementation part rather than spending time on coding the face detection module.<br />
Feel free to share your feedback via <a target="_blank" href="mailto:contact@lakshaykumar.tech">mail</a></p>
]]></content:encoded></item></channel></rss>