本文整理汇总了Python中detector.Detector.detect_bing方法的典型用法代码示例。如果您正苦于以下问题:Python Detector.detect_bing方法的具体用法?Python Detector.detect_bing怎么用?Python Detector.detect_bing使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类detector.Detector
的用法示例。
在下文中一共展示了Detector.detect_bing方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from detector import Detector [as 别名]
# 或者: from detector.Detector import detect_bing [as 别名]
#.........这里部分代码省略.........
counter = 0
while True:
s = f.readline()
if s=='':
break
name = s[s.find(" ")+1:].replace("\n","")
synset_dict[counter] = name
counter = counter + 1
f.close()
if not args.weights_1st_stage_bing_fn is None:
if not os.path.exists(args.weights_1st_stage_bing_fn):
print "Error the path specified for weights_1st_stage_bing_fn %s does not exist."%args.weights_1st_stage_bing_fn
sys.exit(2)
weights_1st_stage_bing = np.genfromtxt(args.weights_1st_stage_bing_fn, delimiter=',', dtype=np.float32)
else:
weights_1st_stage_bing = None
if not args.weights_2nd_stage_bing_fn is None:
if not os.path.exists(args.weights_2nd_stage_bing_fn):
print "Error: the path specified for weights_2nd_stage_bing_fn %s does not exist."%args.weights_2nd_stage_bing_fn
sys.exit(2)
f = open(args.weights_2nd_stage_bing_fn,"r")
weights_2nd_stage_bing_str = f.read()
f.close()
weights_2nd_stage_bing = json.loads(weights_2nd_stage_bing_str)
else:
weights_2nd_stage_bing = None
if not args.sizes_idx_bing_fn is None:
if not os.path.exists(args.sizes_idx_bing_fn):
print "Error: the path specified for sizes_idx_bing_fn %s does not exists."%args.sizes_idx_bing_fn
sys.exit(2)
sizes_idx_bing = np.genfromtxt(args.sizes_idx_bing_fn, delimiter=',').astype(np.int32)
else:
sizes_idx_bing = None
mean, channel_swap = None, None
if args.mean_file:
mean = np.load(args.mean_file)
if args.channel_swap:
channel_swap = [int(s) for s in args.channel_swap.split(',')]
# Make detector.
detector = Detector(args.model_def, args.pretrained_model,
gpu=args.gpu, mean=mean,
input_scale=args.input_scale, raw_scale=args.raw_scale,
channel_swap=channel_swap,
context_pad=args.context_pad,
weights_1st_stage_bing = weights_1st_stage_bing,
sizes_idx_bing = sizes_idx_bing,
weights_2nd_stage_bing = weights_2nd_stage_bing,
num_bbs_psz_bing = args.num_bbs_psz_bing,
num_bbs_final_bing = args.num_bbs_final_bing)
if args.gpu:
print 'GPU mode'
t = time.time()
image_fn = args.image_file
image = imread(image_fn)
image = resize_image(image, args.reference_edge)
if args.crop_mode == "bing":
detections, predictions = detector.detect_bing(image)
else:
print "Crop mode is not supported!"
sys.exit(1)
print("Processed {} windows in {:.3f} s.".format(len(detections),
time.time() - t))
predictions[predictions<args.detection_threshold] = 0
indices = np.nonzero(predictions)
classes = np.unique(indices[1])
resulting_bbs = []
for cls in classes:
mask = indices[1]==cls
bbs_idx = indices[0][mask]
bbs_for_nms = [ np.array( (detections[idx]["window"][1], detections[idx]["window"][0], detections[idx]["window"][3], detections[idx]["window"][2], predictions[idx,cls]) ) for idx in bbs_idx]
for detection in list(nms_detections(np.array(bbs_for_nms))):
score = detection[4]
bb = detection[:4].astype(int)
resulting_bbs.append((bb,score,synset_dict[cls]))
image = cv2.cvtColor(image, cv2.cv.CV_RGB2BGR)
for bb, det_score, class_name in resulting_bbs:
clr = (randint(0,255), randint(0,255), randint(0,255))
cv2.putText(image,"%s"%class_name, (int(bb[0])+ 10,int(bb[1]) + 10), cv2.FONT_HERSHEY_TRIPLEX, 0.6, clr)
cv2.rectangle(image,(int(bb[0]),int(bb[1])),(int(bb[2]),int(bb[3])),color=clr)
cv2.imwrite(args.output_image_file, image)
cv2.imshow("Detection result",image)
cv2.waitKey()