程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Realization of artificial intelligence for fruit recognition with Python

編輯:Python

1) Demand analysis

1. Fruit data processing : For fruit ( Apple , Banana ) Data sets are processed into labels and images , And into one-hot code .

2. Convolution model construction : use keras Build a model , Convolution layer 、 Pooling layer 、Dropout layer 、 Fully connected layer 、 Output layer

3. Model training trains the data set on the established model , And save the best model to h5 In file , It is convenient to test the model directly .

4. Model test : Turn on the camera , Use common objects for testing . The test results will be recorded and displayed on video .

2) Outline design

1. Pre test code :
from keras.applications.resnet50 import ResNet50 #// Import AI software platform keras Inside AI Model ResNet50
from keras.preprocessing import image#// Import image processing library image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
#// Load model
model = ResNet50(weights='imagenet') #// send model Point to ResNet50 Model
img_path = ' bird .jpg' #// Images waiting to be recognized ( Available cars , Fruits, etc. ), notes : You need to put the picture in the same directory of the code
img = image.load_img(img_path, target_size=(224, 224)) #// Load the image
#//- Image preprocessing
x = image.img_to_array(img) #// Convert the image to an array
x = np.expand_dims(x, axis=0) #// Along axis 0( That's ok ) Expand -> Multidimensional arrays
x = preprocess_input(x) #// Do input preprocessing
#// forecast
preds = model.predict(x) #// Run the model to predict
print('Predicted:', decode_predictions(preds, top=3)[0]) #// Decode prediction , Output results 

2. The main program :

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
img_path = 'capyure.jpg'
def capture_camera_pic():
cv2.namedWindow('capture_pic')
cp = cv2.VideoCapture(0) # // Designated Cameras , Default 0 Point to the first one
while cp.isOpened(): # // Check whether the camera is on , If the camera can be turned on successfully , Then, the cyclic video photographing display
ok, frame = cp.read() # Read a frame of data
if not ok: # If photographing fails , sign out
break
cv2.imwrite(img_path, frame) # Save image
# translate
# 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) # Display images
c = cv2.waitKey(10)
if c & 0xff == ord('q'):
break
cp.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) Detailed design

 Improved to a code that can convert text from English to Chinese :
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
# Call the camera
img_path='capyure.jpg'
def capture_camera_pic():
cv2.namedWindow('capture_pic')
cp=cv2.VideoCapture(0) #// Designated Cameras , Default 0 Point to the first one
while cp.isOpened(): #// Check whether the camera is on , If the camera can be turned on successfully , Then, the cyclic video photographing display
ok,frame=cp.read() # Read a frame of data
if not ok: # If photographing fails , sign out
break
cv2.imwrite(img_path,frame) # Save image
#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, " seal ", 10, 65, (255, 0, 0), 20)
#//cv2.imshow('capture_pic',frame) # Display images
c=cv2.waitKey(5)
if c & 0xff == ord('q'):
break
cp.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():
# Read the file
pil_img = Image.open('capyure.jpg',)
# Read cv2 file
#frame = Image.fromarray(cv2.cvtColor(img_path, cv2.COLOR_BGR2RGB))
# pil_img.show()
# Generating brushes
draw = ImageDraw.Draw(pil_img)
# The first parameter is the path to the font file , The second is the font size
font = ImageFont.truetype('msyh.ttc', 30, encoding='utf-8')
# The first parameter is the starting coordinate of the text , The second text to be output , The third is the font color , The fourth is the font type
draw.text((30,50), jieguo(), (0, 0, 0), font=font)
# PIL Picture turn cv2
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
# Become stretchable winname It must be the same , And the setting can stretch in front
cv2.namedWindow('capture_pic', cv2.WINDOW_NORMAL)
# Show
cv2.imshow("capture_pic", img)
# wait for
cv2.waitKey(10)
if __name__=='__main__':

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved