Пример #1
0
	def cook(self, location, interface, attrs):
		# Get x3ds and 3D labels from the cooked location
		x3ds = interface.attr('x3ds')
		if x3ds is None:
			self.logger.error('Could not find attribute: x3ds')
			return

		x3ds_labels = interface.attr('x3ds_labels')
		if x3ds_labels is None:
			self.logger.error('Could not find attribute: x3ds_labels')
			return

		normals = interface.attr('normals')

		x2d_threshold = attrs['x2d_threshold']

		# Set the detections and calibration locations as the cook location if not defined
		x2ds_location = attrs['x2ds']
		if not x2ds_location: x2ds_location = location

		calibrationLocation = attrs['calibration']
		if not calibrationLocation: calibrationLocation = interface.root()

		# Fetch 2D and calibration data
		x2ds = interface.attr('x2ds', atLocation=x2ds_location)
		x2ds_splits = interface.attr('x2ds_splits', atLocation=x2ds_location)
		Ps = interface.attr('Ps', atLocation=interface.root() + '/cameras')

		if x2ds is None or x2ds_splits is None:
			self.logger.error('2D detection data at %s is not valid' % x2ds_location)
			return

		if Ps is None:
			mats = interface.attr('mats', atLocation=interface.root())
			if mats:
				Ps = np.array([m[2] / (np.sum(m[2][0, :3] ** 2) ** 0.5) for m in mats], dtype=np.float32)
			else:
				self.logger.error('Attribute mats not found at %s' % calibrationLocation)
				self.logger.error('Attribute Ps not found at %s' % calibrationLocation)
				return

		# Check if we've got visibility lods
		if 'skeleton' in attrs and attrs['skeleton']:
			skeletonLoc = attrs['skeleton']
			skelDict = interface.attr('skelDict', atLocation=skeletonLoc)
			visibilityLod = interface.getChild('visibilityLod', parent=skeletonLoc)
			if visibilityLod is None:
				self.logger.error('No visibility LODs found at skeleton: %s' % attrs['skeleton'])
				return

			lodNames = visibilityLod['names']
			lodTris = visibilityLod['tris']
			lodVerts = visibilityLod['verts']
			lodNormals = visibilityLod['faceNormals']
			tris = lodVerts[lodTris]

			mats = interface.attr('mats', atLocation=attrs['calibration'])
			cameraPositions = np.array([m[4] for m in mats], dtype=np.float32)

		clouds = ISCV.HashCloud2DList(x2ds, x2ds_splits, x2d_threshold)
		if self.visibility is None: self.visibility = ISCV.ProjectVisibility.create()
		proj_x2ds = None

		if attrs['useVisibility'] and normals is not None:
			self.visibility.setNormalsAndLods(normals, tris, cameraPositions, np.concatenate((lodNormals)), attrs['intersect_threshold'], attrs['generateNormals'])
			# proj_x2ds, proj_splits, proj_labels = ISCV.project_visibility(x3ds, x3ds_labels, Ps, self.visibility)
			# score, x2d_labels, residuals = clouds.assign(proj_x2ds, proj_splits, proj_labels, x2d_threshold)
			score, x2d_labels, residuals = clouds.project_assign_visibility(x3ds, x3ds_labels, Ps, x2d_threshold, self.visibility)
		elif attrs['useNormals'] and normals is not None:
			self.visibility.setNormals(normals)
			proj_x2ds, proj_splits, proj_labels = ISCV.project_visibility(x3ds, x3ds_labels, Ps, self.visibility)
			score, x2d_labels, residuals = clouds.assign(proj_x2ds, proj_splits, proj_labels, x2d_threshold)
		else:
			proj_x2ds, proj_splits, proj_labels = ISCV.project(x3ds, x3ds_labels, Ps)
			score, x2d_labels, vels = clouds.assign(proj_x2ds, proj_splits, proj_labels, x2d_threshold)

		if proj_x2ds is not None:
			projectedLocsAttrs = {
				'x2ds': proj_x2ds, 'x2ds_splits': proj_splits, 'labels': proj_labels,
				'x2ds_colour': (1.0, 0.0, 0.0, 0.7), 'x2ds_pointSize': 10,
				'score': score
			}
			if 'showProjected' in attrs and attrs['showProjected']:
				interface.createChild('projected', 'points2d', attrs=projectedLocsAttrs)
			else:
				interface.createChild('projected', 'group', attrs=projectedLocsAttrs)

		if attrs['newLocation']:
			locAttrs = {
				'x2ds': x2ds, 'x2ds_splits': x2ds_splits, 'labels': x2d_labels,
				'x2ds_colour': eval(attrs['colour']), 'x2ds_pointSize': attrs['pointSize'],
				'score': score
			}

			labelColours = interface.getLabelColours(x2d_labels, eval(attrs['colour']))
			if labelColours.any():
				locAttrs['x2ds_colours'] = labelColours

			interface.createChild('assigned', 'points2d', attrs=locAttrs)
		else:
			interface.setAttr('labels', x2d_labels, atLocation=x2ds_location)
			interface.setAttr('score', score)

			labelColours = interface.getLabelColours(x2d_labels, interface.attr('x2ds_colour', atLocation=x2ds_location))
			if labelColours.any():
				interface.setAttr('x2ds_colours', labelColours, atLocation=x2ds_location)
