PHU LUC CODE HOAT DONG

Một phần của tài liệu đồ án trí tuệ nhân tạo đề tài phân loại hàng hóa bằng đọc mã vạch 2 (Trang 36 - 44)

from tkinter import * # Thu tao giao dién import cv2 # Thu vién su ly anh import numpy as np # Thu vién khai bao mang from PIL import Image,ImageTk # Thu vién lay hinh anh from pyzbar.pyzbar import decode #Thu vién dé doc ma QR va Barcode import pyttsx3 # Thu vién giong nói

from datetime import date ,datetime #Thu vién dé lay thoi gian import time # Thu vién thoi gian

from tkinter import ttk # Thu viện tạo giao diện import speech_recognition # ‘Thu vién thu giong noi from threading import Thread #Thu vién phan luong import webbrowser as wb #Thu vién mo dudng link import pandas as pd # Thu vién cau tric bang đa chiều import pathlib # Thư viện đường dẫn

from openpyxl import Workbook # mo excel import openpyxI,xlrd # mở excel

from openpyxl import load_workbook #mo excel from tkinter import messagebox

Nam = TkQ #code gọi tkimfer Nam.tifle(Đô An Trí Tuệ Nhân Tạo) #đặt tên cho khung giao diện Nam.geometry( 800x610”) # kích thước khung

Nam.resizable(width=False, heipht=False) #khóa khung Nam.iconbitmap("D:\\GiaoDien\\education.ico") #icon cho giao diện thứ TLN = speech_recognition.RecognizerQ#khoi dong giong noi

Hinh=cv2.VideoCapture(0) Hinh.set(3,200)

GN =pyttsx3.initQ) #khởi tạo tiếng nói của máy tính voices = ỚN.getProperty(voIces”)

