1) Demand analysis
1. Fruit data processing: The fruit (apple, banana) data set is processed into labels and images, and converted into one-Hot codes.
2. Convolution model building: KerAS is used to build models, including convolution layer, pooling layer, Dropout layer, full connection layer and output layer
3. Model training The data set is trained on the established model, and the best model is saved in the H5 file, which is convenient to test the model directly.
4. Model test: Open the camera and test with common objects. The test results will be recorded into a video presentation.
2) Outline design
1. Pre-test code:
The from keras. Applications. Resnet50 import resnet50 # / / import AI AI models in the software platform keras resnet50 from keras. The preprocessing import Image# / / import image processing library image from keras. Applications. Resnet50 import preprocess_input, Decode_predictions import numpy as NP # load models model = ResNet50(weights='imagenet') # 'bird.jpg' #// Images waiting for recognition (cars, fruits, etc.), note: Img = image.load_img(img_path, target_size=(224, X = image.img_to_array(img) # x = np.expand_dims(x, Axis =0) #// Extend along axis 0(row) -> Multi-dimensional array x = preprocess_input(x Print ('Predicted:', decode_Predictions (Preds, Top =3)[0]) #//Copy the code
2. 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) # Frame = cp.read() # read a frame of data if not ok: # If the photo fails, Exit the break cv2. 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', 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()}Copy the code
3) Detailed design
To 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 camera img_path=' capure.jpg 'def capture_camera_pic(): Cv2.namedwindow ('capture_pic') cp= cv2.videocapture (0) # Frame =cp.read() # read a frame of data if not ok: # If the photo fails, 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). 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(): Frame = image.fromarray (cv2.cvtcolor (img_path, Cv2.color_bgr2rgb)) # pil_img.show() # generate brush draw = imagedraw.draw (pil_img) # Font = imagefont.trueType (' msyh.ttC ', 30, encoding=' UTF-8 ') # encoding=' utF-8 ' Draw. Text ((30,50), jieguo(), (0, 0, 0), font=font) # PIL picture transfer cv2 img = cv2.cvtcolor (np.array(pil_img), Cv2.color_rgb2bgr) # become stretchable winName must be the same, Cv2.namedwindow ('capture_pic', cv2.window_normal) # show cv2.imshow("capture_pic", Img) # wait cv2. WaitKey (10) if __name__ = = "__main__ ': capture_camera_pic ()Copy the code