<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rajesh</title>
    <description>The latest articles on DEV Community by Rajesh (@rajeshpachaikani).</description>
    <link>https://dev.to/rajeshpachaikani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F402460%2Fccaee7b1-0745-4c50-af7b-72860b2fada7.jpg</url>
      <title>DEV Community: Rajesh</title>
      <link>https://dev.to/rajeshpachaikani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajeshpachaikani"/>
    <language>en</language>
    <item>
      <title>Wechat QR Reader in Rust using Opencv</title>
      <dc:creator>Rajesh</dc:creator>
      <pubDate>Thu, 31 Aug 2023 10:04:40 +0000</pubDate>
      <link>https://dev.to/rajeshpachaikani/wechat-qr-reader-in-rust-using-opencv-5ad9</link>
      <guid>https://dev.to/rajeshpachaikani/wechat-qr-reader-in-rust-using-opencv-5ad9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;WeChat Computer Vision Team has a QR detection and super resolution caffe model that outperforms the QR detection and decoding methods in the OpenCV objdetect library. You need to compile the opencv contrib packages from source if you want to take advantage of this high performance QR library.&lt;/p&gt;

&lt;p&gt;Take a look at my github repo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rajeshpachaikani/rust-wechat-qr-reader"&gt;rust-wechat-qr-reader&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prepare resources
&lt;/h2&gt;

&lt;p&gt;We need to compile opencv and opencv_contrib packages. You can fetch them using the following commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir ~/opencv_build &amp;amp;&amp;amp; cd ~/opencv_build
mkdir build
git clone git@github.com:opencv/opencv.git - depth 1
git clone git@github.com:opencv/opencv_contrib.git - depth 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are several dependencies to be installed based on your OS.&lt;/p&gt;

&lt;p&gt;In windows, you can install the msvc or mingw compiler and follow the instruction in the opencv and keep your fingers crossed since the last time I tested in windows the rust opencv bindings were not able to generate due to some bug in the visual studio build tools.&lt;/p&gt;

&lt;p&gt;In Linux, you can use clang or GCC compiler. I prefer and tested with clang compiler only.&lt;/p&gt;

&lt;p&gt;In both platforms we can use cmake-gui to configure the OpenCV project.&lt;/p&gt;

&lt;p&gt;You can use apt, pacman or yum based on the distro you are using to install all the dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure
&lt;/h2&gt;

&lt;p&gt;Open CMake-Gui and select the source code folder &lt;code&gt;~/opencv_build/opencv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MrrwrMFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjcwd4mjrcsi2ojurao3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MrrwrMFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjcwd4mjrcsi2ojurao3.png" alt="Source and Build directory" width="596" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select &lt;code&gt;~/opencv_build/build&lt;/code&gt; as build directory and click Configure&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XiboXDMu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oi07sg2xbvk6ihosttiv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XiboXDMu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oi07sg2xbvk6ihosttiv.png" alt="CMake setup" width="596" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep “Unix Makefiles” as the generator and click on Specify native compilers. Use the compiler of your choice here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LiFVTb_F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix7awz4ip37ji864ch37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LiFVTb_F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ix7awz4ip37ji864ch37.png" alt="Select Compiler" width="496" height="388"&gt;&lt;/a&gt;&lt;br&gt;
Internet is required while running Configure, as cmake will attempt to download some zip files.&lt;/p&gt;

&lt;p&gt;Once Configure is finished you will see several build options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FvxNYZ06--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipeg9n8bux96hoiwpoqo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FvxNYZ06--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipeg9n8bux96hoiwpoqo.png" alt="Cmake Configure" width="800" height="467"&gt;&lt;/a&gt;&lt;br&gt;
Search for &lt;code&gt;OPENCV_EXTRA_MODULES_PATH&lt;/code&gt; and add &lt;code&gt;~/opencv_build/opencv_contrib/modules&lt;/code&gt; as module path&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Eh6HqETE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2yd42wrpj0x2hpjbqv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Eh6HqETE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2yd42wrpj0x2hpjbqv1.png" alt="Add contrib modules path" width="800" height="467"&gt;&lt;/a&gt;&lt;br&gt;
Now click on Configure again. This will read the modules path and add more build options. Search for BUILD_opencv_wechat_qrcode and make sure it is enabled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KyAf9vov--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwqwgmsqwfj79azl5o5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KyAf9vov--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwqwgmsqwfj79azl5o5d.png" alt="Add contrib modules path" width="800" height="467"&gt;&lt;/a&gt;&lt;br&gt;
Now click on Generate. This will create Unix Makefiles in the build directory.&lt;/p&gt;

&lt;p&gt;Now open your terminal and navigate to the build directory. The make command will start building the opencv library. The &lt;code&gt;-j8&lt;/code&gt; flag specifies the number of jobs (commands) to run simultaneously. If you have more than 8 cpu cores you can use a larger number so that the compilation will finish faster.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/opencv_build/build
make -j8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XaW2vne---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnnns65g7lcz6rxc93ok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XaW2vne---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnnns65g7lcz6rxc93ok.png" alt="make command output" width="800" height="414"&gt;&lt;/a&gt;&lt;br&gt;
Once the build is finished run sudo make install to install the library files in appropriate locations.&lt;/p&gt;

