2023年3月20日 星期一

python yolo object detect

import cv2 as cv

import time

Conf_threshold = 0.5

NMS_threshold = 0.4

COLORS = [(0, 255, 0), (0, 0, 255), (255, 0, 0),

          (255, 255, 0), (255, 0, 255), (0, 255, 255)]


class_name = []

with open('classes.txt', 'r') as f:

    class_name = [cname.strip() for cname in f.readlines()]

# print(class_name)

#net = cv.dnn.readNet('yolov4-tiny.weights', 'yolov4-tiny.cfg')

#net = cv.dnn.readNet('yolov4.weights', 'yolov4.cfg')

net = cv.dnn.readNet('yolov3.weights', 'yolov3.cfg')

#net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)

#net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA_FP16)


cap=cv.VideoCapture(0)

#cap = cv.VideoCapture('output.avi')


ret, frame = cap.read()

img_size_h, img_size_w, channels = frame.shape

    

model = cv.dnn_DetectionModel(net)

#model.setInputParams(size=(416, 416), scale=1/255, swapRB=True)

model.setInputParams(size=(img_size_w, img_size_h), scale=1/255, swapRB=True)



starting_time = time.time()

frame_counter = 0

while True:

    ret, frame = cap.read()

    frame_counter += 1

    if ret == False:

        break

    classes, scores, boxes = model.detect(frame, Conf_threshold, NMS_threshold)

    for (classid, score, box) in zip(classes, scores, boxes):

        if class_name[int(classid)] != "person":

            continue

        color = COLORS[int(classid) % len(COLORS)]

        label = "%s : %.2f" % (class_name[int(classid)], score *100)

        #label = "%f" % round(score *100 , 2)

        cv.rectangle(frame, box, color, 1)

        cv.putText(frame, label, (box[0], box[1]-10),

                   cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)

    endingTime = time.time() - starting_time

    fps = "FPS: %.2f" % (frame_counter/endingTime)

    # print(fps)

    cv.putText(frame, fps, (20, 50),

               cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)

    cv.imshow('frame', frame)

    key = cv.waitKey(1)

    if key == ord('q'):

        break

cap.release()

cv.destroyAllWindows()

 


2023年3月18日 星期六

Python Sending Gmail

import smtplib, ssl
from email.message import EmailMessage

port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "sender@gmail.com" # Enter your address
receiver_email = "receiver@gmail.com" # Enter receiver address
password = "16-digit-app-password"

msg = EmailMessage()
msg.set_content("Hello Underworld!")
msg['Subject'] = "Hello Underworld from Python Gmail!"
msg['From'] = sender_email
msg['To'] = receiver_email

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)

    server.send_message(msg, from_addr=sender_email, to_addrs=receiver_email) 


資料來源:https://leimao.github.io/blog/Python-Send-Gmail/

2023年3月17日 星期五

利用 HttpListener 建立 Simple HTTP Server

 使用 C# 的 System.Net 其 HttpListener 類別建立一個 Simple HTTP Server 的筆記。


建立伺服器


要用 HttpListener 建立伺服器,可使用 Visual Studio 的 Console Application 來撰寫:

HttpListener server = new HttpListener();
server.Prefixes.Add("http://127.0.0.1:2222/");
server.Prefixes.Add("http://localhost:2222/");

server.Start();
即啟動伺服器

權限問題


建立伺服器後,欲啟動必須要使用管理員權限,建議可以先離開 Visual Studio 再以管理員權限重開。

伺服器文字回傳


啟動伺服器後,要針對所有的請求做回應,於是需要加入無限迴圈等待:
while (true)
{
    HttpListenerContext context = server.GetContext();
    HttpListenerResponse response = context.Response;
}

要回應時,在迴圈裡面加入將文字轉 stream 物件丟回去:
while (true)
{
    HttpListenerContext context = server.GetContext();
    HttpListenerResponse response = context.Response;

    //中文回傳測試
    StringBuilder builder = new StringBuilder("中文測試");
    string something = builder.ToString();
    byte[] buffer = Encoding.UTF8.GetBytes(something);
    response.ContentLength64 = buffer.Length;
    Stream st = response.OutputStream;
    st.Write(buffer, 0, buffer.Length);
    context.Response.Close();
}


檔案伺服器


伺服器回傳亦可使用檔案,則將迴圈中的回傳改為:

while (true)
{
    HttpListenerContext context = server.GetContext();
    HttpListenerResponse response = context.Response;

    string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;

    if (page == string.Empty)
        page = "index.html";

    TextReader tr = new StreamReader(page);
    string msg = tr.ReadToEnd();

    byte[] buffer = Encoding.UTF8.GetBytes(msg);

    response.ContentLength64 = buffer.Length;
    Stream st = response.OutputStream;
    st.Write(buffer, 0, buffer.Length);

    context.Response.Close();
}

請求字串


要獲得 Query (GET Parameter) 之類的參數,則可以使用以下方法來獲得:


Console.WriteLine(context.Request.QueryString.Get("test"));
//http://localhost:2222/theme?test=1


其他程式碼: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//must to use
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HttpListener server = new HttpListener();
server.Prefixes.Add("http://127.0.0.1:2222/");
server.Prefixes.Add("http://localhost:2222/");
server.Start();
Console.WriteLine("Listening...");
while (true)
{
HttpListenerContext context = server.GetContext();
HttpListenerResponse response = context.Response;
string page = Directory.GetCurrentDirectory() + context.Request.Url.LocalPath;
Console.WriteLine(context.Request.Url.LocalPath);
if (page == string.Empty)
page = "index.html";
StringBuilder builder;
switch (context.Request.Url.LocalPath) {
case "/":
builder = new StringBuilder("<!DOCTYPE html><html><head><title>測試</title><meta charset=\"utf-8\"></head><body>中文測試</body></html>");
break;
case "/readTheme":
Console.WriteLine(context.Request.QueryString.Get("test"));
builder = new StringBuilder("<!DOCTYPE html><html><head><title>測試</title><meta charset=\"utf-8\"></head><body>中文測試</body></html>");
break;
default:
builder = new StringBuilder("<!DOCTYPE html><html><head><title>測試</title><meta charset=\"utf-8\"></head><body>中文測試</body></html>");
break;
}
string something = builder.ToString();
byte[] buffer = Encoding.UTF8.GetBytes(something);
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
}
}



Reference:
http://www.codingvision.net/networking/c-simple-http-server

資料來源:https://www.prochainsci.com/2018/04/c-httplistener-simple-http-server.html