Пример #2
0
    def cook(self, location, interface, attrs):
        if not self.useFrame(interface.frame(), attrs['frameRange']): return
        if not attrs['calibration'] or not attrs['x3ds']: return

        calibrationLocation = attrs['calibration']
        Ps = interface.attr('Ps', atLocation=calibrationLocation)
        mats = interface.attr('mats', atLocation=calibrationLocation)
        if Ps is None:
            if mats is None:
                self.logger.warning('Could not find calibration data at: %s' %
                                    calibrationLocation)
                return

            Ps = interface.getPsFromMats(mats)
            if Ps is None: return

        # Get the x3ds (and labels if available) from the cooked location
        x3ds = interface.attr('x3ds', atLocation=attrs['x3ds'])
        if x3ds is None:
            self.logger.error('No 3D points found at: %s' % attrs['x3ds'])
            return

        which_labels = interface.attr('which_labels')
        if which_labels is None:
            which_labels = np.arange(len(x3ds))

        x3ds = np.ascontiguousarray(x3ds, dtype=np.float32)
        normals = interface.attr('normals', atLocation=attrs['x3ds'])

        if 'x3dIndex' in attrs and attrs['x3dIndex'] >= 0:
            idx = attrs['x3dIndex']
            x3ds = x3ds[idx].reshape(1, -1)
            which_labels = [idx]

        # Check if we've got visibility lods
        visibilityLod = None
        if 'skeleton' in attrs and attrs['skeleton']:
            skeletonLoc = attrs['skeleton']
            skelDict = interface.attr('skelDict', atLocation=skeletonLoc)
            visibilityLod = interface.getChild('visibilityLod',
                                               parent=skeletonLoc)
            if attrs['useVisibility'] and visibilityLod is None:
                self.logger.error('No visibility LODs found at skeleton: %s' %
                                  attrs['skeleton'])
                return

            mats = interface.attr('mats', atLocation=calibrationLocation)
            cameraPositions = np.array([m[4] for m in mats], dtype=np.float32)

        if self.visibility is None:
            self.visibility = ISCV.ProjectVisibility.create()

        # if attrs['useVisibility'] and normals is not None and visibilityLod is not None:
        if attrs['useVisibility'] and visibilityLod is not None:
            lodNames = visibilityLod['names']
            lodTris = visibilityLod['tris']
            lodVerts = visibilityLod['verts']
            lodNormals = visibilityLod['faceNormals']
            tris = lodVerts[lodTris]

            if attrs['useNormals'] and normals is not None:
                self.visibility.setNormalsAndLods(normals, tris,
                                                  cameraPositions,
                                                  np.concatenate((lodNormals)),
                                                  attrs['intersect_threshold'],
                                                  attrs['generateNormals'])
            else:
                self.visibility.setLods(tris, cameraPositions,
                                        np.concatenate((lodNormals)),
                                        attrs['intersect_threshold'],
                                        attrs['generateNormals'])

            x2ds, x2ds_splits, x2d_labels = ISCV.project_visibility(
                x3ds, which_labels, Ps, self.visibility)
        elif attrs['useNormals'] and normals is not None:
            self.visibility.setNormals(normals)
            x2ds, x2ds_splits, x2d_labels = ISCV.project_visibility(
                x3ds, which_labels, Ps, self.visibility)
        else:
            x2ds, x2ds_splits, x2d_labels = ISCV.project(
                x3ds, which_labels, Ps)

        # Distort if needed
        if 'distort' in attrs and attrs['distort']:
            for ci, (s, e) in enumerate(zip(x2ds_splits[:-1],
                                            x2ds_splits[1:])):
                K, RT, P, ks, T, wh = mats[ci]
                dets = x2ds[s:e]
                ISCV.distort_points(dets, float(-K[0, 2]), float(-K[1, 2]),
                                    float(ks[0]), float(ks[1]), dets)
                x2ds[s:e] = dets

        detsAttrs = {
            'x2ds': x2ds,
            'x2ds_splits': x2ds_splits,
            'labels': x2d_labels,
            'x2ds_colour': eval(attrs['colour']),
            'x2ds_pointSize': attrs['pointSize']
        }

        if 'cameraOffset' in attrs and attrs['cameraOffset'] > 0:
            x2ds_splits_render = np.insert(
                x2ds_splits, np.zeros(attrs['cameraOffset'], dtype=np.int32),
                0)
            detsAttrs['x2ds_splits_render'] = x2ds_splits_render

        interface.createChild(interface.name(),
                              'detections',
                              atLocation=interface.parentPath(),
                              attrs=detsAttrs)
