From 4d27fb30bd712ac72ee1b8cdfee0ec63eec14df6 Mon Sep 17 00:00:00 2001 From: Alexander Iotko Date: Fri, 20 May 2022 17:57:43 +0300 Subject: [PATCH] Detect objects in local images --- detect.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 detect.py diff --git a/detect.py b/detect.py new file mode 100644 index 0000000..d3117ef --- /dev/null +++ b/detect.py @@ -0,0 +1,69 @@ +import glob, os + +import matplotlib.pyplot as plt +import matplotlib.pylab as pylab + +import requests +from io import BytesIO +from PIL import Image +import numpy as np +pylab.rcParams['figure.figsize'] = 20, 12 +from maskrcnn_benchmark.config import cfg +from maskrcnn_benchmark.engine.predictor_glip import GLIPDemo + +def load(file): + pil_image = Image.open(file).convert("RGB") + # convert to BGR format + image = np.array(pil_image)[:, :, [2, 1, 0]] + return image + +def load_url(url): + """ + Given an url of an image, downloads the image and + returns a PIL image + """ + response = requests.get(url) + pil_image = Image.open(BytesIO(response.content)).convert("RGB") + # convert to BGR format + image = np.array(pil_image)[:, :, [2, 1, 0]] + return image + +def imshow(img, caption): + plt.imshow(img[:, :, [2, 1, 0]]) + plt.axis("off") + plt.figtext(0.5, 0.09, caption, wrap=True, horizontalalignment='center', fontsize=20) + +def imsave(img, path): + plt.imsave(path, img[:, :, [2, 1, 0]]) + +# config_file = "configs/pretrain/glip_Swin_T_O365_GoldG.yaml" +# weight_file = "MODEL/glip_tiny_model_o365_goldg_cc_sbu.pth" + +# Use this command to evaluate the GLPT-L model +# ! wget https://penzhanwu2bbs.blob.core.windows.net/data/GLIPv1_Open/models/glip_large_model.pth -O MODEL/glip_large_model.pth +config_file = "configs/pretrain/glip_Swin_L.yaml" +weight_file = "MODEL/glip_large_model.pth" + +# update the config options with the config file +# manual override some options +cfg.local_rank = 0 +cfg.num_gpus = 1 +cfg.merge_from_file(config_file) +cfg.merge_from_list(["MODEL.WEIGHT", weight_file]) +cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) + + +glip_demo = GLIPDemo( + cfg, + min_image_size=800, + confidence_threshold=0.7, + show_mask_heatmaps=False +) + +caption = 'person . bicycle . car . motorcycle . airplane . 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 . couch . potted plant . bed . dining table . toilet . tv . laptop . mouse . remote . keyboard . cell phone . microwave . oven . toaster . sink . refrigerator . book . clock . vase . scissors . teddy bear . hair drier . toothbrush .' +for files in [glob.glob(e) for e in ['INPUT/*.jpg', 'INPUT/*.png', 'INPUT/*.JPG', 'INPUT/*.PNG']]: + for file in files: + image = load(file) + result, _ = glip_demo.run_on_web_image(image, caption, 0.5) + #imshow(result, caption) + imsave(result, "RESULTS/"+file.split("/")[-1]) \ No newline at end of file