YoloV9 Code for Object Detection + Segmentation and Tracking

Yolo V9 Tracking + Object Tracing + Segmentation

First, See The Video below then see the code to do Object Detection + Segmentation and Tracking in that

YoloV9 Code for Object Detection + Segmentation and Tracking

Now, See The Python Code for Video above

import numpy as np
import supervision as sv
from ultralytics import YOLO

model = YOLO("yolov9e-seg.pt")
tracker = sv.ByteTrack()
box_annotator = sv.MaskAnnotator()
label_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK)
trace_annotator = sv.TraceAnnotator()

def callback(frame: np.ndarray, _: int) -> np.ndarray:
    results = model(frame)[0]
    detections = sv.Detections.from_ultralytics(results)
    detections = tracker.update_with_detections(detections)

    labels = [
        f"#{tracker_id} {results.names[class_id]}"
        for class_id, tracker_id
        in zip(detections.class_id, detections.tracker_id)
    ]

    annotated_frame = box_annotator.annotate(
        frame.copy(), detections=detections)
    annotated_frame = label_annotator.annotate(
        annotated_frame, detections=detections, labels=labels)
    return trace_annotator.annotate(
        annotated_frame, detections=detections)

sv.process_video(
    source_path="1.mp4",
    target_path="1-result_2.mp4",
    callback=callback
)

Send Email By C#

Sending Email in C#

Sending Automatic Email from Application to Users is a necessary task, in this post I want to show you how to send Email by using c#.

My Solution is Automatically Connect to a smtp mail server and use that to send email. To connect to a SMTP Server we need connection information from that server. In this post, I want to connect to gmail SMTP Server. below you see Gmail SMTP Server Connection Information :

  1. smtp Server : smtp.gmail.com
  2. smtp port : 587

now, I use my gmail Credential to send Email.

See Code below :

try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                message.From = new MailAddress("codetipsacademy@gmail.com");
                message.To.Add(new MailAddress("codetips@codetipsacademy.com"));
                message.Subject = "Test Subject";
                message.BodyEncoding = Encoding.UTF8;                   // Support UTF-8 Encoding 
                message.IsBodyHtml = true;                              // Message Body is Html  
                message.Body = "<b>Test Email</b><br /><p style='color:red;'>This is for Test</p>";
                smtp.Port = 587;                                        // Gmail SMTP SSL Support Port    
                smtp.Host = "smtp.gmail.com";                           // Gmail SMTP Server 
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new NetworkCredential("[Gmail Address]", "[Gmail Password]");                //Gmail Credential to Login
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(message);
                Console.WriteLine("Success");
                Console.ReadLine();
            }
            catch (Exception ex) {
                Console.WriteLine("Error");
            }

In code above fill [Gmail Address] with your Gmail Address and [Gmail Password] with your Gmail Password.

Email Sent Automatically by c#
Recived Email

Due to the fact that Gmail block connection from apps by default. you should change this behaviour. So, in google account management setting, go to Security section, then, Allow “Less Secure App access”. see image below for help

BackUP a Sql Data Base by C# Code

Backup-Sql-Data-Base-by-C#

There some ways to back Up a Data base, in this project a get a full back up from a database by using C#. However; we can customize the code to only backup data or exclude some tables to backup.
This Back up will Contain all Schema and Data together and generate a sql Code for us that
by Executing that code all the Data and Schema will be created for us.
So, an Important tip is that to restore a data base be using the generated code first Create an empty Data base then Execute the Sql Code.

To write this code first download nuget Microsoft.SqlServer.SqlManagementObjects by typing code below in Package Manager Console of Visual Studio.

Install-Package Microsoft.SqlServer.SqlManagementObjects 

then, add namespace Microsoft.SqlServer.Management.Smo (Code below)

using Microsoft.SqlServer.Management.Smo;

The C# Code that will Back UP a data base for us is below.

string outputFileName = @"d:/4.sql";
            StringBuilder sb = new StringBuilder();
            Server srv = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection("[Server IP]", "[Sql User]", "[Sql Password]"));
            Database dbs = srv.Databases["EShop"];                // EShop is The Database that I want to backUP
            ScriptingOptions options = new ScriptingOptions();
            options.ScriptData = true;
            options.ScriptDrops = false;
            options.FileName = outputFileName;
            options.EnforceScriptingOptions = true;
            options.ScriptSchema = true;
            options.IncludeHeaders = true;
            options.AppendToFile = true;
            options.Indexes = true;
            options.WithDependencies = true;
            options.DriAll = true;

            foreach (Table tbl in dbs.Tables)
            {
                tbl.EnumScript(options);
            }

in the code below Input the Sql Connection of Data Base that you want to create back UP Sql Code of that.

ServerConnection("[Server IP]", "[Sql User]", "[Sql Password]")

and in the code below choose the output Sql File

        string outputFileName = @"d:/4.sql";