def do_image_file(yolov3, image_path, out_dir=''): # set some parameters net_h, net_w = 416, 416 obj_thresh, nms_thresh = 0.5, 0.45 anchors = [[116,90, 156,198, 373,326], [30,61, 62,45, 59,119], [10,13, 16,30, 33,23]] # preprocess the image image = cv2.imread(image_path) image_h, image_w, _ = image.shape new_image = preprocess_input(image, net_h, net_w) # run the prediction yolos = yolov3.predict(new_image) boxes = [] for i in range(len(yolos)): # decode the output of the network boxes += decode_netout(yolos[i][0], anchors[i], obj_thresh, net_h, net_w) # correct the sizes of the bounding boxes correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w) # suppress non-maximal boxes do_nms(boxes, nms_thresh) # draw bounding boxes on the image using labels draw_boxes(image, boxes, labels, obj_thresh) # write the image with bounding boxes to file if out_dir: out_path = out_dir + '/' + re.sub(r'.*/', '', image_path) print(out_path) cv2.imwrite(out_path, image.astype('uint8'))
def predict(self): net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 # For testing input_path = 'raccoon-188.jpg' output_path = 'C:\\Users\\colsson\\uploads\\predictions\\' anchors = [55, 69, 75, 234, 133, 240, 136, 129, 142, 363, 203, 290, 228, 184, 285, 359, 341, 260] # Required because of a bug in Keras when using tensorflow graph cross threads 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'])] for image_path in image_paths: image = cv2.imread(image_path) # predict the bounding boxes boxes = \ get_yolo_boxes(self.model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, ['raccoon'], obj_thresh, quiet=True) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) prediction = {'result': "hej"} return prediction
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.35, 0.35 ############################### # 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 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] filename = output_path + image_path.split('/')[-1] + ".pkl" outfile = open(filename, 'wb') pickle.dump(boxes, outfile) outfile.close() # 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_(args): config_path = args.conf input_path = args.input weights_path = args.weights engine_path = args.engine render_mode = args.gui trt_engine = TRTengine(isCppInf=args.cpp) if engine_path is None: with open(config_path) as config_buffer: config = json.load(config_buffer) trt_engine.import_from_weights(config, weights_path) trt_engine.save_engine() return else: assert trt_engine.load_engine(engine_path), 'Failed to load engine' if not input_path: return image_paths = [] if os.path.isdir(input_path): for inp_file in os.listdir(input_path): image_paths += [os.path.join(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', '.ppm']) ] processing_count = 0 sum_time = 0 for image_path in tqdm(image_paths): image = cv2.imread(image_path) start_time = time.time() boxes = trt_engine.predict_boxes(image) sum_time += time.time() - start_time processing_count += 1 if render_mode: draw_boxes(image, boxes, trt_engine.get_labels(), 0.5) cv2.imshow('result', np.uint8(image)) if cv2.waitKey(0) == 27: break # esc to quit fps = processing_count / sum_time print('Result: {}'.format(fps))
def predict(self, pic): ret = [] with open(self.config_path) as config_buffer: config = json.load(config_buffer) makedirs(self.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.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) image_paths = [] if os.path.isdir(self.input_path): for inp_file in os.listdir(self.input_path): image_paths += [self.input_path + inp_file] else: image_paths += [self.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(pic) print(pic) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] for box in boxes: for i in range(len(config['model']['labels'])): if box.classes[i] > obj_thresh: ret.append([config['model']['labels'][i], box.classes[i]]) print(config['model']['labels'][i], " ", box.classes[i]) # 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 print("file save to {}".format(self.output_path + pic.split('/')[-1])) cv2.imwrite(self.output_path + pic.split('/')[-1], np.uint8(image)) return ret
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) anchors = [] with open('anchors.json') as anchors_str: anchors = json.load(anchors_str) 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.4, 0.45 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']) ] infer_model = load_model(config['train']['saved_weights_name']) # 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, 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 yolo_model_predict(img_path, model): img = image.load_img(img_path) img = np.array(img) print(img.shape) boxes = get_yolo_boxes(model,[img],net_h,net_w,anchors,obj_thresh,nms_thresh)[0] output = draw_boxes(image,boxes,labels,obj_thresh) print(output) return output
def predict_file(self, filepath, output_path): net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 # For testing input_path = filepath print("The filepath is: " + str(filepath)) # output_path = 'C:\\Users\\colsson\\uploads\\predictions\\' # output_path = 'C:\\Users\\colsson\\PycharmProjects\\keras_flask\\app\\static\\' anchors = [55, 69, 75, 234, 133, 240, 136, 129, 142, 363, 203, 290, 228, 184, 285, 359, 341, 260] # Required because of a bug in Keras when using tensorflow graph cross threads 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'])] for image_path in image_paths: image = cv2.imread(image_path) # predict the bounding boxes boxes = \ get_yolo_boxes(self.model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0] # draw bounding boxes on the image using labels draw_boxes(image, boxes, ['raccoon'], obj_thresh, quiet=False) # write the image with bounding boxes to file #cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) # result_filepath = output_path + image_path.split('\\')[-1] #temp_fp = os.path.join(output_path, image_path.split('\\')) # cv2.imwrite(output_path + '/' + image_path.split('\\')[-1], np.uint8(image)) # For windows # result_filepath = image_path.split('\\')[-1] # For windows cv2.imwrite(output_path + '/' + image_path.split('/')[-1], np.uint8(image)) # For Linux result_filepath = image_path.split('/')[-1] # For Linux #print(str(output_path + image_path.split('\\')[-1])) prediction = {'result': "hej filepath!"} return result_filepath
def rankGame(inputPath, draw_output=False): with open(configPath) as config_buffer: config = json.load(config_buffer) makedirs(outputPath) ############################### # 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( os.path.join(curDir, config['train']['saved_weights_name'])) image = cv2.imread(inputPath) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0] # Sort boxes by x position sort in ascending player order sortedBoxes = sorted(boxes, key=lambda box: box.xmin) # draw bounding boxes on the image using labels if draw_output: draw_boxes(image, boxes, config['model']['labels'], obj_thresh) # write the image with bounding boxes to file # cv2.imwrite(outputPath.split('/')[-1], np.uint8(image)) cv2.imshow('Annotated Image', image) cv2.waitKey(0) # Assign ranks to the boxes based on their labels rankedBoxes = rankBoxes(image, boxes, config['model']['labels'], obj_thresh) sortedRanks = [box.playerRank for box in rankedBoxes] return sortedRanks # Returns a list of the integer ranks in order of ascending player number
def predict(image): with open(app.config['CONFIG_FILE']) as config_buffer: config = json.load(config_buffer) ############################### # 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 ############################### # 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) # output the image with bounding boxes to file img = Image.fromarray(image) # create file-object in memory file_object = io.BytesIO() # write PNG in file-object img.save(file_object, 'PNG') # move to beginning of file so `send_file()` it will read from start file_object.seek(0) return send_file(file_object, mimetype='image/PNG', as_attachment=True, attachment_filename='result.png')
def _main_(config_path, input_path, output_path): 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(settings.BASE_DIR + config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### # 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) # 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 image_new, label_result = 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_new)) return label_result, output_path + image_path.split('/')[-1]
def getbboxarr(img): ############################### # Predict bounding boxes and get the position ############################### image = cv2.imread(img) # 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 _, arr = draw_boxes(image, boxes, config['model']['labels'], obj_thresh) return arr
def _main_(): config_path = "config_voc.json" with open(config_path) as config_buffer: config = json.load(config_buffer) net_h, net_w = 64, 64 obj_thresh, nms_thresh = 0.5, 0.45 #os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] #print(os.environ) infer_model = load_model(config['train']['saved_weights_name']) cap = cv2.VideoCapture(0) images = [] 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 = draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) #cv2.imshow('video with bboxes', images[i]) try: print(bbox) print("detection var") except: print("detection yok") pass print('FPS {:.1f}'.format(1 / (time.time() - stime))) 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 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))
nb_class = len(boxes[0].classes) else: return for c in range(nb_class): sorted_indices = np.argsort([-box.classes[c] for box in boxes]) for i in range(len(sorted_indices)): index_i = sorted_indices[i] if boxes[index_i].classes[c] == 0: continue for j in range(i+1, len(sorted_indices)): index_j = sorted_indices[j] if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_thresh: boxes[index_j].classes[c] = 0 boxes[index_i].classes[c] = 0 do_nms(new, .35) img = plt.imread("../new.tif")[:, :, [0,1,2]] img = np.array(img).copy() out = draw_boxes(img, new, ['residential', 'other'], .35) imsave('../diff.png', out) filename = '../diff.pkl' outfile = open(filename, 'wb') pickle.dump(new, outfile) outfile.close()
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 ############################### 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_(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 ############################### 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 with open(os.path.join(output_path, "ans.xml"), "w") as file: file.write("<annotations>\n") 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) filename = os.path.basename(image_path) write_boxes(file, filename, 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)) file.write("</annotations>\n")
def main(img_url_src, yolov3_endpoint, crnn_endpoint, output): # get the image in bytes representation image = preprocess_utils.get_url_image(img_url_src) image_bytes = preprocess_utils.image_to_jpeg_bytes(image) # encode image image_enc = base64.b64encode(image_bytes).decode("utf-8") image_dump = json.dumps({"img": image_enc}) # make yolov3 api request resp = requests.post(yolov3_endpoint, data=image_dump, headers={"content-type": "application/json"}) # parse response boxes_raw = resp.json()["boxes"] boxes = [] for b in boxes_raw: box = bbox_utils.BoundBox(*b) boxes.append(box) # purge bounding boxes with a low confidence score confidence_score = 0.8 aux = [] for b in boxes: label = -1 for i in range(len(b.classes)): if b.classes[i] > confidence_score: label = i if label >= 0: aux.append(b) boxes = aux del aux 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] jpeg = preprocess_utils.image_to_jpeg_nparray(lp) lps.append(jpeg) # encode the cropped license plates lps = pickle.dumps(lps, protocol=0) lps_enc = base64.b64encode(lps).decode("utf-8") lps_dump = json.dumps({"imgs": lps_enc}) # make crnn api request resp = requests.post(crnn_endpoint, data=lps_dump, headers={"content-type": "application/json"}) # parse the response dec_lps = resp.json()["license-plates"] dec_lps = preprocess_utils.reorder_recognized_words(dec_lps) for dec_lp in dec_lps: dec_words.append([word[0] for word in dec_lp]) 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=confidence_score) # and save it to disk cv2.imwrite(output, draw_image)
#infor_model os.chdir(Models_path) infer_model = load_model('B116XTN4B_20210419.h5') #infer_model=load_model(SheetModel_Modelname_dic[GET_KPCData(input_path_03_3[k])[1].split('_')[0]]) ##---1.Preimage CV ---## preCV(down_img, input_img) #照片先抓到down_img後,預處理照片儲存志input_img #read img for img_p in os.listdir(input_img): ##---2.yolo-model---## os.chdir(input_img) image = cv2.imread(img_p) #讀取一般bgr照片 # 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) #從BBOX取出BOXE_SIZE box_value = draw_boxes(image, boxes, labels, obj_thresh) if len(box_value[1]) > 0: #計算數值 datasave(output_csv, Cau_Value(img_p, box_value, float(9.67), 0), 'ASM_sealwidth_' + day) #儲存照片 output_imgname = output_img + img_p print(output_imgname) cv2.imwrite(output_imgname, np.uint8(image)) else: datasave(output_csv, Cau_Value(img_p, box_value, float(9.67), 1), 'ASM_sealwidth_' + day) os.chdir(currentroot)
def _main_(args): config_path = args.conf input_path = args.input output_path = args.output predict_path = args.predict if_show = args.show with open(config_path) as config_buffer: config = json.load(config_buffer) makedirs(output_path) makedirs(predict_path) ############################### # Set some parameter ############################### net_h, net_w = 512, 512 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.5, 0.45 #0.5, 0.45 ############################### # Load the model ############################### os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus'] infer_model = load_model(config['train']['saved_weights_name']) # infer_model = load_model('backend') print('load model') ############################### # Predict bounding boxes ############################### # 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 ['.mhd']) ] # the main loop for image_path in image_paths: print(image_path) slice_i = 1 while slice_i < 1000: slice_i += 1 print('slice:' + str(slice_i)) image = raw_reader(image_path, slice_i) if image is None: break if if_show: image_ini = image[..., 2] max_pix = np.max(image_ini) min_pix = np.min(image_ini) # print(max_pix, min_pix) image_ini, _ = img_windowing(image_ini, max_pix, min_pix) # cv2.imshow('image_ini', image_ini) # cv2.waitKey() # image_ini = np.uint8(np.float64((image_ini + 1000) / 1800) * 255) (imagename, extension) = os.path.splitext(image_path.split('/')[-1]) # predict the bounding boxes boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh, imagename)[0] line = '' textname = predict_path + imagename + '_' + str(slice_i) + '.txt' if if_show and len(boxes) > 0: # draw bounding boxes on the image using labels # print('boxes:' + str(len(boxes))) draw_boxes( image_ini, boxes, sorted(config['model']['labels']), obj_thresh, output_path + imagename + '_' + str(slice_i) + '.jpg') # write the image with bounding boxes to file # cv2.imwrite(output_path + imagename + '_' + str(slice_i) + '.jpg', np.uint8(image_ini)) newline = get_box_info(line, boxes, sorted(config['model']['labels']), obj_thresh) with open(textname, 'w') as f: f.write(newline)
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' or input_path[ -4:] == '.AVI': # do detection on a video video_out = output_path + input_path.split('/')[-1].replace( '.AVI', '.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(*'MP4V'), #(*'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)): bbox0 = [batch_boxes[i][0]] if len( batch_boxes[i]) else [] # draw bounding boxes on the image using labels #draw_boxes(images[i], batch_boxes[i], config['model']['labels'], obj_thresh) draw_boxes(images[i], bbox0, config['model']['labels'], obj_thresh) # take only 1st bbox # 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', '.JPG']) ] # the main loop for image_path in image_paths: #image_path = '/dataset/RZSS_images/1_animal_empty_r/animal/6.JPG' 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] if len(boxes) > 0: pboxes = np.array( [[box.xmin, box.ymin, box.xmax, box.ymax, box.get_score()] for box in boxes]) print(pboxes) #print(boxes[0].xmin, boxes[0].ymin, boxes[0].xmax, boxes[0].ymax, boxes[0].c, boxes[0].classes ); #for k in range(len(boxes)): print(boxes[k].__dict__); import sys; sys.exit(0) # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) #import sys; sys.exit(0) # write the image with bounding boxes to file cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image)) print('OUTPUT SAVED AS ' + output_path + image_path.split('/')[-1])
def predict(self,input_path, output_path): config = self.config infer_model = self.infer_model net_h = self.net_h net_w = self.net_w obj_thresh = self.obj_thresh, nms_thresh = self.nms_thresh print("Net h: ", net_h) ############################### # 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(self.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) #cv2.imshow("test",image) print(image.shape) # 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) #cv2.imshow("Test",image) #cv2.waitKey() # write the image with bounding boxes to file out_path_val = output_path + image_path.split(os.path.sep)[-1] #+output_path print("Out Path: ", out_path_val) cv2.imwrite(out_path_val, 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 (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_(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 ############################### downsample = 32 # ratio between network input's size and network output's size, 32 for YOLOv3 net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster # this only works for squared images if config['model']['min_input_size'] == config['model']['max_input_size']: net_w = config['model']['min_input_size'] // downsample * downsample net_h = config['model']['min_input_size'] // downsample * downsample obj_thresh = config['train']['ignore_thresh'] nms_thresh = 0.45 if config['valid']['duplicate_thresh']: nms_thresh = config['valid']['duplicate_thresh'] ############################### # 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_i, image_path in enumerate(image_paths): image = cv2.imread(image_path) if image_i > 0 and image_i % 50 == 0: print( 'predicted {:4} images out of {:4} images in total'.format( image_i, len(image_paths))) # 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)) print('predicted all {:4} images'.format(len(image_paths)))
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
from utils.bbox import draw_boxes, BoundBox from keras.models import model_from_json import cv2 import numpy as np box = [BoundBox(582, 274, 700, 321, None, [.7])] json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("model.h5") label_map = np.load('label_map.npy', allow_pickle=True).item() im = cv2.imread("1.jpeg") # cv2.imshow("input",im) labels = ["number_plate"] draw_boxes(im, box, loaded_model, label_map, labels, 0.5) cv2.imshow("See here", im) cv2.waitKey()
#! /usr/bin/env python import os, sys import requests, json import cv2 from utils.utils import get_yolo_box_tfs, makedirs from utils.bbox import draw_boxes import numpy as np anchors=[] with open('anchors.json') as anchors_str: anchors = json.load(anchors_str) net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster obj_thresh, nms_thresh = 0.4, 0.45 TFS_URL="http://localhost:8501/v1/models/yolo3:predict" img_path = sys.argv[1] img_data = cv2.imread(img_path) boxes = get_yolo_box_tfs(TFS_URL, img_data, net_h, net_w, anchors, obj_thresh, nms_thresh) draw_boxes(img_data, boxes, ["raccoon"], 0) cv2.imwrite('./output/' + img_path.split('/')[-1], np.uint8(img_data))
def _main_(args): config_path = args.conf input_path = args.input with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Set some parameter ############################### net_h, net_w = 416, 416 obj_thresh, nms_thresh = 0.5, 0.45 ############################### # Load the model ############################### infer_model = load_model(config['train']['saved_weights_name']) ############################### # Predict bounding boxes ############################### # do detection on a video if input_path[-4:] == '.mp4': video_out = input_path[:-4] + '_bbox' + input_path[-4:] 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)) for i in tqdm(range(nb_frames)): _, image = video_reader.read() # predict the bounding boxes boxes = get_yolo_boxes(infer_model, image, net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh) # draw bounding boxes on the image using labels draw_boxes(image, boxes, config['model']['labels'], obj_thresh) video_writer.write(np.uint8(image)) video_reader.release() video_writer.release() # do detection on an image or a set of images else: 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:] == '.jpg' or inp_file == '.png') and '_bbox' not in inp_file ] for image_path in image_paths: 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) # 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(image_path[:-4] + '_bbox' + image_path[-4:], np.uint8(image))
yolos = infer_model.predict(process_image) prev_feature = yolos[3:] yolos = yolos[:3] isFirst = False else: yolos = seq_infer_model.predict( [process_image, prev_feature[0], prev_feature[1], prev_feature[2]]) prev_feature = yolos[3:] yolos = yolos[:3] boxes = [] for i in range(len(yolos)): # decode the output of the network yolo_anchors = anchors[(2 - i) * 6:(3 - i) * 6] boxes += decode_netout(yolos[i][0], yolo_anchors, obj_thresh, net_h, net_w) # correct the sizes of the bounding boxes correct_yolo_boxes(boxes, image_h, image_w, net_h, net_w) # suppress non-maximal boxes do_nms(boxes, nms_thresh) # draw bounding boxes on the image using labels draw_boxes(img, boxes, labels, obj_thresh) cv2.imwrite('outputs/' + img_name + img_num + '_seq_detected.jpg', cv2.resize(img, (1280, 720))) cv2.imshow('video with bboxes', cv2.resize(img, (1280, 720))) cv2.waitKey(9)
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']) json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("model.h5") label_map = np.load('label_map.npy').item() ############################### # 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], loaded_model, label_map, 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): print(input_path + inp_file) 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 tqdm(image_paths): # print(image_path) 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, loaded_model, label_map, sorted(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 run(self): global frame_glob fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi', fourcc, 60.0, (960, 720)) if not self.tello.connect(): print("Tello not connected") return if not self.tello.set_speed(self.speed): print("Not set speed to lowest possible") return # In case streaming is on. This happens when we quit this program without the escape key. if not self.tello.streamoff(): print("Could not stop video stream") return if not self.tello.streamon(): print("Could not start video stream") return frame_read = self.tello.get_frame_read() should_stop = False print('loop started') last_yaw=0 box_cnt = 0 frame_glob = [] frame = [] while not should_stop: frame = frame_read.frame for event in pygame.event.get(): if event.type == USEREVENT + 1: self.update() if event.type == USEREVENT + 2: frame_glob = frame elif event.type == QUIT: should_stop = True elif event.type == KEYDOWN: if event.key == K_ESCAPE: should_stop = True else: self.keydown(event.key) elif event.type == KEYUP: self.keyup(event.key) if frame_read.stopped: frame_read.stop() break self.screen.fill([0, 0, 0]) box_list = [] try: box_list, actions_list = box_q.get_nowait() box = box_list[0] actions = actions_list[0] except Exception: pass box_x = 0 box_y = 0 if len(box_list) > 0: box_cnt = 0 area = (box.xmax - box.xmin) * (box.ymax - box.ymin) area_p = (area / 691200.) * 100.0 box_x = int((box.xmax - box.xmin) / 2) + box.xmin box_y = int((box.ymax - box.ymin) / 2) + box.ymin draw_boxes(frame, box_list, config['model']['labels'], obj_thresh) done = bool(get_dist(box_x, box_y) < 100 and (15 < area_p < 50)) #If not done keep setting speeds if not done: self.yaw_velocity = -int((actions[0])/2) last_yaw = self.yaw_velocity self.up_down_velocity = int((actions[1])/2) if area_p < 25: self.for_back_velocity = 30 elif area_p > 50: self.for_back_velocity = -30 else: self.for_back_velocity = 0 frame = cv2.circle(frame, (box_x, box_y), 5, (255, 0, 0), -1) else: self.yaw_velocity = 0 self.up_down_velocity = 0 self.for_back_velocity = 0 #if no box is detected set the last yaw command so the drone goes into search. else: box_cnt += 1 if box_cnt > 10: self.yaw_velocity = last_yaw else: self.yaw_velocity = 0 self.up_down_velocity = 0 self.for_back_velocity = 0 frame = cv2.circle(frame, (480, 360), 5,(0, 0, 255),-1) out.write(frame) frame = np.rot90(frame) frame = np.flipud(frame) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = pygame.surfarray.make_surface(frame) self.screen.blit(frame, (0, 0)) pygame.display.update() time.sleep(1 / FPS) # Call it always before finishing. I deallocate resources. self.tello.end() out.release()