當前位置:菜譜大全網 - 饑荒食譜 - 樹莓派使用PaddleX對對象進行分類。

樹莓派使用PaddleX對對象進行分類。

1.先用百度AI運行代碼。參考/Aistudio/Project Detail/2160041鏈接網址,從而獲取模型。但是paddlex運行的模型不能直接在樹莓派上運行。所以轉到第二步。

2.將模型轉換為paddle-lite支持的模型。在百度工作室運行,上壹步的代碼。

Paddle _ lite _ opt-model _ fie =您的模型方法。

- param_file= =您的權重路徑

- valid_targets=arm

-optimize _ out _ type = naive _ buffer

- optimize_out= =妳想要的輸出nb模型的方式和名稱。

3.執行以下分類代碼並修改您的參數。

從paddlelite.lite導入*

導入cv2

將numpy作為np導入

導入系統

導入時間

從PIL進口圖片

從PIL導入圖像字體

從PIL進口ImageDraw

#負載模型

定義創建預測器(模型目錄):

config = MobileConfig()

配置文件(模型目錄)

predictor = create _ paddle _ predictor(配置)

回報預測器

#圖像標準化處理

def process_img(image,input_image_size):

原點=圖像

img = origin . resize(input _ Image _ size,圖像。雙線性)

resized_img = img.copy()

if img.mode!= 'RGB ':

img = img.convert('RGB ')

img = np.array(img)。astype('float32 ')。轉置((2,0,1)) # HWC到CHW

img -= 127.5

img *= 0.007843

img = img[np.newaxis,:]

返回原點,img

#預測

def predict(圖像,預測值,輸入圖像大小):

#輸入數據處理

input _ tensor = predictor . get _ input(0)

input_tensor.resize([1,3,input_image_size[0],input_image_size[1]])

image = image . from array(cv2 . CVT color(image,cv2。COLOR_BGRA2RGBA))

origin,img = process_img(圖像,輸入圖像大小)

image_data = np.array(img)。展平()。tolist()

輸入_張量.設置_浮點_數據(圖像_數據)

#執行預測

predictor.run()

#獲取輸出

output _ tensor = predictor . get _ output(0)

print(" output_tensor.float_data()[:]:",output _ tensor . float _ data()[:])

RES = output _ tensor . float _ data()[:]

返回資源

#顯示結果

def post_res(label_dict,res):

打印(最大分辨率)

target_index = res.index(max(res))

print(" Result:"+" "+label _ dict[target _ index])

if __name__ == '__main__ ':

#初始定義

label_dict = {0:"金屬",1:"紙",2:"塑料",3:"玻璃" }

image = "。/test _ pic/images _ original/glass/glass 300 . jpg "

模型_目錄= "。/trained _ model/resnet 50 _ trash _ x86 _ model . nb "

圖像大小= (224,224)

#初始化

預測值=創建預測值(模型方向)

#讀入圖片

image = cv2.imread(image)

#預測

res = predict(圖像,預測值,圖像大小)

#顯示結果

post_res(標簽_字典,資源)

cv2.imshow("image ",圖像)

cv2.waitKey()