GOOGLE Teachable Machine概述构建深度学习模式训练模型导出模型项目实施import numpy as np import cv2 import tensorflow.keras from keras.preprocessing import image # Access WebCam cap = cv2.VideoCapture(0) state = True # Load the TensorFlow model weights model = tensorflow.keras.models.load_model('keras_model.h5') labels = ['thumb_up','thumb_down','random'] # Set the width and height of the frame for video to be saved width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Create a writer object to save the frames writer = cv2.VideoWriter(r'C:UsersSharan BabuDesktoptrimmed_video.mp4',cv2.VideoWriter_fourcc(*'XVID'),25, (width, height)) final_video = [] // List to store our video frames # Video editing! while True: success, image = cap.read() if success==True: final_video.append(image) img = image.copy() # Draw a rectangle to indicate the region of interest and crop it img = cv2.flip(img,1) cv2.rectangle(img,pt1=(450,100),pt2=(620,300),color=(0,255,0),thickness=3) cv2.imshow('Video',img) roi = img[102:298,448:618] # Image pre-processing for making predictions data = cv2.resize(roi,(224,224)) data = np.array(data,dtype=np.float32) data = np.expand_dims(data,axis=0) data = data/255 # Predict output class for the image and save video accordingly prediction = model.predict(data) predicted_class = labels[np.argmax(prediction)] print(prediction, predicted_class) if predicted_class == 'thumb_up': for frame in final_video: writer.write(frame) final_video = [] elif predicted_class == 'thumb_down': final_video = [] # Break the program when key 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Close webcam and other connections writer.release() cap.release() cv2.destroyAllWindows()
|
|
来自: taotao_2016 > 《视觉》