-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathobjectness_saliency.py
More file actions
44 lines (38 loc) · 1.52 KB
/
objectness_saliency.py
File metadata and controls
44 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Packages Import
import argparse
import cv2
import os
import numpy as np
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="path to BING objectness saliency model")
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-n", "--max-detections", type=int, default=10,
help="maximum # of detections to examine")
ap.add_argument("-d", "--diff", required=True, help="Differentiator")
ap.add_argument("-a", "--here", required=False, help="Ignores SaveTo and uses CWD to save output")
args = vars(ap.parse_args())
# Load our input image
image = cv2.imread(args["image"])
di = args["diff"]
# initialize OpenCV's objectness saliency detector and set the path
# to the input model files
saliency = cv2.saliency.ObjectnessBING_create()
saliency.setTrainingPath(args["model"])
# compute the bounding box predictions used to indicate saliency
(success, saliencyMap) = saliency.computeSaliency(image)
numDetections = saliencyMap.shape[0]
# loop over the detections
for i in range(0, min(numDetections, args["max_detections"])):
# extract the bounding box coordinates
(startX, startY, endX, endY) = saliencyMap[i].flatten()
# randomly generate a color for the object and draw it on the image
output = image.copy()
color = np.random.randint(0, 255, size=(3,))
color = [int(c) for c in color]
cv2.rectangle(output, (startX, startY), (endX, endY), color, 2)
# show the output image
cv2.imshow("Image", output)
cv2.waitKey(0)