【Python7】csv/excel/matplotlib,排序/树遍历,线/进程,文件/xml操作,百度人脸API,aiohttp/hal/restful/curl
文章目錄
- 1.csv
- 2.excel
- 3.matplotlib
- 4.時間復(fù)雜度
- 5.順序表/鏈表
- 6.六種排序
- 6.1 選擇
- 6.2 插入
- 6.3 希爾
- 6.4 冒泡
- 6.5 快排
- 6.6 歸并
- 7.樹遍歷
- 8.線/進程
- 8.1 線程
- 8.2 進程
- 8.3 進程池
- 8.4 協(xié)程
- 8.5 GIL
- 9.文件
- 9.1 增:寫入文件內(nèi)容給文本文件
- 9.2 改:批量修改圖片大小
- 9.3 查:查詢文件夾中的文件
- 9.4 查:讀取文件
- 9.5 查:搜索文件夾路徑內(nèi)含有指定內(nèi)容的代碼文件
- 10.xml
- 10.1 labelimg_yolo_txt轉(zhuǎn)pascal voc_xml
- 10.2 刪除 w label
- 10.3 檢查不是people和obstacle的label
- 10.4 讀取指定后綴
- 10.5 檢查是否有圖片漏標(biāo),并刪除漏標(biāo)圖片
- 10.6 檢測標(biāo)記的box是否超過圖片的邊界,若有則顯示刪除與box相關(guān)的xml文件和圖片文件
- 10.7 檢查xmin<0....,并修改xmin....
- 10.8 讀取classname
- 10.9 檢查trainval.txt
- 11.百度人臉API
- 12. hal層
- 12.1 i2c-utils,setup_i2c,i2c_dev_sysfs,syscpld.c
- 12.2 DL_DIR ?= " "
- 13.aiohttp異步框架庫
- 14.restfulAPI
- 15.curl獲取動態(tài)ip
- 15.1 sshpass
- 15.2 restful C&S
1.csv
2.excel
讀:
打印工作表(不是工作簿)名字:
獲取每行:
寫:
3.matplotlib
import numpy as np from matplotlib import pyplot as plt import pandas as pd df =pd.read_excel("Documents/b.xlsx") df plt.figure(figsize=(6,4)) plt.title("Train History") plt.xlabel("Epoch") plt.ylabel("Number of loss")plt.plot(df.c,df.d,"r",marker='*', mec='r', mfc='w') plt.plot(df.a,df.b, marker='*', mec='b', mfc='w') plt.plot(df.g,df.h,marker='*', mfc='w') plt.plot(df.e,df.f,"g",marker='*', mec='g', mfc='w') plt.xticks(range(0,21))plt.legend(["y=CNN-AlexNet-loss","y=CNN-VGGNet-loss","y=CNN-ResNet-loss","y=Improved CNN-ResNet-loss"]) plt.show plt.figure(figsize=(6,4)) plt.title("YOLOV4") plt.xlabel("Batch") plt.ylabel("acc")plt.plot(df.a,df.b,"") plt.plot(df.c,df.d,"") plt.legend(["train","test"]) plt.show plt.figure(figsize=(6,4)) plt.xlabel("Precision") plt.ylabel("Recall")plt.plot(df.a,df.b, marker='o', mec='b', mfc='w') plt.plot(df.c,df.d,"r",marker='o', mec='r', mfc='w') #plt.xticks(range(0,21)) plt.legend(["y=Ours","y=YoloV4"]) plt.show4.時間復(fù)雜度
# 如果a+b+c=1000,且a^2+b^2=c^2(a,b,c為自然數(shù)),如何求出所有a,b,c可能的組合? import time start_time = time.time() for a in range(0,1001):for b in range(0,1001):for c in range(0,1001):if a+b+c==1000 and a**2+b**2==c**2:print('a,b,c:%d,%d,%d'%(a,b,c)) end_time = time.time() print('time:%d'%(end_time-start_time)) print('finished')輸出:
a,b,c:0,500,500
a,b,c:200,375,425
a,b,c:375,200,425
a,b,c:500,0,500
time:203
finished
如下耗時從小到大,用函數(shù)為列表添加元素,因為函數(shù)是基本步驟的封裝,所以不能算作1步
5.順序表/鏈表
程序=數(shù)據(jù)結(jié)構(gòu)+算法:下圖為數(shù)據(jù)的存儲:1個int數(shù)占4個字節(jié)(char或B)(1B=8bit),如下1放在4個字節(jié)中。
如下int型按順序存放即順序表方便查找
下圖左邊為順序表基本形式,右邊為元素外置形式(存地址)
如下是順序表結(jié)構(gòu)
順序表要求存儲空間必須連續(xù),一旦不夠就要動態(tài)改變數(shù)據(jù)區(qū)。線性表分為順序表和鏈表,下圖為鏈表,不用改變原數(shù)據(jù)結(jié)構(gòu),多一個加一個。
6.六種排序
排序算法穩(wěn)定性:按元組中第一個元素大小排序(維持之前次序即如下第一組穩(wěn)定)。
6.1 選擇
from time import time # 計時裝飾器 def timer(func): def inner(*args,**kwargs):start = time()result = func(*args,**kwargs)end = time()usedTime = 1000 * (end - start)print("%s function used %.2f ms" %(func.__name__,usedTime))return resultreturn inner @timer def select_sort(alist):#選擇(遍歷)排序,從右邊選擇最小放左邊n = len(alist)for j in range(n-1):#j:0到n-2min_index=j #假的最小值下標(biāo)動,從第一個開始遍歷for i in range(j+1,n):if alist[min_index]>alist[i]:min_index=ialist[j],alist[min_index]=alist[min_index],alist[j] #這里min_index為真的最小值下標(biāo) if __name__ == "__main__":blist = list(range(1,3000 + 1))import randomblist = random.sample(blist, k=len(blist))alist = blist[:1000]select_sort(alist)print(alist)6.2 插入
@timer def insert_sort(alist):n=len(alist)for j in range(1,n):#從右邊的無序序列中取出多少個元素執(zhí)行這樣的過程#j=[1,2,3,n-1]#i代表內(nèi)層循環(huán)起始值i=j#執(zhí)行從右邊的無序序列中取出第一個元素,即i位置的元素#然后將其插入到前面的正確位置中while i>0:if alist[i]<alist[i-1]:alist[i],alist[i-1]=alist[i-1],alist[i]i-=1else:breakif __name__ == "__main__":blist = list(range(1,3000 + 1))import randomblist = random.sample(blist, k=len(blist))alist = blist[:1000]insert_sort(alist)print(alist)6.3 希爾
選擇排序是從后面無序序列中選一個,放前面最后一位置。
插入排序是從后面無序序列中選一個,插入前面有序序列位置中哪一個(從右往左比對)。
希爾排序是插入排序的改進:對54,77,20按插入排序三三從小到大排序,然后26,31兩兩從小到大排。
下圖第二行是第一行按gap=4排的
6.4 冒泡
@timer def bubble_sort(alist): n = len(alist)for j in range(n-1):for i in range(n-1-j):if alist[i]>alist[i+1]:alist[i],alist[i+1]=alist[i+1],alist[i] if __name__ == "__main__":blist = list(range(1,3000 + 1))import randomblist = random.sample(blist, k=len(blist))#從blist中隨機獲取k個元素alist = blist[:1000]#print(alist)bubble_sort(alist)print(alist)6.5 快排
前面的排序方法都分為左右兩部分。快排中若low指的元素比54大停走,high指的元素比54小停走,則low和high指的元素互相交換,繼續(xù)往中間走。重合時low指的前一個元素就是54位置固定了,54的左右邊分別繼續(xù)low和high操作。
6.6 歸并
下圖拆分兩部分,直到只有一個元素再兩兩比較。
上圖先26和17比,17小拿出來,right指針往后移。26再和93比,26小將26拿出來排在17后。
7.樹遍歷
先序(先根):根左右
中序:投影
后序:左右根(從下到上)
8.線/進程
8.1 線程
如下2(7)是指已借2個,還差7個
8.2 進程
8.3 進程池
多任務(wù)文件夾copy:
8.4 協(xié)程
gevent(協(xié)程)圖片下載:
8.5 GIL
python3 main.py,所以解決GIL:1.換掉cpython解釋器(c是編譯型,python是解釋型)2.用其它語言替換線程。
9.文件
9.1 增:寫入文件內(nèi)容給文本文件
def writeTextFile(filePath, fileContent, encoding='utf8'):with open(filePath, 'w', encoding=encoding) as file:file.write(fileContent)9.2 改:批量修改圖片大小
import os from PIL import Imagedef getFilePathList(dirPath, partOfFileName=''):allFileName_list = list(os.walk(dirPath))[0][2]fileName_list = [k for k in allFileName_list if partOfFileName in k]filePath_list = [os.path.join(dirPath, k) for k in fileName_list]return filePath_listdef batchResizeImage(oldDirPath, newDirPath, height, width):if not os.path.isdir(newDirPath):os.mkdir(newDirPath)jpgFilePath_list = getFilePathList(oldDirPath, '.jpg')for jpgFilePath in jpgFilePath_list:image = Image.open(jpgFilePath)resized_image = image.resize((height, weight), Image.ANTIALIAS)jpgFileName = os.path.split(jpgFilePath)[1]saveFilePath = os.path.join(newDirPath, jpgFileName)resized_image.save(saveFilePath)oldDirPath = 'source_images' newDirPath = 'train_images' height = 640 width = 640 batchResizeImage(oldDirPath, newDirPath, height, width)9.3 查:查詢文件夾中的文件
import osdef getFileNameList(dirPath, partOfFileName=''):allFileName_list = list(os.walk(dirPath))[0][2]fileName_list = [k for k in allFileName_list if partOfFileName in k]return fileName_listdef getFilePathList(dirPath, partOfFileName=''):allFileName_list = list(os.walk(dirPath))[0][2]fileName_list = [k for k in allFileName_list if partOfFileName in k]filePath_list = [os.path.join(dirPath, k) for k in fileName_list]return filePath_list9.4 查:讀取文件
import osdef getdir():for root, dirs, files in os.walk("D:\soft\Dict\8.9.6.0\otherskins\simisent"):#for dir in dirs:print('root {}'.format(root))print('dirs {}'.format(dirs))print('files {}'.format(files)) a=getdir()#11111111111111111111111111111111111111111111111111111111111111111111111 def show_files(path, all_files):file_list = os.listdir(path)for file in file_list:cur_path = os.path.join(path, file)if os.path.isdir(cur_path):show_files(cur_path, all_files)else:all_files.append(file)return all_filescontents = show_files("D:\soft\Dict\8.9.6.0\otherskins\simisent", []) for content in contents: # 循環(huán)打印show_files函數(shù)返回的文件名列表print(content) def readTextFile(filePath, encoding='utf8'):with open(filePath, encoding=encoding) as file:return file.read()9.5 查:搜索文件夾路徑內(nèi)含有指定內(nèi)容的代碼文件
import os # 傳入3個參數(shù):文件夾路徑dirPath、指定內(nèi)容partOfFileContent、代碼文件后綴名suffixOfFileName def searchFileContent(dirPath, partOfFileContent, suffixOfFileName=''):dirPath = os.path.expanduser(dirPath)walk_list = list(os.walk(dirPath))result_list = []for walk in walk_list:filePath_list = [os.path.join(walk[0], k) for k in walk[2] \if k.rsplit('.', maxsplit=1)[1]==suffixOfFileName.strip('.')]for filePath in filePath_list:with open(filePath, encoding='=utf8') as file:fileContent = file.read()if partOfFileContent in fileContent:Wprint(filePath)result_list.append(filePath)return result_list10.xml
10.1 labelimg_yolo_txt轉(zhuǎn)pascal voc_xml
from PIL import Image import os#讀取文件尺寸 def ImgSize(image):img = Image.open(image)w,h = img.width,img.heightreturn w,h#labelimg中yolo轉(zhuǎn)voc圖位轉(zhuǎn)換 #width,height就是原圖的w,h #xmin指中心點占橫比例,xmax指中心點占豎比例 #ymin指bbox占整圖寬比例,ymax指bbox占整圖高比例 def ScaleCovertor(width,height,xmin,xmax,ymin,ymax): center_x = round(float(xmin* width)) center_y = round(float(xmax * height))bbox_width = round(float(ymin * width))bbox_height = round(float(ymax * height))xmin = str(int(center_x - bbox_width / 2 ))ymin = str(int(center_y - bbox_height / 2))xmax = str(int(center_x + bbox_width / 2))ymax = str(int(center_y + bbox_height / 2))return xmin,ymin,xmax,ymaxdef Main(filepath): #filepath是txt文件夾路徑(里面全是需要轉(zhuǎn)換的txt文件) #設(shè)置xml內(nèi)部格式xml_head = '''<annotation><folder>Desktop</folder><filename>{}</filename><path>unknonw</path><source><database>unknow</database></source><size><width>{}</width><height>{}</height><depth>3</depth></size><segmented>0</segmented>'''xml_obj = '''<object> <name>{}</name><pose>no</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>{}</xmin><ymin>{}</ymin><xmax>{}</xmax><ymax>{}</ymax></bndbox></object>'''xml_end = '''</annotation>'''counter = 1 #計數(shù)器for filename in os.listdir(filepath): #現(xiàn)在的filename是帶后綴的print ('Processing:->>',filename,'Number %s'%counter) #打印當(dāng)前文件名 和 第幾個文件#原圖: content=[] #建立內(nèi)容列表,class,中心點占比,bbox占比with open(filepath+'/'+filename,'r') as readlines:for linecontent in readlines.readlines(): #讀取每一行內(nèi)容content.append(linecontent) #添加到列表中 w,h = ImgSize('C:/Users/lenovo/Desktop/yuantu'+'/'+filename.split('.')[0]+'.jpg') #調(diào)用文件尺寸讀取函數(shù)#xml: obj = '' #這里創(chuàng)建xml,建立空字符串head = xml_head.format(str(filename.split('.')[0]+'.jpg'),str(w),str(h)) #向xml head里添加文件名 文件w和hfor info in content: #讀取每個文件里的內(nèi)容infodetail = info.split(' ') #以空格切割列表內(nèi)的數(shù)據(jù)#單獨讀取每個數(shù)據(jù)保存到變量里Class,XMin,XMax,YMin,YMax = infodetail[0],infodetail[1],infodetail[2],infodetail[3],infodetail[4],xmin,ymin,xmax,ymax = ScaleCovertor(w,h,float(XMin),float(XMax),float(YMin),float(YMax))label= {1:'obstacle',0:'people'} #確定label和類的映射關(guān)系,下行用到obj += xml_obj.format(label[int(Class)],xmin,ymin,xmax,ymax) #向主object里循環(huán)添加 一個圖里的物體或類#寫入xml文件with open('C:/Users/lenovo/Desktop/annotation2/xml'+filename.split('.')[0]+'.xml','w') as xmw:#創(chuàng)建寫入 合并 三個 xml主體部分xmw.write(head+obj+xml_end)counter+=1 Main('C:/Users/lenovo/Desktop/annotation2/txt') #txt文件夾 #驗證轉(zhuǎn)的對錯 import matplotlib.pyplot as plt import matplotlib.image as Image #這個讀取庫比較方便 不用把數(shù)據(jù)轉(zhuǎn)來轉(zhuǎn)去,plt可以直接使用 %matplotlib inline img = Image.imread('/Users/Desktop/annotation2/test/yuantu/'+'20190721062948_000394_cc8cdaa5ee38.jpg') #讀取 x1,y1,x2,y2 = 1344, 495, 1722, 1080 # 自己找驗證plt.gca().add_patch ( plt.Rectangle(xy=(x1,y1),width=x2-x1,height=y2-y1,fill=False,edgecolor='red',linewidth=2) )plt.imshow(img) plt.show() #根據(jù)環(huán)境添加10.2 刪除 w label
import re import os rawfolder='123' #存放三張xml的文件夾 newfolder='33333' #生成的新的xml文件夾 for i in os.listdir(rawfolder):print (i) #輸出#20190720073948_000258_cc8cdaa5ee49.xml#20190720073950_000257_cc8cdaa64390.xml#20190720073950_000258_cc8cdaa5ee3e.xmlwith open(rawfolder+'/'+i,'r') as r:content = r.readlines()#print(content) #輸出['<annotation>\n', '\t<folder>img</folder>\n', '\t<filename>20190720073948_000258_cc8cdaa5ee49.JPG</filename>\n', ...]c = 0for j in content:if '<name>w</name>' in j:print (j,'下標(biāo)-》',c) #c為14行<name>w</name>,從0行開始start = 0end = c-1 # c-1為上一行<object> first_part = content[start:end]second_part = content[end+12:] #整個一塊為w的objectfinal = first_part+second_partfor x in final:with open(newfolder+'/'+i,'a+') as w:w.writelines(x)print (x)c+=1# break
10.3 檢查不是people和obstacle的label
# 檢查不是people和obstacle的label import re import os rawfolder='123' #newfolder='33333' for i in os.listdir(rawfolder): # print (i)with open(rawfolder+'/'+i,'r') as r:content = r.readlines() # print(content)for j in content:if '<name>' in j and ('people' not in j and 'obstacle'not in j):print (j)print (i)10.4 讀取指定后綴
import os def get_filePathList(dirPath, partOfFileName=''):all_fileName_list = next(os.walk(dirPath))[2] #['20190720072950_000256_cc8cdaa64390.JPG',#'20190720073948_000258_cc8cdaa5ee49.JPG',# '20190720073950_000257_cc8cdaa64390.JPG',# '20190720074950_000259_cc8cdaa5ee3e .jpg',#'20190720074950_000259_cc8cdaa5ee3e.JPG'] fileName_list = [k for k in all_fileName_list if partOfFileName in k] #去除除了'.JPG'文件,不含前面絕對路徑filePath_list = [os.path.join(dirPath, k) for k in fileName_list] #含全部路徑,['', # '']#return fileName_listreturn filePath_listdirPath='C:/Users/lenovo/Desktop/lian' a=get_filePathList(dirPath,'.JPG') a #print(len(a))10.5 檢查是否有圖片漏標(biāo),并刪除漏標(biāo)圖片
def delete_file(filePath):if not os.path.exists(filePath): #filePath指C:/Users/lenovo/Desktop/lianxi/img\\20190720072950_000256_cc8cdaa64390.JPG'print('%s 這個文件路徑不存在,請檢查一下' %filePath)else:print('%s 這個路徑的文件需手動刪除' %filePath)def check_1(dirPath, suffix): xmlFilePath_list = get_filePathList(dirPath, '.xml') # 與suffix不同,自己指定'.xml'xmlFilePathPrefix_list = [k[:-4] for k in xmlFilePath_list] # 不帶.xmlxmlFilePathPrefix_set = set(xmlFilePathPrefix_list)#print(xmlFilePathPrefix_set) #{'絕對路徑不帶后綴',# ' ' }imageFilePath_list = get_filePathList(dirPath, suffix)imageFilePathPrefix_list = [k[:-4] for k in imageFilePath_list] # 不帶后綴imageFilePathPrefix_set = set(imageFilePathPrefix_list)#print(imageFilePathPrefix_set)redundant_imgFilePathPrefix_list = list(imageFilePathPrefix_set - xmlFilePathPrefix_set)redundant_imgFilePath_list = [k+'.JPG' for k in redundant_imgFilePathPrefix_list]#上行帶.JPG后綴, 如果自定義.0JPG,顯示這個文件路徑不存在,請檢查一下for imgFilePath in redundant_imgFilePath_list: delete_file(imgFilePath)dirPath='C:/Users/lenovo/Desktop/lx' check_1(dirPath,'.JPG')10.6 檢測標(biāo)記的box是否超過圖片的邊界,若有則顯示刪除與box相關(guān)的xml文件和圖片文件
import xml.etree.ElementTree as ET from PIL import Image def check_2(dirPath, suffix):xmlFilePath_list = get_filePathList(dirPath, '.xml')#print(xmlFilePath_list) #['.xml全部路徑',# ' ']allFileCorrect = True # 跳出for循環(huán)則執(zhí)行 if allFileCorrectfor xmlFilePath in xmlFilePath_list:imageFilePath = xmlFilePath[:-4] + '.' + suffix.strip('.')#print(xmlFilePath) #print(imageFilePath)#C:/Users/lenovo/Desktop/lx\20190720072950_000256_cc8cdaa64390.xml#C:/Users/lenovo/Desktop/lx\20190720072950_000256_cc8cdaa64390.JPG#.....image = Image.open(imageFilePath)width, height = image.sizewith open(xmlFilePath) as file:fileContent = file.read()#print(fileContent) #<annotation>...root = ET.XML(fileContent) #根<annotation>...object_list = root.findall('object') # <object>for object_item in object_list:bndbox = object_item.find('bndbox') #<bndbox>xmin = int(bndbox.find('xmin').text)ymin = int(bndbox.find('ymin').text)xmax = int(bndbox.find('xmax').text)ymax = int(bndbox.find('ymax').text)if xmax>xmin and ymax>ymin and xmax<=width and ymax<=height:continueelse:delete_file(xmlFilePath)delete_file(imageFilePath)allFileCorrect = Falsebreakif allFileCorrect:print('祝賀你! 已經(jīng)通過檢驗,所有xml文件中的標(biāo)注框都沒有越界') dirPath='C:/Users/lenovo/Desktop/lx' #lx文件夾里.xml和.JPG混在一起 check_2(dirPath,'.JPG')#''里必須.JPG或不填10.7 檢查xmin<0…,并修改xmin…
#coding=utf-8 import os import shutil import random from xml.etree.ElementTree import ElementTree,Element import cv2def read_xml(in_path):'''讀取并解析xml文件in_path: xml路徑return: ElementTree'''tree = ElementTree()tree.parse(in_path)return treedef check():url = "C:/Users/lenovo/Desktop/source/xml_sum" # xml_sum只存放xml的文件夾for item in os.listdir(url): # item為.xml文件tree = read_xml(url + "/" + item) # read_xml函數(shù)上面定義root = tree.getroot()object = root.findall("object")size = root.find("size")width =int(size.find("width").text)height = int(size.find("height").text)if object == None:print(item)continuefor it in object:bndbox = it.find("bndbox")if bndbox == None:print(item)xmin = int(bndbox.find("xmin").text)xmax = int(bndbox.find("xmax").text)ymin = int(bndbox.find("ymin").text)ymax = int(bndbox.find("ymax").text)if xmin <= 0 or xmin >= xmax or ymin <=0 or ymin >= ymax:print(item)if xmax > width or ymax> height:print(item)if __name__ =='__main__':check() # 不輸出則表示全對。輸出123111.xml,沒有列表引號 import xml.etree.ElementTree as ETdef generateNewXmlFile(old_xmlFilePath, new_xmlFilePath):with open(old_xmlFilePath) as file:fileContent = file.read()root = ET.XML(fileContent)object_list = root.findall('object')for object_item in object_list:bndbox = object_item.find('bndbox')xmin = bndbox.find('xmin')xminValue = int(xmin.text)xmin.text = str(int(xminValue + 1))ymin = bndbox.find('ymin')yminValue = int(ymin.text)ymin.text = str(int(yminValue + 1))xmax = bndbox.find('xmax')xmaxValue = int(xmax.text)xmax.text = str(int(xmaxValue + 1))ymax = bndbox.find('ymax')ymaxValue = int(ymax.text)ymax.text = str(int(ymaxValue + 1))tree = ET.ElementTree(root)tree.write(new_xmlFilePath) old_dirPath ='C:/Users/lenovo/Desktop/999/8' new_dirPath ='C:/Users/lenovo/Desktop/999/9'def batch_modify_xml(old_dirPath, new_dirPath): #修改文件夾中的若干xml文件#以下4行將new_dirPath和xmlFileName名稱結(jié)合,內(nèi)容是調(diào)用generateNewXmlFile函數(shù)改寫xmlFilePath_list = get_filePathList(old_dirPath, '.xml')for xmlFilePath in xmlFilePath_list:xmlFileName = os.path.split(xmlFilePath)[1] #1后#print(xmlFileName) #輸出 20190720073950_000257_cc8cdaa64390.xmlnew_xmlFilePath = os.path.join(new_dirPath, xmlFileName) generateNewXmlFile(xmlFilePath, new_xmlFilePath) batch_modify_xml(old_dirPath, new_dirPath)10.8 讀取classname
def get_classNameList(txtFilePath):with open(txtFilePath, 'r', encoding='utf8') as file:fileContent = file.read()line_list = [k.strip() for k in fileContent.split('\n') if k.strip()!='']className_list= sorted(line_list, reverse=False)return className_list txtFilePath='C:/Users/lenovo/Desktop/labelImg/data/predefined_classes -outofstock.txt' get_classNameList(txtFilePath) # 添加環(huán)境變量 import sys sys.path.append('') import os pathnoname,name=os.path.split("E:/lpthw/zedshaw/ex19.py") print(pathnoname) print(name)10.9 檢查trainval.txt
import cv2 from os import listdir from os.path import isfile,isdir,jointrainval_list = list() with open('./trainval.txt','r') as f:for line in f.readlines():line = line.strip('\n')a = line +'.jpg'trainval_list.append(a) print(trainval_list) for i in trainval_list:img_path = '{}{}'.format('./img3/',i)img = cv2.imread(img_path) try:img.shapeprint(img.shape) # 在img3文件夾中沒有......11111.jpg圖片except:print('fail read:' + img_path)continue11.百度人臉API
對WEB API接口測試(通常對服務(wù)端做的多)。
API接口消息就是業(yè)務(wù)邏輯處理如下。
https://ai.baidu.com/docs#/Auth/top,如下獲取Access Token。
12. hal層
如下3個和meta-s3ip(conf,recipes-bsp/core/kernel/plats/utils)并列。
12.1 i2c-utils,setup_i2c,i2c_dev_sysfs,syscpld.c
1.openbmc-holly/common/recipes-utils/openbmc-utils/files/i2c-utils.sh
SYSFS_I2C_ROOT="/sys/bus/i2c" SYSFS_I2C_DEVICES="${SYSFS_I2C_ROOT}/devices" SYSFS_I2C_DRIVERS="${SYSFS_I2C_ROOT}/drivers"# Return the given i2c device's absolute sysfs path. # $1 - i2c device in <bus>-00<add> format (for example 0-0050). i2c_device_sysfs_abspath() {echo "${SYSFS_I2C_DEVICES}/${1}" }# instantiate an i2c device. # $1 - parent bus number # $2 - device address # $3 - device name/type i2c_device_add() {bus=$"$1"addr="$2"device="$3"echo "$device" "$addr" > "${SYSFS_I2C_DEVICES}/i2c-${bus}/new_device" }# delete an i2c device. # $1 - parent bus number # $2 - device address i2c_device_delete() {bus=$"$1"addr="$2"echo "$addr" > "${SYSFS_I2C_DEVICES}/i2c-${bus}/delete_device" }2.openbmc-holly/meta-hua/meta-holly/recipes-utils/openbmc-utils/files/setup_i2c.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin . /usr/local/bin/openbmc-utils.sh#register cmm cpld at first since query psu/lc/fcb status will relay on it # Bus8 # CH2 i2c_device_add 82 0x0d cmmcpld# Bus0 LC1 # 0-0077 # CH0 i2c_device_add 16 0x0d lccpld ret=$(is_lc_ok 1) if [ $ret -eq 0 ];then# CH1i2c_device_add 17 0x50 24c64 #LC1 EEPROM# CH2i2c_device_add 18 0x50 24c64 #LC1 COME EEPROM fi # CH3 i2c_device_add 19 0x0d fpgacpld #LC1 FPGA3.openbmc-hollywood/common/recipes-kernel/i2c-dev-sysfs-mod/files/i2c_dev_sysfs.h
typedef struct i2c_dev_attr_st_ {const char *ida_name;const char *ida_help;i2c_dev_attr_show_fn ida_show;i2c_dev_attr_store_fn ida_store;int ida_reg;int ida_bit_offset;int ida_n_bits;unsigned int enable_log_on_write:1; } i2c_dev_attr_st;4.openbmc-hua/meta-hua/meta-hollywood/recipes-kernel/cpld-mod/files/syscpld.c
static const i2c_dev_attr_st syscpld_attr_table[] = {{"psu_led_color","0x0: green\n""0x1: yellow\n""0x2: alternate", //echo 0x0 > psu_led_color文件,相當(dāng)于i2csetI2C_DEV_ATTR_SHOW_DEFAULT, // 可讀I2C_DEV_ATTR_STORE_DEFAULT, // 可寫0x40, 4, 2, // 如下第4個字節(jié)開始占兩位}, //一個字節(jié) = 0x(8421)(8421)。source /usr/local/bin/openbmc-utils.sh ( 里面source了很多-utils.sh,如board-utils.sh ) 后就可用get_psu_power_sta 1(board-utils.sh中),tail -f /var/log/messages。
12.2 DL_DIR ?= " "
github上clone下來的openbmc沒有源碼文件如uboot、kernel、fs文件系統(tǒng)等,而是在編譯中過程中根據(jù)配置文件中SRC_URL來下載需要的源碼文件,不修改配置文件,默認(rèn)將下載下來的源碼存放在build/downloads文件夾來下。如果已下載過這些源碼文件,可以修改DL_DIR,指定到你下載文件的路徑。
1.創(chuàng)建layer:創(chuàng)建頂層目錄mkdir -p meta-live,cp -r meta-ibm/meta-palmetto meta-live/meta-test。2.三個配置文件:修改meta-live/meta-test/conf/ layer.conf文件,將palmetto替換成test。修改meta-live/meta-test/conf/ bblayers.conf.sample文件,將改文件中的“/meta-ibm/meta-palmetto”修改為“/meta-live/meta-test”。修改meta-live/meta-test/conf/local.conf.sample文件,將“MACHINE ??= “palmetto””改為“MACHINE ??= “test””。修改meta-live/meta-test/machine/palmetto.conf文件為test.conf。
13.aiohttp異步框架庫
localhost:是一個域名,默認(rèn)指向127.0.0.1這ip(本地回環(huán)地址,速度快,不會出現(xiàn)在主機外部網(wǎng)絡(luò)),綁定了localhost的服務(wù)只能在本機訪問。0.0.0.0:表示所有IP地址,如一個tomcat配置文件中監(jiān)聽的IP地址設(shè)置了0.0.0.0 就表示你的這個tomcat服務(wù)器監(jiān)聽在本機的所有IP地址上,通任何一個IP地址都可以訪問到:如本地的IP地址有 192.168.1.10,那么訪問這個tomcat就可http://192.168.1.10:8080/訪問。
# a.py:創(chuàng)建一個后端app應(yīng)用 from aiohttp import web # from application.routes import setup_routes#1111111111111111111111111111111111111111111111111111111111111111111111 app = web.Application() # setup_routes(app) web.run_app(app, host='0.0.0.0', port=9000)如下python a.py。在瀏覽器中打開http://localhost:9000/或者使用命令curl -X GET http://localhost:9000,對于請求現(xiàn)在只會返回404: Not Found。
如下創(chuàng)建一個視圖(返回值)和路由來展示:在a.py中將注釋的兩行放開運行,$ curl -X GET localhost:9000/hello ,Hello Aiohttp!
14.restfulAPI
# board_endpoint.py:視圖即response import re import rest_help import rest_psu import rest_fan from aiohttp import web from rest_utils import dumps_bytestrclass boardApp_Handler:async def rest_help_hdl(self, request):return web.json_response( rest_help.get_help(), dumps=dumps_bytestr)async def rest_psu_model_name_hdl(self, request):return web.json_response( rest_psu.get_model_name(request.match_info["name"]), dumps=dumps_bytestr) # boardroutes.py:路由即/ board_routes = [] board_routes.append("/api/help") board_routes.append("/api/psu/{name}/model_name") # board_setup_routes.py:路由即/ from board_endpoint import boardApp_Handler from boardroutes import * from aiohttp.web import Applicationdef setup_board_routes(app: Application, write_enabed: bool):bhandler = boardApp_Handler()# 下行第一個參數(shù)路徑(boardroutes.py路由),第二個參數(shù)response(board_endpoint.py視圖)app.router.add_get(board_routes[0], bhandler.rest_help_hdl) app.router.add_get(board_routes[3], bhandler.rest_psu_model_name_hdl)15.curl獲取動態(tài)ip
15.1 sshpass
15.2 restful C&S
# /mnt/data/etc/rc.local中 #!/bin/bash /usr/local/bin/set_fan_speed.sh 30 # pi上 /etc/rc.local _IP=$(hostname -I) || true if [ "$_IP" ]; thenprintf "My IP address is %s\n" "$_IP" fi sh /boot/autorun.sh & # -x是否是可執(zhí)行文件 if [ -x /var/log/rc.local ]; then/var/log/rc.local fi exit 0 # /boot/autorun.sh sleep 30 ifconfig > /boot/autoboot.log # /var/log/rc.local /usr/bin/python3 /var/log/client.py 10.75.92.228 pi1 &# client.py #!/usr/bin/python3 # -*- coding: utf-8 -*- import socket import subprocess import sys import json import timedef get_self_ip():try:s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.connect(("8.8.8.8", 80))ip = s.getsockname()[0]finally:s.close()return ip # if __name__ == "__main__": # a = get_self_ip() # print(a) # python client.py 10.75.92.230if len(sys.argv) == 3:server = sys.argv[1] # 10.75.92.230client = sys.argv[2] # 服務(wù)端中的json文件中記錄著的客戶端的ip地址 else:print ("usage")#1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 while True:url_body = {}url_body["slave"] = clienturl_body["ip"] = get_self_ip() # python client.py 10.75.92.230 a# print(url_body) # {'ip': '10.75.92.230', 'slave': 'a'}cmd = "curl -s http://{}:8080/api/slave -d '{}'".format(server,json.dumps(url_body, ensure_ascii=False))# print(cmd) # curl -s http://10.75.92.230:8080/api/slave -d '{"ip": "10.75.92.230", "slave": "a"}' # -d:data 即server.py中data 即 請求request數(shù)據(jù)(returncode, output) = subprocess.getstatusoutput(cmd) #subprocess啟動一個新進程print (output)if returncode == 0:if output:d = json.loads(output)if d:if 'result' in d.keys():if d['result'] == 'success':print ('success')breaktime.sleep(300) # 5 minute # client.py放在需要得到ip的機器上,下行bmc是標(biāo)識這機器。服務(wù)端和客戶端都是230。 # root@diag-server:~# python3 client.py 10.75.92.230 bmc 必須先開啟服務(wù)端且用python3 # {"result": "success", "data": {"slave": {"bmc": "10.75.92.230", "s3ip-openbmc-bmc": "10.75.92.109", "hollywood-bmc": "10.75.92.146", # "server": "10.75.92.228", "s3ip-bsp-bmc": "10.75.92.96", "pi1": "10.75.92.53", "pi2": "10.75.92.152", "pi3": "10.75.92.51"}}}# root@diag-server:~# curl -s http://10.75.92.230:8080/api/help | python -m json.tool # { # "data": { # "slave": { # "bmc": "10.75.92.230", # "hollywood-bmc": "10.75.92.146", # "pi1": "10.75.92.53", # "pi2": "10.75.92.152", # "pi3": "10.75.92.51", # "s3ip-bsp-bmc": "10.75.92.96", # "s3ip-openbmc-bmc": "10.75.92.109", # "server": "10.75.92.228" # } # }, # "result": "success" # } # server.py #!/usr/bin/python3 # -*- coding: utf-8 -*- from aiohttp import web import json import requests import json import sysPATH = "./usr_slave.json" #bmc鏡像會刷到master或slave,這里獲取的是slaver的bmc的ip def parse_config_file():with open(PATH,'r') as load_f:load_dict = json.load(load_f)return load_dict # if __name__ == "__main__": # a = parse_config_file() # print(a) # root@diag-server:# python server.py # {u'slave': {u's3ip-openbmc-bmc': u'10.75.92.109', u'hollywood-bmc': u'10.75.92.146', u'server': u'10.75.92.228', # u's3ip-bsp-bmc': u'10.75.92.96', u'pi1': u'10.75.92.53', u'pi2': u'10.75.92.152', u'pi3': u'10.75.92.51'}}def req_post_json(str_url, json_data):headers = {'Content-Type': 'application/json'}response = requests.post(url=str_url, headers=headers, data=json.dumps(json_data))return response.json()# client.py 即 curl中 -d data = {'slave' : 'bmc', 'ip' : '10.75.85.34'} def add_slave(data):configs = parse_config_file()if not configs:configs = {}configs['slave'] = {}configs['slave'][data['slave']] = data['ip'] # configs['slave'][bmc]with open(PATH, 'w') as write_f:json.dump(configs, write_f, indent=4, ensure_ascii=False) # 將configs寫入write_f對象return {'result':'success','data': configs}def update_slave(data):configs = parse_config_file()configs['slave'][data['slave']] = data['ip']with open(PATH, 'w') as write_f:json.dump(configs, write_f, indent=4, ensure_ascii=False)return {'result':'success','data': configs}#11111111111111111111111111111111111111111111111111111111111111111111111111111111111111 async def get_help(request):configs = parse_config_file()return web.json_response({'result':'success','data': configs})async def slave(request):data = await request.json()configs = parse_config_file()if 'slave' in data or 'ip' in data:if configs:if configs['slave']:for k in configs['slave'].keys():if k == data['slave']:if k == data['slave'] and configs['slave'][k] == data['ip']:return web.json_response({'result':'success','data': configs})else:return web.json_response(update_slave(data))return web.json_response(add_slave(data))else:return web.json_response(add_slave(data))def app_factory(args=()):app = web.Application()app.router.add_get('/api/help', get_help)app.router.add_post('/api/slave', slave)return app // usr_slave.json,其中a是python3 client.py 10.75.92.230 a 多出 {"slave": {"bmc": "10.75.92.230","s3ip-openbmc-bmc": "10.75.92.109","hollywood-bmc": "10.75.92.146","server": "10.75.92.228","s3ip-bsp-bmc": "10.75.92.96","pi1": "10.75.92.53","pi2": "10.75.92.152","pi3": "10.75.92.51","a": "10.75.92.230",} } # /mnt/data/etc/rc.local if [ ! -f /usr/local/bin/sshpass ];thenln -snf /mnt/data/etc/sshpass /usr/local/bin #將前者sshpass復(fù)制到后面文件夾里 filn -snf /mnt/data/etc/debug.api /usr/local/bin/usr/bin/python3 /mnt/data/etc/client.py 10.75.92.228 s3ip-openbmc-bmc & # debug.api #!/bin/bash pn=$(ps | grep rest | grep runsv |awk '{print $1}');kill $pn;/usr/local/bin/sshpass -p 1 scp -r li@10.75.92.228:/home_a/li/openbmc-s3ip/meta-hua/meta-s3ip/recipes-utils/rest-api/files/* /usr/local/fbpackages/rest-api/# /usr/local/bin/sshpass -p 1 scp -r li@10.75.92.228:/home_a/li/openbmc-s3ip/meta-hua/meta-s3ip/recipes-plats/hal/files/*.py /usr/lib/python3.7/site-packages/hal/runsv /etc/sv/restapi & #sv d restapi #停止
總結(jié)
以上是生活随笔為你收集整理的【Python7】csv/excel/matplotlib,排序/树遍历,线/进程,文件/xml操作,百度人脸API,aiohttp/hal/restful/curl的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python5】图像操作,数字验证码识
- 下一篇: Python之Django框架开发博客