Пример #3
0
    def cook(self, location, interface, attrs):
        startFrame = attrs['startFrame']
        if interface.frame() < startFrame: return

        if not attrs['directory']: return

        skelDict = interface.attr('skelDict')
        if not skelDict: return

        calibrationLocation = attrs['calibration']
        if not calibrationLocation: calibrationLocation = interface.root()

        # if not 'mesh' in attrs or not attrs['mesh']: return
        # mesh = attrs['mesh']

        mats = interface.attr('mats', atLocation=calibrationLocation)
        if not mats: return
        Ps = np.array([m[2] / (np.sum(m[2][0, :3]**2)**0.5) for m in mats],
                      dtype=np.float32)

        from Ops import Interface
        vs, vs_labels = Interface.getWorldSpaceMarkerPos(skelDict)
        normals = interface.attr('normals')
        if normals is None: return

        if self.visibility is None:
            self.visibility = ISCV.ProjectVisibility.create()
        self.visibility.setNormals(normals)
        vs_labels = np.array(skelDict['markerNames'], dtype=np.int32)
        proj_x2ds, proj_splits, _ = ISCV.project_visibility(
            vs, vs_labels, Ps, self.visibility)

        win = interface.win()
        # if not win.view().primitives2D:
        #	 win.view().primitives2D = QGLViewer.makePrimitives2D(([], []), ([], []))
        #
        # win.view().primitives2D[1].setData(proj_x2ds, proj_splits)
        # win.view().primitives2D[1].pointSize = 8
        # win.view().primitives2D[1].colour = [1., 1., 1., 1.]

        numCameras = len(proj_splits) - 1
        directory = attrs['directory']

        # Only draw 2D primitives
        # options = PaintOptions()
        # options.setDrawNone()
        # options.drawPrimitives2D = True

        for ci in range(numCameras):
            cameraIndex = ci + 1
            win.view().camera = win.view().cameras[cameraIndex]
            # win.view().paintGL(options)
            win.view().paintToImageFile(ci, interface.frame(), '')

            cameraName = str(cameraIndex)
            filename = os.path.join(
                directory, 'Camera_' + cameraName + '_Frame_' +
                str(interface.frame()) + '.jpg')

            buffer = GL.glReadPixels(0, 0,
                                     win.view().width,
                                     win.view().height, GL.GL_RGB,
                                     GL.GL_UNSIGNED_BYTE)
            cv_image = cv2.cv.CreateImage(
                (win.view().width, win.view().height), cv2.cv.IPL_DEPTH_8U, 3)
            cv2.cv.SetData(cv_image, buffer)
            cv2.cv.Flip(cv_image)
            cv2.cv.SaveImage(filename, cv_image)