def _main(): # Get the arguments args = get_args() os.makedirs('sht_face', exist_ok=True) if args.image: # Image detection mode print('[i] ==> Image detection mode\n') detect_img(YOLO(args)) else: print('[i] ==> Video detection mode\n') # Call the detect_video method here detect_video(YOLO(args), args.video, args.output) print('Well done!!!')
def __init__(self, args): self.yolo = YOLO(args) self.project_rect_6d = licence_plate_render.ProjectRectangle6D( int(380 * 1.1), int(160 * 1.1)) self.car_threshold = 0.5 self.LP_threshold = 0.5 self._init(args) # -------------------- init LP -------------------- # self.LP_pub = rospy.Publisher(self.yolo.pub_LP, Float32MultiArray, queue_size=1) self.clipped_LP_pub = rospy.Publisher(self.yolo.pub_clipped_LP, Image, queue_size=1) self.mat_LP = Float32MultiArray()
def main(yolo_model_path, anchor_path, n_classes, image_path, conf_threshold): yolo = YOLO(yolo_model_path, anchor_path, n_classes) img = Image.open(image_path) imglist = [img] res = yolo.get_segmented_boxes_batch(imglist, conf_threshold=conf_threshold) res = res[0] vis = np.array(img) for bbox in res: xc, yc, w, h = bbox[:4] cv2.rectangle(vis, (int(xc - w // 2), int(yc - h // 2)), (int(xc + w // 2), int(yc + h // 2)), (0, 255, 0), 5) cv2.imshow("result", vis) cv2.waitKey(0)
def __init__(self, args): if args.version in ['v1', 'v2', 'v3', 'v4']: self.yolo = YOLO(args) else: print(global_variable.red + 'Version Error') print(global_variable.reset_color) sys.exit(0) self.project_rect_6d = licence_plate_render.ProjectRectangle6D( int(380 * 1.1), int(160 * 1.1)) self.car_threshold = 0.5 self.LP_threshold = 0.5 self._init(args) # -------------------- init LP -------------------- # self.LP_pub = rospy.Publisher(self.yolo.pub_LP, Float32MultiArray, queue_size=1) self.clipped_LP_pub = rospy.Publisher(self.yolo.pub_clipped_LP, Image, queue_size=1) self.mat_LP = Float32MultiArray()
class CarLPVideo(Video): def __init__(self, args): self.yolo = YOLO(args) self.project_rect_6d = licence_plate_render.ProjectRectangle6D( int(380 * 1.1), int(160 * 1.1)) self.car_threshold = 0.5 self.LP_threshold = 0.5 self._init(args) # -------------------- init LP -------------------- # self.LP_pub = rospy.Publisher(self.yolo.pub_LP, Float32MultiArray, queue_size=1) self.clipped_LP_pub = rospy.Publisher(self.yolo.pub_clipped_LP, Image, queue_size=1) self.mat_LP = Float32MultiArray() def process(self, net_img, net_out, net_dep): pred_LP = self.yolo.predict_LP([net_out[-1]]) pred_car = self.yolo.predict(net_out[:3]) # --------------- data[5] is depth --------------- # if net_dep: # net_dep != None x = int(net_dep.shape[1] * pred_car[0, 2]) y = int(net_dep.shape[0] * pred_car[0, 1]) pred_car[0, 5] = net_dep[y, x] else: pred_car[0, 5] = -1 # ---------------- data[5] is azi ---------------- # ''' x = pred_car[0, -24:] prob = np.exp(x) / np.sum(np.exp(x), axis=0) c = sum(cos_offset * prob) s = sum(sin_offset * prob) vec_ang = math.atan2(s, c) pred_car[0, 5] = vec_ang ''' # ------------------------------------------------- # yolo_gluon.switch_print( 'cam to pub: %f' % (rospy.get_rostime() - self.net_img_time).to_sec(), verbose) self.ros_publish_array(self.LP_pub, self.mat_LP, pred_LP[0]) self.ros_publish_array(self.car_pub, self.mat_car, pred_car[0]) self.visualize_carlp(pred_car, pred_LP, net_img) def visualize_carlp(self, pred_car, pred_LP, img): LP_out = pred_LP[0] # -------------------- Licence Plate -------------------- # if LP_out[0] > self.LP_threshold: img, clipped_LP = self.project_rect_6d.add_edges(img, LP_out[1:]) clipped_LP_msg = self.bridge.cv2_to_imgmsg(clipped_LP, 'bgr8') self.clipped_LP_pub.publish(clipped_LP_msg) #if self.show: #cv2.imshow('Licence Plate', clipped_LP) self.visualize(pred_car, img)
from yolo3.utils import letterbox_image import os from keras.utils import multi_gpu_model from YOLO import YOLO if __name__ == '__main__': model_path = "logs/20181028/yolov3_20181028_6.h5" classes_path = 'model_data/object_classes2018_10_26.txt' classes_recongition_value_path = 'model_data/object_classes_to_value2018_10_26.txt' # anchors_path='yolov3_anchors_416_20181020.txt' anchors_path = 'model_data/yolov3_anchors_416_20181020.txt' yolo = YOLO(model_image_size=(320, 320), iou=0.2, score=0.1, model_path=model_path, classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path, anchors_path=anchors_path, Use_Soft_NMS=False) image_path = 'Data\\ball2he\\ball2he11_0005.jpg' image_out_path = "image_test/image_out/" if (not os.path.exists(image_out_path)): os.mkdir(image_out_path) # # image = Image.open(image_path).convert('RGB') # # image1 = yolo.detect_image(image) # image_out_name = image_path.split('\\')[-1] # image1.save(image_out_path+image_out_name) image = Image.open(image_path).convert('RGB')
folder_name_all=currentPath1 + '/mAP/Object-Detection-Metrics-master/'+folder_name+'/' if(not os.path.exists(folder_name_all)): os.mkdir(folder_name_all) dict_mAP_all = [] for model_path in model_paths: ave_map = [] for size in sizes: for iou in ious: start1 = timer() score_min=min(scores) use_soft_nms=False K.clear_session() # 加载YOLO模型 model_path=os.path.join(currentPath1,model_path) yolo=YOLO(model_path=model_path,score=score_min,iou=iou,classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path,anchors_path=anchors_path, model_image_size=(size,size),Use_Soft_NMS=use_soft_nms) model_name = model_path.split('/')[-1] print("Now Process model:{} iou:{} size:{}".format(model_name,iou,(size,size))) for line in lines: #恢复score yolo.score=score_min line = line[:-1] a = line.split(' ') image_name = a[0] image_name = os.path.join(currentPath1,image_name) image = Image.open(image_name) out_boxes_yuanshi, out_scores_yuanshi, out_classes_yuanshi = yolo.detect_image_txt(image)
if test_dataloader: model.eval() j = 0 with torch.no_grad(): for x, y in test_dataloader: x_batch = x.cuda() y_batch = y.cuda() out = model(x_batch) loss, c1 = my_loss_func(out, y_batch) step_loss = loss.item() test_loss += step_loss j += 1 test_loss /= j torch.save(model, os.path.join(save_path, cfg.BASE_MODEL + '.pth')) log_file.write(str(epcoh_loss / i) + " " + str(test_loss) + '\n') log_file.flush() print(epcoh_loss / i, test_loss) if __name__ == '__main__': model = YOLO(cfg.BASE_MODEL, cfg.GRID_SIZE, len(cfg.CLASSES), cfg.BOXES_PER_CELL).cuda() dataloader = get_train_data_loader(cfg.DATASET_PATH, cfg.CLASSES, cfg.BATCH_SIZE) test_dataloader = get_test_data_loader(cfg.DATASET_PATH, cfg.CLASSES, cfg.BATCH_SIZE) model.train() train(model, dataloader, test_dataloader, cfg.EPOCHS)
from keras.utils import multi_gpu_model # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" from YOLO import YOLO if __name__ == '__main__': start = timer() model_path = "logs/20181028/yolov3_20181028_6.h5" classes_path = 'model_data/object_classes2018_10_26.txt' classes_recongition_value_path = 'model_data/object_classes_to_value2018_10_26.txt' # anchors_path='yolov3_anchors_416_20181020.txt' anchors_path = 'model_data/yolov3_anchors_416_20181020.txt' yolo = YOLO(model_image_size=(320, 320), iou=0.2, score=0.1, model_path=model_path, classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path, anchors_path=anchors_path, Use_Soft_NMS=False) object_txt = 'Data\\all_train_data.txt' with open(object_txt) as f: lines = f.readlines() image_path = 'image_test' image_out = image_path + '/real_image_20181028/' if (not os.path.exists(image_out)): os.mkdir(image_out)
DataSet = shuffle(ann_path, img_path, labels, batch_size, epoch) x = tf.placeholder(tf.float32, shape=[None, input_h, input_w, input_c], name='input') _probs = tf.placeholder(tf.float32, shape=[None, S * S, C], name='probs') _confs = tf.placeholder(tf.float32, shape=[None, S * S, B], name='confs') _coord = tf.placeholder(tf.float32, shape=[None, S * S, B, 4], name='coord') _proid = tf.placeholder(tf.float32, shape=[None, S * S, C], name='proid') _areas = tf.placeholder(tf.float32, shape=[None, S * S, B], name='areas') _upleft = tf.placeholder(tf.float32, shape=[None, S * S, B, 2], name='upleft') _botright = tf.placeholder(tf.float32, shape=[None, S * S, B, 2], name='botright') model = YOLO() output = model.get_tiny_model(x) print(output.get_shape().as_list()) loss = loss_fn(output, _probs, _confs, _coord, _proid, _areas, _upleft, _botright) tf.summary.scalar("loss", loss) ''' 原文中的学习率策略为: 1.Use learning rate equal to 0.01 to train 75 epochs. 2.Using learning rate equal to 0.001 to train 35 epochs. 3.Using learning rate equal to 0.0001 to train 30 epochs. ''' global_step = tf.Variable(0)
return TODOS, 201 # 这行代码是只让模式初始化一次 if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': model_path='model_data/trained_weights_finally_20101018.h5' classes_path = 'model_data/object_classes2018_10_18.txt' classes_recongition_value_path = 'model_data/object_classes_to_value2018_10_18.txt' yolo = YOLO(model_image_size=(288, 288), iou=0.2, score=0.3, model_path=model_path, classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path) #为了解决keras 在Flask下报错,在初始化之后先运行一次程序 image = Image.open('image_test/1.jpg') object_stutas,image1 = yolo.recognition_image(image) # print(image1) if __name__ == '__main__': parser = reqparse.RequestParser() parser.add_argument('ImageName') parser.add_argument('SwitchName') parser.add_argument('SwitchStatus') app = Flask(__name__)
parser.add_argument('--lr', type=float, default=2e-4) parser.add_argument('--momentum', type=float, default=0.7) parser.add_argument('--model', default='YOLO') parser.add_argument('--e', type=int, default=50) parser.add_argument('--visdom', default=False, type=str, help='Use visdom for loss visualization') opt = parser.parse_args() if __name__ == "__main__": from voc import test_dataloader, train_dataloader, num_class, batch_size, compute_ap if opt.load: model = torch.load(opt.load) elif opt.model == 'YOLO': from YOLO import YOLO model = YOLO(num_class, 1) loss = model.loss(batch_size, 5, 0.5) else: print("the model name is wrong {}".format(opt.model)) exit(-1) if opt.eval: print("not ready") else: vis = None if opt.visdom: vis = visdom.Visdom() train(vis, model, test_dataloader, train_dataloader, loss, compute_ap, num_class, opt)
import os import utils import matplotlib.pyplot as plt import numpy as np from YOLO import YOLO params = utils.Params('experiment/params.json') params.device = "cpu" images = [] for i in range(32): name = utils.get_image_name(i) image = plt.imread('./data/raw_GTSDB/' + name) images.append(image) images = np.array(images) yolo = YOLO(params) output = yolo.predict(images) for i in range(output.shape[0]): plt.subplot(4, 8, i+1) plt.imshow(output[i]) plt.show()
if __name__ == "__main__": if (len(sys.argv) == 2): mode = sys.argv[1] else: mode = "slow" data_path = const.DATA_FILE_PATH cfg_path = const.CFG_FILE_PATH weight_path = const.WEIGHT_FILE_PATH test_data_dir = const.TEST_DATA_DIR_PATH img_output_path = const.IMG_OUTPUT_DIR_PATH # 検証したいモデルを用意 model = YOLO() model.load_network(data_path, cfg_path, weight_path) # 検証クラスを準備 validater = ModelValidation() validater.set_model(model) validater.set_test_data_dir(test_data_dir) if mode == "slow": start_threshold = 0.01 threshold_step = 0.05 end_threshold = 0.99 else: start_threshold = 0.1 threshold_step = 0.4
import os from keras.utils import multi_gpu_model # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" from YOLO import YOLO if __name__ == '__main__': model_path = "logs/20181020_1080Ti/yolov3_20181020_5.h5" classes_path = 'Data/object_classes2018_10_20.txt' classes_recongition_value_path = 'Data/object_classes_to_value2018_10_20.txt' anchors_path = 'model_data/yolov3_anchors_416_20181020.txt' yolo = YOLO(model_image_size=(608, 608), iou=0.2, score=0.3, model_path=model_path, classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path, anchors_path=anchors_path) object_txt = 'Data\\test.txt' class_txt = "Data/object_classes2018_10_20.txt" with open(class_txt) as f: class_names = f.readlines() for i in range(len(class_names)): class_names[i] = class_names[i][:-1] with open(object_txt) as f: lines = f.readlines() for line in lines:
import numpy as np from keras import backend as K from keras.models import load_model from keras.layers import Input from PIL import Image, ImageFont, ImageDraw from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body from yolo3.utils import letterbox_image import os from keras.utils import multi_gpu_model # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" from YOLO import YOLO if __name__=='__main__': yolo=YOLO() image_path='image_test' image_out=image_path + '/out3/' if(not os.path.exists(image_out)): os.mkdir(image_out) image_path_names=os.listdir(image_path) for imagename in image_path_names: if( imagename.endswith('.jpg')): image_in=image_path+'/'+imagename print(image_in) image_out_name=image_out+imagename image = Image.open(image_in).convert('RGB') score,image1 = yolo.recognition_image(image) image1.save(image_out_name)
elif rotations[video_index] == 2: frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # Prepare frame and Run YOLO frame = cv2.resize(frame, (int(frame.shape[1] * frame_scale), int(frame.shape[0] * frame_scale))) yolo_frame = cv2.resize(frame, (int(frame.shape[1] * yolo_frame_scale), int(frame.shape[0] * yolo_frame_scale))) img = prep_image(yolo_frame, inp_dim) im_dim = yolo_frame.shape[1], yolo_frame.shape[0] im_dim = torch.FloatTensor(im_dim).repeat(1, 2) if CUDA: im_dim = im_dim.cuda() img = img.cuda() with torch.no_grad(): output = YOLO(img.cuda(), CUDA) output = write_results(output, confidence, num_classes, nms_conf=nms_thesh) # If not object is found if type(output) == int: if found_flag: found_flag = False if count_found< 0: count_found= 0 else: count_found+= 1 c1 = [-1, -1] c2 = [-1, -1] else:
time_str = datetime.datetime.now().strftime('%Y%m%d-%H%M%S') xlsx_name = 'mAP/mAP测试结果' + time_str + '.xlsx' wb.save(os.path.join(currentPath1, xlsx_name)) dict_mAP_all = [] for model_path in model_paths: ave_map = [] for size in sizes: for iou in ious: for score in scores: for use_soft_nms in use_soft_nmses: K.clear_session() # 加载YOLO模型 model_path=os.path.join(currentPath1,model_path) yolo=YOLO(model_path=model_path,score=score,iou=iou,classes_path=classes_path, classes_recongition_value_path=classes_recongition_value_path,anchors_path=anchors_path, model_image_size=(size,size),Use_Soft_NMS=use_soft_nms) model_name = model_path.split('/')[-1] print("Now Process model:{} iou:{} score:{} size:{} Use_Soft_NMS:{}".format(model_name,iou,score,(size,size),use_soft_nms)) for line in lines: line = line[:-1] a = line.split(' ') image_name = a[0] image_name = os.path.join(currentPath1,image_name) image = Image.open(image_name) out_boxes, out_scores, out_classes = yolo.detect_image_txt(image) image_name1=(image_name.split('\\')[-1]).split('.')[0] with open((currentPath1+'/mAP/Object-Detection-Metrics-master/detections/'+image_name1+".txt"),'w+') as f1: