-
Notifications
You must be signed in to change notification settings - Fork 218
Detect objects in local images #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+69
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
detect.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.