Setting Up Static IP Address on Linux

This page explains how to set up a static IP address on a Linux with NetworkManager.

 

Steps
1. Static IP address(es) can be set up in *.nmconnection file under /etc/NetworkManager/system-connections like below.

[ipv4]
method=manual
addresses=10.10.10.51
gateway=10.10.10.254
dns=10.10.10.254

 

2. Reload and restart

sudo nmcli connection reload
sudo nmcli connection down 'MY HOME WIFI'
sudo nmcli connection up 'MY HOME WIFI'

 

References
[1] Cannot configure a static IP address – raspberrypi.com
[2] NetworkManager – Arch Linux

 

VI Cheat Sheet


This isn’t an extensive list.
I’m not a heavy vi user, so I often forget basic commands I want to use. So, this is a list of those commands.

 

Split screen

  • vertically
    CTRL + w and v
  • horizontally
    CTRL + w and s

Move between screens

  • down
    CTRL + w and j
  • up
    CTRL + w and k
  • right
    CTRL + w and l
  • left
    CTRL + w and h

Move cursor to …

  • specific line
    <line number> + gg
  • top of the file
    gg
  • bottom of the file
    G
  • end of the line
    $

Scroll page

  • up
    CTRL + b
  • down
    CTRL + f

Copy and paste multiple lines

  1. Press V (to enter VISUAL LINE mode)
  2. Select the lines you want to copy
  3. Press y (or d for cut instead of copy)
  4. Move the cursor to where you want to paste the lines
  5. Press p to paste

Comment out multiple lines

  1. Press CTRL + v (to enter VISUAL BLOCK mode)
  2. Move the cursor to select the lines you want to comment out
  3. Press I (to enter INSERT mode)
  4. Type # (in case of shell script)
  5. Enter ESC

Save file as …

  • :w <file name>

Open file

  • :e <file name>

Search selected word

  • Search forward
    *
  • Search backward
    #

Show/hide line number

  • Show
    :set number
  • Hide
    :set nonumber

 

Generating UML Class Diagram from C++ Header File using PlantUML


This post shows a way to generate a UML class diagram from C++ header file(s) using PlantUML [1] and hpp2plantuml [2].

 

Assumptions

  • Debian-based linux distribution (I used Ubuntu 20.04)

 

Steps
1. Install hpp2plantuml
hpp2plantuml is used to convert C++ header file to PlantUML. 

1-1. Install pip if not already installed.

sudo apt install python3-pip -y

1-2. Install hpp2plantuml with pip.

pip install hpp2plantuml

 

2. Install plantuml
plantuml command is used to generate a image from a PlantUML file.

2-1. Install plantuml with apt.

sudo apt install plantuml -y

 

3. Test
In this post, I use a sample c++ header file from hpp2plantuml repo to demonstrate.

3-1. Download a sample header file.

wget https://github.com/thibaultmarin/hpp2plantuml/blob/v0.8.2/tests/simple_classes_1_2.hpp

3-2. Convert the sample C++ header file to a PlantUML file.

hpp2plantuml -i simple_classes_1_2.hpp -o output.puml

3-3. Generate a class diagram image from the PlantUML file.

plantuml -tsvg output.puml

You can change the image format with the output file format option.

 

References
[1] PlantUML
[2] hpp2plantuml – GitHub

 

Showing Video Image on Tkinter Window with OpenCV


This is an example of minimal tkinter application that shows video image on the window using OpenCV.

 

Prerequisites

  • Python 3

 

Required Packages

Install packages if not already.

1. Python Imaging Library (Pillow)

pip install Pillow

2. OpenCV

pip install opencv-python

 

Code 

import tkinter as tk
from PIL import Image, ImageTk
import cv2

class MainWindow():
    def __init__(self, window, cap):
        self.window = window
        self.cap = cap
        self.width = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        self.height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        self.interval = 20 # Interval in ms to get the latest frame

        # Create canvas for image
        self.canvas = tk.Canvas(self.window, width=self.width, height=self.height)
        self.canvas.grid(row=0, column=0)

        # Update image on canvas
        self.update_image()

    def update_image(self):
        # Get the latest frame and convert image format
        self.image = cv2.cvtColor(self.cap.read()[1], cv2.COLOR_BGR2RGB) # to RGB
        self.image = Image.fromarray(self.image) # to PIL format
        self.image = ImageTk.PhotoImage(self.image) # to ImageTk format

        # Update image
        self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image)

        # Repeat every 'interval' ms
        self.window.after(self.interval, self.update_image)

if __name__ == "__main__":
    root = tk.Tk()
    MainWindow(root, cv2.VideoCapture(0))
    root.mainloop()

 

Execute the program like this:

python3 video_on_tkinter.py

 

If you want to get the image from a video file instead of a camera, specify the file name like this:

    MainWindow(root, cv2.VideoCapture('test.mp4'))

 

 

References
[1] How to update an image on a Canvas? — Stack Overflow
[2] How do you run your own code alongside Tkinter’s event loop? — Stack Overflow
[3] Displaying a video feed with OpenCV and Tkinter — pyimagesearch
[4] Python OpenCV – show a video in a Tkinter window — Solarian Programmer