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 __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 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 arg_params_yolo(): # class YOLO defines the default value, so suppress any default HERE parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) # Command line options parser.add_argument('-w', '--path_weights', type=str, help='path to model weight file') parser.add_argument('-a', '--path_anchors', type=str, help='path to anchor definitions') parser.add_argument('--model_image_size', type=int, nargs=2, required=False, help='input image size for the model', default=(None, None)) parser.add_argument('-c', '--path_classes', type=str, help='path to class definitions') parser.add_argument('--nb_gpu', type=int, help='Number of GPU to use', default=str(YOLO.get_defaults("nb_gpu"))) parser.add_argument('-o', '--path_output', required=False, type=str, default='.', help='path to the output directory') return parser
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)
class ObjectDetector(AlgoStep): 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 process(self, container: AlgoContainer): image = Image.fromarray(container.frame.image[..., ::-1]) # bgr to rgb detections = self.yolo.detect_image(image) if not detections: detections = [] # TODO Transform detections into ValueObject return container.extend_with(detections=detections)
class YoloPreditor: 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 predict(self, img): objects = self.model.detect_image(img, with_prob_and_class=True) return objects
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 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 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 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))
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 _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 __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 __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)
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)))
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:
class DroneStabilization: image_sub = None 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 setup_window(self): self.image_pub = rospy.Publisher("image_topic_2", Image, queue_size=10) self.vel_pub = rospy.Publisher('controle/velocity', Vector3D, queue_size=10) self.bridge = CvBridge() # self.stab = camStab() self.detect = detect_window() self.vec_vel = Vector3D() self.vec_vel.x = 0 self.vec_vel.y = 0 self.vec_vel.z = 0 self.pixel_movement = [0, 0] self.frame = 0 self.count_callbacks = 0 self.opt_flow = camStab() self.image_sub = rospy.Subscriber("/bebop/image_raw", Image, self.callback, queue_size=None) rospy.loginfo("setup ok") # self.pub_condition.publish("ok") def state_callback(self, data): if (data.data == "find_window"): rospy.loginfo(" WINDOW !!!") self.setup_window() # condition!! elif (self.image_sub != None): rospy.loginfo(" end! ") cv2.destroyAllWindows() self.image_sub.unregister() self.image_sub = None def callback(self, data): # if self.count_callbacks < 10: # self.count_callbacks += 1 # return try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) (rows, cols, channels) = cv_image.shape if not (cols > 60 and rows > 60): # returns if data have unvalid shape return # self.detect.update(cv_image) if self.frame == 0: pil_image = PIL.Image.fromarray(cv_image) r_image, out_boxes, out_score = self.yolo.detect_image(pil_image) # window detected with min precision of (defined on yolo file) if out_score.shape[0] > 0: rospy.loginfo("detect") m_box = out_boxes.mean(0) self.mean_box_cord = m_box[:] self.image_h = r_image.shape[0] self.image_w = r_image.shape[1] self.image_center = np.array( [self.image_w / 2, self.image_h / 2]) box_margin = 30 max_score_index = np.argmax(out_score) box_lt = (max( int(out_boxes[max_score_index][1]) - box_margin, 0), max( int(out_boxes[max_score_index][0]) - box_margin, 0)) box_rb = (min( int(out_boxes[max_score_index][3]) + box_margin, self.image_w), min( int(out_boxes[max_score_index][2]) + box_margin, self.image_h)) img = cv2.rectangle(r_image, box_lt, box_rb, (0, 220, 200), 2) self.opt_flow.resetFeatures( cv_image, interval=[box_lt[1], box_rb[1], box_lt[0], box_rb[0]], get_corners_only=True) # print(init_feature + np.array(box_lt)) # self.opt_flow.set_p0(init_feature + np.array(box_lt)) # self.opt_flow.set_initial_img(cv_image) self.frame += 1 cv2.imshow("window", img) else: rospy.loginfo("no windows on view, trying again") # self.vel_pub.publish(self.vec_vel) cv2.imshow("window", r_image) else: self.opt_flow.update(cv_image) print(self.opt_flow.get_square_shape()) self.adjust_position(self.opt_flow.get_square_center(), self.opt_flow.get_square_shape()) k = cv2.waitKey(30) & 0xff if k == 27: exit() elif k == 'r' or k == ord('r'): print("r pressed ") self.frame = 0 elif k == ord('p') or k == 'p': print("through_window ") self.pub_condition.publish("ok") try: # self.vel_pub.publish(self.vec_vel) # print(self.vec_vel.x) # self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8")) pass except CvBridgeError as e: print(e) self.count_callbacks = 0 def adjust_position(self, window_center_pos, window_shape, ideal_window_image_rate=0.60): delta = self.image_center - window_center_pos # rospy.loginfo(delta) self.vec_vel.x = 0 self.vec_vel.y = 0 self.vec_vel.z = 0 if abs(delta[0]) > 5 or abs(delta[1]) > 5: self.vec_vel.y = np.sign(delta[0]) * min( np.abs(delta[0]) / 20, 0.2) self.vec_vel.z = np.sign(delta[1]) * min( np.abs(delta[1]) / 20, 0.2) self.count_stable = 0 rate = window_shape[1] / self.image_h delta_rate = ideal_window_image_rate - rate # (x,y) goes back if abs(delta_rate) > 0.02 and abs(delta[0]) < 20 and abs( delta[1]) < 20: self.vec_vel.x = delta_rate * 2 self.count_stable = 0 self.vel_pub.publish(self.vec_vel) self.count_stable += 1 if self.count_stable > 3: rospy.loginfo("GOOD!!") self.pub_condition.publish("ok")
class TLClassifier(object): 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 get_classification(self, image): """Determines the color of the traffic light in the image Args: image (cv::Mat): image containing the traffic light Returns: int: ID of traffic l ight color (specified in styx_msgs/TrafficLight) """ #TODO implement light color prediction if image is not None: image_data = np.asarray(image) if self.is_site: if self.site_use_yolo: pil_image = Image.fromarray(image_data) out_boxes, out_scores, out_classes = self.model.detect_image(pil_image) detected_states = [] for i, c in reversed(list(enumerate(out_classes))): # predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] if c == self.traffic_light_classes: top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(pil_image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(pil_image.size[0], np.floor(right + 0.5).astype('int32')) corp_traffic_light = image_data[top:bottom, left:right, :] detected_states.append(self.detect_traffic_light_state_luminous(corp_traffic_light)) if len(detected_states) > 0: (_, idx, counts) = np.unique(detected_states, return_index=True, return_counts=True) index = idx[np.argmax(counts)] return detected_states[index] else: return TrafficLight.UNKNOWN else: return self.model.detect_traffic_light(image) else: pil_image = Image.fromarray(image_data) out_boxes, out_scores, out_classes = self.model.detect_image(pil_image) detected_states = [] for i, c in reversed(list(enumerate(out_classes))): #predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] if c == self.traffic_light_classes: top, left, bottom, right = box top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min(pil_image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(pil_image.size[0], np.floor(right + 0.5).astype('int32')) corp_traffic_light = image_data[top:bottom, left:right, :] detected_states.append(self.detect_traffic_light_state_color(corp_traffic_light)) if len(detected_states) > 0: (_, idx, counts) = np.unique(detected_states, return_index=True, return_counts=True) index = idx[np.argmax(counts)] return detected_states[index] else: return TrafficLight.UNKNOWN return TrafficLight.UNKNOWN def detect_traffic_light_state_color(self, image): def simple_thresh(img, thresh=(0, 255)): # apply only thresholding on image plane and return binary image binary = np.zeros_like(img) binary[(img > thresh[0]) & (img <= thresh[1])] = 1 return binary rows = image.shape[0] # thresholding on red channel red_channel = image[:, :, 0] # thresholding on green channel green_channel = image[:, :, 1] blue_channel = image[:, :, 2] # patitioning on r_binary r_binary = simple_thresh(red_channel, (196, 255)) # plt.figure() # plt.imshow(r_binary) r_binary_1 = r_binary[0:int(rows / 3) - 1, :] r_binary_2 = r_binary[int(rows / 3):(2 * int(rows / 3)) - 1, :] r_binary_3 = r_binary[(2 * int(rows / 3)):rows - 1, :] is_red = (r_binary_1.sum() > 2 * (r_binary_2.sum())) & (r_binary_1.sum() > 2 * (r_binary_3.sum())) # (GREEN) patitioning on g_binary g_binary = simple_thresh(green_channel, (183, 255)) # plt.figure() # plt.imshow(g_binary) g_binary_1 = g_binary[0:int(rows / 3) - 1, :] g_binary_2 = g_binary[int(rows / 3):(2 * int(rows / 3)) - 1, :] g_binary_3 = g_binary[(2 * int(rows / 3)):rows - 1, :] is_green = (g_binary_3.sum() > 2 * (g_binary_2.sum())) & (g_binary_3.sum() > 2 * (g_binary_1.sum())) # (YELLOW) partition on g_binary and b_binary g_binary = simple_thresh(green_channel, (146, 255)) b_binary = simple_thresh(blue_channel, (143, 255)) combine = g_binary + b_binary y_binary_1 = combine[0:int(rows / 3) - 1, :] y_binary_2 = combine[int(rows / 3):(2 * int(rows / 3)) - 1, :] y_binary_3 = combine[(2 * int(rows / 3)):rows - 1, :] is_yellow = (y_binary_2.sum() > 2 * (y_binary_1.sum())) & (y_binary_2.sum() > 2 * (y_binary_3.sum())) if is_red: return TrafficLight.RED elif is_yellow: return TrafficLight.YELLOW elif is_green: return TrafficLight.GREEN else: return TrafficLight.UNKNOWN def detect_traffic_light_state_luminous(self, image): def adjust_histo(image): img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV) if np.max(img_yuv[:, :, 0]) - np.min(img_yuv[:, :, 0]) < 180: img_yuv[:, :, 0] = cv2.equalizeHist(img_yuv[:, :, 0]) img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) return img_output image = adjust_histo(image) img_h, img_w, img_ch = image.shape l_channel = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)[:, :, 2] top_third_marker = int(img_h / 3) bottom_third_marker = img_h - top_third_marker # Magnitude of L is established for each section of the image top = 0 mid = 0 bottom = 0 count_result = {'RED': 0, 'YELLOW': 0, 'GREEN': 0} for i in range(top_third_marker): for j in range(img_w): top += l_channel[i][j] count_result['RED'] = top # compensate for R, G, B => L, U, V for i in range(top_third_marker, bottom_third_marker): for j in range(img_w): mid += l_channel[i][j] count_result['YELLOW'] = mid for i in range(bottom_third_marker, img_h): for j in range(img_w): bottom += l_channel[i][j] count_result['GREEN'] = bottom # The result is classified into one of the 3 colors and returned max_count = max(count_result, key=count_result.get) std = np.std(list(count_result.values())) mean = np.mean(list(count_result.values())) # print('x', std, std/mean) if (std / mean < 0.1): return TrafficLight.UNKNOWN if max_count == 'RED': return TrafficLight.RED elif max_count == 'YELLOW': return TrafficLight.YELLOW elif max_count == 'GREEN': return TrafficLight.GREEN else: return TrafficLight.UNKNOWN
'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)
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')
# 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())
'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' ] jpeg = 'jpg' input_path = '../jsons' jpg_path = '../images' json_files = glob.glob(os.path.join(input_path, '*.json')) recs = get_recs(json_files) detector = YOLO() image_ids, confidence, BB = get_imgID_con_BB(detector, jpeg, jpg_path) all_p, all_r, all_ap = [], [], [] for cls_name in list(labels.keys()): classname = cls_name # replace class_recs, npos = get_class_recs(json_files, classname) ovthresh = 0.5 # sort by confidence sorted_ind = np.argsort(-confidence) sorted_scores = np.sort(-confidence) BB = BB[sorted_ind, :] image_ids = [image_ids[x] for x in sorted_ind]
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()
from yolo3.yolo import YOLO from yolo3.yolo import detect_camera if __name__ == '__main__': detect_camera(yolo=YOLO())
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_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)
# class YOLO defines the default value, so suppress any default here # When detecting in VIDEO # --model_path k_weights/yolov3-tiny.h5 --anchors_path model_data/tiny_yolo_anchors.txt # --input test_data/cold_lake_trafic.mp4 # When detecting only in IMAGE # --model_path k_weights/yolov3-tiny.h5 --anchors_path model_data/tiny_yolo_anchors.txt # --image --input test_data/cold_lake_trafic.mp4 parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) ''' Command line options ''' parser.add_argument('--model_path', type=str, help='path to model weight file, default ' + YOLO.get_defaults("model_path")) parser.add_argument('--anchors_path', type=str, help='path to anchor definitions, default ' + YOLO.get_defaults("anchors_path")) parser.add_argument('--classes_path', type=str, help='path to class definitions, default ' + YOLO.get_defaults("classes_path")) parser.add_argument('--gpu_num', type=int, help='Number of GPUs to use, default ' + str(YOLO.get_defaults("gpu_num")))
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)