python,人工智能,水果识别
生活随笔
收集整理的這篇文章主要介紹了
python,人工智能,水果识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1) 需求分析
1.水果數據處理:對水果(蘋果,香蕉)數據集進行處理轉化為標簽和圖像,并轉化為one-hot碼。
2.卷積模型搭建:采用keras搭建模型,卷積層、池化層、Dropout層、全連接層、輸出層
3.模型訓練把數據集在建立的模型上進行訓練,并把最好的模型保存到h5文件中,便于直接對模型進行測試。
4.模型測試:打開攝像頭,使用通用物體進行測試。測試結果將錄制成視頻展示。
2) 概要設計
1. 測試前代碼: from keras.applications.resnet50 import ResNet50 #//導入AI軟件平臺keras 里的AI模型 ResNet50 from keras.preprocessing import image#//導入圖像處理庫 image from keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np#//載入模型 model = ResNet50(weights='imagenet') #//使model指向ResNet50模型 img_path = '鳥.jpg' #//等待識別的圖像(可用車,水果等),注:需把圖片放該代碼的同目錄下 img = image.load_img(img_path, target_size=(224, 224)) #//載入圖像#//-圖像的預處理 x = image.img_to_array(img) #//把圖像轉換為數組 x = np.expand_dims(x, axis=0) #//沿軸0(行)擴展 -> 多維數組 x = preprocess_input(x) #//做輸入預處理#//預測 preds = model.predict(x) #//運行模型進行預測 print('Predicted:', decode_predictions(preds, top=3)[0]) #//解碼預測,輸出結果2. 主程序: import cv2 from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as npimg_path = 'capyure.jpg'def capture_camera_pic():cv2.namedWindow('capture_pic')cp = cv2.VideoCapture(0) # //指定攝像頭,默認0指向第一個while cp.isOpened(): # //檢測攝像頭是否打開,如果攝像頭能成功打開,則進行循環的視頻拍照顯示ok, frame = cp.read() # 讀取一幀數據if not ok: # 如果拍照失敗,退出breakcv2.imwrite(img_path, frame) # 保存圖像# 翻譯# translator = Translator(to_lang='chinese')# translation = translator.translate(jieguo())cv2.putText(frame, jieguo(), (30, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 4)cv2.imshow('capture_pic', frame) # 顯示圖像c = cv2.waitKey(10)if c & 0xff == ord('q'):breakcp.release()cv2.destroyAllWindows()def jieguo():model = ResNet50(weights='imagenet')img_path = 'capyure.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)preds = model.predict(x)return decode_predictions(preds, top=3)[0][0][1];if __name__ == '__main__':capture_camera_pic() } 3)詳細設計 改進為可以把文字由英文轉中文的代碼: import cv2 from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.applications.resnet50 import preprocess_input,decode_predictions import numpy as np from translate import Translator from PIL import Image, ImageDraw, ImageFont #調用攝像頭 img_path='capyure.jpg' def capture_camera_pic():cv2.namedWindow('capture_pic')cp=cv2.VideoCapture(0) #//指定攝像頭,默認0指向第一個while cp.isOpened(): #//檢測攝像頭是否打開,如果攝像頭能成功打開,則進行循環的視頻拍照顯示ok,frame=cp.read() #讀取一幀數據if not ok: #如果拍照失敗,退出breakcv2.imwrite(img_path,frame) #保存圖像#cv2.putText(frame,jieguo(),(30,50),cv2.FONT_HERSHEY_COMPLEX,1,(255,0,0),4)#cv2.putText(frame,jieguo(),(30,50), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,1,(255,0,0),4)change()# change_cv2_draw(frame, jieguo(), (0,0), 20, (255, 0, 0))# cv2ImgAddText(frame, "封", 10, 65, (255, 0, 0), 20)#//cv2.imshow('capture_pic',frame) #顯示圖像c=cv2.waitKey(5)if c & 0xff == ord('q'):breakcp.release()cv2.destroyAllWindows()def jieguo():model = ResNet50(weights='imagenet')img_path = 'capyure.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)preds = model.predict(x)translator = Translator(to_lang='chinese')str =decode_predictions(preds, top=3)[0][0][1]translation = translator.translate(str.replace('_', ' '))return translation;def change():# 讀取文件pil_img = Image.open('capyure.jpg',)# 讀取cv2文件#frame = Image.fromarray(cv2.cvtColor(img_path, cv2.COLOR_BGR2RGB))# pil_img.show()# 生成畫筆draw = ImageDraw.Draw(pil_img)# 第一個參數是字體文件的路徑,第二個是字體大小font = ImageFont.truetype('msyh.ttc', 30, encoding='utf-8')# 第一個參數是文字的起始坐標,第二個需要輸出的文字,第三個是字體顏色,第四個是字體類型draw.text((30,50), jieguo(), (0, 0, 0), font=font)# PIL圖片轉cv2img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)# 變得可以拉伸 winname 必須要一樣,且設置可以拉伸在前面cv2.namedWindow('capture_pic', cv2.WINDOW_NORMAL)# 顯示cv2.imshow("capture_pic", img)# 等待cv2.waitKey(10)if __name__=='__main__':capture_camera_pic()總結
以上是生活随笔為你收集整理的python,人工智能,水果识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯云+tipask快速搭建基于lara
- 下一篇: Python利用Matplotlib绘图