&lt;p&gt;In order to make the library accessible from rust binary, we need to add the opencv lib directory to the /etc/ld.so.conf.d/libopencv.conf file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/ld.so.conf.d/libopencv.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and add this line &lt;code&gt;/usr/local/lib&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Save and exit from nano. Execute sudo ldconfig to load the libraries into environment.&lt;br&gt;
Rust project&lt;/p&gt;

&lt;p&gt;Finally, we can start working on the rust code. Create a new rust project by running cargo new --bin qr-app&lt;/p&gt;

&lt;p&gt;Add opencv as a dependency by running cargo add opencv&lt;/p&gt;

&lt;p&gt;Download the wechat qr code caffe models from their github repo . The following code need the caffemodel and prototxt files to be in the model directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vLPiFp1P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm2syy3sk97q3t8vs1oo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vLPiFp1P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm2syy3sk97q3t8vs1oo.png" alt="Folder structure" width="387" height="336"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
use opencv::prelude::*;
use opencv::{highgui, videoio, wechat_qrcode::WeChatQRCode};

fn main() {
    let mut scanner = WeChatQRCode::new(
        "./model/detect.prototxt",
        "./model/detect.caffemodel",
        "./model/sr.prototxt",
        "./model/sr.caffemodel",
    )
    .unwrap();

    let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY).unwrap();
    let mut points = opencv::core::Vector::&amp;lt;opencv::core::Mat&amp;gt;::new();
    let mut frame = Mat::default();

    loop{
        cam.read(&amp;amp;mut frame).unwrap();
        let decoded = scanner.detect_and_decode(&amp;amp;frame, &amp;amp;mut points).unwrap();
        if points.len() &amp;gt;0{
            println!("{:?}", decoded);
        }

        highgui::imshow("winname", &amp;amp;frame).unwrap();
        highgui::wait_key(1).unwrap();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is less than 30 lines and is really simple. The detect_and_decode function returns the vector which contains the decoded list of texts corresponding to the QR code. We grab the frames from the inbuilt webcam in this example, if you need to read files from the hard disk you can use opencv::imgcodecs::imread(filename, flags) and pass the image to the detect_and_decode function. &lt;code&gt;cargo run&lt;/code&gt; will compile and run the QR scanner application.&lt;/p&gt;

&lt;p&gt;That’s all. Thank you for reading till the end. Please leave your comments and let me know what you think about my blogs. Bye!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Getting Started With Embedded Development Using Rust and Arduino</title>
      <dc:creator>Rajesh</dc:creator>
      <pubDate>Wed, 25 May 2022 15:42:16 +0000</pubDate>
      <link>https://dev.to/rajeshpachaikani/getting-started-with-embedded-development-using-rust-and-arduino-2idb</link>
      <guid>https://dev.to/rajeshpachaikani/getting-started-with-embedded-development-using-rust-and-arduino-2idb</guid>
      <description>&lt;p&gt;Using rust for embedded development is much more easier than setting up a C/C++ environment now. You will be able to program and flash your firmware to an Arduino in just a few minutes.&lt;/p&gt;

&lt;p&gt;First setting up an environment may take a little while and then it is as swift as cloning a repository. Let's jump to the initial environment setup.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run this to install AVR compiler and library header files
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install avr-libc gcc-avr pkg-config avrdude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We need cargo-generate to get project template from git
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo install cargo-generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Also to run the program once you compiled it you can use the ravedude cargo package
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo install ravedude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all. The configuration part is over.&lt;/p&gt;

&lt;p&gt;Now whenever you want to start a new project you can just run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo generate --git https://github.com/Rahix/avr-hal-template.git 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be prompted to enter a name for your project, and the board that you have.(Arduino Uno, Mega etc.,)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cargo run 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will build and flash the program to your embedded system but before running it you need to tell the ravedude which of your serial port is connected to arduino.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export RAVEDUDE_PORT=/dev/ttyUSB0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace ttyUSB0 with the name of the port your arduino is connected to. &lt;/p&gt;

&lt;p&gt;Once the default project from the template is flashed you will be greeted with the blinking led in your arduino. You can edit the &lt;code&gt;src/main.rs&lt;/code&gt; file to control the Universe... Nope! Just your Arduino.😄&lt;br&gt;
That's how easy it is to start programming embedded systems in Rust. &lt;/p&gt;

&lt;p&gt;P.S: I am new to blogging so if you find anything odd in this blog let me know.&lt;/p&gt;

&lt;p&gt;Image Credit: &lt;a href="https://commons.wikimedia.org/wiki/File:Arduino_Leonardo_PCB.jpg"&gt;https://commons.wikimedia.org/wiki/File:Arduino_Leonardo_PCB.jpg&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>arduino</category>
      <category>embedded</category>
    </item>
  </channel>
</rss>
