Python實現 感測器的隨機佈置

我可以鴨發表於2020-11-07

程式碼演示

import tkinter as tk
import random
import win32gui
import cv2
import time
import math
from PIL import Image, ImageGrab

class Window:
    def __init__(self, master):
        self.root = master
        self.createpage()
        self.run()

    def createpage(self):
        self.root.geometry('910x510')
        # 設定視窗是否可以變化長/寬,False不可變,True可變,預設為True
        self.root.resizable(width=False, height=False)

        tk.LabelFrame(self.root, text='|引數|', fg='black', padx=10, pady=10,
                      font='Verdana 10 bold').place(x=10, y=10, height=180, width=290)
        tk.Label(self.root, text='感測器數量:', fg='black').place(x=15, y=45)
        tk.Label(self.root, text='感測器探測半徑:', fg='black').place(x=15, y=115)
        # 建立一個輸入框,以輸入內容
        self.inputnum_text = tk.StringVar()
        self.inputnum = tk.Entry(self.root, textvariable=self.inputnum_text)
        self.inputnum.place(x=120, y=50)
        
        self.inputradius_text = tk.StringVar()
        self.inputradius = tk.Entry(
            self.root, textvariable=self.inputradius_text)
        self.inputradius.place(x=120, y=120)

        tk.LabelFrame(self.root, text='|結果|', fg='black', padx=10, pady=10,
                      font='Verdana 10 bold').place(x=10, y=215, height=180, width=290)
        tk.Label(self.root, text='理論覆蓋率(%):', fg='black').place(x=15, y=250)
        tk.Label(self.root, text='實際覆蓋率(%):', fg='black').place(x=15, y=320)

        self.theoryoutput = tk.Text(self.root, width=20, height=1)
        self.theoryoutput.place(x=120, y=255)  # 先建立物件再放置位置,否則會報錯

        self.actualoutput = tk.Text(self.root, height=1, width=20)
        self.actualoutput.place(x=120, y=325)

        self.cv = tk.Canvas(self.root, bg='white', height=500, width=600)
        self.cv.place(x=300)
        # 建立按鈕
        tk.Button(self.root, text='感測器佈置', command=self.circle_create).place(
            x=100, y=400, width=100)
        tk.Button(self.root, text='計算', command=self.Calculation_Area).place(
            x=100, y=460, width=100)

    def Wipe_Data(self):
        # 清空文字框內的內容
        self.theoryoutput.delete(0.0, 'end')
        self.actualoutput.delete(0.0, 'end')

    def CaptureScreen(self):
        # print('執行截圖函式')
        HWND = win32gui.GetFocus()  # 獲取當前視窗控制程式碼
        self.rect = win32gui.GetWindowRect(HWND)  # 獲取當前視窗座標
        x = self.rect[0] + 300
        x1 = x+self.cv.winfo_width()
        y = self.rect[1]
        y1 = y+self.cv.winfo_height()
        image = ImageGrab.grab((x, y, x1, y1))
        image.save("second.jpeg", 'jpeg')  # 前面一個引數是儲存路徑,後面一個引數是儲存格式

    def Calculation_Area(self):
        self.CaptureScreen()
        # print('執行計算函式')
        self.Wipe_Data()
        img = cv2.imread("second.jpeg")  # 圖片讀取
        pictue_size = img.shape
        picture_height = pictue_size[0]
        picture_width = pictue_size[1]
        # 輸出長和寬
        # print(picture_height,picture_width)
        All_area = 304416.0
        Proportion_area_white = 0
        Proportion_area_blue = 0
        for a in range(picture_height):
            for b in range(picture_width):
                if img[a, b].all() > 0:
                    Proportion_area_white = Proportion_area_white + 1

        Proportion_area_blue = float(All_area - Proportion_area_white)
        # 計算實際輸出
        # print(Proportion_area_blue)
        Occupancy_actual = Proportion_area_blue / All_area
        Occupancy_actual = ('%.12f' % Occupancy_actual)
        Occupancy_actual = float(Occupancy_actual)
        Occupancy_actual = Occupancy_actual * 100
        # 計算理論輸出
        # ? = 1 − ?^−???2/? 理論值計算公式
        i = float(self.sensor_num * math.pi *
                  self.sensor_radius * self.sensor_radius)
        Occupancy_theory = (1 - math.exp((i * -1.0)/All_area))*100
        # 將得到的值輸入到文字框裡
        self.theoryoutput.insert('end', Occupancy_theory)
        self.actualoutput.insert('end', Occupancy_actual)

    def circle_create(self):
        # print('執行繪圖函式')
        # 清空畫圖的內容
        self.cv.delete(tk.ALL)
        # 獲取在引數裡輸入的值
        self.sensor_num = self.inputnum.get()
        self.sensor_radius = self.inputradius.get()
        # 轉int
        self.sensor_num = int(self.sensor_num)
        self.sensor_radius = int(self.sensor_radius)
        # 做迴圈開始繪圖
        for num in range(1, (self.sensor_num + 1)):
            circle_center_x = random.randint(10, 590)
            circle_center_y = random.randint(10, 490)
            self.cv.create_oval((circle_center_x-self.sensor_radius, circle_center_y-self.sensor_radius, 
                                circle_center_x+self.sensor_radius, circle_center_y+self.sensor_radius),
                                outline='blue',
                                fill='blue'
                                )

    def run(self):
        try:
            self.root.mainloop()
        except Exception as e:
            print("*** exception:\n".format(e))


def main():
    window = tk.Tk()
    window.title('SensorHomework Design By HJK')
    Window(window).run()


if __name__ == '__main__':
    main()

相關文章