def predict(): #그래프가 뭘까.. # with tf.device(DEVICE): origin_img = skimage.io.imread('static/img/uploaded_img.jpg') class_names = ['1', '2', '3', '4'] * 30 img = np.reshape(origin_img, (1, origin_img.shape[0], origin_img.shape[1], 3)) global sess global graph with graph.as_default(): set_session(sess) results = model.detect(img, verbose=1) r = results[0] print(r) image_name = 'result_img' visualize.save_image(origin_img, image_name, r['rois'], r['masks'], r['class_ids'], r['scores'], class_names, mode=0) #class_ids 값을 중복된 result값을 하나로 r_class = np.unique(r['class_ids']) print("=============================") print("클레스 넘버는", r['class_ids']) print("=============================") return render_template("result.html", class_num=r_class)
def detect(image): print('Shemovedi') results = model.detect([image], verbose=1) r = results[0] print('sdadasdada') save_image(image, 'image_name', r['rois'], r['masks'], r['class_ids'], r['scores'], ['BG', 'Plate'], filter_classs_names=None, scores_thresh=0.1, save_dir='images888/', mode=0) # masked_image = return_image(image, 'kkkk', r['rois'], r['masks'], r['class_ids'], r['scores'], ['BG', 'Plate'], # filter_classs_names=None, # scores_thresh=0.1, save_dir=None, mode=0) #return send_file(BytesIO(masked_image), attachment_filename='pic',as_attachment=True, mimetype='image/jpeg') print('PAAAAAAAAAAAAAAAATH!: ', os.path) try: return send_file( 'C:/Users/iliac/Documents/Machine Learning/FlaskTest/Cars/images888/image_name.jpg', attachment_filename='python.jpg') except: print('e raa')
def Result(image): # Load validation dataset dataset = forest.CustomDataset() dataset.load_custom("D:/Techathon/flask/forest/dataset", "val") # Must call before using the dataset dataset.prepare() print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names)) image_id = random.choice(dataset.image_ids) image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset, config, image_id, use_mini_mask=False) #info = dataset.image_info[image_id] #print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id, # dataset.image_reference(image_id))) print([image],"IMAGEEEEEE") # Run object detection with graph.as_default(): results = model.detect([image], verbose=1) ax = get_ax(1) r = results[0] #print(r[0]) f = list(results[0].values()) print(f) print("saving image") visualize.save_image(image, image_id, r['rois'], r['masks'], r['class_ids'],r['scores'],dataset.class_names, filter_classs_names=['forest', 'deforest'],scores_thresh=0.9,mode=0) print("doneS") return f
def save_image(self, image, detection_result, image_dir): visualize.save_image(image, "detection_result", detection_result['rois'], detection_result['masks'], detection_result['class_ids'], detection_result['scores'], self.class_names, image_dir, mode=0)
def detection(self): class_names = ['bg', "Detail"] # names of classes for model # Optimizing image size before begin detection image = IMG.open(self.source) image = image.resize((1000, 800), IMG.ANTIALIAS) image = np.array(image) self.information = "Детекция начата" # Run detection t1 = time.time() results = self.model.detect([image], verbose=1) print('Detection time is {}'.format(time.time() - t1)) r = results[0] self.information = ('Детекция заняла: {}'.format(time.time() - t1)) # Making temporary file with masks and counting num of details pred_file = save_image(image, "temp", r['rois'], r['masks'], r['class_ids'], r['scores'], class_names) # Show image inside the window and result of counting self.source = pred_file self.ids.mechimage.reload() self.num_of_details = len(r["class_ids"]) self.information = "Количество деталей: " + str(self.num_of_details)
def predict(class_names=args.class_names, filter_classs_names=args.filter_classs_names, scores_thresh=args.scores_thresh, mode=args.mode): # Create models in training mode config = ShapesConfig() config.display() model = modellib.MaskRCNN(mode="inference", config=config, model_dir=MODEL_DIR) model_path = model.find_last() # Load trained weights (fill in path to trained weights here) assert model_path != "", "Provide path to trained weights" print("Loading weights from ", model_path) model.load_weights(model_path, by_name=True) IMAGE_DIR = os.path.join(ROOT_DIR, "test_data") save_dir = os.path.join(IMAGE_DIR, "dec_result") if not os.path.exists(save_dir): os.makedirs(save_dir) file_names = next(os.walk(IMAGE_DIR))[2] i = 1 for file in file_names: image = skimage.io.imread(os.path.join(IMAGE_DIR, file)) # Run detection results = model.detect([image], verbose=1) # Visualize results r = results[0] image_name = 'dec_%s_%s' % (i, r['scores']) print(r['scores']) i += 1 #visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) visualize.save_image(image, image_name, r['rois'], r['masks'], r['class_ids'], r['scores'], class_names, save_dir=save_dir, filter_classs_names=filter_classs_names, scores_thresh=scores_thresh, mode=mode)
def predict(): matplotlib.use('Agg') image = skimage.io.imread(IMAGE_PATH) image, window, scale, padding, crop = utils.resize_image( image, min_dim=config.IMAGE_MIN_DIM, min_scale=config.IMAGE_MIN_SCALE, max_dim=config.IMAGE_MAX_DIM, mode=config.IMAGE_RESIZE_MODE) results = model_inference.detect([image], verbose=1) # get dictionary for first prediction r = results[0] visualize.save_image(image, 'prediction', r['rois'], r['masks'], r['class_ids'], r['scores'], ['BG', 'tutku'], scores_thresh=config.DETECTION_MIN_CONFIDENCE, save_dir=SAVE_DIR) return len(r['class_ids'])
def compute_batch_ap(dataset, image_ids, verbose=1): APs = [] mean_weight_iou = [] for image_id in image_ids: try: # Load image image, image_meta, gt_class_id, gt_bbox, gt_mask =\ modellib.load_image_gt(dataset, config_2class, image_id, use_mini_mask=False) # Run object detection results = model_2class.detect_molded(image[np.newaxis], image_meta[np.newaxis], verbose=0) # Compute AP over range 0.5 to 0.95 r = results[0] visualize.save_image(image, "test"+str(image_id), r['rois'], r['masks'], r['class_ids'],r['scores'],['BG', 'Whole Corn','Bare Cob'],scores_thresh=0.8,mode=0, captions=None, show_mask=True) gt_r = {"class_ids": gt_class_id, "rois": gt_bbox, "masks": gt_mask} gt_corns, gt_corn_masks, gt_red_corn_masks = get_cornList(gt_r, 2, image) # print('gt_mask size: ',gt_corn_masks.shape) pred_corns, pred_cornMasks, pred_redCornMasks = get_cornList(r, 2, image) #print(pred_corns) print(image_id, "Image" , os.path.basename(dataset_2class.source_image_link(image_id))) print(image_id, 'percent_eaten_gt', gt_corns[1]['percent_eaten']) print(image_id, 'percent_eaten_pred', pred_corns[1]['percent_eaten']) print(image_id, 'percent_eaten_gt', gt_corns[0]['percent_eaten']) print(image_id, 'percent_eaten_pred', pred_corns[0]['percent_eaten']) print("*****************************************************************") images.append(os.path.basename(dataset_2class.source_image_link(image_id))) gt_one.append(gt_corns[1]['percent_eaten']) pred_one.append(pred_corns[1]['percent_eaten']) gt_two.append(gt_corns[0]['percent_eaten']) pred_two.append(pred_corns[0]['percent_eaten']) except: print("image Id :", image_id) print(sys.exc_info()) ap = 0 APs.append(ap) if verbose: info = dataset.image_info[image_id] meta = modellib.parse_image_meta(image_meta[np.newaxis,...]) print("{:3} {} AP: {:.2f}".format( meta["image_id"][0], meta["original_image_shape"][0], ap)) pass return APs
def image_annot(path): for image_name in os.listdir(path): #print(image_name) image = skimage.io.imread(os.path.join(path, image_name)) results = model.detect([image], verbose=1) r = results[0] try: visualize.save_image(image, image_name, r['rois'], r['masks'], r['class_ids'], r['scores'], dataset.class_names, scores_thresh=0.5, mode=1) except: continue
def predict(self, image_path, save_vis=False, save_dir=None): """ Returns segentation results as dict and binary masks # Inputs: image_path: path to a single image save_vis: Boolean if segmentation results are to be saved save_dir: directory where results are to be saved (name stays the same) # Return: dict with results in format {'image_id': image_id, 'classes': class_ids, 'boxes': bboxes} binary masks in format [im_h, im_w, num_instances] with the same ordering as in the dict """ print(image_path) image = cv2.imread(image_path) results = self.model.detect([image], verbose=0) r = results[0] image_id=os.path.split(image_path)[1][0:-4] if save_vis: visualize.save_image(image = image[:,:,::-1], image_name=image_id, boxes=r['rois'], masks=r['masks'], class_ids=r['class_ids'], class_names=class_names[1:], scores=r['scores'], save_dir=save_dir) features = {'image_id': image_id, 'classes': r['class_ids'].tolist(), 'boxes': r['rois'].tolist()} return features, r['masks']
def save_pothole(filename, focal_length, sensor_width, sensor_height, dist_from_ground): image = cv2.imread(POTHOLE_DIR + filename , cv2.IMREAD_COLOR) print(image.shape) px = image.shape[1] py = image.shape[0] gsd =[(dist_from_ground*sensor_height/(focal_length*py)), (dist_from_ground*sensor_width/(focal_length*px))] fgsd = max(gsd) actual_x = fgsd * px actual_y = fgsd * py total_grnd_area = actual_x*actual_y results = model.detect([image], verbose=1) _, ax = get_ax(1) r = results[0] ret = visualize.display_instances(filename,image, r['rois'], r['masks'], r['class_ids'], ['BG','pothole'], r['scores'], ax=ax, title="Predictions") visualize.save_image(image, filename,r['rois'], r['masks'], r['class_ids'],r['scores'],['BG','pothole']) print(ret) print('-----------------------------------------') #TODO : Calculate individual area for each pothole. if ret: print(r['rois']) print('-----------------------------------------') print(r['masks'].shape) mask_img = r['masks'][:,:,0] print(type(mask_img)) print('-----------------------------------------') area_in_pixel = np.reshape(r['masks'], (-1, r['masks'].shape[-1])).astype(np.float32).sum() perc_area_covered_pothole = area_in_pixel/(px*py) pothole_area = perc_area_covered_pothole * total_grnd_area return (focal_length,sensor_width,sensor_height,dist_from_ground,pothole_area,perc_area_covered_pothole*100, len(r['class_ids'])) else: return ret
def detect(image): results = model.detect([image], verbose=1) r = results[0] masked_image = save_image(image, 'vvv8', r['rois'], r['masks'], r['class_ids'], r['scores'], ['BG', 'Plate'], filter_classs_names=None, scores_thresh=0.1, save_dir=None, mode=0) return masked_image
# print("image ids", image_ids) # ============ # Interference # ============ APs = [] metrics = dict() for image_id in image_ids: image_name = os.path.basename(dataset_val.image_reference(image_id)) print("===============================") print("Image reference: ", image_name) image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt(dataset_val, inference_config, image_id, use_mini_mask=False) results = infer_model.detect([image], verbose=1) r = results[0] visualize.save_image(image, image_name[:-4] + "-detected", r['rois'], r['masks'], r['class_ids'], r['scores'], ["ship", "Ship", "ship"]) AP, precisions, recalls, overlaps = utils.compute_ap(gt_bbox, gt_class_id, gt_mask, r["rois"], r["class_ids"], r["scores"], r['masks']) APs.append(AP) image_metrics = dict() image_metrics["AP"] = AP image_metrics["precisions"] = precisions.tolist() image_metrics["recalls"] = recalls.tolist() image_metrics["overlaps"] = overlaps.tolist() # print(json.dumps(image_metrics)) metrics[image_name] = image_metrics print("===============================")
def detect(model, image_dir): # Run model detection print("Running predictions on {}".format(image_dir)) files = glob.glob(image_dir + "/*") dataset = FiguresDataset() dataset.load_figures(args.validation_dataset, "val_annotations.json") dataset.prepare() gen = AnnotationsGenerator("Dataset detection results") gen.add_categories(dataset.class_names) i = 0 for file in files: if file.endswith((".png", ".tiff", ".jpg", ".jpeg", ".tif", ".TIF")): print("Predicting \"%s\"..." % (file)) # Load image image = skimage.io.imread(file) # If grayscale. Convert to RGB for consistency. if image.ndim != 3: image = skimage.color.gray2rgb(image) # If has an alpha channel, remove it for consistency if image.shape[-1] == 4: image = image[..., :3] # Detect objects results = model.detect([image], config, verbose=1) r = results[0] # Save image as pred_x.png and save annotations in COCO format image_name = file.replace(image_dir, "").replace("\\", "").replace("/", "") gen.add_image(image_name, image.shape[0], image.shape[1]) image_id = gen.get_image_id(image_name) filename = "pred_%i" % i # Save image as pred_x.png if config.ORIENTATION: save_image(image, filename, r['rois'], r['masks'], r['orientations'], r['class_ids'], r['scores'], dataset.class_names, gen, image_id, save_dir="../results/predictions", mode=4) else: save_image(image, filename, r['rois'], r['masks'], None, r['class_ids'], r['scores'], dataset.class_names, gen, image_id, save_dir="../results/predictions", mode=4) # Save the results in an annotation file following the COCO dataset structure i += 1 # Save the results in an annotation file following the COCO dataset structure gen.save_json("../results/predictions" + "/prediction_annotations.json", pretty=True)
directory = os.fsencode(strDirectory) count = 1 for file in os.listdir(directory): try: r2 = {} filename = os.fsdecode(file) print(str(count) + ": Image Under Process: ", filename) # image = skimage.io.imread("D:/test data/Corn Test Data/5- 95 consumed/1275_1_00404_01248_a1.JPG") image = skimage.io.imread(strDirectory + str(filename)) results_2class = model_2class.detect([image], verbose=0) r2 = results_2class[0] visualize.save_image(image, filename, r2['rois'], r2['masks'], r2['class_ids'], r2['scores'], ['BG', 'Corn', 'Red Corn Kernel'], scores_thresh=0.8, mode=0, captions=None, show_mask=True) #print('Detection results: ' ,r2) cornList_2class = get_cornList(r2, 2) final_rois = [] final_masks = [] final_class = [] final_percent = [] cornList = cornList_2class for corn, index in zip(cornList, range(len(cornList))): print("Corn number ", index + 1, "is ", corn['percent_eaten'], "% Eaten") final_rois.append(corn['cornRoi'])
def detect(working_dir, model, image_path=None): # define the name of the directory to be created folder_path = working_dir # Read image image = skimage.io.imread(image_path) # Detect objects predictions = model.detect([image], verbose=1)[0] scores = predictions['scores'] masks = predictions['masks'] class_ids = predictions['class_ids'] rois = predictions['rois'] ''' if len(scores)>0: index = np.argmax(scores) # for convenience, use simpler notation rois = predictions['rois'][index] score = scores[index] ''' # to iterate through the instance, we need an incrementable enumerator enumerator = 0 # we'll keep the explored data in a dict state_estimation_data = dict() # uncomment to display the results #visualize.display_instances(image, predictions['rois'], predictions['masks'], predictions['class_ids'], class_names, predictions['scores']) # run through the instances for class_id in class_ids: id = class_names[class_id] + str(enumerator) part_type = '' part_type_specifics_confidence = 0.99 # default value for the confidence # if the part is bearing, then classify it if (class_names[class_id] == 'bearing'): part_type, part_type_specifics_confidence = getType(masks[:,:,enumerator], image, rois[enumerator], folder_path) # get the outline coordinates part_mask, poses, outline_poses = getBoundaryPositions(masks[:, :, enumerator]) # skip the mask if the shape is weird. This is a rare situation, but may happen. if (len(poses) == 0 or len(outline_poses) == 0): continue part_type_confidence = scores[enumerator] # save the images to the folder, as well as the .json assos_img_path = folder_path + "/assos_img.png" part_mask_path = folder_path + "/part_mask" + "_" + id + ".png" cv2.imwrite(assos_img_path, image) part_mask = numpy2Mat(part_mask) # convert to opencv type of mat cv2.imwrite(part_mask_path, part_mask) # Define data to be written component_data = { #'part_id': (class_names[class_id] + str(enumerator)), 'part_bounding_box': rois[enumerator].tolist(), 'outline_poses': outline_poses.tolist(), 'part_type': part_type, 'part_type_confidence': float(part_type_confidence), 'part_type_specifics_confidence': float(part_type_specifics_confidence), 'pose': poses } # write everything to another dict state_estimation_data[class_names[class_id] + str(enumerator)] = component_data # increment per component to form the id enumerator = enumerator + 1 # save the result image with all the detections visualize.save_image(image, "result_image", rois, masks, class_ids, scores, class_names, state_estimation_data, filter_classs_names=None, scores_thresh=0.8, save_dir=folder_path, mode=0) # write JSON file with io.open(folder_path + '/state_estimation_data.json', 'w', encoding='utf8') as outfile: str_ = json.dumps(state_estimation_data, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False) outfile.write(to_unicode(str_)) # to test, read the file with open(folder_path + '/state_estimation_data.json') as data_file: data_loaded = json.load(data_file) print("State Estimation -> .json file loaded: ", state_estimation_data == data_loaded)
r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'], ax=ax, title="Predictions") #%% saving the results #mode = 0 , save image with bbox,class_name,score and mask; #mode = 1 , save image with bbox,class_name and score; #mode = 2 , save image with class_name,score and mask; #mode = 3 , save mask with black background; visualize.save_image(image, "kuchbhi", r['rois'], r['masks'], r['class_ids'], r['scores'], dataset.class_names, mode=3) log("gt_class_id", gt_class_id) log("gt_bbox", gt_bbox) log("gt_mask", gt_mask) #%% [markdown] # ## Color Splash # # This is for illustration. You can call `balloon.py` with the `splash` option to get better images without the black padding. #%% running and saving random images from images folder import skimage.io
print((particle_p['rois'][i][0]+particle_p['rois'][i][2])/2,(particle_p['rois'][i][1]+particle_p['rois'][i][3])/2,file=open(particle_centerfile,'a')) print((particle_p['rois'][i][2]-particle_p['rois'][i][0])*(particle_p['rois'][i][3]-particle_p['rois'][i][1]),file=open(particle_areafile,'a')) cell_borderfile=cell_borderpath+cell_file_name cell_borderfile+=".txt" cell_fullfile=cell_fullpath+cell_file_name cell_fullfile+=".txt" np.savetxt(cell_fullfile, cell_coordinate,fmt='%d') np.savetxt(cell_borderfile, cell_border,fmt='%d') for i in range(cell_p['rois'].shape[0]): print((cell_p['rois'][i][0]+cell_p['rois'][i][2])/2,(cell_p['rois'][i][1]+cell_p['rois'][i][3])/2,file=open(cell_centerfile,'a')) visualize.save_image(particle_test_image, particle_savepic, particle_p['rois'], particle_p['masks'], particle_p['class_ids'],particle_p['scores'],class_names,scores_thresh=0.5,mode=0) visualize.save_image(cell_test_image, cell_savepic, cell_p['rois'], cell_p['masks'], cell_p['class_ids'],cell_p['scores'],class_names,scores_thresh=0.5,mode=0) visualize.save_single_image(particle_test_image, particle_savemaskpic, particle_p['rois'], particle_p['masks'], particle_p['class_ids'],particle_p['scores'],class_names,scores_thresh=0.5,mode=0) print("ok") sys.stdout.flush() #PorC=sys.argv[8] ''' px=particle_file_name.split("/")[-1] particle_img_path+="/" particle_img_path+=px particle_centerpath+="/" particle_centerpath+=px particle_areapath+="/"
if not image_ids: continue # Load image print("processing image {} of {}".format(view_index, view_ids.size)) image = dataset.load_image(image_ids[0]) im = [] Rcam = [] Kmat = dataset.K image_ids = image_ids[:config.NUM_VIEWS] for image_id in image_ids: image = dataset.load_image(image_id) im.append(image) Rcam.append(dataset.load_R(image_id)) im = np.stack(im) Rcam = np.stack([Rcam]) Kmat = np.stack([Kmat]) # Run object detection results = model.detect([im], Rcam, Kmat) r = results[0] visualize.save_image(image_name=image_ids[0], image=im[0], boxes=r['rois'], masks=r['masks'], class_ids=r['class_ids'], class_names=selected_classes, scores=r['scores'], save_dir=SAVE_DIR)
log("gt_mask", gt_mask) # DETECT MASK FOR IMAGE results = model.detect([original_image], verbose=1) r = results[0] # Function used to save mask predictions # Mode 3 sets it to a black background with only mask segmentation # creates an output directory for images to be saved in visualize.save_image(original_image, image_name, r['rois'], r['masks'], r['class_ids'], r['scores'], dataset_val.class_names, filter_classs_names=['building'], scores_thresh=0.7, mode=3, save_dir=OUTPUT) # If turned on, get score of pred and ground truth if TYPE == 'score': actual_mask = skimage.io.imread(os.path.join(mask_name)) pred_mask = skimage.io.imread( os.path.join(OUTPUT, str(image_name + ".png"))) Jaccard = (jaccard_score(actual_mask, pred_mask, average='micro')) fName.append(temp)
def main(): st.title("Fall / No-Fall Detection in an Image") st.write("Please upload an image to detect a fall instance / falling person ") image_file = st.file_uploader("Upload images", type=["png","jpeg","jpg"]) if image_file is not None: #st.write(type(image_file)) #st.write(dir(image_file)) #file_details ={"filename":image_file.name,"filetype":image_file.type,"filesize":image_file.size,"directory":image_file.__dir__} #st.write(file_details) st.header("Your uploaded image") st.image(load_image(image_file),width=400,height=400) ROOT_DIR = os.path.abspath("../") #st.write(ROOT_DIR) sys.path.append(ROOT_DIR) MODEL_DIR = os.path.join(ROOT_DIR, "ProjectTwo") WEIGHTS_PATH = os.path.join(MODEL_DIR,"mask_rcnn_fall_0010.h5") #WEIGHTS_PATH = "/home/andmerc/Thesis/Project/ProjectTwo/mask_rcnn_fall_0010.h5" #"/home/andmerc/Thesis/Project/ProjectTwo/mask_rcnn_fall_0010.h5" config = codee.BasicConfig() DATASET_DIR = os.path.join(MODEL_DIR,"Data") #DATASET_DIR = "/home/andmerc/Thesis/Data" class InferenceConfig(config.__class__): GPU_COUNT = 1 IMAGES_PER_GPU = 1 config = InferenceConfig() #config.display() DEVICE = "/cpu:0" TEST_MODE = "inference" def get_ax(rows=1, cols=1, size=16): _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows)) return ax dataset = codee.FallDataset() dataset.load_custom(DATASET_DIR, "val") dataset.prepare() print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names)) with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(WEIGHTS_PATH, by_name=True) image = load_image(image_file) image = np.array(image) if image.shape[2] > 3: image = image[:,:,:3] st.header("Processing...") progress_bar = st.progress(0) for completion_rate in range(100): time.sleep(0.6) progress_bar.progress(completion_rate + 1) pathOut = os.path.join(MODEL_DIR,"Output") results = model.detect([image], verbose=1) #print(results) r = results[0] x = r['class_ids'] #print(type(x)) # Display results ax = get_ax(1) r = results[0] save_image_name = "final" finalpath = pathOut +"/" + save_image_name +".jpg" visualize.save_image(image,save_image_name ,r['rois'], r['masks'], r['class_ids'],r['scores'],dataset.class_names,save_dir=pathOut) if not x.size: st.header("No Fall Instance Detected on the image") st.image(load_image(image_file),height=400,width=400) else: st.header("Fall Instance Detected on the image") final_img = io.imread(finalpath) st.image(final_img, height=400, width=400)
def evaluate_coco(model, dataset, coco, eval_type="bbox", net="BIRANet", augmentation=None, limit=50, image_ids=None): """Runs official COCO evaluation. dataset: A Dataset object with valiadtion data eval_type: "bbox" or "segm" for bounding box or segmentation evaluation limit: if not 0, it's the number of images to use for evaluation """ # Pick COCO images from the dataset image_ids = image_ids or dataset.image_ids # Limit to a subset if limit: image_ids = image_ids[:limit] # Get corresponding COCO image IDs. coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids] t_prediction = 0 t_start = time.time() results = [] for i, image_id in enumerate(image_ids): # Load image image = dataset.load_image(image_id) bbox, class_ids = dataset.load_bbox(image_id) gt_bbox = utils.extract_bbox_boxes(bbox) pointclouds = dataset.image_info[image_id]["pointclouds"] points = pointclouds['points'] ranchor_index, rpoint_fmap, _ = utils.rpoint_image_mapping( points, image.shape[:2], (config.IMAGE_MAX_DIM, config.IMAGE_MAX_DIM), config) # Run detection returns detection on input image size(900,1600) t = time.time() r = model.detect([image], rpoint_fmap, ranchor_index, net, verbose=0)[0] t_prediction += (time.time() - t) ##visualize detection results on image visualize.save_image(image, i, r['rois'], r['class_ids'], dataset.class_names, r['scores']) # Convert results to COCO format # Cast masks to uint8 because COCO tools errors out on bool image_results = build_coco_results(dataset, coco_image_ids[i:i + 1], r["rois"], r["class_ids"], r["scores"]) # , results.extend(image_results) # Load results. This modifies results with additional attributes. coco_results = coco.loadRes(results) # Evaluate cocoEval = COCOeval(coco, coco_results, eval_type) cocoEval.params.imgIds = coco_image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() print("Prediction time: {}. Average {}/image".format( t_prediction, t_prediction / len(image_ids))) print("Total time: ", time.time() - t_start)
for image_name in sorted(os.listdir(image_path)): if image_name.endswith(('.jpg', '.jpeg')): image = skimage.io.imread(os.path.join(image_path, image_name)) if image.ndim != 3: image = skimage.color.gray2rgb(image) if image.shape[-1] == 4: image = image[..., :3] check = 0 results = model.detect([image], verbose=1) r = results[0] n += 1 if r["rois"].shape[0]: count_porn += 1 check = 1 else: count_non += 1 if check: print(image_name, " - P**n") else: print(image_name, " - Not P**n") # visualize.display_instances(image_name,image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores']) visualize.save_image(image_name, image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores']) print("Total: ", n, " images") print("P**n: ", count_porn, " - ", count_porn / n * 100, "%") print("Non: ", count_non, " - ", count_non / n * 100, "%")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) result = model.detect([resize_image(image_path)]) r = result[0] if r['masks'].size > 0: masks = np.zeros((img.shape[0], img.shape[1], r['masks'].shape[-1]), dtype=np.uint8) for m in range(r['masks'].shape[-1]): masks[:, :, m] = cv2.resize(r['masks'][:, :, m].astype('uint8'), (img.shape[1], img.shape[0]), interpolation=cv2.INTER_NEAREST) y_scale = img.shape[0] / IMAGE_SIZE x_scale = img.shape[1] / IMAGE_SIZE rois = (r['rois'] * [y_scale, x_scale, y_scale, x_scale]).astype(int) masks, rois = refine_masks(masks, rois) else: masks, rois = r['masks'], r['rois'] visualize.display_instances(img, rois, masks, r['class_ids'], ['bg'] + label_names, r['scores'], title="0", figsize=(12, 12)) visualize.save_image(img, "= =", rois, masks, r['class_ids'], r['scores'], ['bg'] + label_names)