pn_recognize_fail_YLKK.py

发布时间 2023-10-06 22:18:17作者: 苦逼yw

 

 

#!/usr/bin/python3
import os
import sys
import re
import pymysql
import time
from datetime import timedelta
from datetime import datetime
import logging
import pandas as pd
import requests
from clickhouse_driver import Client
"""
    统计市级卡口的港澳过车总数,识别率
"""
if __name__ == '__main__':
    logging.basicConfig(filename=os.path.dirname(os.path.abspath(__file__)) + "/pn_recognize_fail.log",level=logging.DEBUG)
    try:
        cursor = Client(host='68.109.211.36', port=9001, password='Yisa_fs_2021')
    except:
        logging.info("lighting连接失败!")
        sys.exit(1)
    yesterday = (datetime.now() - timedelta(days = 1)).strftime("%Y-%m-%d")
    query_time = yesterday
    #ct = "date >= '2022-07-01' and date < '2022-07-04'"
    sql = "select license_plate2,xgbdp,ambdp,VIIDOBJECTID,location_id,capture_time,image_url1,location_id  from yisa_oe.vehicle_all where (license_plate2 like '粤Z%澳' or license_plate2 LIKE '粤Z%港') and date = {}".format(query_time)
    try:
        results = cursor.execute(sql)
        sql = "select  count(*)  from yisa_oe.vehicle_all where license_plate2 LIKE '粤Z%港' and date = {}".format(query_time)
        xg_len = cursor.execute(sql)
        sql = "select  count(*)  from yisa_oe.vehicle_all where license_plate2 LIKE '粤Z%澳' and date = {}".format(query_time)
        am_len = cursor.execute(sql)
    except:
        logging.error("lighting语句执行错误!")
        sys.exit(1)
    try:
        mysql_db = pymysql.connect(host='68.109.211.67',user='yisa_oe',password='Yisa_fs_2021',database='yisa_oe')
    except:
        logging.info("mysql连接失败!")
        sys.exit(1)
    pn_list = [] # 元素是列表,0:卡口名称,1:卡口id,2:香港内地牌,3:香港本地牌,4:澳门内地牌,5:澳门本地牌,6:香港识别率,7:澳门识别率
    am_recognize = 0
    for row in results:
        row_list = list(row)
        tmp_list = ['','',0,0,0,0,0,0,0]
        localtion_id = int(row_list[7])
        cursor = mysql_db.cursor()
        if  row_list[0]: # 二次识别成功
            if re.findall(r"澳+",row_list[0]): # 二次识别是澳牌
                tmp_list[4] = 1
                try:
                    sql = "select  pointname,PROVIDER from location where id = {};".format(localtion_id)
                    #print(sql)
                    cursor.execute(sql)
                    result = cursor.fetchall()
                    if result:
                        pn = result[0][0]
                        tmp_list[0] = pn
                        tmp_list[1] = result[0][1]
                    else:
                        pn = '缺失点位'
                        continue
                except:
                    logging.error("mysql语句执行错误!")
                    sys.exit(1)
                if  row_list[2]: #识别澳门本地牌成功
                    tmp_list[5] = 1
                    
            if re.findall(r"港+",row_list[0]):
                tmp_list[2] = 1
                try:
                    sql = "select  pointname,PROVIDER from location where id = {};".format(localtion_id)
                    #print(sql)
                    cursor.execute(sql)
                    result = cursor.fetchall()
                    if result:
                        pn = result[0][0]
                        tmp_list[0] = pn
                        tmp_list[1] = result[0][1]
                    else:
                        pn = '缺失点位'
                        continue
                except:
                    logging.error("mysql语句执行错误!")
                    sys.exit(1)
                if row_list[1]: #识别香港本地牌成功
                    tmp_list[3] = 1
            if pn_list:
                flag = 0
                for i in range(len(pn_list)):
                    if tmp_list[0] in pn_list[i]:
                        pn_list[i][2] = tmp_list[2] + pn_list[i][2]
                        pn_list[i][3] = tmp_list[3] + pn_list[i][3]
                        pn_list[i][4] = tmp_list[4] + pn_list[i][4]
                        pn_list[i][5] = tmp_list[5] + pn_list[i][5]
                        flag = 1
                if flag == 0: #pn_list没有这个卡口
                    pn_list.append(tmp_list)
            else:
                pn_list.append(tmp_list)
    for i in range(len(pn_list)):
        #print(pn_list[i][2],pn_list[i][3],pn_list[i][4],pn_list[i][5])
        flag1 = 0
        flag2 = 0
        pn_list[i][6] = pn_list[i][2] + pn_list[i][4]
        if pn_list[i][2] == 0:
            pn_list[i][7] = '0' + '%'
            flag1 = 1
        if pn_list[i][4] == 0:
            pn_list[i][8] = '0' + '%'
            flag2 = 1
        if flag1 == 0:
            a = (pn_list[i][3] / pn_list[i][2]) * 100
            xg_recognize_rate = round(a ,2)
            pn_list[i][7] = str(xg_recognize_rate) + '%'
        if flag2 == 0:
            b = (pn_list[i][5] / pn_list[i][4]) * 100
            am_recognize_rate = round(b ,2)
            pn_list[i][8] = str(am_recognize_rate) + '%'
    sort_pn_list = sorted(pn_list,key=(lambda x:x[6]),reverse=True)
    df = pd.DataFrame(sort_pn_list,columns=['卡口名称','卡口ID','香港内地牌','香港外地牌','澳门内地牌','澳门本地牌','港澳过车总数','香港识别率','澳门识别率'])
    #df.to_csv('pn_recognize_fail.csv',index=False)
    df2 = pd.read_excel('/home/mypy/YLKK.xlsx')
    #df2 = pd.read_excel('/home/mypy/vehicle_barrier_province_export.xlsx')    
    df['卡口ID'] = df['卡口ID'].astype(int)
    #print('df_type: {}'.format(df['卡口ID'].dtypes))
    #print('df2_type: {}'.format(df2['proid'].dtypes))
    df3 = df[df['卡口ID'].isin(df2['proid'].values)] #导出csv的卡口
    l = [] #无港澳车经过的卡口
    for i in df2['proid'].values:
        tmp_list = [['无港澳过车数据',0,0,0,0,0,0,0,0]]
        if i not in df3['卡口ID'].values:
            l.append(i)
            sql = "select  pointname,PROVIDER from location where PROVIDER = '{}';".format(i)
            cursor.execute(sql)
            result = cursor.fetchall()
            if not result:
                print('不存在点位名称的proid: {}'.format(i))
                pn = '未找到点位名称'
            else:
                pn = result[0][0]
            tmp_list[0][0] = pn
            tmp_list[0][1] = i
            tmp_df = pd.DataFrame(tmp_list,columns=['卡口名称','卡口ID','香港内地牌','香港外地牌','澳门内地牌','澳门本地牌','港澳过车总数','香港识别率','澳门识别率'])
            df3 = df3.append(tmp_df,ignore_index = True)
    csv_name = '一类卡口港澳车情况-' + yesterday '.csv'
    df3.to_csv('/home/mypy/count/YLKK/{}'.format(csv_name),index=False)
    print(df3.head())
    #print(l)