def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if input_path[-4:] in ['.avi', '.mp4']: # do detection on a video video_out = output_path + '/ocr_' + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) csv_out = output_path + '/ocrcsv_' + input_path.split('/')[-1][:-3] + 'csv' video_fps = int(video_reader.get(cv2.CAP_PROP_FPS)) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), video_fps, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False quiet = True detect_text = {} labels = config['model']['labels'] # 主隊, 客隊, 局數, 好壞球, 投球數, 總球數, 球速 ocr_labels = ['score1', 'score2', 'session', 'strikeball', 'total', 'speed'] ocr_result = pd.DataFrame(columns = ocr_labels) for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i+1)/nb_frames) > start_point/100.: images += [image] if (i%batch_size == 0) or (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # detect text # return a list of ocr results # depracated call: images[i], ocr_rec = detect_text(images[i], batch_boxes[i], ocr_labels, config['model']['labels'], obj_thresh) for box in batch_boxes[i]: label_str = '' label_id = -1 # filter label(s) that is recognized for this box for j in range(len(labels)): if box.classes[j] > obj_thresh: if label_str != '': label_str += ', ' label_str += (labels[j] + ' ' + str(round(box.get_score()*100, 2)) + '%') label_id = j if not quiet: print(label_str) # detect text from current box if label_id >= 0 and labels[label_id] in ocr_labels: # do detection cropped = images[i][box.ymin:box.ymax, box.xmin:box.xmax] detect_item = pytesseract.image_to_string(cropped) # cropped image if detect_item == "": detect_text.update({labels[label_id]: "N/A"}) else: detect_text.update({labels[label_id]: detect_item}) # draw box with detected text draw_ocr_text(detect_text, ocr_labels, images[i]) draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # write records into dataframe ocr_result = ocr_result.append(detect_text, ignore_index = True) detect_text = {} # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit print(ocr_result) # TEST output ocr_result.to_csv(csv_out) if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # detect text detect_text(image, boxes, config['model']['labels'], obj_thresh) # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + '/pred_' + image_path.split('/')[-1], np.uint8(image))
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = float(config['test']['obj_thresh']), float( config['test']['nms_thresh']) ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = sorted([ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ]) # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels if not args.ano_only: draw_boxes(image, boxes, config['model']['labels'], obj_thresh) path_xml = image_path.replace(input_path, output_path).replace('jpg', 'xml') write_annos(image_path, path_xml, boxes, config['model']['labels'], obj_thresh) # filename = os.path.basename(image_path) # write the image with bounding boxes to file if not args.ano_only: cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val == True: images += [image] if (len(images) == batch_size) or (ret_val == False and len(images) > 0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] == '.mp4': # do detection on a video video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if image is None: continue print(image.shape) if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
(frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if image is None: continue if (float(i+1)/nb_frames) > start_point/100.: images += [image] if (i%batch_size == 0) or (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, anchors, obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], labels, obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release()
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 webcam_scale = 1.5 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name'], compile=False) ############################### # Predict bounding boxes ############################### if input_path[: 6] == 'webcam': # do detection on the Xth webcam given the parameter 'webcamX' video_reader = cv2.VideoCapture(int(input_path[6:])) # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val == True: images += [image] if (len(images) == batch_size) or (ret_val == False and len(images) > 0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) if webcam_scale != 1: img_shape = images[i].shape images[i] = cv2.resize( images[i], dsize=(int(img_shape[1] * webcam_scale), int(img_shape[0] * webcam_scale)), interpolation=cv2.INTER_NEAREST) images[i] = draw_receipt(images[i], batch_boxes[i], config['model']['labels'], config['entrees'], obj_thresh) cv2.imshow('Chinese Cafeteria Food Recognition', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] in ['.mp4', '.mov']: # do detection on a video video_out = output_path + input_path.split('/')[-1] fps = 30.0 batch_size = 30 print(image_path) video_reader = cv2.VideoCapture(input_path) video_writer = None frame_counter = 0 processing = True # the main loop while processing: images = [] for i in range(batch_size): processing, image = video_reader.read() if not processing: break # image = cv2.resize(image, (round(426 * 2), round(640 * 2)), interpolation=cv2.INTER_AREA) images += [image] frame_counter += 1 if len(images) > 0: print('Processed video frames: {0}'.format(frame_counter)) batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) images[i] = draw_receipt(images[i], batch_boxes[i], config['model']['labels'], config['entrees'], obj_thresh) # create videoWriter if it is the first time wrting result to the output video if not video_writer: height, width, _ = images[i].shape video_writer = cv2.VideoWriter( video_out, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height)) # write result to the output video video_writer.write(images[i]) if isinstance(video_reader, cv2.VideoCapture) and video_reader.isOpened(): video_reader.release() if video_writer: video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) image = draw_receipt(image, boxes, config['model']['labels'], config['entrees'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def _main_(input_path_x,infer_model,infer_model_2,infer_model_3,infer_model_4,infer_model_5): ''' description : main function for predict the anchors on the image using the infer models and anchors of yolo v3 for this labels :param input_path_x:the input path of the image , text (str) :param infer_model: infer model 1, .h5 :param infer_model_2:infer model 2, .h5 :param infer_model_3:infer model 3, .h5 :param infer_model_4:infer model 4, .h5 :return:void ''' input_path = input_path_x # get the input path from the args ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.90, .3 #obj_thresh = .5 mean if less than 50% sure ignore it, nms_thresh =.3 means if 30 % IOU for 2 boxes; merge them #===========================================================================# ############################### # Predict bounding boxes ############################### #============================================ for video ===========================================================# #==================================================================================================================# #============================================ for image ===========================================================# if True: # do detection on an image or a set of images image_paths = [] #==================================== image path ==============================================================# if os.path.isdir(input_path): for inp_file in os.listdir(input_path): # get the input image image_paths += [input_path + inp_file] # get the input image else: image_paths += [input_path] # relative address image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])]#image format #==============================================================================================================# ############################# anchors intialiazation ############################################ model_1_anchors=[4,13, 5,14, 6,12, 6,16, 20,25, 22,21, 27,15, 31,25, 35,17] # model_2_anchors=[26,35, 26,54, 28,36, 34,62, 35,46, 38,23, 43,80, 51,58, 58,90] # model_3_anchors=[55,69, 75,234, 133,240, 136,129, 142,363, 203,290, 228,184, 285,359, 341,260] # #model_4_anchors=[6, 88, 9, 53, 11, 168, 44, 9, 49, 77, 66, 15, 80, 67, 97, 26, 153, 11] # model_4_anchors=[7,65, 11,159, 36,9, 49,77, 50,15, 65,8, 78,15, 107,27, 161,11] # model_5_anchors=[18,47, 64,70, 71,71, 74,13, 74,52, 75,26, 76,79, 80,45, 105,103] # ################################################################################################# ################################## labels intialiazation ###################################### model_1_label=["/","state condition"] # model_2_label=["loop back arrow","state"] # model_3_label=["arrow head"] # model_4_label=["curved arrow","straight arrow"] # model_5_label=["straight arrow"] ############################################################################################### ############################################ the main loop ################################################################ for image_path in image_paths: image = cv2.imread(image_path)# load the image path image=image_preprocess.start_pre(image,condition=object_file.hand_written) image = cv2.resize(image,(1600,1200)) ########################################## predict the bounding boxes ################################################## ######################################### predict box for infer model_1 ################################################ boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, model_1_anchors, obj_thresh, nms_thresh)[0] # # ######################################### predict box for infer model_2 ################################################ boxes_2=get_yolo_boxes(infer_model_2, [image], net_h, net_w, model_2_anchors, obj_thresh, nms_thresh)[0] # # ######################################### predict box for infer model_3 ################################################ boxes_3 =get_yolo_boxes(infer_model_3, [image], net_h, net_w, model_3_anchors, obj_thresh, nms_thresh)[0] # # ######################################### predict box for infer model_4 ################################################ boxes_4 =get_yolo_boxes(infer_model_4, [image], net_h, net_w, model_4_anchors, obj_thresh, nms_thresh)[0] # # ######################################################################################################################## boxes_5 =get_yolo_boxes(infer_model_5, [image], net_h, net_w, model_5_anchors, obj_thresh, nms_thresh)[0] # # ######################################################################################################################## ######################## draw boxes after predict #################### ###################################################################### # draw bounding boxes on the image using labels model 1 # config_boxes(image, boxes, model_1_label, obj_thresh) # # # draw bounding boxes on the image using labels model 2 # config_boxes(image, boxes_2, model_2_label, obj_thresh) # # # draw bounding boxes on the image using labels model 3 # config_boxes(image, boxes_3, model_3_label, obj_thresh) # # # draw bounding boxes on the image using labels model 4 # config_boxes(image, boxes_4, model_4_label, obj_thresh) # # # draw bounding boxes on the image using labels model 5 # config_boxes(image, boxes_5, model_5_label, obj_thresh) # # ###################################################################### ###################################################################### #=====================set the exported path================================================# set_inputpath_and_image(image,image_path)#get the input image path as array removing the image name.type
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam # video_reader = cv2.VideoCapture(0) # # the main loop # batch_size = 1 # images = [] # while True: # ret_val, image = video_reader.read() # if ret_val == True: images += [image] # if (len(images)==batch_size) or (ret_val==False and len(images)>0): # batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) # for i in range(len(images)): # someImage,Xmid,Ymid = draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # cv2.imshow('video with boxes', images[i]) # get_gps(Xmid,Ymid) # images = [] # if cv2.waitKey(1) == 27: # break # esc to quit # cv2.destroyAllWindows() url = "http://192.168.43.22:8080/shot.jpg" temp_GPS = { 'latitude': 0, 'longitude': 0 } while True: img_resp = requests.get(url, verify=False) img_array = np.array(bytearray(img_resp.content), dtype=np.uint8) img = cv2.imdecode(img_array, -1) height, width, channel = img.shape batch_size = 1 images = [] images +=[img] if (len(images)==batch_size) or (len(images)>0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): someImage,Xmid,Ymid = draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) cv2.imshow('video with boxes', images[i]) if(Xmid!=0 and Ymid!=0): GPS = get_gps(Xmid,Ymid,temp_GPS) temp_GPS = GPS images = [] if cv2.waitKey(1) == 27: break elif input_path[-4:] == '.mp4': # do detection on a video video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i+1)/nb_frames) > start_point/100.: images += [image] if (i%batch_size == 0) or (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def _main_(filename): config_path = "config.json" #input_path = args.input input_path = filename output_path = "result/" with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.7, 0.25 #default is 0.5 and 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) # the main loop batch_size = 1 images = [] curr_frame = 0 while True: ret_val, image = video_reader.read() if ret_val == True: images += [image] curr_frame += 1 if (len(images) == batch_size) or (ret_val == False and len(images) > 0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh, True, curr_frame) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] == '.mp4' or input_path[ -4:] == '.MP4': # do detection on a video video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False f_name_extract = filename.split('/') csv_path = output_path + f_name_extract[len(f_name_extract) - 1] + '.csv' with open(csv_path, mode='w') as csv_file: fields = [ 'FrameNumber', 'PredictionString (class,conf,xmin,ymin,xmax,ymax)' ] writer = csv.DictWriter(csv_file, fieldnames=fields) writer.writeheader() for i in tqdm(range(nb_frames)): curr_frame = i + 1 # so first frame is named frame 1 not frame 0 _, image = video_reader.read() if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes( infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels _, info = draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh, True, curr_frame) splitted = info.split( ' ', 1 ) # split only on the first occurrence of space writer.writerow({ 'FrameNumber': splitted[0], 'PredictionString (class,conf,xmin,ymin,xmax,ymax)': splitted[1] }) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() # end open csv else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels _, info = draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.3 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])] # the main loop times = [] images = [cv2.imread(image_path) for image_path in image_paths] #print(images) start = time.time() # predict the bounding boxes boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) boxes = [[box for box in boxes_image if box.get_score() > obj_thresh] for boxes_image in boxes] print('Elapsed time = {}'.format(time.time() - start)) times.append(time.time() - start) boxes_disc = [disconnect(image, boxes_image, z_thresh = 1.8) for image, boxes_image in zip(images, boxes)] for image_path, image, boxes_image in zip(image_paths, images, boxes_disc): #print(boxes_image[0].score) # draw bounding boxes on the image using labels draw_boxes(image, boxes_image, ["disconnect"], obj_thresh) #plt.figure(figsize = (10,12)) #plt.imshow(I) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) file = open(args.output + '/time.txt','w') file.write('Tiempo promedio:' + str(np.mean(times))) file.close()
def _main_(args): # config_path = args.conf config_path = "config.json" input_path = character_path # output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) # makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if input_path[-4:] == '.mp4': # do detection on a video # video_out = output_path + input_path.split('/')[-1] video_out = "outputVideo/Fullvideo_detection.mp4" video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) background_reader = cv2.VideoCapture(background_path) background_frames = int(background_reader.get( cv2.CAP_PROP_FRAME_COUNT)) # the main loop batch_size = 1 images = [] character_image = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes try: batch_boxes = get_yolo_boxes( infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): character = get_object_img(batch_boxes[i], images[i]) character_image.append(character) # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] except AttributeError: print("not found shape") if show_window and cv2.waitKey(1) == 27: break # esc to quit # Background detection print( "background detection---------------------------------------------" ) batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(background_frames)): _, image = background_reader.read() if (float(i + 1) / background_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (background_frames - 1) and len(images) > 0): # predict the bounding boxes try: batch_boxes = get_yolo_boxes( infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): if (len(batch_boxes[i]) > 0): images_background.append(images[i]) box = batch_boxes[i][0] object_xmax.append(box.xmax) object_ymax.append(box.ymax) object_xmin.append(box.xmin) object_ymin.append(box.ymin) images = [] except AttributeError: print("not found shape") if show_window and cv2.waitKey(1) == 27: break # esc to quit print("make animation----------------------") build_animation(character_image) print("done----------------------") if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release()
def _main_(args): '''실행 명령어: python predict.py -c config.json -i 분석할 파일의 경로(웹캠 실행 시: webcam 입력)''' # 환경변수가 저장되어 있는 파일의 경로 config_path = args.conf # 분석할 파일의 경로 input_path = args.input # 결과물을 저장할 디렉토리 output_path = './output/' # 환경설정 파일을 연다 with open(config_path) as config_buffer: config = json.load(config_buffer) ''' def makedirs(path): try: # 해당 path 의 디렉토리를 생성한다 # 디렉토리가 이미 있으면 -> 에러 발생 os.makedirs(path) except OSError: # 그러나 디렉토리가 이미 있는 경우는 에러를 발생시키지 않도록 처리한다 if not os.path.isdir(path): # 그 외의 오류일 경우 -> 에러를 발생시킨다 raise ''' # 결과물을 저장할 폴더를 생성한다 makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model -- 이미 학습된 모델을 로드한다. 환경설정에서 모델을 변경할 수 있음 ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### '''웹캠 영상을 분석하는 경우''' if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) # the main loop batch_size = 1 # 1 프레임 단위로 분석한다 -- 이걸 10으로 늘이면 화면이 멈춘다. 왜? images = [] while True: ret_val, frame = video_reader.read() # 카메라에서 프레임 이미지를 얻는다 if ret_val == True: images += [frame] if (len(images) == batch_size) or (ret_val == False and len(images) > 0): # 사진에서 오브젝트를 검출한다 batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # 오브젝트 주변에 박스를 그린다 draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # 사진을 화면에 띄운다 cv2.imshow('video with bboxes', images[i]) # 변수 초기화 images = [] if cv2.waitKey(1) == 27: break # esc to quit # 사용자가 esc 키를 누르면 웹캠 화면을 없앤다 cv2.destroyAllWindows() elif input_path[-4:] == '.mp4': # do detection on a video '''mp4 영상을 분석하는 경우''' # 결과물의 이름과 경로를 지정한다 # [-1]: 리스트의 맨 마지막 요소 video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) # 원본 영상의 총 프레임 수, 프레임당 크기를 가져온다 nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) # videoWriter: 비디오를 저장하는 객체 # fourcc: 비디오 코덱을 특정하는 4바이트의 코드이다 video_writer = cv2.VideoWriter( video_out, # 비디오 경로 cv2.VideoWriter_fourcc( *'MPEG'), # 비디오 코덱 -- MPEG 말고 다른걸로 바꾸면 어떻게 됨? 50.0, # 프레임 수 -- 원래 50이었는데, 20으로 줄이면 어떻게 됨? (frame_w, frame_h)) # 프레임 크기 # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False # tqdm = 커맨드 창에 띄울 수 있는 프로그레스 바. A Fast, Extensible Progress Bar for Python and CLI # 영상의 총 프레임 수만큼 루프를 돌면서, 각각의 프레임에서 오브젝트를 검출한다. 검출된 오브젝트 주변에는 박스표시를 한다 # 아래 절차는 웹캠 영상을 처리하는 것과 동일 for i in tqdm(range(nb_frames)): _, frame = video_reader.read() # 영상에서 프레임 이미지를 가져온다 if (float(i + 1) / nb_frames) > start_point / 100.: images += [frame] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images '''사진을 분석하는 경우''' image_paths = [] # input_path 에 file 이름을 넣지 않고 디렉토리까지만 지정했을 경우 if os.path.isdir(input_path): # os.listdir : 이 디렉토리에 있는 전체 파일의 이름을 리스트 형태로 반환한다 for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: # input_path 에 file 이름까지 지정했을 경우 image_paths += [input_path] # 그중에서 jpg, png, jpeg 확장자를 가진 파일만 남긴다 image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + "processed_" + image_path.split('/')[-1], np.uint8(image))
def predict_main_(args): config_path = args.conf input_path = args.input output_path = args.output yolo_config_file_exit('pass') \ if os.path.exists(config_path) \ else yolo_config_file_exit('fail') with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) yolo_create_exit('pass') \ if os.path.isdir(output_path) \ else yolo_create_exit('fail') ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### yolo_model_exit('pass') \ if os.path.isfile(config['train']['saved_weights_name']) \ else yolo_model_exit('fail') infer_model = load_model(config['train']['saved_weights_name'], compile=False) ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) yolo_webcam_exit('pass') \ if video_reader.isOpened() \ else yolo_webcam_exit('fail') # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val: images += [image] if (len(images) == batch_size) or \ (ret_val is False and len(images) > 0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) person, use_boxes = count_person(batch_boxes[i], config['model']['labels'], obj_thresh) average_density = density_estimator(person, use_boxes) show_density(image[i], average_density) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() try: yolo_process_exit('pass') except RuntimeError as e: yolo_process_exit('fail') elif input_path[-4:] == '.mp4': # do detection on a video yolo_video_file_exit('pass') \ if os.path.isfile(input_path) \ else yolo_video_file_exit('fail') video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 show_window = True save_window = True for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or \ (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for j in range(len(images)): # draw bounding boxes on the image using labels if show_window: draw_boxes(images[j], batch_boxes[j], config['model']['labels'], obj_thresh) person, use_boxes = count_person( batch_boxes[j], config['model']['labels'], obj_thresh) average_density = density_estimator(person, use_boxes) show_density(images[j], person, average_density) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[j]) # write result to the output video if save_window: video_writer.write(images[j]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() yolo_release_exit('pass') \ if not video_reader.release() \ else yolo_release_exit('fail') yolo_release_exit('pass') \ if not video_writer.release() \ else yolo_release_exit('fail') try: yolo_process_exit('pass') except RuntimeError as e: yolo_process_exit('fail') else: # do detection on an image or a set of images image_paths = [] yolo_image_file_exit('pass') \ if os.path.isfile(input_path) \ else yolo_image_file_exit('fail') if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: yolo_path_exit('pass') \ if os.path.isfile(image_path) \ else yolo_path_exit('fail') image = cv2.imread(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # print the number of predicted boxes person, use_boxes = count_person(boxes, config['model']['labels'], obj_thresh) average_density = density_estimator(person, use_boxes) show_density(image, person, average_density) cv2.imshow('video with bboxes', image) cv2.waitKey(3000) # write the image with bounding boxes to file yolo_save_exit('pass') \ if cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) \ else yolo_save_exit('fail') try: yolo_process_exit('pass') except RuntimeError as e: yolo_process_exit('fail')
def main(): config_path = "src/config_voc.json" with open(config_path) as config_buffer: config = json.load(config_buffer) net_h, net_w = 128, 128 obj_thresh, nms_thresh = 0.5, 0.45 infer_model = load_model(config['train']['saved_weights_name']) cap = cv2.VideoCapture(2) #cap = cv2.VideoCapture('test.mp4') images = [] cap.set(3, 1280) cap.set(4, 720) cam_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) cam_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) first_slice = math.ceil(cam_width / 3) #720p --> first_slice = 427 second_slice = math.ceil(first_slice * 2) #720p --> second_slice = 854 line_1_1 = (first_slice, 0) line_1_2 = (first_slice, int(cam_height)) line_2_1 = (second_slice, 0) line_2_2 = (second_slice, int(cam_height)) print(first_slice) print(second_slice) print("WIDTH : ", cam_width) print("HEIGHT :", cam_height) """ PC Cam Resolution: width : 640 heigt : 480 """ """ External Cam Resolution width : 1280 height : 720 """ while True: ret, image = cap.read() stime = time.time() if ret: images += [image] batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): images[i], bbox, xmax, ymax = draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) if xmax != None: print("Detection True") print("XMAX :", xmax[0]) print("YMAX :", ymax[0]) print("XMIN : ", bbox[0][0]) print("Mid Point : ", (xmax[0]+(bbox[0][0] + 3)) / 2) mid_point = math.ceil((xmax[0]+(bbox[0][0] + 3)) / 2) mid_point_line_1 = (mid_point, ymax + 10) mid_point_line_2 = (mid_point, ymax - 30) cv2.line(images[i], mid_point_line_1, mid_point_line_2, (0, 255, 0)) print("Detection is True") #0-640 arası degerler degisecek if (mid_point > 0) and (mid_point < first_slice): MESSAGE = "first_pwm".encode() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) print("PWM_1") break elif (mid_point >= first_slice) and (mid_point < second_slice): MESSAGE = "second_pwm".encode() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) print("PWM_2") break elif (mid_point >= second_slice): MESSAGE = "third_pwm".encode() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) print("PWM_3") break print('FPS {:.1f}'.format(1 / (time.time() - stime))) cv2.line(images[i], line_1_1, line_1_2, (255, 0, 0)) cv2.line(images[i], line_2_1, line_2_2, (255, 0, 0)) cv2.namedWindow("Video Stream", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("Video Stream", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.imshow('Video Stream', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output config_path = "config424.json" input_path = "webcam" output_path = "outvideo" input_model = "tftrt_int8_fire424.pb" with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.35, 0.8 ############################### # Load the model ############################### # os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] # infer_model = load_model(config['train']['saved_weights_name']) input_tensor, output_tensors = \ read_pb_return_tensors(tf.get_default_graph(), input_model, ["input_1:0", 'conv_81/BiasAdd:0', 'conv_93/BiasAdd:0', 'conv_105/BiasAdd:0']) # perform inference with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions( per_process_gpu_memory_fraction=0.3))) as sess: ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture("videoplayback (1).mp4") # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val == False: print('ret:', ret_val) video_reader = cv2.VideoCapture("videoplayback (1).mp4") ret_val, image = video_reader.read() if ret_val == True: images += [image] if (len(images) == batch_size) or (ret_val == False and len(images) > 0): prev_time = time.time() batch_boxes = get_yolo_boxes(sess, input_tensor, output_tensors, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) curr_time = time.time() exec_time = curr_time - prev_time result = np.asarray(image) info = "time:" + str(round( 1000 * exec_time, 2)) + " ms, FPS: " + str( round((1000 / (1000 * exec_time)), 1)) cv2.putText(result, text=info, org=(50, 70), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=(255, 0, 0), thickness=2) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] == '.mp4': # do detection on a video video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = True for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes( infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def post(self): if not request.is_json: return None collection = request.get_json() images = [io.imread(item["url"]) for item in collection] with open(self.model_config["model_1"]) as config_buffer: config_1 = json.load(config_buffer) with open(self.model_config["model_2"]) as config_buffer: config_2 = json.load(config_buffer) with open(self.model_config["model_3"]) as config_buffer: config_3 = json.load(config_buffer) labels_1 = config_1["model"]["labels"] labels_2 = config_2["model"]["labels"] labels_3 = config_3["model"]["labels"] boxes_p_1 = get_yolo_boxes( self.model["model_1"], images, self.net_h, self.net_w, config_1["model"]["anchors"], self.obj_thresh, self.nms_thresh, ) boxes_p_2 = get_yolo_boxes( self.model["model_2"], images, self.net_h, self.net_w, config_2["model"]["anchors"], self.obj_thresh, self.nms_thresh, ) boxes_p_3 = get_yolo_boxes( self.model["model_3"], images, self.net_h, self.net_w, config_3["model"]["anchors"], self.obj_thresh, self.nms_thresh, ) boxes_p_1 = [[ box for box in boxes_image if box.get_score() > self.obj_thresh ] for boxes_image in boxes_p_1] boxes_p_2 = [[ box for box in boxes_image if box.get_score() > self.obj_thresh ] for boxes_image in boxes_p_2] boxes_p_3 = [[ box for box in boxes_image if box.get_score() > self.obj_thresh ] for boxes_image in boxes_p_3] boxes_p_3 = [ disconnected(image, boxes_image) for image, boxes_image in zip(images, boxes_p_3) ] for index, item in enumerate(collection): item["objects"] = list() for boxes in boxes_p_1[index]: item["objects"].append({ "class": labels_1[boxes.label], "label": "Soiling Fault", "score": str(boxes.get_score()), "xmax": boxes.xmax, "xmin": boxes.xmin, "ymax": boxes.ymax, "ymin": boxes.ymin, }) for boxes in boxes_p_2[index]: item["objects"].append({ "class": labels_2[boxes.label], "score": str(boxes.get_score()), "label": "Diode Fault", "xmax": boxes.xmax, "xmin": boxes.xmin, "ymax": boxes.ymax, "ymin": boxes.ymin, }) for boxes in boxes_p_3[index]: item["objects"].append({ "class": labels_3[boxes.label], "score": str(boxes.classes[0]), "label": "Panel Disconnect", "xmax": boxes.xmax, "xmin": boxes.xmin, "ymax": boxes.ymax, "ymin": boxes.ymin, }) return collection
def validate(config, weights_path: str, dataset_file: str, output_dir: str, save_output: bool = False): with open(dataset_file, 'r') as f: lines = f.readlines() dst = Path(output_dir) if dst.is_dir(): shutil.rmtree(dst) dst.mkdir() gt_dir = dst.joinpath("groundtruth") gt_dir.mkdir() detect_dir = dst.joinpath("detection_results") detect_dir.mkdir() if save_output: bbox_dir = dst.joinpath("bbox_images") bbox_dir.mkdir() net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.05, 0.45 #os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] model = load_model(weights_path) for l in tqdm(lines): filepath, annots = parse_from_yolo(line=l) assert filepath.is_file() # handle groundtruth parse_and_store_groundtruth(target_dir=gt_dir, image_path=filepath, annotations=annots) # handle inference img = cv2.imread(str(filepath)) det_boxes = get_yolo_boxes(model, [img], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] bboxes = [ Detection(xmin=b.xmin, ymin=b.ymin, xmax=b.xmax, ymax=b.ymax, class_name=UFO_CLASSES[b.get_label()], score=b.get_score()) for b in det_boxes if b.get_score() > 0.0 ] sample = Sample(filepath.name, bboxes) sample.save_in_folder(detect_dir) if save_output: img_out = os.path.join(str(bbox_dir), filepath.name) draw_boxes(img, det_boxes, config['model']['labels'], obj_thresh) cv2.imwrite(img_out, np.uint8(img))
def _main_(args): # ----- Initialization ----------- input_path = args.input output_path = args.output dataset_name = args.dataset trial_version = args.trial_version net_h, net_w = 416, 416 #obj_thresh, nms_thresh = 0.7, 0.7 obj_thresh, nms_thresh = 0.7, 0.7 os.environ['CUDA_VISIBLE_DEVICES'] = "0,1" if trial_version != "WITH_TRAIN" and trial_version != "WITH_TEST": print("Wrong Trial version parameter") return #-START---- Model Loading------ if dataset_name == "parking": try: infer_model = load_model('W/' + trial_version + '/parking_detection_' + trial_version + '.h5') anchors = [ 9, 7, 10, 4, 16, 7, 17, 15, 24, 10, 30, 14, 57, 41, 71, 26, 99, 62 ] print("Parking detection weights loaded") except: print("Could not load parking detection weights") return elif dataset_name == "cctv": try: infer_model = load_model('W/' + trial_version + '/cctv_detection_' + trial_version + '.h5') #anchors = [4,2, 8,7, 9,4, 13,5, 14,11, 19,16, 19,7, 24,10, 29,13] anchors = [ 8, 4, 9, 8, 13, 5, 15, 13, 17, 7, 20, 17, 22, 8, 26, 10, 29, 14 ] print("CCTV detection weights loaded") except: print("Could not load CCTV detection weights") return else: print("Please specify dataset") return model = get_Model(training=False) if dataset_name == "parking": try: model.load_weights('W/' + trial_version + '/parking_recognition_' + trial_version + '.hdf5') print("Parking recognition weights loaded") except: raise Exception("No parking recognition weight file!") return elif dataset_name == "cctv": try: model.load_weights('W/' + trial_version + '/cctv_recognition_' + trial_version + '.hdf5') print("CCTV recognition weights loaded") except: raise Exception("No cctv recognition weight file!") return #-END---- Model Loading------ image_paths = [] for fold in os.listdir(input_path): for fname in os.listdir(os.path.join(input_path, fold)): if fname.endswith('.jpg') or fname.endswith('.png'): image_paths.append(os.path.join(input_path, fold, fname)) image_paths = sorted(image_paths) print("Number of images to test: " + str(len(image_paths))) # ----- Initialization ----------- # -START----- Main loop ------------- total_time = 0 for i, image_path in enumerate(image_paths): #if i == 10: # break print('[{}] / [{}]'.format(i + 1, len(image_paths))) image = cv2.imread(image_path) tic = time.time() boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0] if len(boxes) != False: box = boxes[0] else: print("Box not found! " + image_path) continue if box.ymin < 0 or box.ymax < 0 or box.xmin < 0 or box.xmax < 0: print("Negative box found. Cannot proceed! " + image_path) continue label = get_label(image[box.ymin:box.ymax, box.xmin:box.xmax, 0], model) toc = time.time() total_time += toc - tic write_result(output_path, dataset_name, image_path, label, box) # -END----- Main loop ------------- print('Avg. PT: {} ms.'.format(total_time / len(image_paths) * 1000))
def _main_(args): '''실행 명령어: python predict.py -c config.json -i 분석할 파일의 경로(웹캠 실행 시: webcam 입력)' ex) python predict.py -c config.json -i ./screenshot_output/ (이미지의 경우, 폴더 단위로도 실행 가능) ''' # 환경변수가 저장되어 있는 파일의 경로 config_path = args.conf # 분석할 파일의 경로 input_path = args.input # 결과물을 저장할 디렉토리 output_path = './predict_output/' # 환경설정 파일을 연다 with open(config_path) as config_buffer: config = json.load(config_buffer) ''' def makedirs(path): try: # 해당 path 의 디렉토리를 생성한다 # 디렉토리가 이미 있으면 -> 에러 발생 os.makedirs(path) except OSError: # 그러나 디렉토리가 이미 있는 경우는 에러를 발생시키지 않도록 처리한다 if not os.path.isdir(path): # 그 외의 오류일 경우 -> 에러를 발생시킨다 raise ''' # 결과물을 저장할 폴더를 생성한다 makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster # define the probability threshold for detected objects -- 60% 이상의 확실성을 가져야 해당 오브젝트로 인정 # nms: non-maximal boxes -- 여러 박스가 중첩되었을 경우, 50% 이상의 확실성을 가져야 중첩 허용 obj_thresh, nms_thresh = 0.6, 0.5 ############################### # Load the model -- 이미 학습된 모델을 로드한다. 환경설정에서 모델을 변경할 수 있음 ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### # do detection on an image or a set of images '''사진을 분석하는 경우''' count = 0 while True: image_paths = [] # input_path 에 file 이름을 넣지 않고 디렉토리까지만 지정했을 경우 if os.path.isdir(input_path): # os.listdir : 이 디렉토리에 있는 전체 파일의 이름을 리스트 형태로 반환한다 for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] # 그중에서 jpg, png, jpeg 확장자를 가진 파일만 남긴다 image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] # the main loop for image_path in image_paths: image = cv2.imread(image_path) # predict the bounding boxes -- 검출된 object 박스를 list 형태로 반환한다 boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] if boxes != "error": print("image path: " + image_path + " /// number of boxes: " + str(len(boxes))) labels = config['model']['labels'] isDog = False # 이 사진에서 검출한 박스를 하나씩 검사한다. 조건에 맞는지 확인 for box in boxes: for i in range(len(labels)): if box.classes[i] > obj_thresh and "dog" == labels[ i]: # 60% 이상의 확률로 개인지 확인 print("It's a dog!! " + str(round(box.get_score() * 100, 2)) + '%') isDog = True filename = image_path.split('/')[-1] # draw bounding boxes on the image using labels if isDog: # 이 사물이 개라면 count += 1 print('count = ' + str(count)) # 사진 위의 개 주변에 박스를 그린다 -- 굳이 안 그려도 됨. 앱 사용자한테 보여줄 사진이기 때문에, 박스처리하지 않음 draw_boxes_for_dogs(image, boxes, config['model']['labels'], obj_thresh) # output 폴더에 가공된 사진을 저장한다 -- 가공하지 않았음 # write the image with bounding boxes to file cv2.imwrite(output_path + "detected__" + filename, np.uint8(image)) # 원본 사진을 originals 폴더로 옮긴다 shutil.move(image_path, "./originals/" + filename) else: print("It's not a dog") # 원본 사진을 originals 폴더로 옮긴다 shutil.move(image_path, "./originals/" + filename) else: print("box value is null") time.sleep(3)
def predict(self, payload): # download image img_url = payload["url"] image = preprocess_utils.get_url_image(img_url) # detect the bounding boxes boxes = utils.get_yolo_boxes( self.yolov3_model, image, self.net_h, self.net_w, self.anchors, self.obj_thresh, self.nms_thresh, len(self.labels), tensorflow_model=False, ) # purge bounding boxes with a low confidence score aux = [] for b in boxes: label = -1 for i in range(len(b.classes)): if b.classes[i] > self.box_confidence_score: label = i if label >= 0: aux.append(b) boxes = aux del aux # if bounding boxes have been detected dec_words = [] if len(boxes) > 0: # create set of images of the detected license plates lps = [] for b in boxes: lp = image[b.ymin:b.ymax, b.xmin:b.xmax] lps.append(lp) # run batch inference try: prediction_groups = self.recognition_model_pipeline.recognize( lps) except ValueError: # exception can occur when the images are too small prediction_groups = [] # process pipeline output image_list = [] for img_predictions in prediction_groups: boxes_per_image = [] for predictions in img_predictions: boxes_per_image.append( [predictions[0], predictions[1].tolist()]) image_list.append(boxes_per_image) # reorder text within detected LPs based on horizontal position dec_lps = preprocess_utils.reorder_recognized_words(image_list) for dec_lp in dec_lps: dec_words.append([word[0] for word in dec_lp]) # if there are no recognized LPs, then don't draw them if len(dec_words) == 0: dec_words = [[] for i in range(len(boxes))] # draw predictions as overlays on the source image draw_image = bbox_utils.draw_boxes( image, boxes, overlay_text=dec_words, labels=["LP"], obj_thresh=self.box_confidence_score, ) # image represented in bytes byte_im = preprocess_utils.image_to_jpeg_bytes(draw_image) # encode image image_enc = base64.b64encode(byte_im).decode("utf-8") # image with draw boxes overlayed return image_enc
def _main_(args): app = App() config_path = args.conf num_cam = int(args.count) with open(config_path) as config_buffer: config = json.load(config_buffer) # makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Set up the Tracker ############################### # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metrics = [] trackers = [] for i in range(num_cam): metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) trackers.append(tracker) ############################### # Predict bounding boxes ############################### # if 'webcam' in input_path: # do detection on the first webcam video_readers = [] for i in range(num_cam): video_reader = cv2.VideoCapture(i) video_readers.append(video_reader) # the main loop batch_size = num_cam images = [] values = [] messages = [] while True: for i in range(num_cam): ret_val, image = video_readers[i].read() if ret_val == True: images += [image] if (len(images) == batch_size) or (ret_val == False and len(images) > 0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): boxs = [[box1.xmin, box1.ymin, box1.xmax - box1.xmin, box1.ymax - box1.ymin] for box1 in batch_boxes[i]] features = encoder(images[i], boxs) message = "" # print(features) # score to 1.0 here). detections = [] for j in range(len(boxs)): label = batch_boxes[i][j].label detections.append(Detection(boxs[j], batch_boxes[i][j].c, features[j], label)) # Call the tracker trackers[i].predict() trackers[i].update(detections) n_without_helmet = 0 n_with_helmet = 0 for track in trackers[i].tracks: message+=track.message track.message="" if not track.is_confirmed() or track.time_since_update > 1: continue if track.label == 2: n_without_helmet += 1 if track.label == 1: n_with_helmet += 1 bbox = track.to_tlbr() draw_box_with_id(images[i], bbox, track.track_id, track.label, config['model']['labels']) messages.append(message) values.append(n_with_helmet) values.append(n_without_helmet) app.update(images, values,messages) values = [] images = [] messages = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if input_path[-4:] in ['.avi', '.mp4']: # do detection on a video video_out = output_path + '/traj_' + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) video_fps = int(video_reader.get(cv2.CAP_PROP_FPS)) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False balls_coords = [] balls_frms = [] labels = config['model']['labels'] for i in tqdm(range(nb_frames)): _, image = video_reader.read() # stack all frames balls_frms.append(image) if (float(i + 1) / nb_frames) > start_point / 100.: images += [image] if (i % batch_size == 0) or (i == (nb_frames - 1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for k in range(len(images)): # stack all ball coords coords = get_coord(images[k], batch_boxes[k], labels, obj_thresh, i) # i: frame no balls_coords = balls_coords + coords # show the video after above operations if show_window: cv2.imshow('video with bboxes', images[k]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit # track trajectory traj_track(balls_coords, balls_frms, video_out, video_fps, frame_w, frame_h, quiet=False) if show_window: cv2.destroyAllWindows() video_reader.release() else: print("Trajectory tracking can only be done on videos. ")
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 light color (specified in styx_msgs/TrafficLight) """ #TODO implement light color prediction ''' uint8 UNKNOWN=4 uint8 GREEN=2 uint8 YELLOW=1 uint8 RED=0 ''' rospy.logwarn("[tl_det] Before get_yolo_boxes") tStart = time.time() # predict the bounding boxes boxes = get_yolo_boxes(self.infer_model, [image], self.net_h, self.net_w, self.model_anchors, self.obj_thresh, self.nms_thresh)[0] tEnd = time.time() rospy.logwarn("[tl_det] After get_yolo_boxes") rospy.logwarn("[tl_det] Box detecting num {}".format(len(boxes))) rospy.logwarn("[tl_det]It cost {} sec".format(tEnd - tStart)) #debug state_prob = -99 state = -99 state_idx = -99 for w in range(len(boxes)): rospy.logwarn("[tl_det] Box num {}".format(w)) rospy.logwarn("[tl_det] boxes {}, {}, {}".format( boxes[w].classes, boxes[w].score, boxes[w].label)) state_idx = 0 for class_prob in boxes[w].classes: rospy.logwarn("[tl_det] class_prob {}, state_prob {}".format( class_prob, state_prob)) if class_prob > state_prob and class_prob != 0.0: state_prob = class_prob state = state_idx rospy.logwarn("[tl_det] state {}".format(state)) state_idx = state_idx + 1 if state == -99: state = TrafficLight.UNKNOWN #draw bounding boxes on the image using labels #draw_boxes(image, boxes, self.model_labels, self.obj_thresh) # write the image with bounding boxes to file #cv2.imwrite("output_"+str(tEnd)+'.png', np.uint8(image)) #rospy.logwarn("[traffic classifier] state: {}".format(state)) return state
def _main_(input_path_x, infer_model, infer_model_2, infer_model_3, infer_model_4, infer_model_5): ''' description : main function for predict the anchors on the image using the infer models and anchors of yolo v3 for this labels :param input_path_x:the input path of the image , text (str) :param infer_model: infer model 1, .h5 :param infer_model_2:infer model 2, .h5 :param infer_model_3:infer model 3, .h5 :param infer_model_4:infer model 4, .h5 :return:void ''' input_path = input_path_x # get the input path from the args ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster 416, 416 obj_thresh, nms_thresh = 0.90, .3 #obj_thresh = .5 mean if less than 50% sure ignore it, nms_thresh =.3 means if 30 % IOU for 2 boxes; merge them #===========================================================================# ############################### # Predict bounding boxes ############################### #============================================ for video ===========================================================# #==================================================================================================================# #============================================ for image ===========================================================# if True: # do detection on an image or a set of images image_paths = [] #==================================== image path ==============================================================# if os.path.isdir(input_path): for inp_file in os.listdir(input_path): # get the input image image_paths += [input_path + inp_file] # get the input image else: image_paths += [input_path] # relative address image_paths = [ inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG']) ] #image format #==============================================================================================================# ############################# anchors intialiazation ############################################ model_1_anchors = [ 13, 17, 17, 14, 17, 19, 22, 28, 27, 12, 27, 18, 28, 37, 35, 52, 48, 74 ] # model_2_anchors = [ 4, 13, 5, 14, 6, 12, 6, 16, 20, 25, 22, 21, 27, 15, 31, 25, 35, 17 ] # model_3_anchors = [ 55, 69, 75, 234, 133, 240, 136, 129, 142, 363, 203, 290, 228, 184, 285, 359, 341, 260 ] # model_4_anchors = [ 55, 69, 75, 234, 133, 240, 136, 129, 142, 363, 203, 290, 228, 184, 285, 359, 341, 260 ] # ################################################################################################# ################################## labels intialiazation ###################################### model_1_label = ["loop back arrow", "state", "state condition"] # model_2_label = ["/"] # model_3_label = ["arrow head"] # model_4_label = ["curved arrow"] # ############################################################################################### ############################################ the main loop ################################################################ for image_path in image_paths: ########################################## image preprocess ############################# image = cv2.imread( image_path ) # load the image path # #image=image_preprocess.resize_image_less_than_600_800(image) # object_file.original_image = cv2.imread( image_path) # set the original image # image = image_preprocess.start_pre( image, condition=object_file.hand_written) # # line_detector.line_detector(image) # ######################################################################################### ########################################## predict the bounding boxes ################################################## boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, model_1_anchors, obj_thresh, nms_thresh)[0] # boxes_2 = get_yolo_boxes(infer_model_2, [image], net_h, net_w, model_2_anchors, obj_thresh, nms_thresh)[0] # boxes_3 = get_yolo_boxes(infer_model_3, [image], net_h, net_w, model_3_anchors, obj_thresh, nms_thresh)[0] # boxes_4 = get_yolo_boxes( infer_model_4, [image], net_h, net_w, model_4_anchors, obj_thresh, nms_thresh )[0] # # ######################################################################################################################## # ######################## draw boxes after predict ######## config_boxes(image, boxes, model_1_label, obj_thresh) # config_boxes(image, boxes_2, model_2_label, obj_thresh) # config_boxes(image, boxes_3, model_3_label, obj_thresh) # config_boxes(image, boxes_4, model_4_label, obj_thresh) # ########################################################## ######################### set the exported path ###################### set_inputpath_and_image(image, image_path) #
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val == True: images += [image] if (len(images)==batch_size) or (ret_val==False and len(images)>0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] == '.mp4': # do detection on a video video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i+1)/nb_frames) > start_point/100.: images += [image] if (i%batch_size == 0) or (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
def _main_(): print(1) # path to image or video input_path = "./video.mp4" output_path = "./" print(2) labels = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"] anchors = [55,69, 75,234, 133,240, 136,129, 142,363, 203,290, 228,184, 285,359, 341,260] print(3) makedirs(output_path) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 print(4) ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = "0,1" infer_model = load_model("model.h5") ############################### # Predict bounding boxes ############################### if 'webcam' in input_path: # do detection on the first webcam video_reader = cv2.VideoCapture(0) print("it's a webcam") # the main loop batch_size = 1 images = [] while True: ret_val, image = video_reader.read() if ret_val == True: images += [image] if (len(images)==batch_size) or (ret_val==False and len(images)>0): batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, anchors, obj_thresh, nms_thresh) for i in range(len(images)): draw_boxes(images[i], batch_boxes[i], labels, obj_thresh) cv2.imshow('video with bboxes', images[i]) images = [] if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() elif input_path[-4:] == '.mp4': # do detection on a video print("it's a video") # [-1]: 뒤에서 첫번째 요소 video_out = output_path + input_path.split('/')[-1] video_reader = cv2.VideoCapture(input_path) nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT)) frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)) video_writer = cv2.VideoWriter(video_out, cv2.VideoWriter_fourcc(*'MPEG'), 50.0, (frame_w, frame_h)) # the main loop batch_size = 1 images = [] start_point = 0 #% show_window = False for i in tqdm(range(nb_frames)): _, image = video_reader.read() if (float(i+1)/nb_frames) > start_point/100.: images += [image] if (i%batch_size == 0) or (i == (nb_frames-1) and len(images) > 0): # predict the bounding boxes batch_boxes = get_yolo_boxes(infer_model, images, net_h, net_w, anchors, obj_thresh, nms_thresh) for i in range(len(images)): # draw bounding boxes on the image using labels draw_boxes(images[i], batch_boxes[i], labels, obj_thresh) # show the video with detection bounding boxes if show_window: cv2.imshow('video with bboxes', images[i]) # write result to the output video video_writer.write(images[i]) images = [] if show_window and cv2.waitKey(1) == 27: break # esc to quit if show_window: cv2.destroyAllWindows() video_reader.release() video_writer.release() else: # do detection on an image or a set of images image_paths = [] print("it's an image") if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [input_path + inp_file] else: image_paths += [input_path] image_paths = [inp_file for inp_file in image_paths if (inp_file[-4:] in ['.jpg', '.png', 'JPEG'])] # the main loop for image_path in image_paths: image = cv2.imread(image_path) print(image_path) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, labels, obj_thresh) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) # if __name__ == '__main__': # argparser = argparse.ArgumentParser(description='Predict with a trained yolo model') # argparser.add_argument('-c', '--conf', help='path to configuration file') # argparser.add_argument('-i', '--input', help='path to an image, a directory of images, a video, or webcam') # argparser.add_argument('-o', '--output', default='output/', help='path to output directory') _main_()