def main(iou_threshold, confidence_threshold, img_names): batch_size = len(img_names) batch = load_images(img_names, model_size=_MODEL_SIZE) class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) inputs = tf.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: saver.restore(sess, r'/weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) draw_boxes(img_names, detection_result, class_names, _MODEL_SIZE) print('Detections have been saved successfully.')
def main(weights_file='./weights/yolov3.weights', class_names_file='./data/labels/coco.names'): class_names = load_class_names(class_names_file) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=(416, 416), max_output_size=5, iou_threshold=0.5, confidence_threshold=0.5) inputs = tf.compat.v1.placeholder(tf.float32, [1, 416, 416, 3]) model(inputs, training=False) model_vars = tf.compat.v1.global_variables(scope='yolo_v3_model') assign_ops = load_weights(model_vars, weights_file) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: sess.run(assign_ops) saver.save(sess, './weights/model.ckpt') print('Model has been saved successfully.')
def main(iou_threshold, confidence_threshold, input_names): global detection_result class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_names) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) fps = cap.get(cv2.CAP_PROP_FPS) if not os.path.exists('detections'): os.mkdir('detections') head, tail = os.path.split(input_names) name = './detections/' + tail[:-4] + '_yolo.mp4' out = cv2.VideoWriter(name, fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: print("Show video") while (cap.isOpened()): ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) if ret == True: cv2.imshow(win_name, frame) out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.')
def main(): # Cargando el archivo de etiquetas class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) #Llamada al modelo Yolo-v3 model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) #iniciando secion si cuenta con tensorflow 1 with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') # Pesos del modelo url = 'http://192.168.1.111:8080/shot.jpg' # Direccion de streaming # Convercion de las imagenes de streaming image = urllib.request.urlopen(url) img = np.array(bytearray(image.read()), dtype=np.uint8) frame = cv2.imdecode(img, -1) frame_size = (850, 500) try: while True: # Obtencion de las imagenes image = urllib.request.urlopen(url) img = np.array(bytearray(image.read()), dtype=np.uint8) frame = cv2.imdecode(img, -1) frame = cv2.resize(frame, dsize=(850, 500), interpolation=cv2.INTER_NEAREST) resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) # Activamos yolo detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) # Clasificacion del imagenes clasification = clasifier(detection_result, class_names) # Obtencion de los datos Diccionario print(json.dumps(clasification)) time.sleep(0.05) # Time Slepeer yield (json.dumps(clasification) + '\n') finally: print('FaceID end')
def main(): class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') url = 'http://192.168.1.111:8080/shot.jpg' image = urllib.request.urlopen(url) img = np.array(bytearray(image.read()), dtype=np.uint8) frame = cv2.imdecode(img, -1) frame_size = (850, 500) try: while True: image = urllib.request.urlopen(url) img = np.array(bytearray(image.read()), dtype=np.uint8) frame = cv2.imdecode(img, -1) frame = cv2.resize(frame, dsize=(850, 500), interpolation=cv2.INTER_NEAREST) resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) clasification = clasifier(frame, frame_size, detection_result, class_names, _MODEL_SIZE) print(json.dumps(clasification)) yield (json.dumps(clasification) + '\n') key = cv2.waitKey(10) & 0xFF if key == ord('q'): break finally: print('FaceID end')
def main(iou_threshold, confidence_threshold, input_names): class_names = load_file(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_names[0]) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter( f'./detections/{input_names[0][7:-4]}_output.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) counter = 0 try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) counter = draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE, counter) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.')
def load_model(self): self.shared_variables.class_names = load_class_names(_CLASS_NAMES_FILE) self.shared_variables.n_classes = len( self.shared_variables.class_names) self.shared_variables.model_size = _MODEL_SIZE model = Yolo_v3(n_classes=self.shared_variables.n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) self.inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) self.detections = model(self.inputs, training=False) return tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))
def main(): model = Yolo_v3(n_classes=80, model_size=(416, 416), max_output_size=5, iou_threshold=0.5, confidence_threshold=0.5) inputs = tf.placeholder(tf.float32, [1, 416, 416, 3]) model(inputs, training=False) model_vars = tf.global_variables(scope='yolo_v3_model') assign_ops = load_weights(model_vars, './weights/yolov3.weights') saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: sess.run(assign_ops) saver.save(sess, './weights/model.ckpt') print('Model has been saved successfully.')
def main(type, iou_threshold, confidence_threshold, venue, input_names): class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) if type == 'images': batch_size = len(input_names) batch = load_images(input_names, model_size=_MODEL_SIZE) inputs = tf.compat.v1.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) pass res = draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE) filtered_cars = [ x for x in res if x.get("car") and x.get("car") > 60.0 ] filtered_persons = [ x for x in res if x.get("person") and x.get("person") > 60.0 ] print('Detections have been saved successfully.') print(f'Number of persons: {len(filtered_persons)}') print(f'Number of cars: {len(filtered_cars)}') put_res_on_queue({ "nmr_of_cars": len(filtered_cars), "nrm_of_persons": len(filtered_persons), "time_generated": int(datetime.now().timestamp()), "venue": venue }) elif type == 'video': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_names[0]) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') elif type == 'webcam': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Webcam detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(0) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') else: raise ValueError( "Inappropriate data type. Please choose either 'video' or 'images'." )
def main(type, iou_threshold, confidence_threshold, input_names): global detection_result class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) if type == 'images': #batch_size = len(input_names) batch = load_images(input_names, model_size=_MODEL_SIZE) inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: saver.restore(sess, './weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) #detection_result = detection_result[0] print("detection_result", detection_result) draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE) print('Detections have been saved successfully.') elif type == 'video': inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_names[0]) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') else: raise ValueError( "Inappropriate data type. Please choose either 'video' or 'images'." )
def main(type, iou_threshold, confidence_threshold, input_file): """Detect the no of person and chair in the meeting hall in case of video/webcam. Parameters ---------- type : str Defines the type of input, video file or webcam. iou_threshold : float Max Intersection over Union that can be allowed in range of (0,1). confidence_threshold : float Likelihood that can be accepted and ranges betweeen (0,1). input_file : str Location if the input video file in case of video type. Returns ------- None. """ # Creating database in influxdb under the name detection client = InfluxDBClient(host="localhost",port="8086") client.create_database("detection") client.switch_database("detection") print("Created database with the name detection") # Loading the model with the coco dataset names class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) print("Loading the model was successful.") if type == 'video': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables(scope='yolo_v3_model')) print("Initiating detection on sample video.") with tf.compat.v1.Session() as sess: # Loading the preTrained weight saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_file) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'MP4V') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./output/sample_output.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: # Reading video to detect no of chair and person in each frames while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run(detections, feed_dict={inputs: [resized_frame]}) chair_count, person_count, ratio = draw_frame(frame, frame_size, detection_result,class_names, _MODEL_SIZE) json_body=[ {"measurement":"meetingroom","fields":{"Chair":chair_count, "People": person_count,"Ratio": ratio} } ] client.write_points(json_body) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') elif type == 'webcam': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables(scope='yolo_v3_model')) print("Initiating detection on sample Webcam.") with tf.compat.v1.Session() as sess: # Loading the preTrained weight saver.restore(sess, './weights/model.ckpt') win_name = 'Webcam detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(0) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'MP4V') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./output/sample_output.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: # Reading video to detect no of chair and person in each frames while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run(detections, feed_dict={inputs: [resized_frame]}) chair_count, person_count, ratio = draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) json_body=[ {"measurement":"meetingroom","fields":{"Chair":chair_count, "People": person_count,"Ratio": ratio} } ] client.write_points(json_body) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') else: raise ValueError("Inappropriate data type. Please choose either 'video' or 'webcam'")
droi1 = [(0, int(f_height / 2)), (f_width, int(f_height / 2)), (f_width, f_height), (0, f_height)] droi_all = [(0, f_height), (f_width, f_height), (f_width, 0), (0, 0)] #Initialize Tracker mot_tracker = Sort() print("jaksfjahkdfj") _MODEL_SIZE = (416, 416) class_names = load_class_names("weights/vehicle.names") n_classes = len(class_names) _MAX_OUTPUT_SIZE = 20 model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=0.5, confidence_threshold=0.5) inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) def generate(): with tf.Session() as sess: saver.restore(sess, './weights/model.ckpt') while True: k = cv2.waitKey(1)
def main(type, iou_threshold, confidence_threshold, cat, temp_input): # def main(type, iou_threshold, confidence_threshold, input_names): class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) input_names = [] model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) if type == 'images': # batch_size = len(input_names) batch_size = len(temp_input) if batch_size == 1: # txtFile = 'myCommand.txt' # print(input_names) # if input_names[0] == txtFile: # print(input_names[0]) # if temp_input[0] == txtFile: if temp_input[0].endswith('.txt'): with open(temp_input[0], 'r') as file1: for line in file1: count = 0 for word in line.split(): input_names.append(word) count += 1 # print(input_names) # detection(input_names, class_names, model, batch_size) batch_size = count # print(batch_size) # file1 = open(txtFile,'r+') # input_names = file1.readlines() # print(input_names) else: input_names = temp_input # print(input_names) # detection(input_names, class_names, model, batch_size) else: input_names = temp_input # print(input_names) # detection(input_names, class_names, model, batch_size) batch = load_images(input_names, model_size=_MODEL_SIZE) inputs = tf.compat.v1.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) print('detection result is out') draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE) save_detections(cat, input_names, detection_result, class_names) # for img_name, boxes_dict in zip(input_names, detection_result): # file1 = open('./detections/detection.csv', 'a') # for cls in range(len(class_names)): # boxes = boxes_dict[cls] # count = 0 # if np.size(boxes) != 0: # for box in boxes: # count += 1 # file1.writelines(str(count) + ', ') # tempstr = str(cat) + '\n' # file1.writelines(tempstr) # file1.close() print('Detections have been saved successfully.') elif type == 'video': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Video detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(input_names[0]) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') elif type == 'webcam': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Webcam detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(0) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') else: raise ValueError( "Inappropriate data type. Please choose either 'video' or 'images'." )
def messy_or_not(): try: os.environ['CUDA_VISIBLE_DEVICES'] = '0' _MODEL_SIZE = (416, 416) _CLASS_NAMES_FILE = 'coco.names' _MAX_OUTPUT_SIZE = 50 global detection_result detection_result = {} iou_threshold = 0.5 confidence_threshold = 0.5 filepath = get_test_filename() if not filepath: return 'Info: Test Image does not exist' input_names = filepath class_names = load_class_names(_CLASS_NAMES_FILE) n_classes = len(class_names) #import tensorflow.compat.v1 as tf1 # tf.compat.disable_v2_behavior() # def main(iou_threshold, confidence_threshold, input_names): # model.save_weights( # 'E:/NTT DATA/CCE/CCE Codebase/Sentiment Analysis/SentimentAnalysis/MessyOrNot/model_saved.h5') #imagename = '0_qzQUpL-gOOSVKXbi.jpg' # serialize model to JSON #model_json = model.to_json() # load json and create model #json_file = open('model.json', 'r') #loaded_model_json = json_file.read() #json_file.close() #loaded_model = model_from_json(loaded_model_json) # load weights into new model #loaded_model.load_weights("/model_saved.h5") #loaded_model = load_model('trained_model.h5') model_path = path + '/model_saved/model_saved.h5' print('model_path=', model_path) loaded_model = load_model(model_path) #loaded_model.load_weights(checkpoint_path) print("Loaded model from disk") test_image = image.load_img(filepath, target_size=(224, 224)) test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis=0) result = loaded_model.predict(test_image) #labels = (train_generator.class_indices) #labels = dict((v, k) for k, v in labels.items()) #predictions = [labels[k] for k in result] # list_clean = os.listdir(path_clean) # dir is your directory path #number_files_clean = len(list_clean)+1 # list_messy = os.listdir(path_messy) # dir is your directory path #number_files_messy = len(list_messy)+1 #messagebox.showinfo("MessyOrNot", "Testing") # object detection # Yolo v3 image detection model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) batch = load_images(input_names, model_size=_MODEL_SIZE) inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model')) with tf.Session() as sess: saver.restore(sess, base_dir + '/weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE) print('Detections have been saved successfully.') class_count = [] my_string = "" for cls in range(len(class_names)): class_count.append(len(detection_result[0][cls])) if len(detection_result[0][cls]) > 0: my_string = my_string + \ str(class_names[cls])+"-"+str(len(detection_result[0][cls]))+"\n" print(my_string) print(result) print(np.argmax(result[0])) pred_class = (np.argmax(result[0])) # user result display K.clear_session() if pred_class == 0: #messagebox.showinfo("MessyOrNot", "Clean " + "\n" + my_string) #delete_files(dirpath) return "Clean " + " -- " + my_string if pred_class == 1: #messagebox.showinfo("MessyOrNot", "Messy " + "\n" + my_string) #delete_files(dirpath) return "Messy " + " -- " + my_string if pred_class == 2: #messagebox.showinfo("MessyOrNot", "Not a Conference Room " + "\n") #delete_files(dirpath) return "Not a Conference Room " except Exception as exc: logger.error(traceback.format_exc()) print(traceback.format_exc()) finally: K.clear_session()
def main(type, input_names, save_folder='./detections', iou_threshold=0.5, confidence_threshold=0.5, class_names_file=_CLASS_NAMES_FILE, create_csv=False): # Get class names and number class_names = load_class_names(class_names_file) n_classes = len(class_names) # Tensorflow prep tf.compat.v1.reset_default_graph() # Load Yolo_v3 model model = Yolo_v3(n_classes=n_classes, model_size=_MODEL_SIZE, max_output_size=_MAX_OUTPUT_SIZE, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) if type == 'images': # Load pictures and set up detection inputs batch_size = len(input_names) batch = load_images(input_names, model_size=_MODEL_SIZE) inputs = tf.compat.v1.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) # Load the weights model.ckpt and run detection on inputs with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') detection_result = sess.run(detections, feed_dict={inputs: batch}) # Using detection results, draw detection boxes on input pictures and save them draw_boxes(input_names, detection_result, class_names, _MODEL_SIZE, save_folder) print('Detections have been saved successfully.') elif type == 'video': # Set yolo_v3 to tensorflow inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) # Run tensorflow session with tf.compat.v1.Session() as sess: # Load model saver.restore(sess, './weights/model.ckpt') # Create window for output video win_name = 'Video detection' cv2.namedWindow(win_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(win_name, 1280, 720) # Create OpenCV capture and get video metadata cap = cv2.VideoCapture(input_names[0]) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) # Set name and save destination for output video input_name_base = os.path.basename(input_names[0]) video_save_path = save_folder + '/' + os.path.splitext(input_name_base)[0] + '_analysed' + \ os.path.splitext(input_name_base)[1] if os.path.splitext(input_name_base)[1] in _TO_MP4_FORMAT_LIST: video_save_path = save_folder + '/' + os.path.splitext( input_name_base)[0] + '_analysed.mp4' # Create output video out = cv2.VideoWriter(video_save_path, fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) # Create csv file and insert row of time, frame, and classes if create_csv is set to True if create_csv: csv_save_path = save_folder + '/' + os.path.splitext( input_name_base)[0] + '_statistics.csv' csv_field_names = class_names[:] csv_field_names.insert(0, "time") csv_field_names.insert(0, "frame") print(fps) sec_counter = 0 with open(csv_save_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_file.write("sep=,") csv_file.write('\n') csv_writer.writerow(csv_field_names) csv_input_dict = {"frame": cap.get(cv2.CAP_PROP_POS_FRAMES)} try: while True: ret, frame = cap.read() if not ret: break # Resize frame to fit model and run detection resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) # In one second intervals, insert the maximum number of detections per class to a new row in csv if create_csv: csv_input_dict["frame"] = cap.get( cv2.CAP_PROP_POS_FRAMES) csv_input_dict["time"] = cap.get(cv2.CAP_PROP_POS_MSEC) for cls in range(len(class_names)): number_of_obj = len(detection_result[0][cls]) if number_of_obj != 0: print(class_names[cls] + str(number_of_obj)) if class_names[cls] in csv_input_dict: csv_input_dict[class_names[cls]] = max( number_of_obj, csv_input_dict[class_names[cls]]) else: csv_input_dict[ class_names[cls]] = number_of_obj if cap.get( cv2.CAP_PROP_POS_MSEC) / 1000 >= sec_counter: with open(csv_save_path, 'a', newline='') as csv_file: csv_writer = csv.DictWriter( csv_file, fieldnames=csv_field_names) csv_writer.writerow(csv_input_dict) sec_counter += 1 for cls in range(len(class_names)): csv_input_dict.pop(class_names[cls], None) print(sec_counter) # Draw detection boxes on the frame being handled draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) # Show the current output frame on window cv2.imshow(win_name, frame) # Poll for key inputs, if 'q' is pressed, break to end processing video key = cv2.waitKey(1) & 0xFF if key == ord('q'): break # Write the current frame to the output file out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') # Not in use currently elif type == 'webcam': inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3]) detections = model(inputs, training=False) saver = tf.compat.v1.train.Saver( tf.compat.v1.global_variables(scope='yolo_v3_model')) with tf.compat.v1.Session() as sess: saver.restore(sess, './weights/model.ckpt') win_name = 'Webcam detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(0) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'X264') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('./detections/detections.mp4', fourcc, fps, (int(frame_size[0]), int(frame_size[1]))) try: while True: ret, frame = cap.read() if not ret: break resized_frame = cv2.resize(frame, dsize=_MODEL_SIZE[::-1], interpolation=cv2.INTER_NEAREST) detection_result = sess.run( detections, feed_dict={inputs: [resized_frame]}) draw_frame(frame, frame_size, detection_result, class_names, _MODEL_SIZE) cv2.imshow(win_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break out.write(frame) finally: cv2.destroyAllWindows() cap.release() print('Detections have been saved successfully.') else: raise ValueError( "Inappropriate data type. Please choose either 'video' or 'images'." )