infer_simple10帧备份

#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from collections import defaultdict
import argparse
import numpy as np
import cv2
import glob
import logging
import os
import sys
import time

from caffe2.python import workspace

from detectron.core.config import assert_and_infer_cfg
from detectron.core.config import cfg
from detectron.core.config import merge_cfg_from_file
from detectron.utils.io import cache_url
from detectron.utils.logging import setup_logging
from detectron.utils.timer import Timer
import detectron.core.test_engine as infer_engine
import detectron.datasets.dummy_datasets as dummy_datasets
import detectron.utils.c2 as c2_utils
import detectron.utils.vis as vis_utils
np.set_printoptions(threshold=np.inf)
from primesense import openni2
from primesense import _openni2 as c_api
c2_utils.import_detectron_ops()
cv2.ocl.setUseOpenCL(False)

dist ='/home/victor/software/OpenNI-Linux-x64-2.3/Redist'
openni2.initialize(dist)
if (openni2.is_initialized()):
    print ("openNI2 initialized")
else:
    print ("openNI2 not initialized")
dev = openni2.Device.open_any()
rgb_stream = dev.create_color_stream()
depth_stream = dev.create_depth_stream()
rgb_stream.set_video_mode(c_api.OniVideoMode(pixelFormat=c_api.OniPixelFormat.ONI_PIXEL_FORMAT_RGB888, resolutionX=320, resolutionY=240, fps=30))
depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat=c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_1_MM, resolutionX=320, resolutionY=240, fps=30))
rgb_stream.start()
depth_stream.start()
depth_stream.set_mirroring_enabled(False)
rgb_stream.set_mirroring_enabled(False)

def shandiao(total_data):
    sb=[]
    median = np.median(total_data)
    b = 1.4826
    mad = b * np.median(np.abs(total_data-median))
    lower_limit = median - (3*mad)
    upper_limit = median + (3*mad)
    qudiao=[]
    for i in total_data:
        if lower_limit<i<upper_limit:
            pass
        else:
            qudiao.append(i)
    std = np.std(total_data)
    mean = np.mean(total_data)
    b = 3
    lower_limit = mean-b*std
    upper_limit = mean+b*std
    for i in total_data:
        if lower_limit<i<upper_limit:
            pass
        else:
            qudiao.append(i)
    zuizhong=[]
    zuizhong=set(total_data).difference(set(qudiao))
    list2=list(zuizhong)
    sb=[]    
    sb=np.array(list2)
    return sb

def parse_args():
    parser = argparse.ArgumentParser(description='End-to-end inference')
    parser.add_argument(
        '--cfg',
        dest='cfg',
        help='cfg model file (/path/to/model_config.yaml)',
        default=None,
        type=str
    )
    parser.add_argument(
        '--wts',
        dest='weights',
        help='weights model file (/path/to/model_weights.pkl)',
        default=None,
        type=str
    )
    parser.add_argument(
        '--output-dir',
        dest='output_dir',
        help='directory for visualization pdfs (default: /home/victor/facebook/infer_simple)',
        default='/home/victor/facebook/infer_simple',
        type=str
    )
    parser.add_argument(
        '--image-ext',
        dest='image_ext',
        help='image file name extension (default: jpg)',
        default='jpg',
        type=str
    )
    parser.add_argument(
        '--always-out',
        dest='out_when_no_box',
        help='output image even when no object is found',
        action='store_true'
    )
    parser.add_argument(
        '--output-ext',
        dest='output_ext',
        help='output image file format (default: pdf)',
        default='jpg',
        type=str
    )
    parser.add_argument(
        '--thresh',
        dest='thresh',
        help='Threshold for visualizing detections',
        default=0.7,
        type=float
    )
    parser.add_argument(
        '--kp-thresh',
        dest='kp_thresh',
        help='Threshold for visualizing keypoints',
        default=2.0,
        type=float
    )
    parser.add_argument(
        'im_or_folder', help='image or folder of images', default=None
    )
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    return parser.parse_args()

workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
setup_logging(__name__)
args = parse_args()
def get_rgb():
    bgr   = np.fromstring(rgb_stream.read_frame().get_buffer_as_uint8(),dtype=np.uint8).reshape(240,320,3)
    rgb   = cv2.cvtColor(bgr,cv2.COLOR_BGR2RGB)
    return rgb
def get_depth():
    dmap = np.fromstring(depth_stream.read_frame().get_buffer_as_uint16(),dtype=np.uint16).reshape(240,320)
    d4d = np.uint8(dmap.astype(float) *255/ 2**12-1)
    d4d = cv2.cvtColor(d4d,cv2.COLOR_GRAY2RGB)
    d4d = 255 - d4d
    return dmap, d4d

s=0
done = False
logger = logging.getLogger(__name__)
merge_cfg_from_file(args.cfg)
cfg.NUM_GPUS = 1
assert_and_infer_cfg(cache_urls=False)
assert not cfg.MODEL.RPN_ONLY, \
    'RPN models are not supported'
assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
    'Models that require precomputed proposals are not supported'

model = infer_engine.initialize_model_from_cfg(args.weights)
dummy_coco_dataset = dummy_datasets.get_coco_dataset()
timeF=100
c=1
while not done:
    key = cv2.waitKey(1) & 255
    if key == 27: 
        print ("\tESC key detected!")
        done = True
    elif chr(key) =='s': #screen capture
        print ("\ts key detected. Saving image {}".format(s))
        cv2.imwrite("ex2_"+str(s)+'.png', rgb)
    im = get_rgb()
    dmap,d4d = get_depth()
    if(c%timeF == 0):
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
            model, im, None, timers=timers
        )
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
            logger.info(
            ' \ Note: inference on the first image will be slower than the '
            'rest (caches and auto-tuning need to warm up)'
        )

        vis_utils.vis_one_image(
        im[:, :, ::-1],  # BGR -> RGB for visualization
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2
)
        im=vis_utils.vis_one_image_opencv(im,cls_boxes, cls_segms,cls_keyps,dataset=dummy_coco_dataset,thresh=0.7,show_class=True)
        classxy=vis_utils.vis_one_image_opencv2(im,cls_boxes, cls_segms,cls_keyps,dataset=dummy_coco_dataset,thresh=0.7,show_class=True)

    
        if type(classxy[0][0])==np.ndarray:
        pass
        else:
            print(classxy)
        yinying=[]
        yinying=    vis_utils.vis_one_image(
        im,
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
)
        if yinying is None:
        pass
        else:
        print (yinying.shape)
        for p in range(yinying.shape[0]):
        zhuan=tuple(yinying[p])
        print("zhuan=")
        print(zhuan)
        f=[]
        woca=[]
        for i in range(len(zhuan)):
            zuobiao=zhuan[i]
            for d in range(dmap.shape[0]):
                for e in range(dmap.shape[1]):
                    f=(d,e)
                    if (f==zuobiao).all():
                        if dmap[f]<20:
                            pass
                        else:                    
                            woca.append(dmap[f])

                    else:
                        pass
    
        sumhe=0
        pingjun=0
        if woca==[]:
            pass
        else:
            woca=np.array(woca)
            woca_unique=[]
            wocazuizhong=[]
            woca_unique=np.unique(woca)
            if woca_unique.shape[0]==1:
                wocazuizhong=woca_unique
            else:
                wocazuizhong=shandiao(woca_unique)
            for i in range(len(wocazuizhong)):
                sumhe=sumhe+wocazuizhong[i]
            print ("**********depth******************")
            print (sumhe/len(wocazuizhong))
 
        cv2.imshow('rgb10', im)
        cv2.imshow('depth10', d4d)
    cv2.imshow('rgb', im)
    cv2.imshow('depth', d4d)    
    c=c+1
cv2.destroyAllWindows()
rgb_stream.stop()
openni2.unload()
print ("Terminated")
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值