GN.setProperty("voice” voices[1].id) #setup giong cho may tinh TocDoDoc=GN. getProperty(‘rate’) # t6c độ đọc của máy tính chậm GN.setProperty(rate',TocDoDoc - 80) #giám tốc độ đọc của giộng nói xuống 80 N1 = Label(Nam, text="",bg="white" padx=2 1 0,pady=5,relief='solid’) #khung chữ thứ

nhat

N1.place(x=375,y=180) # vi trí của khung chữ thứ nhất

N2 = Label(Nam, text="",bg="white" padx=2 | 0,pady=8 ,relief='solid’) #khung chữ thứ hai

N2.place(x=375,y=330) # vị trí của khung chữ thứ hai

N3 = Label(Nam, text="",bg="white" padx=2 | 0,pady=45,relief='solid))#khung chtr thir Ba

N3.place(x=375,y=215) # vi tri cua khung chữ thứ Ba

N4 = Label(Nam, text="",bg="white" padx=400,pady=106,relief='solid’) #khung chữ thir hai

N4.place(x=6, y=370) # vi tri cua khung chtr thir hai

NTT = ImageTk.PhotoImage(Image.open("D:\\GiaoDien\\_LG80.jpg")) #khung anh Nguyễn Tắt Thành

TNTT = LabelGimage=NTT) TNTT.place(x=50,y=0) # vi tri cua khung anh TieuDe = Label(Nam, text="Phan Loại Hàng Hóa Tự Động Bang Quét Mã",font=( bold”,20),fg=red #khung chữ tiêu đề

TieuDe.place(x=130, y=15) #vi tri khung chữ tiêu đề

TVN=Label(Nam, text="Thanh Vién Nhom:",font=(" Arial" ,12),fg='red’) TVN.place(x=380, y=70)

TV1=Label(Nam, text="1. Huynh Hoai Nam 1900005832", font=(" Arial", 10)) TV 1.place(x=390, y=90)

TV2=Label(Nam, text="2. Nguyễn Văn Đồng 1900006916”,font=('Arial",10)) TV2.place(x=390, y=110)

TV3=Label(Nam, text="3. Trần Quốc Bảo 1900006121”,font=('Arial",10)) TV3.place(x=390, y=130)

GVHD=Label(Nam, text="Giao Vién Huong Dan:",font=(" Arial", 12),fg='red’) GVHD.place(x=380, y=155)

GVHD 1=Label(Nam, text="T.s Tran Quang Huy",font=(" Arial",12)) GVHDI.place(x=550, y=l55)

37

MaVach = Label(Nam, text="Ma Vach:",font=(’ Arial", 12),bg="white") #khung chit ma vach

MaVach.place(x=380, y=182)#vi tri cua khung ma vach

HienMV = Label(Nam, text="",fg= "red" ,font=("Arial",12),bg="white") #Khung hiện ma vach

HienMV place(x=500, y=182)#vi tri cua khung hiện mã vạch

HNT = Label(Nam, text="",font=("Arial" ,12),bg="white") HNT.place(x=600,y=335)

TG = Label(Nam, text="",font=(" Arial", 12),bg="white") TG.place(x=380,y=335)

L1 = Label(Nam,relief='solid’) LI.place(x=7,y=70)

STT = Label(Nam,text="STT" ,font=(" Arial", 10),bg="Wwhite") STT-place(x = 382, y=220)

SoMV = Label(Nam,text="Ma Vach",font=(" Arial", 10),bg="white") SoMV.place(x = 450, y=220)

TenSP = Label(Nam,text="Tén San Phẩm",font=('Arial",10),bg="white") TenSP.place(x = 560, y=220)

KhuVuc = Label(Nam,text="Khu Vyc",font=("Arial" ,10),bg="white") KhuVuc.place(x = 700, y=220)

KhungSTT = Entry(Nam, width=5,relief='solid’) KhungSTT.place(x=380,y=240)

KhungSoMV = Entry(Nam, width=20,relief='solid’) KhungSoMV.place(x=420, y=240)

KhungTenSP = Entry(Nam, width=20,relief='solid’) KhungTenSP.place(x=550,y=240)

KhungKhuVuc = Entry(Nam, width=18 ,relief='solid’) KhungKhu Vuc.place(x=680, y=240)

MXN = Label(Nam, text="__", bg="white", padx=1, pady=1, relief='solid’)

MXN. place(x=775, y=184)

file = pathlib.Path(”Lri TueNhanTao.xlsx”) if file.existsQ:

pass else:

file=WorkbookQ) sheet=file.active sheet["A1"]="ID"

sheet["B1"]="Ma San Pham"

sheet["C1"]="Tên Sản Phẩm"

sheet["D1"]="Khu Vuc"

file.save("TriTueNhanTao.xlsx")

def submit():

y=KhungSTT.getQ) z=KhungSoMV.get() z1=KhungTenSP. getQ) yl=KhungKhuVuc.getQ

if y=="" or z=="" or z1=="" or yl==

messagebox.showinfo("Canh Bao","Ban Chua Nhap Du Dtr Liéu") else:

fie=openpyxLload_ workbook( "Trí TueNhan Tao.xlsx") sheet=file.active

sheet.cell(column=Í,row=sheet.max_row+ẽ ,value=y) sheet.cell(column=2,row=sheet.max_row,value=z) sheet.cell(column=3,row=sheet.max_row,value=zl) sheet.cell(column=4,row=sheet.max_row, value=y 1) file.save("TriTueNhanTao.xIsx")

we HH,

file_openQ KhungSTT.delete(O, END) KhungSoMV.delete(O, END) KhungTenSP.delete(O, END) KhungKhuVuc.delete(0, END)

my_frame =Frame(Nam) my_frame.place(x=7, y=372)

39

M =ttk.Treeview(my_frame) def file_openQ:

filename= pathlib.Path('TriTueNhanTao.xlsx") if filename:

try:

filename=r" { }" .format(filename) df=pd.read_excel(filename) except ValueError:

my_label.config(text="File Couldn't Be Open") except FileNotFoundError:

my_label.config(text="File Couldn't Be Found") clear_tree()

M["column"] =list(df.columns) M["show"] ="headings"

for column in M["column"]:

M.heading(column,text=column)

df_rows = df.to_numpy().tolistO for row in df_rows:

M.insert(”","end”,values=row) M.packQ

wb=load_workbook("TriTueNhanTao.xIsx") ws=wb.active

column_b = ws["B"]

for cell in column_b:

global Ghi Ghi = cell. value

def clear_treeQ:

M.delete(*M.get_childrenQ)

my_label=Label(Nam,text=""") my_label.place(x=1,y=1)

40

# def chonQ:

selected = M.s for item in selected:

Na = M.item(item) Nal = Na.get("values") print(Nal[1], Nal[2])

se oH tk He HE

def XoaDLOQ:

filel = openpyxl.load_workbook("TriTueNhanTao.xlsx") sheet] = filel.active

s = int(KhungXoa.getQ) ifs !=1:

sheet1.delete_rows(idx=s,amount=1) filel.save("TriTueNhanTao.xlsx") file_openQ

elif s == 1:

messagebox.showinfo("Cảnh Báo","Bạn không thê xóa dòng số 1") KhungXoa.delete(0, END)

Them=Button(Nam, text=""Them San Pham" ,command=submit,font=("Arial" ,10),relief='solid',fg='green',bg=" white") Them.place(x=380,y=290)

TenXoa = Label(Nam,text="S6 Dong:" font=("Arial",10),bg=" white") TenXoa.place(x = 520, y=293)

KhungXoa = Entry(Nam, width=5,relief='solid’,) KhungXoa.place(x=5 80, y=295)

Xoa=Button(Nam,text="X6a" ,ommand=XoaDL,font=("Arial", 10),relief='solid',fg='gre en',bg="white")

Xoa.place(x=620,y=290)

Upload=Button(Nam,text="Cap Nhat",command=file_open,font=("'Arial", 1 0),relief='solid’,fg='green',bg=" white") Upload.place(x=730, y=290)

file_openO

def TLQ: #khoi chay tro ly ao robot = ""

while True:

4]

Noi = speech_recognition.Recognizer() with speech_recognition.Microphone() as mic:

printc"....") Tieng = Noi.listen(mic)#thu giong nói try:

Ban = Noi.recognize_google(Tieng, language="vi") #lăng nghe bằng tiếng việt except:

Ban=""

if Ban ==":

robot = "”

elif "Xin chao" in Ban:

robot = "Xin Chao Ban”

elif "hôm nay" in Ban:

homnay = date.todayQ robot = homnay.strftime('%d %B %Y") elif "gian" in Ban:

ThoiGian = datetime.nowQ robot = ThoiGian.strftime("%H : %M : %S") elif "Google" in Ban:

wb.open(“https://www.google.com.vn/?hl=vi"

robot = ("dang mo Google") elif "dong" in Ban:

robot = ("dang dong lai”) print('Tro Ly: " + robot) GN.say('Đã đóng”) break

elif "YouTube" in Ban:

robot="Dang mo"

wb.open(“https://www.youtube.com/") elif "ban là ai" in Ban:

robot =” Tôi tên là kim giang là trợ ly ảo của bạn”

elif "xoá dữ liệu” in Ban:

robot ="Bạn muốn xóa dòng nào”

print("Ban: " + Ban) prin((C “Trợ Lý: ” + robot) GN.say(robot) GN.runAndWaitQ)

TroLy=Button(Nam,text="Tro Ly" ,command=TL,font=("' Arial" ,10),relief='solid',fg='red' ,bg=" white") TroLy.place(x=745,y=333)

while True:

NTN = date.todayQ #ngay thang HTNTN = NTN: strftime("%d %B %Y") t = time.localtimeQ #phút giây t = time.strftime("%H:%M:%S", t)

1mg = Hinh.readO|[ I | 1mg = cv2.cvtColor(mg, cv2.COLOR_BGR2RGB) for barcode in decode(img): #nhan dién ma vach

print(barcode.data) MV = barcode.data.decode(‘utf-8') print(Mv)

if MV in Ghi:

XN = 'Xac Nhan' MXN = Label(Nam, text=" ", bg=”green”, padx=l, pady=[l,relief= solid) MXN.place(x=775, y=l84)

else:

XN = 'Chua Xac Nhan' MXN = Label(Nam, text="___", bg="red", padx=1, pady=1 ,relief='solid’) MXN. place(x=775, y=184)

pts = np.array([barcode.polygon], np.int32) pts = pts.reshape((-1, 1, 2))

cv2.polylines(img, [pts], True, (255, 0, 255), 5) pts2 = barcode.rect

cv2.putText(img, XN,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,0.9,(255,0,255),2)

HienMV.configure(text=MV) def XNCQ:

time.sleep(0.5) if MV in Ghi:

GN.say( Mã Sản Phâm La:" + MV) else:

GN.say("San Phâm Chưa Được Thêm") Xne = Thread(target=XNC)

Xnc.startQ

HNT.configure(text=HTNTN) TG.configure(text=t) img = ImageTk.PhotoImage(Image.fromarray(img)) L1[‘image'] = img

43

Một phần của tài liệu đồ án trí tuệ nhân tạo đề tài phân loại hàng hóa bằng đọc mã vạch 2 (Trang 36 - 44)

Tải bản đầy đủ (PDF)

(44 trang)