开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開APP
          userphoto
          未登錄

          開通VIP,暢享免費電子書等14項超值服

          開通VIP
          python實現(xiàn)小區(qū)車牌識別計費程序
          userphoto

          2022.06.20 福建

          關(guān)注

          示例簡介

          python利用百度文字識別功能,實現(xiàn)小區(qū)車輛進出識別,并顯示進出信息和收費信息;可查看停車信息,也可查看歷史數(shù)據(jù);

          開發(fā)環(huán)境:Windows7+python3.7+pycharm2018.2.4(開發(fā)工具);

          目錄機構(gòu):

          最終效果:由于涉及隱私,不能上傳動態(tài)效果;左邊為攝像頭的畫面(這里是視頻播放頁面),右上角顯示車位情況和最近十條車輛信息,右下角顯示識別信息、收費信息或提示信息;

          百度端創(chuàng)建應(yīng)用

          創(chuàng)建過程參考《微信小程序利用百度AI實現(xiàn)掃描身份證獲取信息功能》(把“圖像識別”換為“文字識別”)。

          實現(xiàn)過程

          1、文件“settings.py”用來設(shè)置該程序的基礎(chǔ)信息;

          class Settings(): def __init__(self): ''' 初始化設(shè)置 ''' # 屏幕設(shè)置(寬、高、背景色、線顏色) self.screen_width = 1000 self.screen_height = 484 self.bg_color = (255, 255, 255) # 停車位 self.total = 100 # 識別顏色、車牌號、進來時間、出入場信息 self.ocr_color = (212, 35, 122) self.carnumber = '' self.comeInTime = '' self.message = ''

          2、文件“timeutil.py”有兩個函數(shù):

          函數(shù)time_cmp主要用來比較出場時間跟卡有效期,判斷業(yè)主是否需要收費;

          函數(shù)priceCalc用來計算停車時間,里面存在兩種情況,一種是外來車,只需要比較出入場時間差;另一種是業(yè)主車,入場時,卡未到期,但出場時間已經(jīng)到期,所以需要比較卡有效期和出場時間的差值;

          Tips:由于讀取Excel的卡有效期字段,會多“.xxxx”這部分,所以需要經(jīng)過split('.')處理。

          import datetimeimport math# 計算兩個日期大小def time_cmp(first_time, second_time):    # 由于有效期獲取后會有小數(shù)數(shù)據(jù)    firstTime = datetime.datetime.strptime(str(first_time).split('.')[0], '%Y-%m-%d %H:%M:%S')    secondTime = datetime.datetime.strptime(str(second_time), '%Y-%m-%d %H:%M')    number = 1 if firstTime > secondTime else 0    return number# 計算停車時間四舍五入def priceCalc(inDate, outDate):    if '.' in str(inDate):        inDate = str(inDate).split('.')[0]        inDate = datetime.datetime.strptime(inDate, '%Y-%m-%d %H:%M:%S')        print('特殊處理')    else:        inDate = datetime.datetime.strptime(inDate, '%Y-%m-%d %H:%M')    outDate = datetime.datetime.strptime(str(outDate), '%Y-%m-%d %H:%M')    rtn = outDate - inDate    # 計算停車多少小時(往上取整)    y = math.ceil(rtn.total_seconds() / 60 / 60)    return y

          3、文件“button.py”用來繪制按鈕,這里用來繪制“識別”按鈕;

          import pygame.fontclass Button(): def __init__(self, screen, msg): '''初始化按鈕的屬性''' self.screen = screen self.screen_rect = screen.get_rect() # 設(shè)置按鈕的尺寸和其他屬性 self.width, self.height = 100, 50 self.button_color = (0, 120, 215) self.text_color = (255, 255, 255) self.font = pygame.font.SysFont('SimHei', 25) # 創(chuàng)建按鈕的rect對象,并使其居中 self.rect = pygame.Rect(0, 0, self.width, self.height) # 創(chuàng)建按鈕的rect對象,并設(shè)置按鈕中心位置 self.rect.centerx = 640 - self.width / 2 + 2 self.rect.centery = 480 - self.height / 2 + 2 # 按鈕的標簽只需創(chuàng)建一次 self.prep_msg(msg) def prep_msg(self, msg): '''將msg渲染為圖像,并使其在按鈕上居中''' self.msg_image = self.font.render(msg, True, self.text_color, self.button_color) self.msg_image_rect = self.msg_image.get_rect() self.msg_image_rect.center = self.rect.center def draw_button(self): # 繪制一個用顏色填充的按鈕,再繪制文本 self.screen.fill(self.button_color, self.rect) self.screen.blit(self.msg_image, self.msg_image_rect)

          4、文件“textboard.py”用來繪制背景和文字;

          import pygame.font# 線顏色line_color = (0, 0, 0)# 顯示文字信息時使用的字體設(shè)置text_color = (0, 0, 0)def draw_bg(screen):    # 背景文圖案    bgfont = pygame.font.SysFont('SimHei', 15)    # 繪制橫線    pygame.draw.aaline(screen, line_color, (662, 30), (980, 30), 1)    # 渲染為圖片    text_image = bgfont.render('識別信息:', True, text_color)    # 獲取文字圖像位置    text_rect = text_image.get_rect()    # 設(shè)置文字圖像中心點    text_rect.left = 660    text_rect.top = 370    # 繪制內(nèi)容    screen.blit(text_image, text_rect)# 繪制文字(text-文字內(nèi)容、xpos-x坐標、ypos-y坐標、fontSize-字體大小)def draw_text(screen, text, xpos, ypos, fontsize, tc=text_color):    # 使用系統(tǒng)字體    xtfont = pygame.font.SysFont('SimHei', fontsize)    text_image = xtfont.render(text, True, tc)    # 獲取文字圖像位置    text_rect = text_image.get_rect()    # 設(shè)置文字圖像中心點    text_rect.left = xpos    text_rect.top = ypos    # 繪制內(nèi)容    screen.blit(text_image, text_rect)

          5、文件“ocrutil.py”用來調(diào)用百度文字識別SDK,獲取圖片中的車牌信息;

          Tips:文件“test.jpg”為從攝像頭讀取的圖片,每次循環(huán)獲取一次,這里為了測試方便使用視頻;

          from aip import AipOcrimport os# 百度識別車牌# 申請地址 https://login.bce.baidu.com/filename = 'file/key.txt' # 記錄申請的Key的文件位置if os.path.exists(filename): # 判斷文件是否存在 with open(filename, 'r') as file: # 打開文件 dictkey = eval(file.readlines()[0]) # 讀取全部內(nèi)容轉(zhuǎn)換為字典 # 以下獲取的三個Key是進入百度AI開放平臺的控制臺的應(yīng)用列表里創(chuàng)建應(yīng)用得來的 APP_ID = dictkey['APP_ID'] # 獲取申請的APIID API_KEY = dictkey['API_KEY'] # 獲取申請的APIKEY SECRET_KEY = dictkey['SECRET_KEY'] # 獲取申請的SECRETKEYelse: print('請先在file目錄下創(chuàng)建key.txt,并且寫入申請的Key!格式如下:' '\n{'APP_ID':'申請的APIID', 'API_KEY':'申請的APIKEY', 'SECRET_KEY':'申請的SECRETKEY'}')# 初始化AipOcr對象client = AipOcr(APP_ID, API_KEY, SECRET_KEY)# 讀取文件def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read()# 根據(jù)圖片返回車牌號def getcn(): # 讀取圖片 image = get_file_content('images/test.jpg') # 調(diào)用車牌識別 results = client.licensePlate(image)['words_result']['number'] # 輸出車牌號 return results

          6、文件“procedure_functions.py”存放跟程序相關(guān)的業(yè)務(wù)邏輯函數(shù);

          里面比較復(fù)雜的就是點擊“識別”按鈕后的邏輯處理(event.type == pygame.MOUSEBUTTONDOWN):

          1)當(dāng)停車場沒有停車時,只需要識別后,把車輛信息存入“停車場車輛表”并把相關(guān)信息顯示到界面右下角;

          2)當(dāng)停車場已有停車時,會出現(xiàn)兩種情況,一種是入場,一種是出場:

          入場需判斷是否停車場已滿,已滿則不給進入并顯示提示信息;未滿則把車輛信息存入“停車場車輛表”并把相關(guān)信息顯示到界面右下角;

          出場分業(yè)主有效、業(yè)主過期、外來車三種情況收費,并刪除車輛表相應(yīng)的車輛信息,并把車輛信息和收費信息等存入“停車場歷史表”(可用于后面數(shù)據(jù)的匯總統(tǒng)計);

          import sysimport pygameimport timeimport pandas as pdimport ocrutilimport timeutil# 事件def check_events(settings, recognition_button, ownerInfo_table, carInfo_table, history_table, path):    ''' 響應(yīng)按鍵和鼠標事件 '''    for event in pygame.event.get():        if event.type == pygame.QUIT:            sys.exit()        elif event.type == pygame.MOUSEBUTTONDOWN:            mouse_x, mouse_y = pygame.mouse.get_pos()            button_clicked = recognition_button.rect.collidepoint(mouse_x, mouse_y)            if button_clicked:                try:                    # 獲取車牌                    carnumber = ocrutil.getcn()                    # 轉(zhuǎn)換當(dāng)前時間 2018-12-11 16:18                    localtime = time.strftime('%Y-%m-%d %H:%M', time.localtime())                    settings.carnumber = '車牌號碼:' + carnumber                    # 判斷進入車輛是否業(yè)主車輛                    # 獲取業(yè)主車輛信息(只顯示卡未過期)                    ownerInfo_table = ownerInfo_table[ownerInfo_table['validityDate'] > localtime]                    owner_carnumbers = ownerInfo_table[['carnumber', 'validityDate']].values                    carnumbers = ownerInfo_table['carnumber'].values                    # 獲取車輛表信息                    carInfo_carnumbers = carInfo_table[['carnumber', 'inDate', 'isOwner', 'validityDate']].values                    cars = carInfo_table['carnumber'].values                    # 增加車輛信息                    append_carInfo = {                        'carnumber': carnumber                    }                    # 增加歷史信息                    append_history = {                        'carnumber': carnumber                    }                    carInfo_length = len(carInfo_carnumbers)                    # 車輛表未有數(shù)據(jù)                    if carInfo_length == 0:                        print('未有車輛數(shù)據(jù)入場')                        in_park(owner_carnumbers, carnumbers, carInfo_table, append_carInfo, carnumber, localtime, settings, path)                    # 車輛表有數(shù)據(jù)                    else:                        if carnumber in cars:                            # 出停車場                            i = 0                            for carInfo_carnumber in carInfo_carnumbers:                                if carnumber == carInfo_carnumber[0]:                                    if carInfo_carnumber[2] == 1:                                        if timeutil.time_cmp(carInfo_carnumber[3], localtime):                                            print('業(yè)主車,自動抬杠')                                            msgMessage = '業(yè)主車,可出停車場'                                            parkPrice = '業(yè)主卡'                                        else:                                            print('業(yè)主車,但卡已過期,收費抬杠')                                            # 比較卡有效期時間                                            price = timeutil.priceCalc(carInfo_carnumber[3], localtime)                                            msgMessage = '停車費用:' + str(5 * int(price)) + '(提醒業(yè)主,卡已到期)'                                            parkPrice =  5 * int(price)                                    else:                                        print('外來車,收費抬杠')                                        # 比較入場時間                                        price = timeutil.priceCalc(carInfo_carnumber[1], localtime)                                        msgMessage = '停車費用:' + str(5 * price)                                        parkPrice = 5 * int(price)                                    print(i)                                    carInfo_table = carInfo_table.drop([i])                                    # 增加數(shù)據(jù)到歷史表                                    append_history['inDate'] = carInfo_carnumber[1]                                    append_history['outData'] = localtime                                    append_history['price'] = parkPrice                                    append_history['isOwner'] = carInfo_carnumber[2]                                    append_history['validityDate'] = carInfo_carnumber[3]                                    history_table = history_table.append(append_history, ignore_index=True)                                    settings.comeInTime = '出場時間:' + localtime                                    settings.message = msgMessage                                    # 更新車輛表和歷史表                                    pd.DataFrame(carInfo_table).to_excel(path + '停車場車輛表' + '.xlsx', sheet_name='data',                                                                         index=False, header=True)                                    pd.DataFrame(history_table).to_excel(path + '停車場歷史表' + '.xlsx', sheet_name='data',                                                                         index=False, header=True)                                    break                                i += 1                        else:                            # 入停車場                            print('有車輛表數(shù)據(jù)入場')                            if carInfo_length < settings.total:                                in_park(owner_carnumbers, carnumbers, carInfo_table, append_carInfo, carnumber, localtime, settings, path)                            else:                                print('停車場已滿')                                settings.comeInTime = '進場時間:' + localtime                                settings.message = '停車場已滿,無法進入'                except Exception as e:                    print('錯誤原因:', e)                    continue                pass# 車輛入停車場def in_park(owner_carnumbers, carnumbers, carInfo_table, append_carInfo, carnumber, localtime, settings, path):    if carnumber in carnumbers:        for owner_carnumber in owner_carnumbers:            if carnumber == owner_carnumber[0]:                print('業(yè)主車,自動抬杠')                msgMessage = '提示信息:業(yè)主車,可入停車場'                append_carInfo['isOwner'] = 1                append_carInfo['validityDate'] = owner_carnumber[1]                # 退出循環(huán)                break    else:        print('外來車,識別抬杠')        msgMessage = '提示信息:外來車,可入停車場'        append_carInfo['isOwner'] = 0    append_carInfo['inDate'] = localtime    settings.comeInTime = '進場時間:' + localtime    settings.message = msgMessage    # 添加信息到車輛表    carInfo_table = carInfo_table.append(append_carInfo, ignore_index=True)    # 更新車輛表    pd.DataFrame(carInfo_table).to_excel(path + '停車場車輛表' + '.xlsx', sheet_name='data',                                         index=False, header=True)

          7、文件“main.py”為程序的主函數(shù),用來初始化程序,并同步更新程序的信息。

          import pygameimport cv2import osimport pandas as pd# 引入自定義模塊from settings import Settingsfrom button import Buttonimport textboardimport procedure_functions as pfdef run_procedure(): # 獲取文件的路徑 cdir = os.getcwd() # 文件夾路徑 path = cdir + '/file/' # 讀取路徑 if not os.path.exists(path + '停車場車輛表' + '.xlsx'): # 車牌號 進入時間 離開時間 價格 是否業(yè)主 carnfile = pd.DataFrame(columns=['carnumber', 'inDate', 'outData', 'price', 'isOwner', 'validityDate']) # 生成xlsx文件 carnfile.to_excel(path + '停車場車輛表' + '.xlsx', sheet_name='data') carnfile.to_excel(path + '停車場歷史表' + '.xlsx', sheet_name='data') settings = Settings() # 初始化并創(chuàng)建一個屏幕對象 pygame.init() pygame.display.set_caption('智能小區(qū)車牌識別系統(tǒng)') ic_launcher = pygame.image.load('images/icon_launcher.png') pygame.display.set_icon(ic_launcher) screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) try: # cam = cv2.VideoCapture(0) # 開啟攝像頭 cam = cv2.VideoCapture('file/test.mp4') except: print('請連接攝像頭') # 循環(huán)幀率設(shè)置 clock = pygame.time.Clock() running = True # 開始主循環(huán) while running: screen.fill(settings.bg_color) # 從攝像頭讀取圖片 sucess, img = cam.read() # 保存圖片,并退出。 if sucess: cv2.imwrite('images/test.jpg', img) else: # 識別不到圖片或者設(shè)備停止,則退出系統(tǒng) running = False # 加載圖像 image = pygame.image.load('images/test.jpg') # 設(shè)置圖片大小 image = pygame.transform.scale(image, (640, 480)) # 繪制視頻畫面 screen.blit(image, (2, 2)) # 創(chuàng)建識別按鈕 recognition_button = Button(screen, '識別') recognition_button.draw_button() # 讀取文件內(nèi)容 ownerInfo_table = pd.read_excel(path + '住戶車輛表.xlsx', sheet_name='data') carInfo_table = pd.read_excel(path + '停車場車輛表.xlsx', sheet_name='data') history_table = pd.read_excel(path + '停車場歷史表.xlsx', sheet_name='data') inNumber = len(carInfo_table['carnumber'].values) # 繪制背景 textboard.draw_bg(screen) # 繪制信息標題 textboard.draw_text(screen, '共有車位:' + str(settings.total) + ' 剩余車位:' + str(settings.total - inNumber), 680, 0, 20) # 繪制信息表頭 textboard.draw_text(screen, ' 車牌號 進入時間', 700, 40, 15) # 繪制停車場車輛前十條信息 carInfos = carInfo_table.sort_values(by='inDate', ascending=False) i = 0 for carInfo in carInfos.values: if i >= 10: break i += 1 textboard.draw_text(screen, str(carInfo[1])+' '+str(carInfo[2]), 700, 40 + i * 30, 15) # 繪制識別信息 textboard.draw_text(screen, settings.carnumber, 660, 400, 15, settings.ocr_color) textboard.draw_text(screen, settings.comeInTime, 660, 422, 15, settings.ocr_color) textboard.draw_text(screen, settings.message, 660, 442, 15, settings.ocr_color) ''' 響應(yīng)鼠標事件 ''' pf.check_events(settings, recognition_button, ownerInfo_table, carInfo_table, history_table, path) pygame.display.flip() # 控制游戲最大幀率為 60 clock.tick(60) # 關(guān)閉攝像頭 cam.release()run_procedure()
          本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
          打開APP,閱讀全文并永久保存 查看更多類似文章
          猜你喜歡
          類似文章
          使用跨平臺Python模塊Pygame ---- 飛機大戰(zhàn)微信小游戲
          Python寫游戲: PyGame入門
          Python 游戲編程之實現(xiàn)飛機大戰(zhàn)(含源代碼)
          用Python寫一個植物大戰(zhàn)僵尸!簡直一模一樣
          牛逼了!Python 開發(fā)植物大戰(zhàn)僵尸游戲
          玩過貪吃蛇嗎?5 分鐘教你用 Python 實現(xiàn)貪吃蛇!
          更多類似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
          綁定賬號成功
          后續(xù)可登錄賬號暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點擊這里聯(lián)系客服!

          聯(lián)系客服