def predict(data_type, seq_length, saved_model, image_shape, video_name, class_limit, config): model = load_model(saved_model) feature_file_path= config.featureFileName work_dir = config.workDir classlist= config.classes # Get the data and process it. if image_shape is None: data = DataSet(seq_length=seq_length, class_limit=class_limit, feature_file_path = feature_file_path, repo_dir = config.repoDir, work_dir=work_dir, classlist=classlist) else: data = DataSet(seq_length=seq_length, image_shape=image_shape, class_limit=class_limit, feature_file_path = feature_file_path, repo_dir = config.repoDir, work_dir=work_dir, classlist=classlist) # Extract the sample from the data. sample = data.get_frames_by_filename(video_name, data_type) # Predict! prediction = model.predict(np.expand_dims(sample, axis=0)) print(prediction) data.print_class_from_prediction(np.squeeze(prediction, axis=0))
def predict(data_type, seq_length, saved_model, image_shape, video_name, class_limit): model = load_model(saved_model) # Get the data and process it. if image_shape is None: data = DataSet(seq_length=seq_length, class_limit=class_limit) else: data = DataSet(seq_length=seq_length, image_shape=image_shape, class_limit=class_limit) # Extract the sample from the data. sample = data.get_frames_by_filename(video_name, data_type) # Predict! prediction = model.predict(np.expand_dims(sample, axis=0)) print(prediction) data.print_class_from_prediction(np.squeeze(prediction, axis=0))
def Classify(self, request_iterator, context): saved_model = 'data/checkpoints/lstm-features.037-0.131.h5' point_count = 0 seq_length = 40 class_limit = 10 # Number of classes to extract. Can be 1-101 or None for all. data = DataSet(seq_length=seq_length, class_limit=class_limit) modelE = Extractor() model = load_model(saved_model) sequence = [] for Chunk in request_iterator: byt = Chunk.Content byt = pickle.loads(byt) features = modelE.extract(byt) sequence.append(features) point_count += 1 if point_count == 40: print(np.shape(sequence)) prediction = model.predict(np.expand_dims(sequence, axis=0)) print(prediction) message = [] classs = [] sorted_lps = data.print_class_from_prediction( np.squeeze(prediction, axis=0)) if sorted_lps is not None: for i, class_prediction in enumerate(sorted_lps): if i > 10 - 1 or class_prediction[1] == 0.0: break print("%s: %.2f" % (class_prediction[0], class_prediction[1])) message.append(class_prediction[1]) classs.append(class_prediction[0]) first = class_prediction[0] v1 = class_prediction[1] yield humanaction_pb2.label(message1=message[0], message2=message[1], message3=message[2], message4=message[3], message5=message[4], message6=message[5], message7=message[6], message8=message[7], message9=message[8], message10=message[9], class1=classs[0], class2=classs[1], class3=classs[2], class4=classs[3], class5=classs[4], class6=classs[5], class7=classs[6], class8=classs[7], class9=classs[8], class10=classs[9]) sequence = [] point_count = 0
def classify(video_file, seq_length=20, saved_model='./cnn_lstm_VGGFace10.h5'): capture = cv2.VideoCapture(os.path.join(video_file)) width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) # float height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT) # float print('#########################################################', video_file, '#########################################################') # Get the dataset. data = DataSet(seq_length=seq_length, class_limit=2, image_shape=(224, 224, 3)) # get the model. extract_model = Extractor(image_shape=(height, width, 3)) rm = ResearchModels(len(data.classes), 'lstm', seq_length, saved_model, features_length=2622) saved_LSTM_model = rm.lstm() saved_LSTM_model.load_weights(saved_model) frames = [] frame_count = 0 while True: ret, frame = capture.read() print(ret) # Bail out when the video file ends if not ret: break # Save each frame of the video to a list frame_count += 1 frames.append(frame) if frame_count < seq_length: continue # capture frames untill you get the required number for sequence else: frame_count = 0 # For each frame extract feature and prepare it for classification sequence = [] for image in frames: image = cv2.resize(image, (224, 224), 3) features = extract_model.extract_image(image) sequence.append(features) # Clasify sequence prediction = saved_LSTM_model.predict(np.expand_dims(sequence, axis=0)) print('classofyyyyyyyyyyy') print(prediction) values = data.print_class_from_prediction( np.squeeze(prediction, axis=0)) # print(np.argmax(prediction)) frames = [] print(np.argmax(prediction)) return np.argmax(prediction)
def predict(data_type, seq_length, saved_model, image_shape, video_name, class_limit): model = load_model(saved_model) # Get the data and process it. if image_shape is None: data = DataSet(seq_length=seq_length, class_limit=class_limit) else: data = DataSet(seq_length=seq_length, image_shape=image_shape, class_limit=class_limit) # Extract the sample from the data. #sample = data.get_frames_by_filename(video_name, data_type) for X, y in data.frame_generator(2, 'test', "images"): # Predict! prediction = model.predict(X) print(prediction) data.print_class_from_prediction(prediction[0]) print() data.print_class_from_prediction(prediction[1]) print('-------------------------')
def predict(seq_length, class_limit, feature_length, saved_model, video_name): # 获取数据 data = DataSet(seq_length=seq_length, class_limit=class_limit) # 利用视频名称提取样本的序列特征值 sample = data.get_frames_by_filename(video_name) sample = np.reshape(sample, [-1, seq_length, feature_length]) # 确定训练的类数量 if class_limit is None: class_num = 101 else: class_num = class_limit # 设置输入 input_x = tf.placeholder(tf.float32, [None, seq_length, feature_length], name="input-x") # 前向传播的计算 _, output_y = lstm_inference.inference(input_x, class_num, None, train=False) saver = tf.train.Saver() with tf.Session() as sess: tf.global_variables_initializer().run() model = tf.train.get_checkpoint_state(saved_model) if model and model.model_checkpoint_path: saver.restore(sess, model.model_checkpoint_path) prediction = sess.run(output_y, feed_dict={input_x: sample}) print("the prediction of the video %s is:" % video_name) data.print_class_from_prediction(np.squeeze( prediction, axis=0)) # 删除prediction中第1维(并且该维大小为1)
def predict(data_type, seq_length, saved_model, image_shape, video_name, class_limit): print("**********************************") print("\nstart loading model...") print(datetime.datetime.now()) print("**********************************") global model if not model: model = load_model(saved_model) print("**********************************") print("model loaded successfully...") print(datetime.datetime.now()) print("**********************************") # Get the data and process it. if image_shape is None: data = DataSet(seq_length=seq_length, class_limit=class_limit) else: data = DataSet(seq_length=seq_length, image_shape=image_shape, class_limit=class_limit) # Extract the sample from the data. sample = data.get_frames_by_filename(video_name, data_type) # Predict! print("**********************************") print(datetime.datetime.now()) print("**********************************") prediction = model.predict(np.expand_dims(sample, axis=0)) print("**********************************") print(datetime.datetime.now()) print("**********************************") #print(keras.np_utils.probas_to_classes(prediction)) print(prediction) print("**********************************") print(datetime.datetime.now()) print("**********************************") return data.print_class_from_prediction(np.squeeze(prediction, axis=0))
if frame_count < seq_length: continue # capture frames untill you get the required number for sequence else: frame_count = 0 # For each frame extract feature and prepare it for classification sequence = [] for image in frames: features = extract_model.extract_image(image) #print(features) sequence.append(features) # Clasify sequence prediction = saved_LSTM_model.predict(np.expand_dims(sequence, axis=0)) print(prediction) values = data.print_class_from_prediction(np.squeeze(prediction, axis=0)) # Add prediction to frames and write them to new video for image in frames: for i in range(len(values)): cv2.putText(image, values[i], (40, 40 * i + 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA) cv2.imshow("Frame", image) if cv2.waitKey(1) and 0xFF == ord('q'): break video_writer.write(image)
def show_webcam(mirror=False): # initialize the video stream and pointer to output video file, then # allow the camera sensor to warm up print("[INFO] starting video stream...") writer = None # saved_model = 'data/checkpoints/lstm-features.037-0.131.h5' vs = cv2.VideoCapture(-1) time.sleep(2) # Set defaults. seq_length = 40 class_limit = 10 # Number of classes to extract. Can be 1-101 or None for all. data = DataSet(seq_length=seq_length, class_limit=class_limit) # get the model. modelE = Extractor() model = load_model(saved_model) # loop over frames from the video file stream while True: # grab the frame from the threaded video stream first ="" v1 ="" sequence = [] for i in range (0,40): ret_val,frame = vs.read() if ret_val == True: if mirror: frame = cv2.flip(frame, 1) width = np.size(frame, 1) height = np.size(frame, 0) x = width/2 y = height/2 cv2.imshow('my webcam', frame) cv2.putText(frame, first + v1, (x,y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255,0,0), thickness=1) frame = cv2.resize(frame,(299,299), interpolation = cv2.INTER_CUBIC) if cv2.waitKey(1) == 27: break # esc to quit else: break features = modelE.extract(frame) sequence.append(features) # Predict! print( np.shape(sequence)) prediction = model.predict(np.expand_dims(sequence, axis=0)) print(prediction) sorted_lps = data.print_class_from_prediction(np.squeeze(prediction, axis=0)) for i, class_prediction in enumerate(sorted_lps): if i > 10 - 1 or class_prediction[1] == 0.0: break print("%s: %.2f" % (class_prediction[0], class_prediction[1])) first = class_prediction[0] v1 = class_prediction[1]
def video(video_file): #print('time take to load imports {:0.3f}'.format(time.time() - start)) start = time.time() '''print(sys.argv) if (len(sys.argv) == 2): #seq_length = int(sys.argv[1]) #class_limit = int(sys.argv[2]) #saved_model = sys.argv[3] #video_file = sys.argv[1] else: print ("Usage: python clasify.py video_file_name") print ("Example: python clasify.py some_video.mp4") exit (1) ''' file_path = re.compile(r'[^\\/:*?"<>|\r\n]+$') file_name = file_path.search(video_file) start = None f_name = file_name.group()[start:-4] capture = cv2.VideoCapture(video_file) width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) # float height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) # float seq_length = 5 fourcc = cv2.VideoWriter_fourcc(*'mp4v') print(video_file) video_writer = cv2.VideoWriter(f_name + "_result.mp4", fourcc, 15, (width, height)) # Get the dataset. data = DataSet(seq_length=41, class_limit=2, image_shape=(240, 320, 3)) #os.environ['CUDA_VISIBLE_DEVICES'] = '-1' os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2" # get the model. #start = time.time() #print("Loading Model .......") #extract_model = Extractor(image_shape=(240,320, 3)) #saved_LSTM_model = load_model("F:\\BE Project work\\LSTM-video-classification-master\\data\\checkpoints\\lstm-features.007-0.264.hdf5",compile='False') #print(capture) #print("Captured Video....") #print("Model Loaded.......") #print('time required to load model{:0.3f}'.format(time.time() - start)) cam_id = 1002 frames = [] frame_count = 0 try: conn = connect() except: print("DATABASE Error") start = time.time() frameRate = capture.get(5) #frame rate while True: r1, frame1 = capture.read() ret, frame = capture.read() #frameId = capture.get(1) #current frame number #print(capture.get(1)) # Bail out when the video file ends if not ret: break # Save each frame of the video to a list frame_count += 1 image1 = cv2.resize(frame, (240, 320)) frames.append(image1) #print("LINE 86") #print("LINE 89") if frame_count < seq_length: continue # capture frames untill you get the required number for sequence else: frame_count = 0 # For each frame extract feature and prepare it for classification sequence = [] for image in frames: features = extract_model.extract_image(image) #print(features) sequence.append(features) # Clasify sequence prediction = saved_LSTM_model.predict(np.expand_dims(sequence, axis=0)) values = data.print_class_from_prediction( np.squeeze(prediction, axis=0)) #print(values) #for i in range(len(prediction)): if prediction.item(1) >= 0.9: insert_img(conn, frame, cam_id, 'high') winsound.Beep(2500, 100) elif prediction.item(1) >= 0.75: insert_img(conn, frame, cam_id, 'medium') winsound.Beep(1000, 50) elif prediction.item(1) >= 0.65: insert_img(conn, frame, cam_id, 'low') winsound.Beep(500, 25) #else: # print ("value is too high") # Add prediction to frames and write them to new video for image in frames: for i in range(len(values)): cv2.putText(image, values[i], (40, 40 * i + 40), cv2.FONT_HERSHEY_SIMPLEX, 0.50, (255, 255, 255), lineType=cv2.LINE_AA) #cv2.imshow("Frame",image) image = cv2.resize(image, (height, width)) f1 = image.copy() encode_return_code, image_buffer = cv2.imencode('.jpg', f1) io_buf = io.BytesIO(image_buffer) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + io_buf.read() + b'\r\n') video_writer.write(image) if cv2.waitKey(1) and 0xFF == ord('q'): break frames = [] cv2.destroyAllWindows() print('time required {:0.3f}'.format(time.time() - start)) conn.close() video_writer.release()