def __init__(self, is_site, configuration): #TODO load classifier self.is_site = is_site self.site_use_yolo = False if is_site: if self.site_use_yolo: self.model = YOLO(model_path=configuration["model_path"], anchors_path=configuration["model_anchor"], classes_path=configuration["model_classes"]) self.traffic_light_classes = 9 # TODO: hard code self.model.detect_image(Image.new('RGB', (800, 600))) else: self.model = Model(model_path=configuration["model_path"]) self.model.detect_traffic_light(np.zeros((800, 600, 3))) else: self.model = YOLO(model_path=configuration["model_path"], anchors_path=configuration["model_anchor"], classes_path=configuration["model_classes"]) self.traffic_light_classes = 9 # TODO: hard code self.model.detect_image(Image.new('RGB', (800, 600))) rospy.loginfo("Traffic light classifier: Initialization completed.")
def _main(path_weights, path_anchors, model_image_size, path_classes, nb_gpu, path_output=None, images=False, videos=False, stream=False): assert any([images, videos, stream]), 'nothing to do...' yolo = YOLO(path_weights, path_anchors, path_classes, model_image_size, nb_gpu=nb_gpu) if images: # Image detection mode, disregard any remaining command line arguments logging.info('Image detection mode') loop_detect_image(yolo, path_output) elif videos: logging.info('Video detection mode') loop_detect_video(yolo, path_output) elif stream: logging.info('Video detection mode') loop_detect_stream(yolo, path_output)
def __init__(self): rospy.Subscriber("/state_machine/state", String, self.state_callback) self.pub_condition = rospy.Publisher( "/state_machine/window/find_condition", String, queue_size=10) rospy.init_node('window', anonymous=True) self.yolo = YOLO() self.count_stable = 0
def __init__(self, is_site, configuration): #TODO load classifier self.is_site = is_site self.site_use_yolo = False if is_site: if self.site_use_yolo: self.model = YOLO(model_path=configuration["model_path"], anchors_path=configuration["model_anchor"], classes_path=configuration["model_classes"]) self.traffic_light_classes = 9 # TODO: hard code else: self.model = Model(model_path=configuration["model_path"]) else: self.model = YOLO(model_path=configuration["model_path"], anchors_path=configuration["model_anchor"], classes_path=configuration["model_classes"]) self.traffic_light_classes = 9 # TODO: hard code
def __init__(self, score=0.25, dataset=DataSet.tiny): self.logger = get_logger(self) if dataset == DataSet.normal: self.yolo = YOLO( score=score ) elif dataset == DataSet.tiny: self.yolo = YOLO( score=score, model_path='model_data/yolov3-tiny.h5', anchors_path='model_data/yolov3-tiny_anchors.txt' ) elif dataset == DataSet.crowdhuman: self.yolo = YOLO( score=score, model_path='model_data/yolov3-tiny-crowdhuman-80000.h5', anchors_path='model_data/yolov3-tiny-crowdhuman_anchors.txt', classes_path='model_data/crowdhuman.names' ) else: raise ValueError('Dataset {} unknown'.format(dataset))
def load_model(model_weights, anchors_path, classes_path, input_shape): """load trained model for performing prediction.""" # define YOLO detector yolo = YOLO( **{ "model_path": model_weights, "anchors_path": anchors_path, "classes_path": classes_path, "model_image_size": input_shape }) return yolo
def get(self): self.set_header('Access-Control-Allow-Origin', '*') self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') modelId = self.get_query_argument('modelId', 'None') if int(config["server1"]["modelId"]) == int(modelId): _,B = model.detect_image("/home/nlp/lmy/pdf8-web/01.jpg") else: model = YOLO() _,B = model.detect_image("/home/nlp/lmy/pdf8-web/01.jpg") respon = {"obj":str(B)} self.set_header('Content-Type', 'application/json; charset=UTF-8') self.write(json.dumps(respon)) self.finish()
def load_model(model_path): anchors_path = "./model_data/yolo_anchors.txt" classes_path = "./model_data/data_classes.txt" yolo = YOLO( **{ "model_path": model_path, "anchors_path": anchors_path, "classes_path": classes_path, "score": 0.25, "gpu_num": 1, "model_image_size": (416, 416), } ) return yolo
def test(filename, model_path, anchors_path, classes_path, *, score_threshold=0.2, iou=0.45, max_boxes=100): yolo = YOLO( model_path=model_path, anchors_path=anchors_path, classes_path=classes_path, score_threshold=score_threshold, iou=iou, max_boxes=max_boxes, ) x = imread(filename) x = Image.fromarray(x) x = yolo.detect_image(x) x = np.array(x) imsave('out.png', x)
def _main(path_weights, path_anchors, model_image_size, path_classes, path_output, nb_gpu=0, **kwargs): yolo = YOLO(weights_path=path_weights, anchors_path=path_anchors, classes_path=path_classes, model_image_size=model_image_size, nb_gpu=nb_gpu) logging.info('Start image/video processing..') if 'path_image' in kwargs: for path_img in tqdm.tqdm(kwargs['path_image'], desc='images'): logging.debug('processing: "%s"', path_img) predict_image(yolo, path_img, path_output) if 'path_video' in kwargs: for path_vid in tqdm.tqdm(kwargs['path_video'], desc='videos'): logging.debug('processing: "%s"', path_vid) predict_video(yolo, path_vid, path_output)
def evaluate(data, model_path, anchors_path, classes_path): yolo = YOLO( model_path=model_path, anchors_path=anchors_path, classes_path=classes_path, score_threshold=0.0, iou=iou_threshold, max_boxes=10000, ) lines = open(data).readlines() B_list = [] BP_list = [] for i, l in enumerate(lines): toks = l.strip().split(' ') image_filename = toks[0] boxes = toks[1:] x = imread(image_filename) x = Image.fromarray(x) B = [list(map(int, b.split(','))) for b in boxes] out_boxes, out_scores, out_classes = yolo.predict_image(x) BP = [ list(tuple(b) + (c, s)) for b, s, c in zip(out_boxes, out_scores, out_classes) ] B_list.extend(B) BP_list.extend(BP) if i % 10 == 0: print('[{:05d}]/[{:05d}]'.format(i, len(lines))) break stats = get_stats(B_list, BP_list) for k in sorted(stats.keys()): v = stats[k] print('{}: {:.2f}'.format(k, v))
'chimney', 'string', 'luggage', 'letters', 'stop sign', 'duck', 'horses', 'panel', 'sand', 'chairs', 'fork', 'mountains', 'trunk', 'leaves', 'white', 'leg', 'carpet', 'tennis racket', 'steps', 'shelf', 'holder', 'pepper', 'background', 'controller', 'train', 'ski', 'stripes', 'ball', 'speaker', 'sauce', 'umbrella', 'vehicle', 'face', 'bench', 'boot', 'fruit', 'can', 'neck', 'tower', 'vegetables', 'sandwich', 'bridge', 'awning', 'tail', 'couch', 'hat', 'line', 'wheel', 'skirt', 'tracks', 'stem', 'clouds', 'sofa', 'this', 'trees', 'vest', 'shoe', 'flag', 'lid', 'tree trunk', 'side', 'cow', 'water', 'sock', 'track', 'shore', 'headlight', 'napkin', 'bush', 'tennis ball', 'cat', 'sunglasses', 'knee', 'design', 'flower', 'platform', 'counter', 'laptop', 'lettuce', 'flowers' ] n_classes = len(labels) save_json_path = '12345.json' detector = YOLO() results = {} name_list = [] for file in os.listdir(input_path): if file.endswith(".jpg"): name_list.append(file) number_objects = 0 for name in name_list: #test img_path = os.path.join(input_path, name) result = {} image = Image.open(img_path) image_ = cv2.imread(img_path) height, width, depth = image_.shape _, res = detector.detect_image(image)
# coding=utf-8 import os import json import yaml import tornado.web import tornado.ioloop from yolo3.yolo import YOLO model = YOLO() with open('./port.yml','rb') as file_config: config = yaml.load(file_config) class FileUploadHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', '*') self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') modelId = self.get_query_argument('modelId', 'None') if int(config["server1"]["modelId"]) == int(modelId): _,B = model.detect_image("/home/nlp/lmy/pdf8-web/01.jpg") else: model = YOLO() _,B = model.detect_image("/home/nlp/lmy/pdf8-web/01.jpg") respon = {"obj":str(B)} self.set_header('Content-Type', 'application/json; charset=UTF-8') self.write(json.dumps(respon)) self.finish() app = tornado.web.Application([ (r'/redfile', FileUploadHandler),
(200, 0, 0), -1) img = cv2.circle(img, (int( (mean_box_cord[3] + mean_box_cord[1]) / 2), int((mean_box_cord[0] + mean_box_cord[2]) / 2)), 5, (0, 200, 0), -1) frame += 1 # print(type(r_image)) cv2.imshow("boxed", img) cv2.imshow( "main_box", image[mean_box_cord[0]:mean_box_cord[2], mean_box_cord[1]:mean_box_cord[3]]) else: print("none") k = cv2.waitKey(30) & 0xff if (k == 27): break yolo.close_session() cv2.destroyAllWindows() cap.release() FLAGS = None if __name__ == '__main__': print("Image detection mode") detect_img(YOLO())
model_path = cfg.model_path anchors_path = cfg.anchors_path classes_path = cfg.classes_path # 图片和video路径改这里 #jpg path # 测试某文件夹所有图片 data_set_root = cfg.data_set_root img_path = cfg.img_path # 测试mp4 video_path = cfg.video_path rot_number = cfg.rot_number # 测试单张jpg single_img_path = cfg.single_img_path ####################### end ################################################ obj = { 'model_path': model_path, 'anchors_path': anchors_path, 'classes_path': classes_path } if __name__ == '__main__': choice = 3 if choice == 1: #一张图片 dk.detect_img(YOLO(obj), single_img_path) if choice == 2: # 图片list dk.detect_img_list(YOLO(obj), img_path=img_path) elif choice == 3: # 视频 dk.detect_video(YOLO(obj), video_path, rot_number, output_path="") else: print('done')
rot_number = cfg.rot_number # 测试单张jpg single_img_path = cfg.single_img_path deepsort_model_filename = cfg.deepsort_model_filename ####################### end ################################################ setting_dict = { 'model_path': model_path, 'anchors_path': anchors_path, 'classes_path': classes_path } if __name__ == '__main__': choice = 4 if choice == 1: #一张图片 dk.detect_img(YOLO(setting_dict), single_img_path) if choice == 2: # 图片list dk.detect_img_list(YOLO(setting_dict), img_path=img_path) elif choice == 3: # 视频 dk.detect_video(YOLO(setting_dict), video_path, rot_number, output_path="") elif choice == 4: # yolo视频流+目标跟踪deepsort dk.detect_video_with_deepsort( YOLO(setting_dict), video_path, rot_number, output_path="", deepsort_model_filename=deepsort_model_filename) else:
from yolo3.yolo import YOLO from yolo3.yolo import detect_camera if __name__ == '__main__': detect_camera(yolo=YOLO())
default='./result/', help='output path of detect results') parser.add_argument( '--model', type=str, # set it use command, default value is in yolo.py help='path to keras-model weight file') parser.add_argument( '--anchors', type=str, # set it use command, default value is in yolo.py help='path to anchor definitions') parser.add_argument( '--classes', type=str, # set it use command, default value is in yolo.py help='path to class definitions') parser.add_argument( '--gpu_num', type=int, # set it use command, default value is in yolo.py help='Number of GPU to use') parser.add_argument('--single', default=False, action="store_true", help='single test or batch test, default batch') opt = parser.parse_args() detect_img(YOLO(**vars(opt)))
def main(): data_path = "/home/hjl/data2/datasets/train_202009" # 类别 categories = ['pig'] # 类别数 num_classes = len(categories) # 图片的输入尺寸, hw input_shape = (320, 320) # coco数据集在yolo上的anchors, wh anchors_mask = np.array([[6, 7, 8], [3, 4, 5], [0, 1, 2]]) anchors = np.array([[24, 31.], [43, 49], [56, 74], [75, 56], [100, 70], [141, 78], [154, 58], [184, 70], [216, 85]]) # 用了3个网络层 out_layers = len(anchors_mask) # 训练数据的批次大小 batch_size = 16 ignore_thresh = 0.5 learning_rate_base = 1e-4 log_dir = 'logs/' # 创建模型 model = YOLO(input_shape=input_shape, anchors=anchors, anchors_mask=anchors_mask, num_classes=num_classes, ignore_thresh=ignore_thresh, freeze=False, loss="ciou").model('xception', training=True) model.summary() model.load_weights( "/home/hjl/data2/skyeyed/yolo3/logs/diou_model-loss0.6251-val_loss0.6233.h5" ) # 生成日志信息 以及 配置回调函数 logging = TensorBoard(log_dir=log_dir) checkpoint = ModelCheckpoint( log_dir + "model_epoch:{epoch:03d}-loss:{loss:.4f}-val_loss:{val_loss:.4f}.h5", monitor='loss', mode='min', save_weights_only=False, save_best_only=False, period=1) early_stopping = EarlyStopping(monitor='loss', min_delta=0, patience=5, verbose=1) # 定义loss loss = [ YOLO(input_shape=input_shape, anchors=anchors[mask], anchors_mask=anchors_mask, num_classes=num_classes, ignore_thresh=ignore_thresh).calculation_loss for mask in anchors_mask ] model.compile(optimizer=Adam(lr=learning_rate_base), loss=loss) val_annotation_path = '/home/hjl/data2/skyeyed/yolo3/data/val_30k.txt' # 读取数据 #with open(val_annotation_path) as f: # train_lines = f.readlines() model.fit_generator( data_gen(data_path, batch_size, input_shape, anchors, out_layers, num_classes), steps_per_epoch=500, validation_data=data_gen(data_path, batch_size, input_shape, anchors, out_layers, num_classes), validation_steps=100, epochs=100, initial_epoch=10, callbacks=[logging, checkpoint, early_stopping], #use_multiprocessing=True, #workers=8 ) # model.fit_generator(data_gen_val(train_lines, batch_size, input_shape, anchors, out_layers, num_classes), # steps_per_epoch=len(train_lines) // batch_size, # validation_data=data_gen_val(train_lines, batch_size, input_shape, anchors, out_layers, num_classes), # validation_steps=10, # epochs=100, # initial_epoch=0, # callbacks=[logging, checkpoint, early_stopping], # use_multiprocessing = True, # workers = 8 # ) model.save(log_dir + 'trained_weights.h5')
def __init__(self, model_path=None, classes_path=None, classes=None): model_path = model_path or '' self.model = YOLO(model_path=model_path, classes_path=classes_path)
def __init__(self,model_flag='local'): self.model_flag = model_flag super(MainWindow, self).__init__() loadUi("./UI/MainForm.ui", self) self.cam_01 = False self.cam_02 = False self.cam_03 = False self.run_flag = False self.vs1 = None self.feed1 = None self.vs2 = None self.feed2 = None self.vs3 = None self.feed3 = None self.cnt = -1 res = QMessageBox.question(self,'提示','是否使用在线识别模式?',QMessageBox.Yes|QMessageBox.No,QMessageBox.No) if res == QMessageBox.Yes: self.model_flag = 'online' else: self.model_flag = 'local' if (self.model_flag=='local'): self.yolo = YOLO() elif (self.model_flag == 'online'): self.yolo = YOLO_ONLINE() self.st = time.time() self.coco = False self.first = True self.CameraA.setScaledContents(True) self.CameraA.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.CameraB.setScaledContents(True) self.CameraB.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.CameraC.setScaledContents(True) self.CameraC.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self.cam_clear_gaurd = False self.processor = MainProcessor(['cam_01','cam_02','cam_03']) self.database = Database.getInstance() #----------数据库初始化-------------------------------------# # self.database.insertIntoCamera('cam_01') # self.database.insertIntoCamera('cam_02') # self.database.insertIntoCamera('cam_03') # self.database.insertIntoSetting() # for i in range(19)[1:]: # flow_type=class_name[str(i)] # self.database.insertIntoFlawStatistic(flaw_type=flow_type,flaw_cont=0) # self.database.deleteAllFlaw() #----------------------------------------------------------# #----------测试数据-----------------------------------------# # for i in range(19)[1:]: # self.database.insertIntoFlaw(flaw_id=i-1,flaw_type=class_name[str(i)]) # for i in range(19)[1:]: # self.database.updateFlawStatistic(flaw_type=class_name[str(i)],flaw_cont=1) #----------------------------------------------------------# self.statistic_cnt = {} for i in range(19)[1:]: self.statistic_cnt[class_name[str(i)]]=self.database.getcntFromFlawStatistic(flaw_type=class_name[str(i)]) self.database.updataCaminfo('cam_01') self.database.updataCaminfo('cam_02') self.database.updataCaminfo('cam_03') self.database.updataOpenflag('cam_01',0) self.database.updataOpenflag('cam_02',0) self.database.updataOpenflag('cam_03',0) self.ID = self.database.getFlawCount() #获取最大的标号 self.updateCamInfo() self.PB_CamAsettings.clicked.connect(self.setCameraA) self.PB_CamBsettings.clicked.connect(self.setCameraB) self.PB_CamCsettings.clicked.connect(self.setCameraC) self.PB_picture.clicked.connect(self.picworkForm) self.PB_start.clicked.connect(self.start) self.PB_end.clicked.connect(self.stop) self.User.triggered.connect(self.helpform) self.PB_statistics.clicked.connect(self.statisForm) self.PB_settings.clicked.connect(self.setting) self.tablemodel = QStandardItemModel(10, 8) self.tablemodel.setHorizontalHeaderLabels(['编号','瑕疵种类','相机','X坐标','Y坐标','瑕疵宽度','瑕疵高度','时间戳']) self.tV_info.setModel(self.tablemodel) self.timer = QTimer(self) self.timer.timeout.connect(self.update_image) self.timer.start(50)
import sys import json import tornado.web import tornado.ioloop import utils from yolo3.yolo import YOLO class FileUploadHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', '*') self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') path = self.get_query_argument('path', 'None') _, res = model.detect_image(path) respon = {"obj": str(res)} self.set_header('Content-Type', 'application/json; charset=UTF-8') self.write(json.dumps(respon)) self.finish() if __name__ == '__main__': pid = os.getpid() port, model_path, labels = sys.argv[1], sys.argv[2], sys.argv[3] #预先锁定 model = YOLO(model_path=model_path, labels=labels) utils.set_port(port=port, pids=pid, modelPath=model_path, status=-1) app = tornado.web.Application([(r'/yolo', FileUploadHandler), ]) app.listen(port) tornado.ioloop.IOLoop.instance().start()
import sys if len(sys.argv) < 2: print("Usage: $ python {0} [video_path] [output_path(optional)]", sys.argv[0]) exit() from yolo3.yolo import YOLO from yolo3.yolo import detect_video if __name__ == '__main__': video_path = sys.argv[1] if len(sys.argv) > 2: output_path = sys.argv[2] detect_video(YOLO(), video_path, output_path) else: detect_video(YOLO(), video_path)